{"word_count": 430, "character_count": 3593}
Consider a (stationary) autoregressive process, say of order 2,for some white noise  with variance . Here is a code to generate such a process,&#62; phi1=.5&#62; phi2=-.4&#62; sigma=1.5&#62; set.seed(1)&#62; n=240&#62; WN=rnorm(n,sd=sigma)&#62; Z=rep(NA,n)&#62; Z[1:2]=rnorm(2,0,1)&#62; for(t in 3:n){Z[t]=phi1*Z[t-1]+phi2*Z[t-2]+WN[t]}Here, we have to estimate two sets of parameters: the autoregressive coefficients, and the variance of the innovation process . There are (at least) three techniques to estimate those parameters.using least square regressionA natural idea is to see here a regression model, and thus, if we consider a matrix formulation,Here we can run (conditional) ordinary least squares estimation,&#62; base=data.frame(Y=Z[3:n],X1=Z[2:(n-1)],X2=Z[1:(n-2)])&#62; regression=lm(Y~0+X1+X2,data=base)&#62; summary(regression)Call:lm(formula = Y ~ 0 + X1 + X2, data = base)Residuals:Min      1Q  Median      3Q     Max-4.3491 -0.8890 -0.0762  0.9601  3.6105Coefficients:Estimate Std. Error t value Pr(&#62;|t|)X1  0.45107    0.05924   7.615 6.34e-13 ***X2 -0.41454    0.05924  -6.998 2.67e-11 ***---Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1Residual standard error: 1.449 on 236 degrees of freedomMultiple R-squared: 0.2561,Adjusted R-squared: 0.2497F-statistic: 40.61 on 2 and 236 DF,  p-value: 6.949e-16&#62; regression$coefficientsX1         X20.4510703 -0.4145365&#62; summary(regression)$sigma[1] 1.449276using Yule-Walker equationsAs we've seen in class, we can easily get the following equations for the autocovariance functions,which can also be writtenSo we just have to solve a simple linear system of equations. Note that if we divide by the variance, those equations can be written in terms of the autocorrelation functionsThe code is the following&#62; rho1=cor(Z[1:(n-1)],Z[2:n])&#62; rho2=cor(Z[1:(n-2)],Z[3:n])&#62; A=matrix(c(1,rho1,rho1,1),2,2)&#62; b=matrix(c(rho1,rho2),2,1)&#62; (PHI=solve(A,b))[,1][1,]  0.4517579[2,] -0.4155920Now, we need to extract the estimated innovation process, from this set of parameters (note that it could be possible to include the variance term in Yule-Walker equations, to get a three dimensional linear equation)&#62; estWN=base$Y-(PHI[1]*base$X1+PHI[2]*base$X2)&#62; sd(estWN)[1] 1.445706This estimator is probably not the best one (we can take into account that we've lost two degrees of freedom), but as a starting point, let us consider this one.using (conditional) likelihood estimatorsFinally, we can assume some distribution for the innovation process. Thestandard model is a Gaussian model, i.e.In that case, the conditional log likelihood (conditional since we set the first two observations here) is&#62; CondLogLik=function(A,TS){+ phi1=A[1];  phi2=A[2]+ sigma=A[3]; L=0+ for(t in 3:length(TS)){+ L=L+dnorm(TS[t],mean=phi1*TS[t-1]++ phi2*TS[t-2],sd=sigma,log=TRUE)}+ return(-L)}Now, we can run standard optimization procedures,&#62; LogL=function(A) CondLogLik(A,TS=Z)&#62; optim(c(0,0,1),LogL)$par[1]  0.4509685 -0.4144938  1.4430930$value[1] 425.0164$countsfunction gradient88       NA$convergence[1] 0$messageNULLHere, our three estimators are rather close. Actually, if we generate 1,000 time series (of size 240), those are the Box-plots of our three estimators, for the first order autoregressive coefficientfor the second one,and finally for the standard deviation of the innovation processAll those estimators behave nicely, and are rather close. Note that they all might be biased, but they are consistent (see Davidson and MacKinnon for instance, in their book, for more details).