smpl @all
!observations=2470 'number of observations
!rolling_window=1000 'insample observations = rolling window
!horizon=22 'forecast horizon
for %y smirv arma arima egarch vsmi naive 'variables for forecast and comparison
series(!observations-¬!horizon) {%y}_22d
next
series(!observations) smirv_dummy 'help variable
vector(!horizon) forecastbuffer 'buffer
matrix(2448,38) smirvdesstat=0 'for descriptive statistics
vector(38) smirvdesstatbuffer=0 'buffer for descriptive statistics
matrix(2448,8) dickey=0 'for ADF
matrix(2448,14) arma21parameters=0 'initial parameters ARMA(2,1)
matrix(2448,11) arima111parameters=0 'initial parameters ARI-¬MA(1,1,1)
matrix(2448,14) egarchparameters=0 'initial parameters EGARCH
matrix(2448,22) arma21forecast=0 '22 day forecasts ARMA(2,1)
matrix(2448,22) arima111forecast=0 '22 day forecasts ARI-¬MA(1,1,1)
matrix(2448,22) egarchforecast=0 '22 day forecasts EGARCH
vector(14) arma21buffer=0
vector(11) arima111buffer=0
vector(14) egarchbuffer=0
for !i=!rolling_window to !observations-!horizon
smpl @first+!i-!rolling_window @first+!i-1
'GENERATE BASIC SERIES
'=====================
!rvaverage=0
!naive=0
for !j=1 to !horizon
!rvaverage=!rvaverage+smirv(!i+!j)^2 'calculate the RV for 22 days 
!naive=!naive+smirv(!i-22+!j)^2
next !j
smirv_22d(!i)=@SQRT((1/!horizon)*!rvaverage)
vsmi_22d(!i)=vsmi(!i)/100*@SQRT(1/252) 'y to m and calendar day adjustment
naive_22d(!i)=@SQRT((1/!horizon)*!naive)
'CALCULATION OF DESCRIPTIVE STATISTICS
'====================================
!length=252
!ljungbox_dummy=0
for !ljung=1 to 250
!ljungbox_dummy=!ljungbox_dummy+@cor(smirv,smirv(-!ljung))^2/(!length-!ljung)

