Thanks.
Here's an attempt to estimate and generate 1-step ahead VaR and ES forecasts, SV State-Space model, using fixed rolling windows. I'm ultimately aiming for multistep ahead: h=2, h=3, ... , h=T, saving the hth forecast, as with SKIPSAVE in FORECAST instruction.
Do I need to loop round the DLM instruction, because DLM's yhat is the 1-step ahead forecasts?
The following probably has errors.
Code: Select all
*===============================
open data xrates.xls
data(format=xls,org=columns) 1 946 usxuk usxger usxjpn usxsui
*===============================
set retsq = 100.0*((log(usxjpn{0}/usxjpn{1}))**2.0)
set dlp = 100.0*log(usxjpn{0}/usxjpn{1})
set dlp = -dlp; * would give you the return to a long position of $100 US
set y = dlp
prin /
disp %allocend()
*===============================
comp WIN = 750
compute rstart= 1
compute rend = WIN
*===============================
*
* meanx2=mean of log chi-square,varx2=variance of log chi-square
*
compute meanx2=%digamma(0.5)-log(0.5)
compute varx2 =%trigamma(0.5)
*===============================
infobox(action=define,lower=0,upper=%allocend()-WIN-1,progress) "SV Model Backtest: JPYUSD"
infobox(action=modify) "LONG"
*
do jj = 0, %allocend()-WIN
*
* The "gamma" and "phi" variables are very highly correlated when
* phi is near 1, which makes estimation by general hill-climbing
* procedures somewhat tricky. Instead, we reparameterize this to
* use gammax=gamma/(1-phi) in place of gamma.
*
* Remove any deterministic components (in this case, just the CONSTANT).
*
linreg dlp rstart+1+jj rend+1+jj
# constant
set demean = %resids
*
nonlin phi sw gammax
set ysq rstart+1+jj rend+1+jj = log(demean^2)-meanx2
*
* Get initial guess values from ARMA(1,1) model
*
boxjenk(ar=1,ma=1,constant,print,MAXL) ysq rstart+1+jj rend+1+jj
*
* PHI is taken directly as the AR(1) coefficient The variance SW is backed out by
* matching first order autocorrelations in the MA term. gammax is chosen as the
* mean of the ysq series. (BOXJENK sets %MEAN to the mean of the dependent
* variable).
*
compute phi=%beta(2),sw=-phi*varx2*(1+%beta(3)^2)/%beta(3)-(1+phi^2)*varx2
compute sw=%if(sw<0,.1,sw)
compute gammax=%mean
*
* Estimate the unconstrained model
*
dlm(method=bfgs,sw=sw,sv=varx2,y=ysq,type=filter,c=1.0, $
sx0=sw/(1-phi^2),x0=gammax,a=phi,z=gammax*(1-phi), $
yhat=yhat,svhat=fhat) rstart+1+jj rend+1+jj states
INFOBOX(CURRENT=jj)
end do jj
*
infobox(action=remove)
*===============================
set fy WIN+1+1 %allocend()+1 = %scalar(yhat(t))
set efy WIN+1+1 %allocend()+1 = exp(fy)
set VaR WIN+1+1 %allocend()+1 = -%invnormal(.01) * sqrt(efy)
set ES WIN+1+1 %allocend()+1 = %density(%invnormal(.01))/(.01) * sqrt(efy)
print / retsq efy VaR ES