Wednesday, February 13, 2013

Business Applications Lab Session #6 on 12th Feb 2013


Assignment: 

Create log of returns data  and calculate its historical volatility

Commands : 

1) logSt-logSt-1/logSt-1
OR
2) log(St-St-1/St-1)

Create ACF Plot for log returns and do the ADF test and analyse on it
Data :
NSE Index –Jan 2012 –Jan 2013
NIFTY data –Closing prices

Commands:-

> niftychart<-read.csv(file.choose(),header=T)
> closingval<-niftychart$Close

> closingval.ts<-ts(closingval,frequency=252)
> plot(log( closingval.ts))
> minusone.ts<-lag(closingval.ts,K=-1)
> plot(log( minusone.ts))
> z<-log(closingval.ts)-log(minusone.ts)
> z



> returns<-z/log(minusone.ts)
> plot(returns,main="Plot of Log Returns;CNX NSE Nifty Jan-2012 to Jan-2013" )

 > acf(returns,main=" The Auto Correlation Plot;   Dotted line shows 95% confidence interval ")



The ACF plot shows that all the correlations lie within our expectations of a 95% confidence interval so there is a fairly good chance of considering the Data to be "STATIONARY"

> adf.test(returns)




Now with the ADF test and its P-value we can confirm that the Data is "Stationary"

# Now calculating the Historical volatility of the Data

> T<-252^0.5
> histvolatality<-sd(returns)/T

> histvolatality

Thursday, February 7, 2013

Business Applications Lab Session #5 on 5th Feb 2013


ASSIGNMENT 1 :

Converting the data in the time series format and then calculating the returns from it.
(Data taken - NSE MIDCAP 50 from July 31st to Dec 31st, 2012)

COMMANDS:

> z<-read.csv(file.choose(),header=T)
> Close<-z$Close
> Close

 [1] 1994.30 1993.30 2006.55 1990.00 2002.30 2033.70 2042.00 2046.85 2054.05
[10] 2057.85 2033.65 2063.55 2116.10 2155.80 2134.05 2191.65 2198.40 2203.40
[19] 2210.90 2216.90 2252.45 2269.65 2286.75 2298.00 2275.55 2255.90 2271.65
[28] 2238.95 2287.35 2286.05 2287.05 2254.00 2251.40 2281.30 2258.20 2258.80
[37] 2239.60 2228.80 2199.00 2188.10 2162.00 2174.40 2207.10 2226.45 2208.50
[46] 2214.35 2238.80 2242.30 2219.80 2229.75 2233.80 2233.70 2200.05 2178.80
[55] 2152.10 2168.00 2176.80 2176.10 2195.60 2226.20 2248.25 2288.45 2315.55
[64] 2332.05 2343.85 2369.60 2360.10 2377.95 2350.85 2361.85 2323.15 2347.85
[73] 2363.65 2388.25 2391.65 2379.35 2325.35 2327.45 2345.10 2334.00 2357.25
[82] 2369.50

> Close.ts<-ts(Close)
> Close.ts<-ts(Close,deltat=1/252)
> z1<-ts(data=Close.ts[10:95],frequency=1,deltat=1/252)
> z1.ts<-ts(z1)
> z1.ts

Time Series:
Start = 1
End = 86
Frequency = 1
 [1] 2057.85 2033.65 2063.55 2116.10 2155.80 2134.05 2191.65 2198.40 2203.40
[10] 2210.90 2216.90 2252.45 2269.65 2286.75 2298.00 2275.55 2255.90 2271.65
[19] 2238.95 2287.35 2286.05 2287.05 2254.00 2251.40 2281.30 2258.20 2258.80
[28] 2239.60 2228.80 2199.00 2188.10 2162.00 2174.40 2207.10 2226.45 2208.50
[37] 2214.35 2238.80 2242.30 2219.80 2229.75 2233.80 2233.70 2200.05 2178.80
[46] 2152.10 2168.00 2176.80 2176.10 2195.60 2226.20 2248.25 2288.45 2315.55
[55] 2332.05 2343.85 2369.60 2360.10 2377.95 2350.85 2361.85 2323.15 2347.85
[64] 2363.65 2388.25 2391.65 2379.35 2325.35 2327.45 2345.10 2334.00 2357.25
[73] 2369.50      NA      NA      NA      NA      NA      NA      NA      NA
[82]      NA      NA      NA      NA      NA

> z1.diff<-diff(z1)
> z2<-lag(z1.ts,K=-1)
> Returns<-z1.diff/z2
> plot(Returns,main="10th to 95th day returns")
> z3<-cbind(z1.ts,z1.diff,Returns)
> plot(z3,main="Data from 10th to 95th day, Difference, Returns")



------------------------------------------------------------------------------------------------------------

ASSIGNMENT 2 :

Do logit analysis for 700 data points and then predict for 150 data points.


COMMANDS:

> z<-read.csv(file.choose(),header=T)
> z1<-z[1:700,1:9]
> head(z1)

  age ed employ address income debtinc creddebt othdebt default
1  41  3     17      12    176     9.3    11.36    5.01       1
2  27  1     10       6     31    17.3     1.36    4.00       0
3  40  1     15      14     55     5.5     0.86    2.17       0
4  41  1     15      14    120     2.9     2.66    0.82       0
5  24  2      2       0     28    17.3     1.79    3.06       1
6  41  2      5       5     25    10.2     0.39    2.16       0

> z1$ed<-factor(z1$ed)

> z1.est<-glm(default ~ age + ed + employ + address + income + debtinc + creddebt + othdebt, data=z1, family = "binomial")
> summary(z1.est)

Call:
glm(formula = default ~ age + ed + employ + address + income +
    debtinc + creddebt + othdebt, family = "binomial", data = z1)

Deviance Residuals:
    Min       1Q   Median       3Q      Max 
-2.4322  -0.6463  -0.2899   0.2807   3.0255 

Coefficients:
             Estimate Std. Error z value Pr(>|z|)   
(Intercept) -1.589302   0.605324  -2.626  0.00865 **
age          0.035514   0.017588   2.019  0.04346 * 
ed2          0.307623   0.251629   1.223  0.22151   
ed3          0.352448   0.339937   1.037  0.29983   
ed4         -0.085359   0.472938  -0.180  0.85677   
ed5          0.874942   1.293734   0.676  0.49886   
employ      -0.260737   0.033410  -7.804 5.99e-15 ***
address     -0.105426   0.023264  -4.532 5.85e-06 ***
income      -0.007855   0.007782  -1.009  0.31282   
debtinc      0.070551   0.030598   2.306  0.02113 * 
creddebt     0.625177   0.112940   5.535 3.10e-08 ***
othdebt      0.053470   0.078464   0.681  0.49558   
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 804.36  on 699  degrees of freedom
Residual deviance: 549.56  on 688  degrees of freedom
AIC: 573.56

Number of Fisher Scoring iterations: 6

> forecast<-z[701:850,1:8]
> forecast$ed<-factor(forecast$ed)
> forecast$probability<-predict(z1.est, newdata=forecast, type="response")
> head(forecast)

    age ed employ address income debtinc creddebt othdebt probability
701  36  1     16      13     32    10.9     0.54    2.94  0.00783975
702  50  1      6      27     21    12.9     1.32    1.39  0.07044926
703  40  1      9       9     33    17.0     4.88    0.73  0.63780431
704  31  1      5       7     23     2.0     0.05    0.41  0.07471587
705  29  1      4       0     24     7.8     0.87    1.01  0.34464735
706  25  2      1       3     14     9.9     0.23    1.15  0.45584645