if !ljung=1 then
smirvdesstatbuffer(10)=!length*(!length+2)*!ljungbox_dummy
smirvdesstatbuffer(11)=@chisq(smirvdesstatbuffer(10),1)
endif
if !ljung=2 then
smirvdesstatbuffer(13)=!length*(!length+2)*!ljungbox_dummy
smirvdesstatbuffer(14)=@chisq(smirvdesstatbuffer(13),2)
endif
if !ljung=3 then
smirvdesstatbuffer(16)=!length*(!length+2)*!ljungbox_dummy
smirvdesstatbuffer(17)=@chisq(smirvdesstatbuffer(16),3)
endif
if !ljung=4 then
smirvdesstatbuffer(19)=!length*(!length+2)*!ljungbox_dummy
smirvdesstatbuffer(20)=@chisq(smirvdesstatbuffer(19),4)
endif
if !ljung=5 then
smirvdesstatbuffer(22)=!length*(!length+2)*!ljungbox_dummy
smirvdesstatbuffer(23)=@chisq(smirvdesstatbuffer(22),5)
endif
if !ljung=10 then
smirvdesstatbuffer(25)=!length*(!length+2)*!ljungbox_dummy
smirvdesstatbuffer(26)=@chisq(smirvdesstatbuffer(25),10)
endif
if !ljung=20 then
smirvdesstatbuffer(28)=!length*(!length+2)*!ljungbox_dummy
smirvdesstatbuffer(29)=@chisq(smirvdesstatbuffer(28),20)
endif
if !ljung=50 then
smirvdesstatbuffer(31)=!length*(!length+2)*!ljungbox_dummy
smirvdesstatbuffer(32)=@chisq(smirvdesstatbuffer(31),50)
endif
if !ljung=100 then
smirvdesstatbuffer(34)=!length*(!length+2)*!ljungbox_dummy
smirvdesstatbuffer(35)=@chisq(smirvdesstatbuffer(34),100)
endif
if !ljung=250 then
smirvdesstatbuffer(37)=!length*(!length+2)*!ljungbox_dummy
smirvdesstatbuffer(38)=@chisq(smirvdesstatbuffer(37),250)
endif
next !ljung
smirvdesstatbuffer(1)=@mean(smirv)
smirvdesstatbuffer(2)=@stdev(smirv)
smirvdesstatbuffer(3)=@min(smirv)
smirvdesstatbuffer(4)=@max(smirv)
smirvdesstatbuffer(5)=@kurt(smirv)
smirvdesstatbuffer(6)=@skew(smirv)
smirvdesstatbuffer(7)=!rolling_window/6*(@skew(smirv)^2+((@kurt(smirv)-3)^2)/4)
smirvdesstatbuffer(8)=@chisq(smirvdesstatbuffer(7),2)
smirvdesstatbuffer(9)=@cor(smirv,smirv(-1))
smirvdesstatbuffer(12)=@cor(smirv,smirv(-2))
smirvdesstatbuffer(15)=@cor(smirv,smirv(-3))
smirvdesstatbuffer(18)=@cor(smirv,smirv(-4))
smirvdesstatbuffer(21)=@cor(smirv,smirv(-5))
smirvdesstatbuffer(24)=@cor(smirv,smirv(-10))
smirvdesstatbuffer(27)=@cor(smirv,smirv(-20))
smirvdesstatbuffer(30)=@cor(smirv,smirv(-50))
smirvdesstatbuffer(33)=@cor(smirv,smirv(-100))
smirvdesstatbuffer(36)=@cor(smirv,smirv(-252))
rowplace(smirvdesstat,@transpose(smirvdesstatbuffer),!i)
smirv.uroot(adf,save=dickeybuffer)
rowplace(dickey,@transpose(dickeybuffer),!i)
'CALCULATION OF ARMA(2,1)
'========================
equation estmodel.ls smirv c ar(1) ar(2) ma(1)
!dof= estmodel.@regobs-estmodel.@ncoef 'degree of freedom
'estimate parameters for ARMA(2,1) and put them into arma21parameters(!i)
arma21buffer(1)=@coefs(1)
arma21buffer(2)=@tstats(1)
arma21buffer(3)=@tdist(estmodel.@tstat(1),!dof)
arma21buffer(4)=@coefs(2)
arma21buffer(5)=@tstats(2)
arma21buffer(6)=@tdist(estmodel.@tstat(2),!dof)
arma21buffer(7)=@coefs(3)
arma21buffer(8)=@tstats(3)
arma21buffer(9)=@tdist(estmodel.@tstat(3),!dof)
arma21buffer(10)=@coefs(4)
arma21buffer(11)=@tstat(4)
arma21buffer(12)=@tdist(estmodel.@tstat(4),!dof)
arma21buffer(13)=@r2
arma21buffer(14)=@rbar2
rowplace(arma21parameters,@transpose(arma21buffer),!i)
!rvaverage=0
smirv_dummy=smirv
for !j=0 to !horizon-1
smpl !i-!rolling_window+1!j !i+!j 'Sample Error here!!!!
equation estmodel.ls smirv_dummy c ar(1) ar(2) ma(1)
forecastbuffer(!j+1)=estmodel.@coefs(2)*smirv_dummy(!i+!j)+estmodel.@coefs(3)*smirv_dummy(!i-¬
1+!j)+estmodel.@coefs(4)*resid(!i+!j)
if forecastbuffer(!j+1)<0 then 'fix negative forecasts
endif
smirv_dummy(!i+!j+1)=forecastbuffer(!j+1)
!rvaverage=!rvaverage+forecastbuffer(!j+1)^2
next !j
smpl @first+!i-!rolling_window @first+!i-1
rowplace(arma21forecast,@transpose(forecastbuffer),!i)
arma_22d(!i)=@SQRT((1/!horizon)*!rvaverage)
'CALCULATIONS FOR ARIMA(1,1,1)
'===========================
equation estmodel.ls d(smirv) c ar(1) ma(1)
!dof= estmodel.@regobs-¬estmodel.@ncoef
arima111buffer(1)=@coefs(1)
arima111buffer(2)=@tstat(1)
arima111buffer(3)=@tdist(estmodel.@tstat(1),!dof)
arima111buffer(4)=@coefs(2)
arima111buffer(5)=@tstat(2)
arima111buffer(6)=@tdist(estmodel.@tstat(2),!dof)
arima111buffer(7)=@coefs(3)
arima111buffer(8)=@tstat(3)
arima111buffer(9)=@tdist(estmodel.@tstat(3),!dof)
arima111buffer(10)=@r2
arima111buffer(11)=@rbar2
rowplace(arima111parameters,@transpose(arima111buffer),!i)
!rvaverage=0
smirv_dummy=smirv
for !j=0 to !horizon-¬1
smpl !i-¬!rolling_window+1+!j !i+!j
equation estmodel.ls d(smirv_dummy) c ar(1) ma(1)
forecastbuff-¬
er(!j+1)=smirv_dummy(!i+!j)+estmodel.@coefs(2)*(smirv_dummy(!i+!j)-¬smirv_dummy(!i-¬
1+!j))+estmodel.@coefs(3)*resid(!i+!j)
if forecastbuffer(!j+1)<0 then 'fix negative forecasts by setting them
zero
forecastbuffer(!j+1)=0
endif
smirv_dummy(!i+!j+1)=forecastbuffer(!j+1)
!rvaverage=!rvaverage+forecastbuffer(!j+1)^2
next !j
smpl !i-¬!rolling_window+1 !i
rowplace(arima111forecast,@transpose(forecastbuffer),!i)
arima_22d(!i)=@SQRT((1/!horizon)*!rvaverage)
'CALCULATIONS FOR EGARCH
'=========================
equation estmodel.arch(egarch) smilog c
estmodel.makegarch garchcondvar
!dof= estmodel.@regobs-¬estmodel.@ncoef
egarchbuffer(1)=@coefs(2)
egarchbuffer(2)=@tstat(2)
egarchbuffer(3)=@tdist(estmodel.@tstat(2),!dof)
egarchbuffer(4)=@coefs(3)
egarchbuffer(5)=@tstat(3)
egarchbuffer(6)=@tdist(estmodel.@tstat(3),!dof)
egarchbuffer(7)=@coefs(4)
egarchbuffer(8)=@tstat(4)
egarchbuffer(9)=@tdist(estmodel.@tstat(4),!dof)
egarchbuffer(10)=@coefs(5)
egarchbuffer(11)=@tstat(5)
egarchbuffer(12)=@tdist(estmodel.@tstat(5),!dof)
egarchbuffer(13)=@r2
egarchbuffer(14)=@rbar2
rowplace(egarchparameters,@transpose(egarchbuffer),!i)
!rvaverage=0
for !j=1 to !horizon
if !j=1 then
forecastbuff-¬
er(1)=@SQRT(@EXP(estmodel.@coefs(2)+estmodel.@coefs(3)*@ABS(resid(!i)/@SQRT(garchcondvar
(!i)))+estmodel.@coefs(4)*(resid(!i)/@SQRT(garchcondvar(!i)))+estmodel.@coefs(5)*@LOG(ga
rchcondvar(!i))))
endif
if !j>1 then
forecastbuff-¬
er(!j)=@SQRT(@EXP(estmodel.@coefs(2)+estmodel.@coefs(3)*@ABS(forecastbuffer(!j-¬
1)/@SQRT(garchcondvar(!i)))+estmodel.@coefs(4)*(forecastbuffer(!j-¬
1)/@SQRT(garchcondvar(!i)))+estmodel.@coefs(5)*@LOG(forecastbuffer(!j-¬1)^2)))
endif
!rvaverage=!rvaverage+forecastbuffer(!j)^2
next !j
rowplace(egarchforecast,@transpose(forecastbuffer),!i)
egarch_30d(!i)=@sqrt((1/!horizon)*!rvaverage)
next !i 'move the rolling_window by 1
'PREDICTIVE POWER
'=================
matrix(28,7) predictive_power=0
smpl !rolling_window !observations-¬!horizon
for !i=1 to 4
if !i=1 then
equation regr.ls(n) smirv_22d c arma_22d
endif
if !i=2 then
equation regr.ls(n) smirv_22d c arima_22d
endif
if !i=3 then
equation regr.ls(n) smirv_22d c egarch_22d
endif
if !i=4 then
equation regr.ls(n) smirv_22d c vsmi_22d
endif
!dof= regr.@regobs-¬1
predictive_power(!i*4-¬3,1)=@coefs(1)
predictive_power(!i*4-¬2,1)=@stderrs(1)
predictive_power(!i*4-¬1,1)=@tstat(1)
predictive_power(!i*4,1)=@tdist(regr.@tstat(1),!dof)
predictive_power(!i*4-¬3,!i+1)=@coefs(2)
predictive_power(!i*4-¬2,!i+1)=@stderrs(2)
predictive_power(!i*4-¬1,!i+1)=@tstat(2)
predictive_power(!i*4,!i+1)=@tdist(regr.@tstat(2),!dof)
predictive_power(!i*4-¬3,6)=@r2
predictive_power(!i*4-¬3,7)=@rbar2
next !i
for !i=5 to 7
if !i=5 then
equation regr.ls(n) smirv_22d c arma_22d vsmi_22d
endif
if !i=6 then
equation regr.ls(n) smirv_22d c arima_22d vsmi_22d
endif
if !i=7 then
equation regr.ls(n) smirv_22d c egarch_22d vsmi_22d
endif
!dof= regr.@regobs-¬1
predictive_power(!i*4-¬3,1)=@coefs(1)
predictive_power(!i*4-¬2,1)=@stderrs(1)
predictive_power(!i*4-¬1,1)=@tstat(1)
predictive_power(!i*4,1)=@tdist(regr.@tstat(1),!dof)
predictive_power(!i*4-¬3,!i-¬3)=@coefs(2)
predictive_power(!i*4-¬2,!i-¬3)=@stderrs(2)
predictive_power(!i*4-¬1,!i-¬3)=@tstat(2)
predictive_power(!i*4,!i-¬3)=@tdist(regr.@tstat(2),!dof)
predictive_power(!i*4-¬3,5)=@coefs(3)
predictive_power(!i*4-¬2,5)=@stderrs(3)
predictive_power(!i*4-¬1,5)=@tstat(3)
predictive_power(!i*4,5)=@tdist(regr.@tstat(3),!dof)
predictive_power(!i*4-¬3,6)=@r2
predictive_power(!i*4-¬3,7)=@rbar2
next !i
'INFORMATION CONTENT
'====================
matrix(16,7) information_content=0
for !i=1 to 4
if !i=1 then
equation regr.ls(n) smirv_future c arma_22d
endif
if !i=2 then
equation regr.ls(n) smirv_future c arima_22d
endif
if !i=3 then
equation regr.ls(n) smirv_future c egarch_22d
endif
if !i=4 then
equation regr.ls(n) smirv_future c vsmi_22d
endif
!dof= regr.@regobs-¬1
information_content(!i*4-¬3,1)=@coefs(1)
information_content(!i*4-¬2,1)=@stderrs(1)
information_content(!i*4-¬1,1)=@tstat(1)
information_content(!i*4,1)=@tdist(regr.@tstat(1),!dof)
information_content(!i*4-¬3,!i+1)=@coefs(2)
information_content(!i*4-¬2,!i+1)=@stderrs(2)
information_content(!i*4-¬1,!i+1)=@tstat(2)
information_content(!i*4,!i+1)=@tdist(regr.@tstat(2),!dof)
information_content(!i*4-¬3,6)=@r2
information_content(!i*4-¬3,7)=@rbar2
next !i
'BECKER
'=======
smpl @all
for %y rv_1d rv_5d rv_10d rv_15d drv_1d drv_5d drv_10d drv_15d
drv_22d 'defining variables for rv and drv
series(!observations-¬!horizon) smi{%y}
next
matrix(!observations-¬!horizon,7) rv=0
matrix(!observations-¬!horizon,7) drv=0
for !i=!rolling_window to !observations-¬!horizon 'calculate RV and dRV
!buffer=0
for !j=1 to !horizon
!buffer=!buffer+smirv(!i+!j)^2
if !j=1 then
smirv_1d(!i)=@SQRT(!buffer)
smidrv_1d(!i)=@SQRT(!buffer)-¬smirv(!i)
endif
if !j=5 then
smirv_5d(!i)=@SQRT(!buffer/5)
smidrv_5d(!i)=@SQRT(!buffer/5)-¬smirv(!i)
endif
if !j=10 then
smirv_10d(!i)=@SQRT(!buffer/10)
smidrv_10d(!i)=@SQRT(!buffer/10)-¬smirv(!i)
endif
if !j=15 then
smirv_15d(!i)=@SQRT(!buffer/15)
smidrv_15d(!i)=@SQRT(!buffer/15)-¬smirv(!i)
endif
if !j=22 then
'smirv_22d has already been calculated above
smidrv_22d(!i)=@SQRT(!buffer/22)-¬smirv(!i)
endif
next !j
next !i
matrix(36,5) becker_rv=0
matrix(36,5) becker_drv=0
smpl 1000 2448
'RV
for !i=1 to 7
if !i=1 then
equation exinf.gmm(b=a, n) vsmi_22d c arma_22d arima_22d egarch_22d smirv
@ c arma_22d arima_22d egarch_22d smirv
endif
if !i=2 then
equation exinf.gmm(b=a, n) vsmi_22d c arma_22d arima_22d egarch_22d smirv
@ c arma_22d arima_22d egarch_22d smirv smirv_22d smirv_15d
smirv_10d smirv_5d smirv_1d
endif
if !i=3 then
equation exinf.gmm(b=a, n) vsmi_22d c arma_22d arima_22d egarch_22d smirv
@ c arma_22d arima_22d egarch_22d smirv smirv_22d
endif
if !i=4 then
equation exinf.gmm(b=a, n) vsmi_22d c arma_22d arima_22d egarch_22d smirv
@ c arma_22d arima_22d egarch_22d smirv smirv_15d
endif
if !i=5 then
equation exinf.gmm(b=a, n) vsmi_22d c arma_22d arima_22d egarch_22d smirv
@ c arma_22d arima_22d egarch_22d smirv smirv_10d
endif
if !i=6 then
equation exinf.gmm(b=a, n) vsmi_22d c arma_22d arima_22d egarch_22d smirv
@ c arma_22d arima_22d egarch_22d smirv smirv_5d
endif
if !i=7 then
equation exinf.gmm(b=a, n) vsmi_22d c arma_22d arima_22d egarch_22d smirv
@ c arma_22d arima_22d egarch_22d smirv smirv_1d
endif
!dof1= exinf.@regobs-¬1
for !j=1 to 5
becker_rv(!i*4-¬3,!j)=exinf.@coefs(!j)
becker_rv(!i*4-¬2,!j)=exinf.@tstat(!j)
becker_rv(!i*4-¬1,!j)=@tdist(exinf.@tstat(!j), !dof1)
next !j
becker_rv(!i*4,2)=exinf.@regobs*exinf.@jstat
if !i=1 then
becker_rv(!i*4,2)=0 'does not exist
endif
if !i=2 then
becker_rv(!i*4,4)=1-¬@cchisq(becker_rv(!i*4,2),12-¬5)
endif
if !i>2 then
becker_rv(!i*4,4)=1-¬@cchisq(becker_rv(!i*4,2),1)
endif
'dRV
if !i=1 then
equation exinf.gmm(b=a, n) vsmi_22d c arma_22d arima_22d egarch_22d smirv
@ c arma_22d arima_22d egarch_22d smirv
endif
if !i=2 then
equation exinf.gmm(b=a, n) vsmi_22d c arma_22d arima_22d egarch_22d smirv
@ c arma_22d arima_22d egarch_22d smirv smidrv_22d smidrv_15d smidrv_10d smidrv_5d
smidrv_1d
endif
if !i=3 then
equation exinf.gmm(b=a, n) vsmi_30d c arma_30d arima_30d egarch_30d smirv
@ c arma_30d arima_30d egarch_30d smirv smidrv_22d
endif
if !i=4 then
equation exinf.gmm(b=a, n) vsmi_30d c arma_30d arima_30d egarch_30d smirv
@ c arma_30d arima_30d egarch_30d smirv smidrv_15d
endif
if !i=5 then
equation exinf.gmm(b=a, n) vsmi_30d c arma_30d arima_30d egarch_30d smirv
@ c arma_30d arima_30d egarch_30d smirv smidrv_10d
endif
if !i=6 then
equation exinf.gmm(b=a, n) vsmi_30d c arma_30d arima_30d egarch_30d smirv
@ c arma_30d arima_30d egarch_30d smirv smidrv_5d
endif
if !i=7 then
equation exinf.gmm(b=a, n) vsmi_30d c arma_30d arima_30d egarch_30d smirv
@ c arma_30d arima_30d egarch_30d smirv smidrv_1d
endif
!dof1= exinf.@regobs-¬1
for !j=1 to 5
becker_drv(!i*4-¬3,!j)=exinf.@coefs(!j)
becker_drv(!i*4-¬2,!j)=exinf.@tstat(!j)
becker_drv(!i*4-¬1,!j)=@tdist(exinf.@tstat(!j), !dof1)
next !j
becker_drv(!i*4,2)=exinf.@regobs*exinf.@jstat
if !i=1 then
becker_drv(!i*4,2)=0 'does not exist
endif
if !i=2 then
becker_drv(!i*4,4)=1-¬@cchisq(becker_drv(!i*4,2),12-¬5)
endif
if !i>2 then
becker_drv(!i*4,4)=1-¬@cchisq(becker_drv(!i*4,2),1)
endif
next !i
smpl @all
