Nonlinear estimation algorithms are required for obtaining estimates of the parameters of a regression model with innovations having an ARMA structure. The three estimation methods employed by the ARIMA procedure in SAS/ETS software are written in IML in the following program. The algorithms employed are slightly different from those used by PROC ARIMA, but the results obtained should be similar. This example combines the IML functions ARMALIK, PRODUCT, and RATIO to perform the estimation. Note the interactive nature of this example, illustrating how you can adjust the estimates when they venture outside the stationary or invertible regions.
proc iml;
reset noname;
/*-----------------------------------------------------------*/
/* name: ARMAREG Modules */
/* purpose: Perform Estimation for regression model with */
/* ARMA errors */
/* usage: Before invoking the command */
/* */
/* run armareg; */
/* */
/* define the global parameters */
/* */
/* x - matrix of predictors. */
/* y - response vector. */
/* iphi - defines indices of nonzero AR parameters, */
/* omit the index 0 which corresponds to the zero */
/* order constant one. */
/* itheta - defines indices of nonzero MA parameters, */
/* omit the index 0 which corresponds to the zero */
/* order constant one. */
/* ml - estimation option: -1 if Conditional Least */
/* Squares, 1 if Maximum Likelihood, otherwise */
/* Unconditional Least Squares. */
/* delta - step change in parameters (default 0.005). */
/* par - initial values of parms. First ncol(iphi) */
/* values correspond to AR parms, next ncol(itheta)*/
/* values correspond to MA parms, and remaining */
/* are regression coefficients. */
/* init - undefined or zero for first call to ARMAREG. */
/* maxit - maximum number of iterations. No other */
/* convergence criterion is used. You can invoke */
/* ARMAREG without changing parameter values to */
/* continue iterations. */
/* nopr - undefined or zero implies no printing of */
/* intermediate results. */
/* */
/* notes: Optimization using Gauss-Newton iterations */
/* */
/* No checking for invertibility or stationarity during */
/* estimation process. The parameter array par can be */
/* modified after running armareg to place estimates */
/* in the stationary and invertible regions, and then */
/* armareg can be run again. If a nonstationary AR operator */
/* is employed, a PAUSE will occur after calling ARMALIK */
/* because of a detected singularity. Using STOP will */
/* permit termination of ARMAREG so that the AR */
/* coefficients can be modified. */
/* */
/* T-ratios are only approximate and can be undependable, */
/* especially for small series. */
/* */
/* The notation follows that of the IML function ARMALIK; */
/* the autoregressive and moving average coefficients have */
/* signs opposite those given by PROC ARIMA. */
/* Begin ARMA estimation modules */
/* Generate residuals */
start gres;
noise=y-x*beta;
previous=noise[:];
if ml=-1 then do; /* Conditional LS */
noise=j(nrow(y),1,previous)//noise;
resid=product(phi,noise`)[,nrow(y)+1:nrow(noise)];
resid=ratio(theta,resid,ncol(resid));
resid=resid[,1:ncol(resid)]`;
end;
else do; /* Maximum likelihood */
free l;
call armalik(l,resid,std,noise,phi,theta);
/* Nonstationary condition produces PAUSE */
if nrow(l)=0 then do;
print ,
'In GRES: Parameter estimates outside stationary region.';
end;
else do;
temp=l[3,]/(2#nrow(resid));
if ml=1 then resid=resid#exp(temp);
end;
end;
finish gres; /* finish module GRES */
start getpar; /* get parameters */
if np=0 then phi=1;
else do;
temp=parm[,1:np];
phi=1||j(1,p,0);
phi[,iphi] =temp;
end;
if nq=0 then theta=1;
else do;
temp=parm[,np+1:np+nq];
theta=1||j(1,q,0);
theta[,itheta] =temp;
end;
beta=parm[,(np+nq+1):ncol(parm)]`;
finish getpar; /* finish module GETPAR */
/* Get SS Matrix - First Derivatives */
start getss;
parm=par;
run getpar;
run gres;
s=resid;
oldsse=ssq(resid);
do k=1 to ncol(par);
parm=par;
parm[,k]=parm[,k]+delta;
run getpar;
run gres;
s=s||((resid-s[,1])/delta); /* append derivatives */
end;
ss=s`*s;
if nopr^=0 then print ,'Gradient Matrix', ss;
sssave=ss;
do k=1 to 20; /* Iterate if no reduction in SSE */
do ii=2 to ncol(ss);
ss[ii,ii]=(1+lambda)*ss[ii,ii];
end;
ss=sweep(ss,2:ncol(ss)); /* Gaussian elimination */
delpar=ss[1,2:ncol(ss)]; /* update parm increments */
parm=par+delpar;
run getpar;
run gres;
sse=ssq(resid);
ss=sssave;
if sse<oldsse then do; /* reduction, no iteration */
lambda=max(lambda/10,1e-12);
k=21;
end;
else do; /* no reduction */
/* increase lambda and iterate */
if nopr^=0 then print ,
'Lambda=' lambda 'SSE=' sse 'OLDSSE=' oldsse,
'Gradient Matrix', ss ;
lambda=min(10*lambda,1e12);
if k=20 then do;
print 'In module GETSS:'
'No improvement in SSE after twenty iterations.';
print ' Possible Ridge Problem. ';
return;
end;
end;
end;
if nopr^=0 then print ,'Gradient Matrix', ss;
finish getss; /* Finish module GETSS */
start armareg; /* ARMAREG main module */
/* Initialize options and parameters */
if nrow(delta)=0 then delta=0.005;
if nrow(maxiter)=0 then maxiter=5;
if nrow(nopr)=0 then nopr=0;
if nrow(ml)=0 then ml=1;
if nrow(init)=0 then init=0;
if init=0 then do;
p=max(iphi);
q=max(itheta);
np=ncol(iphi);
nq=ncol(itheta);
/* Make indices one-based */
do k=1 to np;
iphi[,k]=iphi[,k]+1;
end;
do k=1 to nq;
itheta[,k]=itheta[,k]+1;
end;
/* Create row labels for Parameter estimates */
if p>0 then parmname = concat("AR",char(1:p,2));
if q>0 then parmname = parmname||concat("MA",char(1:q,2));
parmname = parmname||concat("B",char(1:ncol(x),2));
/* Create column labels for Parameter estimates */
pname = {"Estimate" "Std. Error" "T-Ratio"};
init=1;
end;
/* Generate starting values */
if nrow(par)=0 then do;
beta=inv(x`*x)*x`*y;
if np+nq>0 then par=j(1,np+nq,0)||beta`;
else par=beta`;
end;
print ,'Parameter Starting Values',;
print par [colname=parmname]; /* stderr tratio */
lambda=1e-6; /* Controls step size */
do iter=1 to maxiter; /* Do maxiter iterations */
run getss;
par=par+delpar;
if nopr^=0 then do;
print ,'Parameter Update',;
print par [colname=parmname]; /* stderr tratio */
print ,'Lambda=' lambda,;
end;
end;
sighat=sqrt(sse/(nrow(y)-ncol(par)));
print ,'Innovation Standard Deviation:' sighat;
ss=sweep(ss,2:ncol(ss)); /* Gaussian elimination */
estm=par`||(sqrt(diag(ss[2:ncol(ss),2:ncol(ss)]))
*j(ncol(par),1,sighat));
estm=estm||(estm[,1] /estm[,2]);
if ml=1 then print ,'Maximum Likelihood Estimation Results',;
else if ml=-1 then print ,
'Conditional Least Squares Estimation Results',;
else print ,'Unconditional Least Squares Estimation Results',;
print estm [rowname=parmname colname=pname] ;
finish armareg;
/* End of ARMA Estimation modules */
/* Begin estimation for Grunfeld's investment models */
use grunfeld;
read all var {gei} into y;
read all var {gef gec} into x;
close grunfeld;
x=j(nrow(x),1,1)||x;
iphi=1;
itheta=1;
maxiter=10;
delta=0.0005;
ml=-1;
/*---- To prevent overflow, specify starting values ----*/
par={-0.5 0.5 -9.956306 0.0265512 0.1516939};
run armareg; /*---- Perform CLS estimation ----*/