Professional Documents
Culture Documents
2023-11-27
require(quantmod)
##
## Attaching package: ’zoo’
library(tseries)
library(ggplot2)
library(forecast)
library(rugarch)
##
## Attaching package: ’rugarch’
library(rmgarch)
##
## Attaching package: ’rmgarch’
1
## The following objects are masked from ’package:xts’:
##
## first, last
library(readxl)
library("anytime")
library(lubridate)
##
## Attaching package: ’lubridate’
tail(NSE_M)
2
## 2020-11-30 32.05 493299 31.31736
## 2020-12-31 31.50 991799 30.77993
## 2021-01-31 40.90 252408 39.96505
## 2021-02-28 42.65 159643 41.67505
## 2021-03-31 47.15 77756 46.07219
## 2021-04-30 65.25 883305 63.75843
tail(SHIVAMILLS_M)
## T-Bill Data ##
## Month Yield%
## 1 2023-10-01 0.6125833
## 2 2023-09-01 0.6008333
## 3 2023-08-01 0.5971667
## 4 2023-07-01 0.5976667
## 5 2023-06-01 0.5925000
## 6 2023-05-01 0.5824167
colnames(T_Bills_M)
3
## Date
## 2020-11-01 0.4925833
## 2020-12-01 0.4911667
## 2021-01-01 0.4957500
## 2021-02-01 0.5190833
## 2021-03-01 0.5147500
## 2021-04-01 0.5025000
### Part 1.1 - Estimating Beta using the CAPM model ###
## NSEI.Close SHIVAMILLS.NS.Close
## 2020-11-30 13981.75 32.05
## 2020-12-31 13634.60 31.50
## 2021-01-31 14529.15 40.90
## 2021-02-28 14690.70 42.65
## 2021-03-31 14631.10 47.15
4
NIFTY Monthly Price
19000 19000
18000 18000
Price
17000 17000
16000 16000
15000 15000
14000 14000
Time
plot.new()
plot(Close_M$SHIVAMILLS.NS.Close, mar=c(6,4.1,4.1,2.1))
title(main = "SHIVAMILLS Monthly Price", xlab = "Time", ylab = "Price",
cex.main = 1, font.main = 1, cex.lab = 0.5)
5
SHIVAMILLS Monthly Price
160 160
140 140
120 120
Price
100 100
80 80
60 60
40 40
Time
## NSEI.Close SHIVAMILLS.NS.Close
## 2020-12-31 -0.024828823 -0.01716066
## 2021-01-31 0.065608878 0.29841275
## 2021-02-28 0.011119012 0.04278728
## 2021-03-31 -0.004057028 0.10550996
## 2021-04-30 0.065046389 0.38388119
tail(Returns_M, 5)
## NSEI.Close SHIVAMILLS.NS.Close
## 2023-05-31 0.03532083 -0.070476229
## 2023-06-30 0.02943085 0.018442707
## 2023-07-31 -0.02531158 -0.017438001
## 2023-08-31 0.01997008 -0.004778136
## 2023-09-30 -0.02844957 0.043209897
plot.new()
plot(M_Ret_SHIVAMILLS, mar=c(6,4.1,4.1,2.1))
6
title(main = "SHIVAMILLS Monthly Returns", xlab = "Time", ylab = "Returns",
cex.main = 1, font.main = 1, cex.lab = 0.5)
0.4 0.4
0.2 0.2
Returns
0.0 0.0
−0.2 −0.2
Time
(1 + mean(M_Ret_SHIVAMILLS))ˆ12 - 1
## [1] 0.6262445
sd(M_Ret_SHIVAMILLS)*sqrt(12)
## [1] 0.6533406
plot.new()
plot(M_Ret_NIFTY, mar=c(6,4.1,4.1,2.1))
title(main = "NIFTY Monthly Returns", xlab = "Time", ylab = "Returns",
cex.main = 1, font.main = 1, cex.lab = 0.5)
7
NIFTY Monthly Returns
0.06 0.06
0.04 0.04
Returns
0.02 0.02
0.00 0.00
−0.02 −0.02
−0.04 −0.04
Time
(1 + mean(M_Ret_NIFTY))ˆ12 - 1
## [1] 0.1248352
sd(M_Ret_NIFTY)*sqrt(12)
## [1] 0.1294646
## NSEI.Close
tail(exNSE_M)
## NSEI.Close
8
exSHIVAMILLS_M <- Returns_M$SHIVAMILLS.NS.Close - M_TBills_xts
head(exSHIVAMILLS_M)
## SHIVAMILLS.NS.Close
tail(exSHIVAMILLS_M)
## SHIVAMILLS.NS.Close
summary(M_regression)
##
## Call:
## lm(formula = SHIVAMILLS.NS.Close ~ NSEI.Close, data = data.frame(Returns_M[]))
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.31602 -0.11677 -0.01349 0.08162 0.50122
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.02822 0.03279 0.86 0.396
## NSEI.Close 1.33349 0.86039 1.55 0.131
##
## Residual standard error: 0.1847 on 32 degrees of freedom
## Multiple R-squared: 0.06982, Adjusted R-squared: 0.04076
## F-statistic: 2.402 on 1 and 32 DF, p-value: 0.131
# END OF CAPM
## Returns
## 2020-12-31 -0.01716066
9
## 2021-01-31 0.29841275
## 2021-02-28 0.04278728
## 2021-03-31 0.10550996
## 2021-04-30 0.38388119
160 160
140 140
120 120
100 100
80 80
60 60
40 40
plot(M_Returns_SHIVAMILLS$Returns) #can seem more stationary with mean ~ 0 with very few outliers OR fi
10
M_Returns_SHIVAMILLS$Returns 2020−12−31 / 2023−09−30
0.4 0.4
0.2 0.2
0.0 0.0
−0.2 −0.2
# Stationarity Test
##
## Augmented Dickey-Fuller Test
##
## data: M_Returns_SHIVAMILLS$Returns
## Dickey-Fuller = -3.8125, Lag order = 3, p-value = 0.0317
## alternative hypothesis: stationary
11
Series M_Returns_SHIVAMILLS$Returns
1.0
0.6
ACF
0.2
−0.2
Lag
12
Series M_Returns_SHIVAMILLS$Returns
0.3
0.1
Partial ACF
−0.1
−0.3
Lag
## Series: M_Returns_SHIVAMILLS
## ARIMA(0,0,0) with zero mean
##
## sigma^2 = 0.03624: log likelihood = 8.16
## AIC=-14.31 AICc=-14.19 BIC=-12.79
## $pred
## Time Series:
## Start = 2937601
## End = 3715201
## Frequency = 1.15740740740741e-05
## [1] 0 0 0 0 0 0 0 0 0 0
##
## $se
## Time Series:
13
## Start = 2937601
## End = 3715201
## Frequency = 1.15740740740741e-05
## [1] 0.1903554 0.1903554 0.1903554 0.1903554 0.1903554 0.1903554 0.1903554
## [8] 0.1903554 0.1903554 0.1903554
Standardized Residuals
3
1
−1
ACF ofTime
Residuals
1.0
−0.2 0.4
ACF
Lag
p values for Ljung−Box statistic
0.0 0.4 0.8
p value
2 4 6 8 10
lag
### Part 1.3 - GARCH and EGARCH models ###
# Getting returns
M_Ret_SHIVAMILLS <- Returns_M$SHIVAMILLS.NS.Close
##
## *---------------------------------*
## * GARCH Model Spec *
## *---------------------------------*
##
14
## Conditional Variance Dynamics
## ------------------------------------
## GARCH Model : sGARCH(1,1)
## Variance Targeting : FALSE
##
## Conditional Mean Dynamics
## ------------------------------------
## Mean Model : ARFIMA(1,0,1)
## Include Mean : TRUE
## GARCH-in-Mean : FALSE
##
## Conditional Distribution
## ------------------------------------
## Distribution : norm
## Includes Skew : FALSE
## Includes Shape : FALSE
## Includes Lambda : FALSE
# Implementing EGARCH
eg_spec = ugarchspec(variance.model = list(model="eGARCH"))
eg_spec
##
## *---------------------------------*
## * GARCH Model Spec *
## *---------------------------------*
##
## Conditional Variance Dynamics
## ------------------------------------
## GARCH Model : eGARCH(1,1)
## Variance Targeting : FALSE
##
## Conditional Mean Dynamics
## ------------------------------------
## Mean Model : ARFIMA(1,0,1)
## Include Mean : TRUE
## GARCH-in-Mean : FALSE
##
## Conditional Distribution
## ------------------------------------
## Distribution : norm
## Includes Skew : FALSE
## Includes Shape : FALSE
## Includes Lambda : FALSE
15
ugfit_M #lower AIC value models are better
##
## *---------------------------------*
## * GARCH Model Fit *
## *---------------------------------*
##
## Conditional Variance Dynamics
## -----------------------------------
## GARCH Model : sGARCH(1,1)
## Mean Model : ARFIMA(1,0,1)
## Distribution : norm
##
## Optimal Parameters
## ------------------------------------
## Estimate Std. Error t value Pr(>|t|)
## mu 0.037338 0.032256 1.1576 0.247047
## ar1 -0.508985 0.399954 -1.2726 0.203157
## ma1 0.652719 0.337519 1.9339 0.053129
## omega 0.000000 0.000399 0.0000 1.000000
## alpha1 0.000000 0.057281 0.0000 1.000000
## beta1 0.994341 0.050457 19.7065 0.000000
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu 0.037338 0.033619 1.1106 0.266728
## ar1 -0.508985 0.207150 -2.4571 0.014007
## ma1 0.652719 0.091816 7.1090 0.000000
## omega 0.000000 0.000239 0.0000 1.000000
## alpha1 0.000000 0.061078 0.0000 1.000000
## beta1 0.994341 0.075136 13.2339 0.000000
##
## LogLikelihood : 9.597701
##
## Information Criteria
## ------------------------------------
##
## Akaike -0.211629
## Bayes 0.057728
## Shibata -0.262290
## Hannan-Quinn -0.119771
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.07316 0.7868
## Lag[2*(p+q)+(p+q)-1][5] 0.33495 1.0000
## Lag[4*(p+q)+(p+q)-1][9] 1.49784 0.9971
## d.o.f=2
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
16
## statistic p-value
## Lag[1] 0.001335 0.9709
## Lag[2*(p+q)+(p+q)-1][5] 1.149591 0.8250
## Lag[4*(p+q)+(p+q)-1][9] 2.030005 0.9014
## d.o.f=2
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[3] 0.571 0.500 2.000 0.4498
## ARCH Lag[5] 1.608 1.440 1.667 0.5645
## ARCH Lag[7] 1.752 2.315 1.543 0.7695
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 3.0976
## Individual Statistics:
## mu 0.34672
## ar1 0.28272
## ma1 0.29423
## omega 0.07659
## alpha1 0.06646
## beta1 0.07860
##
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic: 1.49 1.68 2.12
## Individual Statistic: 0.35 0.47 0.75
##
## Sign Bias Test
## ------------------------------------
## t-value prob sig
## Sign Bias 0.8995 0.3758
## Negative Sign Bias 0.7278 0.4726
## Positive Sign Bias 0.4997 0.6211
## Joint Effect 0.9311 0.8179
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 28.35 0.07687
## 2 30 45.41 0.02682
## 3 40 50.71 0.09923
## 4 50 68.94 0.03164
##
##
## Elapsed time : 0.04285383
17
egfit_M #lower AIC value models are better
##
## *---------------------------------*
## * GARCH Model Fit *
## *---------------------------------*
##
## Conditional Variance Dynamics
## -----------------------------------
## GARCH Model : eGARCH(1,1)
## Mean Model : ARFIMA(1,0,1)
## Distribution : norm
##
## Optimal Parameters
## ------------------------------------
## Estimate Std. Error t value Pr(>|t|)
## mu -0.01388 0.000014 -1026.4 0
## ar1 -0.76732 0.000160 -4802.7 0
## ma1 0.83108 0.000213 3902.3 0
## omega -1.57685 0.000417 -3778.1 0
## alpha1 0.50251 0.000053 9522.5 0
## beta1 0.63138 0.000138 4584.7 0
## gamma1 -1.94959 0.000505 -3860.0 0
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu -0.01388 0.000126 -110.374 0
## ar1 -0.76732 0.000941 -815.265 0
## ma1 0.83108 0.001478 562.119 0
## omega -1.57685 0.018285 -86.238 0
## alpha1 0.50251 0.000275 1826.691 0
## beta1 0.63138 0.007309 86.377 0
## gamma1 -1.94959 0.015293 -127.484 0
##
## LogLikelihood : 23.4691
##
## Information Criteria
## ------------------------------------
##
## Akaike -0.96877
## Bayes -0.65452
## Shibata -1.03570
## Hannan-Quinn -0.86160
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.01525 0.9017
## Lag[2*(p+q)+(p+q)-1][5] 2.45990 0.7995
## Lag[4*(p+q)+(p+q)-1][9] 4.60352 0.5464
## d.o.f=2
## H0 : No serial correlation
##
18
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 7.603 0.005826
## Lag[2*(p+q)+(p+q)-1][5] 9.029 0.016272
## Lag[4*(p+q)+(p+q)-1][9] 10.478 0.039577
## d.o.f=2
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[3] 0.6017 0.500 2.000 0.4379
## ARCH Lag[5] 2.1856 1.440 1.667 0.4316
## ARCH Lag[7] 2.7891 2.315 1.543 0.5542
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 0.8201
## Individual Statistics:
## mu 0.04049
## ar1 0.02660
## ma1 0.02792
## omega 0.02143
## alpha1 0.04119
## beta1 0.02838
## gamma1 0.05132
##
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic: 1.69 1.9 2.35
## Individual Statistic: 0.35 0.47 0.75
##
## Sign Bias Test
## ------------------------------------
## t-value prob sig
## Sign Bias 0.3707 0.7135
## Negative Sign Bias 1.2615 0.2172
## Positive Sign Bias 1.1041 0.2786
## Joint Effect 2.9169 0.4046
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 16.59 0.6177
## 2 30 27.76 0.5305
## 3 40 34.24 0.6868
## 4 50 42.47 0.7334
##
##
## Elapsed time : 0.64819
# Forecasting
19
ugforecast_M
##
## *------------------------------------*
## * GARCH Model Forecast *
## *------------------------------------*
## Model: sGARCH
## Horizon: 10
## Roll Steps: 0
## Out of Sample: 0
##
## 0-roll forecast [T0=2023-09-30]:
## Series Sigma
## T+1 0.04616 0.1661
## T+2 0.03285 0.1656
## T+3 0.03962 0.1652
## T+4 0.03617 0.1647
## T+5 0.03793 0.1642
## T+6 0.03704 0.1638
## T+7 0.03749 0.1633
## T+8 0.03726 0.1629
## T+9 0.03738 0.1624
## T+10 0.03732 0.1619
##
## *------------------------------------*
## * GARCH Model Forecast *
## *------------------------------------*
## Model: eGARCH
## Horizon: 10
## Roll Steps: 0
## Out of Sample: 0
##
## 0-roll forecast [T0=2023-09-30]:
## Series Sigma
## T+1 0.002712 0.09574
## T+2 -0.026611 0.10334
## T+3 -0.004111 0.10845
## T+4 -0.021376 0.11180
## T+5 -0.008128 0.11398
## T+6 -0.018293 0.11537
## T+7 -0.010493 0.11626
## T+8 -0.016478 0.11682
## T+9 -0.011886 0.11718
## T+10 -0.015410 0.11740
20