Examples / EGARCHSIMULATE.RPF |
EGARCHSIMULATE.RPF is an example of simulating an EGARCH model for forecasting variance out-of-sample and/or producing a random sample from it. A companion (and almost identical) program EGARCHBOOTSTRAP.RPF does the same type of analysis, but with random simulations rather than bootstrapping.
This uses a sample data set of (old) exchange rates. The one being analyzed is the US$/Deutsche Mark exchange rate. The log returns are multiplied by 100 for improved performance of non-linear estimation. Note that this means that what you read off from the analysis are returns to 100 DM.
open data garch.asc
data(format=free,org=columns) 1 1867 bp cd dm jy sf
*
set dlogdm = 100*log(dm/dm{1})
This estimates a univariate GARCH model with asymmetry, saving the estimated variances in the series H. (Output)
garch(p=1,q=1,exp,asymmetric,hseries=h) / dlogdm
This pulls the coefficients out of the output.
compute gmu=%beta(1),c=%beta(2),a=%beta(3),b=%beta(4),d=%beta(5)
This pulls out the estimation range, and computes the standardized residuals.
compute gstart=%regstart(),gend=%regend()
*
set stdu = %resids/sqrt(h)
This sets the number of draws and number of out-of-sample steps:
compute ndraws=10000
compute nsteps=50
This is the out-of-sample range over which we will do the simulations.
compute bstart=gend+1,bend=gend+nsteps
We will be saving statistics on draws for H and for the data X over the BSTART to BEND range. For each of those, we need a VECTOR of size NDRAWS at each data point, so HDRAWS and XDRAWS will be type SERIES[VECT]. We also need series for the variance H and simulated values X to do the recursion in each simulation.
set h bstart bend = %na
set x bstart bend = %na
dec series[vect] hdraws xdraws
gset hdraws bstart bend = %zeros(ndraws,1)
gset xdraws bstart bend = %zeros(ndraws,1)
This is the draw loop. EGARCH is a recursion in H and the standardized residuals. This draws a standard Normal (into SU), computes the new value of H using the past values of H and STDU, computes a new value of X using the estimated mean plus a rescaled (by the standard deviation) SU, and saves the new draw for the Standard Normal for the next period. The SET instruction generates new values of H and X over the BSTART to BEND range. X is then accumulated into multiple period returns (the original X simulations are just simulated future single period returns) and saves the H and X values into the DRAW location in each of the HDRAWS and XDRAWS.
do draw=1,ndraws
set stdu bstart bend = su=%ran(1.0),$
h=exp(c+b*log(h{1})+a*abs(stdu{1})+d*stdu{1}),$
x=gmu+sqrt(h)*su,su
*
* Replace x by its accumulation
*
acc x bstart bend
do t=bstart,bend
compute hdraws(t)(draw)=h(t)
compute xdraws(t)(draw)=x(t)
end do t
end do draw
This extracts the .05 and .95 values for the H simulations for each out-of-sample entry, and graphs those.
set lower bstart bend = %fractiles(hdraws(t),.05)(1)
set upper bstart bend = %fractiles(hdraws(t),.95)(1)
set median bstart bend = %fractiles(hdraws(t),.50)(1)
*
graph(header="Variance Forecasts for EGARCH model using Simulations",min=0.0) 4
# h 1800 1867
# median 1868 1868+nsteps-1
# lower 1868 1868+nsteps-1 3
# upper 1868 1868+nsteps-1 3
And this extracts and graphs the lower 10% and 5% cumulative returns (again, per 100 DM).
set lower10 bstart bend = %fractiles(xdraws(t),.10)(1)
set lower05 bstart bend = %fractiles(xdraws(t),.05)(1)
graph(header="Extreme Cumulative Returns by Simulation") 2
# lower10
# lower05
Full Program
open data garch.asc
data(format=free,org=columns) 1 1867 bp cd dm jy sf
*
set dlogdm = 100*log(dm/dm{1})
garch(p=1,q=1,exp,asymmetric,hseries=h) / dlogdm
*
* Pull out the coefficients for the EGARCH process
*
compute gmu=%beta(1),c=%beta(2),a=%beta(3),b=%beta(4),d=%beta(5)
compute gstart=%regstart(),gend=%regend()
*
set stdu = %resids/sqrt(h)
*
compute ndraws=10000
compute nsteps=50
*
compute bstart=gend+1,bend=gend+nsteps
set h bstart bend = %na
set x bstart bend = %na
dec series[vect] hdraws xdraws
gset hdraws bstart bend = %zeros(ndraws,1)
gset xdraws bstart bend = %zeros(ndraws,1)
do draw=1,ndraws
set stdu bstart bend = su=%ran(1.0),$
h=exp(c+b*log(h{1})+a*abs(stdu{1})+d*stdu{1}),$
x=gmu+sqrt(h)*su,su
*
* Replace x by its accumulation
*
acc x bstart bend
do t=bstart,bend
compute hdraws(t)(draw)=h(t)
compute xdraws(t)(draw)=x(t)
end do t
end do draw
*
set lower bstart bend = %fractiles(hdraws(t),.05)(1)
set upper bstart bend = %fractiles(hdraws(t),.95)(1)
set median bstart bend = %fractiles(hdraws(t),.50)(1)
*
graph(header="Variance Forecasts for EGARCH model using Simulations",min=0.0) 4
# h 1800 1867
# median 1868 1868+nsteps-1
# lower 1868 1868+nsteps-1 3
# upper 1868 1868+nsteps-1 3
*
set lower10 bstart bend = %fractiles(xdraws(t),.10)(1)
set lower05 bstart bend = %fractiles(xdraws(t),.05)(1)
graph(header="Extreme Cumulative Returns by Simulation") 2
# lower10
# lower05
Output
GARCH Model - Estimation by BFGS
Convergence in 47 Iterations. Final criterion was 0.0000014 <= 0.0000100
Dependent Variable DLOGDM
Usable Observations 1866
Log Likelihood -2065.1214
Variable Coeff Std Error T-Stat Signif
************************************************************************************
1. Mean(DLOGDM) -0.027982045 0.016275592 -1.71926 0.08556624
2. C -0.184511441 0.024287988 -7.59682 0.00000000
3. A 0.215085921 0.028043749 7.66966 0.00000000
4. B 0.967687031 0.009281720 104.25729 0.00000000
5. D -0.017257427 0.012664163 -1.36270 0.17297778
Graphs
Copyright © 2025 Thomas A. Doan