-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathrVol_VX.R
111 lines (98 loc) · 3.78 KB
/
rVol_VX.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
rm(list=ls())
setwd('/Users/Eric/Desktop/Vol_prediction')
# SPXdata<- read.csv('SPX_rvol.csv')
# SPXdata$SPX2.rvol<- sqrt( SPXdata$SPX2.rv)
# VX<- read.csv('VX.csv')
#
# ## bind SPXdata and VX
# SPXVX<- merge(SPXdata, VX, by='DATE', all.x = TRUE)
# SPXVX<- SPXVX[ complete.cases(SPXVX),]
# write.csv(SPXVX, file='SPXVX.csv',row.names = FALSE)
SPXVX<- read.csv('SPXVX.csv')
## liner model
library(lubridate)
library(zoo)
library(quantmod)
SPXVX_lag1<- read.zoo(SPXVX)
SPXVX_lag1<-( lag( SPXVX_lag1, -1, na.pad = TRUE))
colnames(SPXVX_lag1)<- paste0(colnames(SPXVX_lag1), '_lag1')
SPXVX_lag1$SPX2.rvol<- SPXVX$SPX2.rvol
SPXVX_lag1<- SPXVX_lag1[complete.cases(SPXVX_lag1), ]
summary( lm( SPX2.rvol~ ., data = SPXVX_lag1))
library(glmnet)
lm.lasso<- (glmnet(x= SPXVX_lag1[, 1: (dim(SPXVX_lag1)[2]-1)],
y= SPXVX_lag1$SPX2.rvol,
alpha = 1,
lambda = .001))
lm.lasso$beta
lm.lasso.cv<-cv.glmnet(x= as.matrix(SPXVX_lag1[, 1: (dim(SPXVX_lag1)[2]-1)]),
y= as.matrix(SPXVX_lag1$SPX2.rvol),
alpha = 1)
plot(lm.lasso.cv)
## lasso variable selection implies that there is no linear significance in VX varibles
## It indicates that VX volatility measure affects the realized vol in a more
## sophisicated way, if there exists.
## introduce VX to ARMA EGARCH model
library(rugarch)
## The lasso reg select variables:
## VIX.Close_lag1
## VX.C1_0_lag1
## VX.C2_1_lag1
## Skew_lag1
## VIX_daily_Range_lag1
## VIX.Open_lag1
## VIX.Close_lag2
## VX1.OpenInt_lag1_lag1
## SPX2.highlow_lag1
external.regressors<- c('VIX.Close_lag1',
'VX.C1_0_lag1',
'VX.C2_1_lag1',
'Skew_lag1',
'VIX.daily_Range_lag1',
'VIX.Open_lag1',
'VX1.OpenInt_lag1_lag1',
'SPX2.highlow_lag1'
)
external.regressors<- data.frame(SPXVX_lag1[, external.regressors])
rVol<- data.frame(SPXVX_lag1$SPX2.rvol)
ret<- data.frame(SPX2.r= SPXVX$SPX2.r, DATE= SPXVX$DATE)[-1, ]
rownames(ret)<- ymd( ret$DATE)
ret$DATE<- NULL
egarch_vx<- ugarchspec(variance.model = list( model='eGARCH',
external.regressors= as.matrix(external.regressors)),
mean.model = list( armaOrder= c(2,0)),
distribution.model = 'std')
# egarch_vx_fitted<- ugarchfit(egarch_vx, data= ret)
whole_len<- dim(ret)[1]
burning<- 500
forecast_len<- whole_len- burning
egarch_vx_roll<- ugarchroll( spec = egarch_vx,
data= ret,
n.ahead = 1,
forecast.length = forecast_len,
refit.every = 5)
tmp_df<- data.frame(x=tail(ymd(rownames(ret)), forecast_len),
realized_vol= tail(ret[,1], forecast_len),
egarch_vx.predicted_vol= egarch_vx_roll@forecast$density[,'Sigma'] )
egarch_vx.g<- ggplot(melt(tmp_df, id.var= 'x'), aes(x=x, y= value))+
geom_line(aes(colour= variable, group= variable))+
scale_color_manual(values = c('grey', 'green'))+
ylab('daily volatility')+
xlab('date index')+
theme(legend.title= element_blank())+
ggtitle('ARMA(2,0)-EGARCH(1,1) with extReg vol prediction')
jpeg('ARMAEGARCH_VX.jpeg')
egarch_vx.g
dev.off()
egarch_vx.MSE<- mean(( tmp_df$realized_vol- tmp_df$egarch_vx.predicted_vol)^2)
summary( (tmp_df$realized_vol-tmp_df$egarch_vx.predicted_vol)^2)
egarch_vx.MSE
cor( tmp_df$realized_vol, tmp_df$egarch_vx.predicted_vol)
summary( lm( tmp_df$realized_vol~ tmp_df$egarch_vx.predicted_vol))
egarch_vx_model<- list()
egarch_vx_model$spec<- egarch_vx
egarch_vx_model$roll<- egarch_vx_roll
egarch_vx_model$plot<- egarch_vx.g
egarch_vx_model$MSE<- egarch_vx.MSE
egarch_vx_model$roll.pred<- tmp_df
save(egarch_vx_model, file= 'egarch_vx_model')