forked from ericwbzhang/Vol_prediction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.Rhistory
512 lines (512 loc) · 17 KB
/
.Rhistory
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
forecast_len<- whole_len- burning
egarch_vx_roll<- ugarchroll( spec = egarch_vx,
data= ret,
n.ahead = 1,
forecast.length = forecast_len,
refit.every = 5)
load('egarch_model')
load('egarch_model')
load('egarch_model')
egarch_model$spec
egarch_model$spec
egarch_model$plot
rm(list=ls())
library(lubridate)
setwd('/Users/Eric/Documents/Vol_prediction')
SPXdata<- read.csv('SPX_rvol.csv')
rownames(SPXdata)<- ymd( SPXdata$DATE)
SPXdata$SPX2.rvol<- sqrt(SPXdata$SPX2.rv)
## preliminary analysis for SPX2.rv
acf(SPXdata$SPX2.rvol)
acf(SPXdata$SPX2.r)
library(tseries)
adf.test(SPXdata$SPX2.rvol)
kpss.test(SPXdata$SPX2.rvol, null = 'Level')
# ADF and KPSS test get both rejected. It is a flag of LRD.
plot(SPXdata$DATE, SPXdata$SPX2.rvol, type = 'n')
lines( SPXdata$DATE, SPXdata$SPX2.rvol)
## Fact:
## 1. SPX2.r shows week auto corr and SPX2.rv shows strong LRD
## 2. SPX2.r accepts the hypo of stationary
Box.test(SPXdata$SPX2.r, lag =1 , type= 'Ljung-Box')
shapiro.test(SPXdata$SPX2.r)
plot(density(SPXdata$SPX2.r))
library(moments)
kurtosis(SPXdata$SPX2.r)
## model SPX2.r by t dist
library(MASS)
t.pars<-fitdistr(SPXdata$SPX2.r, densfun = 't', start= list(m=0,s= 0.01 ,df= 1))
plot(density(SPXdata$SPX2.r), xlim= c(-.1,.1), ylim=c(-1, 55) )
par(new=TRUE)
curve( dt( (x- t.pars$estimate[1])/t.pars$estimate[2],
df= t.pars$estimate[3])/ t.pars$estimate[2],
from= -.1,
to= .1, xlim= c(-.1,.1),
ylim=c (-1, 55),
col= 'green')
library(forecast)
auto.arima(SPXdata$SPX2.r)
## auto.arima indicates ARMA(2,0) model for SPX2.r
## So apply standard ARMA(2,0)-GARCH(1,1) model for SPX2.r
library(rugarch)
sgarch<- ugarchspec(mean.model = list( armaOrder= c(2,0),
include.mean= TRUE),
distribution.model = 'std')
sgarch_fitted<- ugarchfit(sgarch, data = SPXdata$SPX2.r)
whole_len<- length( SPXdata$SPX2.r)
burning_len<- 1000
forecast_len<- whole_len- burning_len
ret<- data.frame(SPX2.r= SPXdata$SPX2.r)
rownames(ret)<- rownames(SPXdata)
# rolling estimation and forecasting
sgarch_roll<- ugarchroll(spec = sgarch,
data= ret,
n.ahead = 1,
forecast.length =forecast_len,
refit.every = 5)
rm(list=ls())
library(lubridate)
setwd('/Users/Eric/Documents/Vol_prediction')
SPXdata<- read.csv('SPX_rvol.csv')
rownames(SPXdata)<- ymd( SPXdata$DATE)
SPXdata$SPX2.rvol<- sqrt(SPXdata$SPX2.rv)
## preliminary analysis for SPX2.rv
acf(SPXdata$SPX2.rvol)
acf(SPXdata$SPX2.r)
library(tseries)
adf.test(SPXdata$SPX2.rvol)
kpss.test(SPXdata$SPX2.rvol, null = 'Level')
# ADF and KPSS test get both rejected. It is a flag of LRD.
plot(SPXdata$DATE, SPXdata$SPX2.rvol, type = 'n')
lines( SPXdata$DATE, SPXdata$SPX2.rvol)
## Fact:
## 1. SPX2.r shows week auto corr and SPX2.rv shows strong LRD
## 2. SPX2.r accepts the hypo of stationary
Box.test(SPXdata$SPX2.r, lag =1 , type= 'Ljung-Box')
shapiro.test(SPXdata$SPX2.r)
plot(density(SPXdata$SPX2.r))
library(moments)
kurtosis(SPXdata$SPX2.r)
## model SPX2.r by t dist
library(MASS)
t.pars<-fitdistr(SPXdata$SPX2.r, densfun = 't', start= list(m=0,s= 0.01 ,df= 1))
plot(density(SPXdata$SPX2.r), xlim= c(-.1,.1), ylim=c(-1, 55) )
par(new=TRUE)
curve( dt( (x- t.pars$estimate[1])/t.pars$estimate[2],
df= t.pars$estimate[3])/ t.pars$estimate[2],
from= -.1,
to= .1, xlim= c(-.1,.1),
ylim=c (-1, 55),
col= 'green')
library(forecast)
auto.arima(SPXdata$SPX2.r)
## auto.arima indicates ARMA(2,0) model for SPX2.r
## So apply standard ARMA(2,0)-GARCH(1,1) model for SPX2.r
library(rugarch)
sgarch<- ugarchspec(mean.model = list( armaOrder= c(2,0),
include.mean= TRUE),
distribution.model = 'std')
sgarch_fitted<- ugarchfit(sgarch, data = SPXdata$SPX2.r)
whole_len<- length( SPXdata$SPX2.r)
burning_len<- 1000
forecast_len<- whole_len- burning_len
ret<- data.frame(SPX2.r= SPXdata$SPX2.r)
rownames(ret)<- rownames(SPXdata)
# rolling estimation and forecasting
sgarch_roll<- ugarchroll(spec = sgarch,
data= ret,
n.ahead = 1,
forecast.length =forecast_len,
refit.every = 5)
sgarch_roll
rm(list=ls())
library(lubridate)
setwd('/Users/Eric/Documents/Vol_prediction')
SPXdata<- read.csv('SPX_rvol.csv')
rownames(SPXdata)<- ymd( SPXdata$DATE)
SPXdata$SPX2.rvol<- sqrt(SPXdata$SPX2.rv)
## preliminary analysis for SPX2.rv
acf(SPXdata$SPX2.rvol)
acf(SPXdata$SPX2.r)
library(tseries)
adf.test(SPXdata$SPX2.rvol)
kpss.test(SPXdata$SPX2.rvol, null = 'Level')
# ADF and KPSS test get both rejected. It is a flag of LRD.
plot(SPXdata$DATE, SPXdata$SPX2.rvol, type = 'n')
lines( SPXdata$DATE, SPXdata$SPX2.rvol)
## Fact:
## 1. SPX2.r shows week auto corr and SPX2.rv shows strong LRD
## 2. SPX2.r accepts the hypo of stationary
Box.test(SPXdata$SPX2.r, lag =1 , type= 'Ljung-Box')
shapiro.test(SPXdata$SPX2.r)
plot(density(SPXdata$SPX2.r))
library(moments)
kurtosis(SPXdata$SPX2.r)
## model SPX2.r by t dist
library(MASS)
t.pars<-fitdistr(SPXdata$SPX2.r, densfun = 't', start= list(m=0,s= 0.01 ,df= 1))
plot(density(SPXdata$SPX2.r), xlim= c(-.1,.1), ylim=c(-1, 55) )
par(new=TRUE)
curve( dt( (x- t.pars$estimate[1])/t.pars$estimate[2],
df= t.pars$estimate[3])/ t.pars$estimate[2],
from= -.1,
to= .1, xlim= c(-.1,.1),
ylim=c (-1, 55),
col= 'green')
library(forecast)
auto.arima(SPXdata$SPX2.r)
## auto.arima indicates ARMA(2,0) model for SPX2.r
## So apply standard ARMA(2,0)-GARCH(1,1) model for SPX2.r
library(rugarch)
sgarch<- ugarchspec(mean.model = list( armaOrder= c(2,0),
include.mean= TRUE),
distribution.model = 'std')
sgarch_fitted<- ugarchfit(sgarch, data = SPXdata$SPX2.r)
whole_len<- length( SPXdata$SPX2.r)
burning_len<- 1000
forecast_len<- whole_len- burning_len
ret<- data.frame(SPX2.r= SPXdata$SPX2.r)
rownames(ret)<- rownames(SPXdata)
# rolling estimation and forecasting
sgarch_roll<- ugarchroll(spec = sgarch,
data= ret,
n.ahead = 1,
forecast.length =forecast_len,
refit.every = 5)
## plot the predicted vol and realized vol(5-min estimator by OxManLab)
library(ggplot2)
library(reshape2)
x<- tail( ymd( SPXdata$DATE), forecast_len)
realized_vol<- sqrt(tail( SPXdata$SPX2.rv, forecast_len))
sgarch.predicted_vol<- sgarch_roll@forecast$density[,'Sigma']
tmp_df<- data.frame(x, realized_vol, sgarch.predicted_vol)
sgarch.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', 'red'))+
ylab('daily volatility')+
xlab('date index')+
theme(legend.title= element_blank())+
ggtitle('ARMA(2,0)-GARCH(1,1) vol prediction')
jpeg('ARMAGARCH.jpeg')
sgarch.g
dev.off()
## compute the squared error
sgarch.MSE<- mean(( realized_vol- sgarch.predicted_vol)^2)
summary( (realized_vol-sgarch.predicted_vol)^2)
sgarch.MSE
cor( realized_vol, sgarch.predicted_vol)
summary( lm( realized_vol~ sgarch.predicted_vol))
## save sgarch model
sgarch_model<- list()
sgarch_model$spec<- sgarch
sgarch_model$roll<- sgarch_roll
sgarch_model$plot<- sgarch.g
sgarch_model$MSE<- sgarch.MSE
sgarch_model$roll.pred<- tmp_df
save(sgarch_model, file = 'sgarch_model')
## EGARCH estimation
egarch<- ugarchspec(variance.model = list( model='eGARCH'),
mean.model = list( armaOrder= c(2,0)),
distribution.model = 'std')
egarch_fitted<- ugarchfit(egarch, data= SPXdata$SPX2.r)
egarch_roll<- ugarchroll( spec = egarch,
data= ret,
n.ahead = 1,
forecast.length = forecast_len,
refit.every = 5)
x<- tail( ymd( SPXdata$DATE), forecast_len)
realized_vol<- sqrt(tail( SPXdata$SPX2.rv, forecast_len))
egarch.predicted_vol<- egarch_roll@forecast$density[,'Sigma']
tmp_df<- data.frame(x, realized_vol, egarch.predicted_vol)
egarch.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', 'blue'))+
ylab('daily volatility')+
xlab('date index')+
theme(legend.title= element_blank())+
ggtitle('ARMA(2,0)-EGARCH(1,1) vol prediction')
jpeg('ARMAEGARCH.jpeg')
egarch.g
dev.off()
egarch.MSE<- mean(( realized_vol- egarch.predicted_vol)^2)
summary( (realized_vol-egarch.predicted_vol)^2)
egarch.MSE
cor( realized_vol, egarch.predicted_vol)
summary( lm( realized_vol~ egarch.predicted_vol))
egarch_model<- list()
egarch_model$spec<- egarch
egarch_model$roll<- egarch_roll
egarch_model$plot<- egarch.g
egarch_model$MSE<- egarch.MSE
egarch_model$roll.pred<- tmp_df
save(egarch_model, file= 'egarch_model')
load('egarch_model')
egarch_model$plot
cor( egarch_model$roll.pred$realized_vol, egarch_model$roll.pred$egarch.predicted_vol, method = 'spearman')
cor( egarch_model$roll.pred$realized_vol, egarch_model$roll.pred$egarch.predicted_vol, method = 'spearman')
cor( egarch_model$roll.pred$realized_vol, egarch_model$roll.pred$egarch.predicted_vol, method = 'spearman')
cor( egarch_model$roll.pred$realized_vol, egarch_model$roll.pred$egarch.predicted_vol, method = 'spearman')
summary(egarch_model$roll.pred$realized_vol-
egarch_model$roll.pred$egarch.predicted_vol)
rgarch_model$plot
rgarch_model$plot
load('rgarch_model')
rgarch_model$spec
rgarch_model$plot
rm(list=ls())
setwd('/Users/Eric/Desktop/Vol_prediction')
library(lubridate)
setwd('/Users/Eric/Desktop/Vol_prediction')
SPXdata<- read.csv('SPX_rvol.csv')
rownames(SPXdata)<- ymd( SPXdata$DATE)
SPXdata$SPX2.rvol<- sqrt(SPXdata$SPX2.rv)
library(rugarch)
library(xts)
rgarch.model<- ugarchspec(mean.model = list(armaOrder= c(2,0)),
variance.model = list(model= 'realGARCH',
garchOrder= c(2,1)))
setbounds(rgarch.model)<- list(alpha2=c (-1,1))
SPXdata.xts<- SPXdata
SPXdata.xts$DATE<- NULL
SPXdata.xts<- as.xts(SPXdata.xts)
rgarch.fit<- ugarchfit(spec = rgarch.model,
data= SPXdata.xts$SPX2.r,
solver= 'hybrid',
realizedVol= SPXdata.xts$SPX2.rvol)
whole_len<- dim(SPXdata.xts)[1]
burning<- 1000
forecast_len<- whole_len- burning
rgarch.roll<- ugarchroll(spec = rgarch.model,
data= SPXdata.xts$SPX2.r,
n.ahead = 1,
forecast.length = forecast_len,
refit.every = 5,
solver= 'hybrid',
realizedVol= SPXdata.xts$SPX2.rvol
)
library(ggplot2)
library(reshape2)
tmp_df<- data.frame(x= tail( ymd(index(SPXdata.xts)) ,forecast_len),
realized_vol= tail( SPXdata$SPX2.rvol, forecast_len),
rgarch.prediction_vol= rgarch.roll@forecast$density$RVolForecast)
rgarch.g<- ggplot( data= melt( tmp_df, id.var= 'x'), aes( x=x, y= value))+
geom_line(aes(colour= variable, group= variable))+
scale_colour_manual(values= c( 'grey', 'orange'))+
ylab('daily volatility')+
xlab( 'date')+
theme( legend.title= element_blank())+
ggtitle( 'realizedGARCH(ARMA(2,0)-rGARCH(2,1)) rolling prediction')
jpeg( 'rGARCH.jpeg')
rgarch.g
dev.off()
rgarch.MSE<- mean( (tmp_df$realized_vol- tmp_df$rgarch.prediction_vol)^2)
summary( (tmp_df$realized_vol- tmp_df$rgarch.prediction_vol)^2)
rgarch.MSE
cor( tmp_df$realized_vol, tmp_df$rgarch.prediction_vol)
summary( lm( tmp_df$realized_vol~ tmp_df$rgarch.prediction_vol))
rgarch_model<- list()
rgarch_model$spec<- rgarch.model
rgarch_model$roll<- rgarch.roll
rgarch_model$plot<- rgarch.g
rgarch_model$MSE<- rgarch.MSE
rgarch_model$roll.pred<- tmp_df
save(rgarch_model, file = 'rgarch_model')
rgarch_model$plot
rm(list=ls())
setwd('/Users/Eric/Desktop/Vol_prediction')
SPXdata<- read.csv('SPX_rvol.csv')
library(lubridate)
rownames(SPXdata)<- ymd(SPXdata$DATE)
SPXdata$SPX2.rvol<- sqrt(SPXdata$SPX2.rv)
acf( SPXdata$SPX2.rvol, 1000)
library(tseries)
library(pracma)
hurstexp(SPXdata$SPX2.rvol)
adf.test(SPXdata$SPX2.rvol)
kpss.test(SPXdata$SPX2.rvol)
## acf of SPX2.rv decays slowly. It indicates LRD.
## Model SPX2.rv by fractional ARIMA
library(rugarch)
## armaOrder should be (0,0)
# pq<- c()
# aic<- c()
# for (p in 0:1){
# for (q in 0:1){
# arfima.model<- ugarchspec(mean.model = list(armaOrder= c(p, q),
# arfima=TRUE))
# arfima.fitted<- ugarchfit( spec= arfima.model,
# data= SPXdata$SPX2.rv)
# pq<- c(pq, paste(c(p,q), collapse = ','))
# aic<- c(aic, infocriteria(arfima.fitted)[1])
# }
# }
# tmp.df<- data.frame(pq= pq, aic= aic)
# tmp.df
rVol<- data.frame(rvol= SPXdata$SPX2.rvol)
rownames(rVol)<- ymd(SPXdata$DATE)
arfima_egarch.model<- ugarchspec(mean.model = list(armaOrder= c(0,0),
arfima=TRUE),
variance.model = list(model='eGARCH'))
arfima_egarch.fitted<- ugarchfit(spec = arfima_egarch.model,
data= rVol)
arfima_egarch.fitted
library(ggplot2)
library(lubridate)
library(reshape2)
tmp_df<- data.frame(x= rownames(rVol),
realized= rVol$rvol,
arfima_egarch.fitted= arfima_egarch.fitted@fit$fitted.values )
# g<- ggplot(melt(tmp_df, id.vars = 'x'), aes(x=x, y= value))+
# geom_line(aes(colour= variable, group= variable))+
# scale_color_manual(values= c('grey', 'red'))+
# ylab('daily volatility')+
# xlab('date index')+
# theme(legend.title= element_blank())
# g
cor( tmp_df$realized, tmp_df$arfima_egarch.fitted)
summary( lm( tmp_df$realized~ tmp_df$arfima_egarch.fitted))
##residual analysis
resi<- arfima_egarch.fitted@fit$residuals
plot(resi, type= 'l')
acf(resi, 30)
adf.test(resi)
kpss.test(resi)
library(lmtest)
## rolling prediction
whole_len<- dim(rVol)[1]
burning_len<- 1000
forecast_len<- whole_len- burning_len
arfima_egarch.roll<- ugarchroll(arfima_egarch.model,
data = rVol,
n.ahead = 1,
forecast.length = forecast_len,
refit.every = 5)
library(ggplot2)
library(reshape2)
tmp_df<- data.frame(x= tail( ymd( SPXdata$DATE), forecast_len),
realized_vol= tail(rVol$rvol, forecast_len),
arfima_egarch.predicted_vol= arfima_egarch.roll@forecast$density[,'Mu'])
arfima_egarch.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', 'purple'))+
ylab('daily volatility')+
xlab('date index')+
theme(legend.title= element_blank())+
ggtitle('Realized Vol ARFIMA(0,d,0)_EGARCH(1,1) rolling prediction')
jpeg('rVolARFIMAEGARCH.jpeg')
arfima_egarch.g
dev.off()
## compute the squared error
arfima_egarch.MSE<- mean(( tmp_df$realized_vol- tmp_df$arfima_egarch.predicted_vol)^2)
summary( (tmp_df$realized_vol-tmp_df$arfima_egarch.predicted_vol)^2)
arfima_egarch.MSE
cor( tmp_df$realized_vol, tmp_df$arfima_egarch.predicted_vol)
summary( lm( tmp_df$realized_vol~ tmp_df$arfima_egarch.predicted_vol))
arfima_egarch_model<- list()
arfima_egarch_model$spec<- arfima_egarch.model
arfima_egarch_model$roll<- arfima_egarch.roll
arfima_egarch_model$plot<- arfima_egarch.g
arfima_egarch_model$MSE<- arfima_egarch.MSE
arfima_egarch_model$roll.pred<- tmp_df
save(arfima_egarch_model, file='arfima_egarch_model')
arfima_egarch_model$plot
arfima_egarch_model$plot
rm(list=ls())
setwd('/Users/Eric/Desktop/Vol_prediction')
load('egarch_model')
load('arfima_egarch_model')
load('rgarch_model')
library(lubridate)
library(xts)
library(zoo)
SPXdata<- read.csv('SPX_rvol.csv')
SPXdata$SPX2.rvol<- sqrt( SPXdata$SPX2.rv)
rownames(SPXdata)<- ymd( SPXdata$DATE)
## VX_lag1 contains the information of implied vol with 1 day lag
VX<- read.csv('VX.csv')
VX$DATE<- ymd(VX$DATE)
rownames(VX)<- ymd(VX$DATE)
VX$DATE<- NULL
VX<- as.xts(VX)
VX_lag1<- lag( VX, 1, na.pad = TRUE)
VX_lag1<- as.data.frame(VX_lag1)
VX_lag1$DATE<- ymd(rownames(VX_lag1))
VX_lag1<- VX_lag1[ complete.cases(VX_lag1), ]
## forecasting is the time series 1 day ahead forecasting
forecasting<- data.frame(DATE=ymd(egarch_model$roll.pred$x),
real= egarch_model$roll.pred$realized_vol,
egarchPred= egarch_model$roll.pred$egarch.predicted_vol,
rgarchPred= rgarch_model$roll.pred$rgarch.prediction_vol,
arfima_egarchPred= arfima_egarch_model$roll.pred$arfima_egarch.predicted_vol)
## combine the VX_lag1 and forecasting
fore_vx<- merge( VX_lag1, forecasting, by='DATE')
fore_vx<- fore_vx[complete.cases(fore_vx),]
rownames(fore_vx)<- ymd(fore_vx$DATE)
fore_vx$DATE<- NULL
fore_vx<- as.xts( fore_vx)
tmp<- list()
tmp$rvol<- fore_vx$real
fore_vx$real<- NULL
tmp$x<- fore_vx
fore_vx<- tmp
## rf ensemble to get an average forecasting ( not successful)
library(randomForest)
# # cv<- rfcv(trainx = fore_vx$x, trainy = fore_vx$rvol, cv.fold = 5)
#
# ensemble_rf<- randomForest(x = fore_vx$x ,
# y= fore_vx$rvol,
# ntree= 1000,
# importance= TRUE)
#
# plot(ensemble_rf)
# varImpPlot(ensemble_rf)
# var_selection<- c( 'rgarchPred',
# 'arfima_egarchPred',
# 'VIX.Close',
# 'egarchPred',
# 'VIX.Open',
# 'VX.C5_2',
# 'VX.C2_1',
# 'VX.C1_0',
# 'VX4.OpenInt_lag1',
# 'VX1.Settle')
# tmp<- fore_vx$x[, var_selection]
#
# ensemble_rf<- randomForest(x = tmp ,
# y= fore_vx$rvol,
# ntree= 1000,
# importance= TRUE)
#
## rf ensemble the time series forecasting to get an average
rownames(forecasting)<- forecasting$DATE
forecasting$DATE<- NULL
ensemble_rf<- randomForest(real~.,
data = forecasting,
ntree= 1000,
mtry= 2,
importance= TRUE)
ensemble_rf
plot(ensemble_rf)
ensemble_rf$mse
ensemble_rf$rsq
library(ggplot2)
library(reshape2)
tmp<- data.frame( x= ymd( egarch_model$roll.pred$x),
reallized_vol= forecasting$real,
rf.predicted_vol= ensemble_rf$predicted)
rf.g<- ggplot(melt(data = tmp, id.var='x'), aes( x=x, y= value))+
geom_line(aes(colour= variable, group= variable))+
scale_color_manual(values = c('grey', 'dark green'))+
ylab('daily volatility')+
xlab('date index')+
theme(legend.title= element_blank())+
ggtitle('RandomForest ensemble prediction')
jpeg('RF_ENSEMBLE.jpeg')
rf.g
dev.off()
rf<- list()
rf$model<- ensemble_rf
rf$plot<- rf.g
rf$MSE<- mean( ensemble_rf$mse)
rf$roll.pred<- tmp
save(rf, file='rf')