# SAS/ETS Examples

## Efficient Method of Moments Estimation of a Stochastic Volatility Model

Contents | Back to Example

# SAS Program

```   /*---------------------------------------------------------------------------------
Example: Efficient Method of Moments Estimation of a Stochastic Volatility Model
Requires: SAS/ETS
Version: 9.0
---------------------------------------------------------------------------------*/

/*-------------- Define n and T --------------------*/
%let nobs=1000;
%let nsim=20000;

/* ------------- Generate model data ---------------*/
data _tmpdata;
a = -0.736; b=0.9; s=0.363;
ll=sqrt( exp(a/(1-b)));;
do i=-10 to &nobs;
u = rannor( 101 );
z = rannor( 98761 );
lnssq = a + b*log(ll**2) +s*u;
st = sqrt(exp(lnssq));
ll = st;
y = st * z;
if i > 0 then output;
end;
run;

/*-------- estimate GARCH(1,1) model ---------------*/
proc autoreg data=_tmpdata outest=garchest;
model y =  / noint garch=( q=1, p=1 ) ;
output out=garchout cev=gsigmasq r=resid;
run;

/* --------------- compute the V matrix ------------------*/

data vvalues;
set garchout(keep=y gsigmasq resid);

/* --- compute the scores of the GARCH model -----*/
score_1 = (-1 + y**2/gsigmasq)/ gsigmasq;
score_2 = (-1 + y**2/gsigmasq)*lag(gsigmasq) / gsigmasq;
score_3 = (-1 + y**2/gsigmasq)*lag(y**2) / gsigmasq;

array score{*} score_1-score_3;
array v_t{*} v_t_1-v_t_6;
array v{*} v_1-v_6;

/* - compute the external product of the score vectors - */
do i=1 to 3;
do j=i to 3;
v_t{j*(j-1)/2 + i} = score{i}*score{j};
end;
end;

/* -------- average them over t ------- */
do s=1 to 6;
v{s}+ v_t{s}/&nobs;
end;
run;

proc print data=vvalues(firstobs=&nobs);
var v_1-v_6;
run;

/* --------- Create a dataset acceptable to PROC MODEL --------- */

/* ---- Transpose the last obs in the dataset ----- */
proc transpose data=vvalues(firstobs=&nobs keep=v_1-v_6) out=tempv;
run;

/* ----- Add eq and instrument labels -----*/
data vhat;
set tempv(drop=_name_);
value = col1;
drop col1;
input _type_ \$ eq_row \$ eq_col \$ inst_row \$ inst_col \$;
datalines;
gmm m1 m1 1 1  /* intcpt is the only instrument */
gmm m1 m2 1 1
gmm m2 m2 1 1
gmm m1 m3 1 1
gmm m2 m3 1 1
gmm m3 m3 1 1
;
run;

proc print data=vhat;
run;

/* ---------------------- FIND EMM ESTIMATES ---------------------*/

/* ------- Simulate the error sequences with datastep -------*/

data emm;
set garchest(obs=1 keep = _mse_ _ah_0 _ah_1 _gh_1);
do i=1 to &nsim;
u = rannor( 8801 );
z = rannor( 9701 );
output;
end;
drop i;
run;

/* ------------- Estimate parameters with GMM -----------------*/
title 'EMM Estimates';
proc model data=emm;
parms a -.7 b .9 s .363;
instrument _exog_ / intonly;

/*-- Describe the structural model ----*/
lsigmasq = xlag(sigmasq,exp(a));

lnsigmasq = a + b * log(lsigmasq) + s * u;

sigmasq = exp( lnsigmasq );

ysim = sqrt(sigmasq) * z;

/*--- Volatility of the GARCH model ---*/
gsigmasq = _ah_0 + _gh_1*xlag(gsigmasq, _mse_) +
_ah_1*xlag(ysim**2, _mse_);

/*---- Scores of the GARCH model as moment conditions ----*/
eq.m1 = (-1 + ysim**2/gsigmasq)/ gsigmasq;

eq.m2 = (-1 + ysim**2/gsigmasq)*xlag(gsigmasq, _mse_) / gsigmasq;

eq.m3 = (-1 + ysim**2/gsigmasq)*xlag(ysim**2, _mse_) / gsigmasq;

/*----- Fit the GARCH scores using GMM and estimated Vhat -----*/
fit m1 m2 m3 / gmm vdata=vhat kernel=(bart,0,) no2sls;
bounds s > 0, -1<b<1;

run;

title '';
```