10.07.2015 Views

Homework 1 - Jacek Suda

Homework 1 - Jacek Suda

Homework 1 - Jacek Suda

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

(j) Estimate an AR(2) model for the time trend case by typing the following in Quick/EstimateEquation, lgdpq c t ar(1) ar(2), with sample period set to 1955:1 1969:4 in the samplefield. Paste output. Click the Forecast button. In the Forecast name field type lgdpqf1.In the Standard Error field type sef1. In the sample range field type 1970:1 2007:3.Click Okay. Type genr uf1=lgdpqf1+1.96*sef1 and genr lf1=lgdpqf1-1.96*sef1 to getstandard error bands. Select lgdpq, lgdpqf1, uf1, and lf1 (hold down the ctrl key toselect multiple series). Click View/Open Selected One Window/Open Group. ClickView again and select Graph/Line. You should have a picture with the series forecastfor the time trend model (with 95% confidence bands) and the actual realized series.Paste graph. What do you notice?NOTE: be very careful to follow directions about sample periods here!!!(k) Again, setting the sample to 1955:1 1969:4, estimate an AR(2) model for the first differencescase (i.e., an ARIMA(2,1,0) model for lgdpq) by typing d(lgdpq) c ar(1) ar(2). Bytyping the equation in this d(*) format, you will get forecasts for the level, rather thanfirst differences. Paste output. Click the Forecast button. In the Forecast name fieldtype lgdpqf2. In the Standard Error field type sef2. In the sample range field type 1970:12007:3. Click Okay. Type genr uf2=lgdpqf2+1.96*sef2 and genr lf2=lgdpqf2-1.96*sef2.Select and graph series as in part j. Paste graph.4


* PROGRAM SIMULATES AR(1) DATAALSO, CALCULATES MLE FOR AR(1)3 PROCEDURES1. Log likelihood for CMLE2. Log likelihood for full sample MLE3. Transformation to constrain parametersPROGRAM DESIGNED BY JAMES MORLEY 1/23/2000MODIFIED AND ADAPTED BY <strong>Jacek</strong> <strong>Suda</strong> 12/2/2009 */@INITIAL GAUSS STUFF@new;format /m1 /rd 9,6;@clear memory@@set details of output format@@SIMULATE DATA@mu=1;phi=0.5;sig_e=0.5;T=50;@unconditional mean@@autoregressive coefficient@@standard deviation of error term@@sample size@if abs(phi) ge 1;y0=0;@initial demeaned value for nonstationary case@else;y0= sqrt((sig_e^2)/(1-phi^2))*rndn(1,1);@initial demeaned value for stationary case@endif;e=sig_e*rndn(T,1); @Normal random errors with variance sig_e^2@u=recserar(e,y0,phi); @autoregressive series with zero mean@y = mu + u;@autoregressive series with mean mu when abs(phi)


@here@/*output file=hw1.out reset;output off;@ESTIMATE CMLE AR(1) PARAMETERS@prmtr_in=0|phi|ln(sig_e);{xout,fout,gout,cout}=QNewton(&lik_fcn1,prmtr_in);prm_fnl1=trans_cmle(xout);lik1=-fout;hessn0=hessp(&lik_fcn1,xout); @calculate hessian@cov0=inv(hessn0);@estimate var-cov for parameters@grdn_fnl=gradp(&TRANS_CMLE,xout);cov=grdn_fnl*cov0*grdn_fnl’; @delta method to address transformation@SD1 =sqrt(diag(cov));@calculate standard errors@@ESTIMATE exact AR(1) PARAMETERS@prmtr_in=mu|0|ln(sig_e);{xout,fout,gout,cout}=QNewton(&lik_fcn2,prmtr_in);prm_fnl2=trans_emle(xout);lik2=-fout;hessn0=hessp(&lik_fcn2,xout); @calculate hessian@cov0=inv(hessn0);@estimate var-cov for parameters@grdn_fnl=gradp(&TRANS_EMLE,xout);cov=grdn_fnl*cov0*grdn_fnl’; @delta method to address transformation@SD2 =sqrt(diag(cov));@calculate standard errors@output on;"CMLE Estimates (standard errors in parenthesis)";"c phi sig_e";prm_fnl1’;"(";;sd1’;;")";""; "ll=";;lik1;"";if abs(prm_fnl1[2])


" Exact MLE Estimates (standard errors in parenthesis)";"mu phi sig_e";prm_fnl2’;"(";;sd2’;;")";""; "ll=";;lik2;output off;END;@End of main program@/*-------------------*/@PROCEDURE TO CALCULATE CONDITIONAL LIKELIHOOD VALUE FOR AR(1)@proc lik_fcn1(prmtr1);local prmtr,c_hat,phi_hat,sig_hat,j,e_t,llikv;@local variables for procedure@prmtr=trans_cmle(prmtr1); @constrain parameters@c_hat=prmtr[1];phi_hat=prmtr[2];sig_hat=prmtr[3];@Initialize for CONDITIONAL MLE@llikv = 0;@Errors 2 through T needed to calculate CONDITIONAL (treat y[1] asgiven) likelihood@j=2;do until j>T;e_t = y[j] - c_hat - phi_hat*y[j-1];llikv = llikv -0.5*ln(2*PI*sig_hat^2)-0.5*(e_t^2)/sig_hat^2; @loglikelihood value@j=j+1;endo;retp(-llikv);endp;@return minus llikv since qnewton minimizes function@7


*-------------------*/@PROCEDURE TO CALCULATE EXACT LIKELIHOOD VALUE FOR AR(1)@proc lik_fcn2(prmtr1);local prmtr,mu_hat,phi_hat,sig_hat,j,e_t,llikv;@local variables for procedure@prmtr=trans_emle(prmtr1);@constrain parameters@mu_hat=prmtr[1];phi_hat=prmtr[2];sig_hat=prmtr[3];@Initialize for FULL SAMPLE MLE@e_t = y[1] - mu_hat;llikv = -0.5*ln(2*PI*((sig_hat^2)/(1-phi_hat^2)))-0.5*(e_t^2)/((sig_hat^2)/(1-phi_hat^2));@log likelihood value@@Errors 2 through T and log likelihood@j=2;do until j>T;e_t = y[j] - mu_hat - phi_hat*(y[j-1] - mu_hat);llikv = llikv -0.5*ln(2*PI*sig_hat^2)-0.5*(e_t^2)/sig_hat^2;@log likelihood value@j=j+1;endo;retp(-llikv);endp;@return minus llikv since optmum minimizes@/*-------------------*/@PROCEDURE TO CONSTRAIN PARAMETERS@proc trans_cmle(c0);local c1;c1 = c0;c1[3]=exp(c0[3]);@constrain variance to be non-negative@8


etp(c1);endp;/*-------------------*//*-------------------*/@PROCEDURE TO CONSTRAIN PARAMETERS@proc trans_emle(c0);local c1;c1 = c0;c1[2]=c0[2]/(1+abs(c0[2])); @constrain AR to be stable@c1[3]=exp(c0[3]); @constrain variance to be non-negative@retp(c1);endp;/*-------------------*/*/9

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!