Reference:http://www.principlesofeconometrics.com/poe4/poe4sas_files/chap14.sas
/* file chap14.sas */
/* Set up Information */
/* This code assumes that the SAS data sets for
Principles of Econometics 4e are in the default
directory. */
/* Data sets can be downloaded from
http://www.principlesofeconometrics.com/poe4/poe4sas.htm
See http://www.principlesofeconometrics.com/poe4/usingsas.htm
for SAS code for all chapters in Principles of Econometrics, 4e
Hill, Griffiths and Lim (2011), John Wiley & Sons, Inc.
Copyright C 2012 by R. Carter Hill and Randall C. Campbell
used for "Using SAS for Econometrics"
by R. Carter Hill and Randall C. Campbell (2012)
John Wiley and Sons, Inc. */
options nodate nonumber linesize=78 label;
data returns; * open data set;
set 'returns'; * read returns;
run;
proc contents data=returns position; * examine contents;
run;
options nolabel; * turn off labels;
/* summary statistics using PROC MEANS */
proc means data=returns;
title 'summary statistics for stock returns data';
run;
/* create date variable */
data returns; * open data set;
set returns; * read data;
retain date '1dec87'd; * date variable;
date=intnx('mon',date,1); * update dates;
format date yymmc.; * format for date;
year = 1988 + int((_n_-1)/12); * year;
month = mod(_n_-1, 12) + 1; * month;
run;
proc print data=returns (obs=6);
title 'returns data with date variables';
run;
/* plot series using PROC GPLOT */
symbol1 value=point interpol=join; * symbol for diagram;
proc gplot data=returns;
plot nasdaq*date=1 / hminor=1;
title 'United States: Nasdaq';
run;
plot allords*date=1 / hminor=1;
title 'Australia: All Ordinaries';
run;
plot ftse*date=1 / hminor=1;
title 'United Kingdom: FTSE';
run;
plot nikkei*date=1 / hminor=1;
title 'Japan: Nikkei';
run;
/* generate histograms of returns for each series */
proc univariate data=returns;
var nasdaq;
histogram / normal endpoints=-30 to 30 by 2.5;
title 'United States: Nasdaq';
run;
proc univariate data=returns;
var allords;
histogram / normal endpoints=-20 to 20 by 1;
title 'Australia: All Ordinaries';
run;
proc univariate data=returns;
var ftse;
histogram / normal endpoints=-16 to 20 by 1;
title 'United Kingdom: FTSE';
run;
proc univariate data=returns;
var nikkei;
histogram / normal endpoints=-30 to 30 by 2.5;
title 'Japan: Nikkei';
run;
/* open byd */
data byd; * open data set;
set 'byd'; * read byd;
time = _n_; * time variable;
run;
/* summary statistics */
proc means data=byd;
title 'summary statistics for BYD data';
run;
/* plot series using PROC GPLOT */
symbol1 value=point interpol=join; * symbol for diagram;
proc gplot data=byd;
plot r*time=1 / hminor=1;
title 'BYD returns';
run;
/* regress byd returns on constant and save residuals */
proc autoreg data=byd;
model r = ;
output out=bydout r=ehat;
title 'estimate mean of byd returns and save residuals';
run;
/* create squared residual and its lag for ARCH test */
data bydout; * open data set;
set bydout; * read data;
ehatsq = ehat**2; * squared residuals;
ehatsq1 = lag(ehatsq); * lagged squared residuals;
run;
/* test for ARCH effects */
proc autoreg data=bydout;
model ehatsq = ehatsq1; * auxiliary regression;
title 'test for ARCH effects in byd data';
run;
/* calculate LM test statistic and critical values */
data archtest;
t = 500; * sample size;
q = 1; * # ARCH effects;
rsq = 0.1246; * Regression R-sq;
lm = (t-q)*rsq; * LM test statistic;
chic_95 = cinv(.95,q); * 95% critical value;
chic_99 = cinv(.99,q); * 99% critical value;
pval = 1 - probchi(lm,1); * p-value;
run;
proc print data=archtest; * print;
var t rsq lm chic_95 chic_99 pval; * variable list;
title 'LM test for ARCH effects';
run;
/* estimate ARCH(1) model using PROC AUTOREG */
proc autoreg data=bydout;
model r = / method=ml archtest
garch=(q=1); * ARCH(1) errors;
output out=bydout r=ehat_arch ht=harch;* forecast volatility;
title 'estimate ARCH(1) model and forecast volatility';
run;
/* estimate ARCH(1) model using PROC MODEL */
proc model data=bydout; * initiate model;
r = intercept; * mean equation;
h.r = arch0+arch1*xlag(resid.r**2, mse.r); * variance equation;
fit r / fiml method=marquardt;
bounds arch0 arch1 >= 0; * restrict arch parameters;
title 'ARCH(1) estimates using PROC MODEL';
run;
/* plot conditional variances using PROC GPLOT */
proc gplot data=bydout;
plot harch*time=1 / hminor=10;
title 'plot of conditional variance: ARCH(1) model';
run;
/* estimate GARCH(1,1) model using PROC AUTOREG */
proc autoreg data=bydout;
model r = / method=ml archtest
garch=(p=1,q=1); * GARCH(1,1) errors;
output out=bydout r=ehat_garch ht=hgarch11;* forecast volatility;
title 'estimate GARCH(1,1) model and forecast volatility';
run;
/* plot conditional variances using PROC GPLOT */
proc gplot data=bydout;
plot hgarch11*time=1 / hminor=10;
title 'plot of conditional variance: GARCH(1,1) model';
run;
/* estimate GARCH(1,1) model using PROC MODEL */
proc model data=bydout; * initiate model;
parms arch0 .1 arch1 .1 garch .1;
r = intercept; * mean equation;
h.r = arch0+arch1*xlag(resid.r**2, mse.r)+garch1*xlag(h.r, mse.r);
fit r / fiml method=marquardt;
bounds arch0 arch1 garch1 >= 0; * restrict arch parameters;
title 'GARCH(1,1) estimates using PROC MODEL';
run;
/* create bad news indicator variable and interaction term */
data bydout; * open data set;
set bydout; * read data;
dt = (ehat_garch<0); * bad news indicator;
dt1 = lag(dt); * lag;
ehat_gsq = (ehat_garch**2); * squared residual;
ehat_gsq1 = lag(ehat_gsq); * lag;
desq1 = dt1*(ehat_gsq1); * variable for TGARCH;
run;
proc print data=bydout (obs=5);
title 'byd data with bad news indicator';
run;
/* estimate T-GARCH(1,1) model using PROC AUTOREG */
proc autoreg data=bydout;
model r = / method=ml archtest
garch=(p=1,q=1); * GARCH(1,1) errors;
output out=bydout ht=htgarch; * forecast volatility;
hetero desq1; * bad news term;
title 'estimate T-GARCH(1,1) model and forecast volatility';
run;
/* plot conditional variances using PROC GPLOT */
proc gplot data=bydout;
plot htgarch*time / hminor=10;
title 'plot of conditional variance: T-GARCH(1,1) model';
run;
/* estimate T-GARCH(1,1) model using PROC MODEL */
proc model data=bydout; * initiate model;
label intercept='mean'
arch0='var intercept'
arch1='et_1_sq'
gamma='dt_1*et_1_sq'
garch1='ht_1';
parms arch0 .1 arch1 .1 garch .1 delta .1;
r = intercept; * mean equation;
h.r = arch0+arch1*xlag(resid.r**2,mse.r)+garch1*xlag(h.r,mse.r)
+gamma*xlag(-resid.r<0,mse.r)*xlag(resid.r**2,mse.r);
fit r / fiml method=marquardt;
bounds arch0 arch1 garch1 >= 0; * restrict arch parameters;
title 'T-GARCH(1,1) estimates using PROC MODEL';
run;
/* estimate GARCH-M model with time varying volatility */
proc autoreg data=bydout;
model r = / method=ml archtest
garch=(p=1,q=1,mean=linear);* GARCH-M(1,1) errors;
hetero desq1; * bad news term;
output out=bydout ht=hgarchm p=preturn;* forecast volatility;
title 'GARCH-M model';
run;
/* plot conditional variance using PROC GPLOT */
proc gplot data=bydout;
plot hgarchm*time=1 / hminor=10;
title 'plot of conditional variance: GARCH-M';
run;
/* plot predicted returns using PROC GPLOT */
proc gplot data=bydout;
plot preturn*time=1 / hminor=10;
title 'plot of predicted returns: GARCH-M';
run;
/* estimate T-GARCH(1,1) model using PROC MODEL */
proc model data=bydout; * initiate model;
label intercept='mean'
theta='h_t (return due to risk)'
arch0='var intercept'
arch1='et_1_sq'
gamma='dt_1*et_1_sq'
garch1='ht_1';
parms arch0 .1 arch1 .1 garch .1 delta .1 theta .1;
h = arch0+arch1*xlag(resid.r**2,mse.r)+garch1*xlag(h.r,mse.r)
+gamma*xlag(-resid.r<0,mse.r)*xlag(resid.r**2,mse.r);
r = intercept+theta*h; * mean equation;
h.r = h;
fit r / fiml method=marquardt;
bounds arch0 arch1 garch1 >= 0; * restrict arch parameters;
title 'GARCH-M estimates using PROC MODEL';
run;