## EMM Estimation of a Stochastic Volatility Model

```/*--------------------------------------------------------------

SAS Sample Library

Name: modex19.sas
Description: Example program from SAS/ETS User's Guide,
The MODEL Procedure
Title: EMM Estimation of a Stochastic Volatility Model
Product: SAS/ETS Software
Keys: nonlinear simultaneous equation models
PROC: MODEL
Notes:

--------------------------------------------------------------*/

%let nobs=1000;
data svdata;
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( 101 );
lnssq = a+b*log(ll**2) +s*u;
st = sqrt(exp(lnssq));
ll = st;
y = st * z;
if i > 0 then output;
end;
run;

title1 'Efficient Method of Moments for Stochastic Volatility Model';

/* estimate GARCH(1,1) model */
proc autoreg data=svdata(keep=y)
outest=garchest
noprint covout;
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 scores of 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 external product of score vector */
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;

/* Create a VDATA 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 inst 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 inst we use */
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
;

/* USE SMM TO FIND EMM ESTIMATES */

/* Generate dataset of length T */
data emm;
set garchest(obs=1 keep = _ah_0 _ah_1 _gh_1 _mse_);
do i=1 to 20000;
output;
end;
drop i;
run;

title2 'EMM estimates';
/* Find the EMM estimates */
proc model data=emm maxiter=1000;
parms a -0.736 b 0.9 s 0.363;
instrument _exog_ / intonly;

/* Describe the structural model */
u = rannor( 8801 );
z = rannor( 9701 );
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_);

/* Use 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 scores using SMM and estimated Vhat */
fit m1 m2 m3 / gmm npreobs=10 ndraw=1 /* smm options */
vdata=vhat /* use estimated Vhat */
kernel=(bart,0,) /* turn smoothing off */;
bounds s > 0, 1>b>0;
run;

```