Wednesday, February 13, 2013

Business App IT Lab - Session 6


Assignment 1
Create log returns data and calculate historical volatility


Data Set: NSE Nifty Indices Data from 1st Jan 2012 to 31st Jan 2013
Working Column : Closing Price named Close
Formula Used: (logSt - logS(t-1))/logS(t-1)

Commands:
> stockprice <- read.csv(file.choose(),header=T)
> closingprice <- stockprice$Close
> closingprice.ts <- ts(closingprice , frequency=252)
> log.returns1 <- log(closingprice.ts , base=exp(1)) - log(lag(closingprice.ts,k=-1), base = exp(1))
> log.returns <- log.returns1/log(lag(closingprice.ts,k=-1), base = exp(1))
> log.returns
> T = (252)^0.5
> historical.volatility <- sd(log.returns) *T
> historical.volatility







Assignment 2:
 Create acf plot and interpret the result for log returns data and do ADF test and interpret it. 


Command:
> acf(log.returns)

Graph Interpretation: The two dotted lines represent the confidence interval of 95%. This is a visual tool to interpret the stationarity of time series. Autocorrelation calculates the correlation between different time steps/lags within the same variable. Since the correlation measurements lie within the confidence interval and there is apparent pattern in the correlation we can say that time series is stationary. 




Augmented Dickey-Fuller(ADF) Test

Command:
>adf.test(log.returns)

Interpretation: Since the p value obtained in ADF test is 0.01 which is less than alpha . The default value of alpha is 0.05. So we are going to reject the null hypothesis and accept the alternate hypothesis. Here the alternate hypothesis is that the time series is stationary. Hence by looking at p value we can say that time series is stationary.





Thursday, February 7, 2013

Business App IT Lab - Session 5


Returns and Forecasting


Objective 1 : Find returns of NSE data > 6 months.having selected the 10th data as start and 95th data point as end.Also plot the assignment .

Solution:

Step 1: Read data  in the form of CSV file for the period 1/12/2011 to 5/02/2013
Command:
 z<-read.csv(file.choose(),header=T)

Step 2: Choose the Close column.
Command:
 close<-z$Close

Step 3: Vectorised the data i.e form a matrix of order 1X298 as 298 data points are available in close.
Command:
dim(close)<-c(1,298)

Step 4: Create time-series objects for close data from element (1,10 to1,95)
Command:
close.ts<-ts(close[1,10:95],deltat=1/252)

Step 5: Calculate difference between preceding and succeeding value
Command:
close.diff<-diff(close.ts)

Step 6: Calculate return :
Command:
return<-close.diff/lag(close.ts,k=-1)
final<-cbind(close.ts,close.diff,return)

Step 7: Plot
Command:
plot(return,main="Return from 10th to 95th")
plot(final,main="Data from 10th to 95, Difference, Return")






Objective 2: 1-700 data is available, Predict the data from 701-850, use the GLM estimation using LOGIT Analysis for the same

Solution:

Step 1: Read data  in the form of CSV file

Command:
z<-read.csv(file.choose(),header=T)

Step 2: Check the dimension of z
Command
dim(z)


Step 3: Choose 1-700 data
Command

 new<-z[1:700,1:9]

Step 4:
Command
head(new)

Step 5:
Identify the factor and run the Logit regression 
Command

 new$ed <- factor(new$ed)
 new.est<-glm(default ~ age + ed + employ + address + income, data=new, family ="binomial")
 summary(new.est)

Step 6:
Prediction<-z[701:850,1:8]
 Prediction$ed<-factor(Prediction$ed)
 Prediction$prob<-predict(new.est, newdata =Prediction, type = "response")
 head(Prediction)