19  MA and AR processes

Setup
library(dplyr)
# required for figures  
library(ggplot2)
library(gridExtra)
# required for render latex 
library(backports)
library(latex2exp)
# required for render tables
library(knitr)
library(kableExtra)
# Random seed 
set.seed(1)

19.1 MA(q)

In time series analysis an univariate time series Yt is defined as Moving Average process with order q (MA(q)), when it satisfy the equations of the differences, i.e.  (19.1)Yt=ut+θ1ut1++θqutq, where utWN(0,σu2) (). An MA(q) process can be equivalently expressed as a polynomial in θ of the Lag operator (), i.e.  Yt=Θ(L)ut,utWN(0,σu2), where Θ(L) is a polynomial of the form Θ(L)=1+θ1L++θqLq. Given this representation it is clear that an MA(q) process is stationary independently on the value of the parameters. Moreover, the stationary process Yt admits has a infinite moving average or MA() representation (see Wold ()) if it satisfies the equations of differences, i.e.  Yt=Ψ(L)ut=ut+ψ1ut1+=i=1ψjutj, under the following condition the process is stationary and ergodic, i.e.
i=1|ψj|<. If the above condition holds, then the process MA() can be written in compact form as: Yt=Ψ(L)ut,utWN(0,σu2),Ψ(L)=i=1ψjLj.

Proposition 19.1 (Expectation of an MA(q))
In general, the expected value of an MA(q) process depends on the distribution of ut. Under the standard assumption that ut is a White Noise (), then it’s expected value is equal to zero for every t, i.e.  E{Yt}=i=0qθiE{uti}=0.

Proof. Given a process Yt such that E{Yt}=μ, it is always possible to simply reparametrize the as: Yt=μ+ut+θ1ut1++θqutq, or rescale the process, i.e Y~t=Ytμ and work under a process with zero mean. Then, let’s consider a process an MA process of order q, then the expectation of the process is computed as E{Yt}=E{ut+i=1qθiuti}=i=0qθiE{uti}=0. Hence, the expected value of Yt depends on the expected value of the residuals ut, that under the White Noise assumption is zero for every t.

Proposition 19.2 (Autocovariance of an MA(p))
For every lag k>0, the autocovariance function, denoted as γk, is defined as: γk=Cv{Yt,Ytk}={σu2j=0qkθjθj+kkq0k>q. where by convention θ0=1.

The covariance is different from zero only when the lag k is lower than the order of the process q. Setting k=0 one obtain the variance V{Yt}, i.e. γ0=σu2i=1qθi2. It follows that, the autocorrelation function is bounded up to the lag q, i.e.  ρk={1k=0σu2j=0qkθjθj+kσu2(1+θ12++θq2)0<kq0k>q where ρk=Cr{Yt,Ytk}.

19.1.1 MA(1)

Proposition 19.3 (Moments an MA(1))
Let’s consider a process YtMA(1), i.e. Yt=μ+θ1ut1+ut. Independently, from the specific distribution of ut, the process has to be a White Noise, hence with an expected value equal to zero. Therefore, the expectation of an MA(1) process is equal to μ, i.e. E{Yt}=μ. The variance instead is equal to γ0=V{Yt}=σu2(1+θ12). In general, the auto covariance function for the order k is defined as γk=Cv{Yt,Ytk}={θ1σu2k10k>1 It follows that, the auto covariance function is bounded up to the first lag, i.e.  k=0|γk|=σu2(1+θ1+θ12), and therefore the process is always stationary without requiring any condition on the parameter θ1. Also the autocorrelation is different from zero only between the first two lags, i.e. the process is said to have a short memory ρk=Cr{Yt,Ytk}={θ11+θ12k10k>1

Proof. Let’s consider an MA(1) process Yt=μ+θ1ut1+ut, where ut is a White Noise process (). The expected value of Yt depends on the intercept μ, i.e. E{Yt}=μ+θ1E{ut1}+E{ut}=μ. Under the White Noise assumption the residuals are uncorrelated, hence the variance is computed as γ0=V{μ+ut+θ1ut1}==V{ut+θ1ut1}==V{ut}+θ12V{ut1}==σu2+θ12σu2==σu2(1+θ12) By definition, the autocovariance function between time t and a generic lagged value tk reads γk=Cv{Yt,Ytk}=E{YtYtk}E{Yt}E{Ytk}==E{(μ+ut+θ1ut1)(μ+utk+θ1utk1)}μ2==E{ututk}+θ1E{utkut1}+θ1E{ututk1}+θ12E{ut1utk1}++μ2+μE{utk}+μθ1E{utk1}+μE{ut}+μθ1E{ut1}==θ1E{utkut1} This is a consequence of ut being a White Noise and so uncorrelated in time, i.e. E{ututk}=0 for every t. This implies that, also the correlation between two lags is zero if k>1.

Example 19.1 Under the assumption that the residuals are Gaussian, i.e. utN(0,σu2), we can simulate scenarios of a moving-average process of order 1 of the form (19.2)YtMA(1)Yt=μ+θ1ut1+ut.

MA(1) simulation
set.seed(1) # random seed
# *************************************************
#                      Inputs 
# *************************************************
# Number of steps 
t_bar <- 100000 
# Long term mean
mu <- 1
# MA parameters
theta <- c(theta1 = 0.15)
# Variance residuals 
sigma2_u <- 1 
# *************************************************
#                    Simulation 
# *************************************************
# Initialization
Yt <- c(mu) 
# Simulated residuals
u_t <- rnorm(t_bar, 0, sqrt(sigma2_u)) 
# Simulated MA(1) process
for(t in 2:t_bar){
  Yt[t] <- mu + theta[1]*u_t[t-1] + u_t[t]
}
1
Next step dynamics from .
Figure 19.1: Simulation of an MA(1) process with long term expected value (red, top) and empiric autocovariance for the first 30 lag.(bottom).

In we compute the expectation, variance and covariance on simulated values and according to the formulas in .

MA(1) moments
# Expectation 
e_x <- c(mu, mean(Yt))
# Variance 
v_x <- c(sigma2_u * (1 + theta[1]^2), var(Yt))
# Covariace 
cv_x <- c(sigma2_u * theta[1], cov(Yt[-1], lag(Yt,1)[-1]))
# Correlation 
cr_x <- c(theta[1]/(1 + theta[1]^2), cor(Yt[-1], lag(Yt,1)[-1]))
Statistic Formula Monte Carlo |Error|
E{Yt} 1.0000000 0.9974245 0.258%
V{Yt} 1.0225000 1.0301274 -0.74%
Cv{Yt,Yt1} 0.1500000 0.1513608 -0.899%
Cr{Yt,Yt1} 0.1466993 0.1469331 -0.159%
Table 19.1: Empiric and theoric expectation, variance, covariance and correlation (first lag) for a stationary MA(1) process.

19.2 AR(P)

In time series analysis an univariate time series Yt is defined as Autoregressive process of order p (AR(p)), when it satisfy the equations of the differences, i.e.  (19.3)Yt=ϕ1Yt1++ϕpYtp+ut, where p defines the order of the process and utWN(0,σu2). In compact form: Yt=i=1pϕiYti+ut. An Autoregressive process can be equivalently expressed in terms of the polynomial operator, i.e.  Φ(L)Yt=ut,Φ(L)=1ϕ1LϕpLp. From it follows that it exists a stationary AR(p) process if and only if all the solutions of the characteristic equations, i.e. Φ(z)=0, are greater than 1 in absolute value. In such case the AR(p) process admits an equivalent representation in terms of MA(), i.e.  Yt=Φ(L)1ut=11ϕ1LϕpLput=(1+ψ1L+ψ2L2+)=i=1ψiuti.

Proposition 19.4 (Expectation of an AR(p))
The unconditional expected value of a stationary AR(p) process reads E{Yt}=μ1i=1pϕi.

Proof. Let’s consider an AR(p) process Yt, then the unconditional expectation of the process is computed as E{Yt}=E{μ+i=1pϕiYti+ut}==μ+i=1qϕiE{Yti}+E{ut}==μ+i=1qϕiE{Yt} Since, under the assumption of stationarity the long term expectation of E{Yti} is the same as the long term expectation of E{Yt}. Hence, solving for the expected value one obtain: E{Yt}(1i=1pϕi)=μE{Yt}=μ1i=1pϕi.

If the AR(p) process is stationary, the covariance function satisfies the recursive relation, i.e.  {γkϕ1γk1ϕkpγkp=0γ0=ϕ1γ1++ϕpγp+σu2 where γk=γk. For k=0,,p the above equations forms a system of p+1 linear equations in p+1 unknowns γ0,,γp, also known as Yule-Walker equations.

Proposition 19.5 (Variance of an AR(1))
Let’s consider an AR(1) model without intercept so that E{Yt}=0. Then, its covariance function reads: γk=σu21ϕ12ϕ1k=γ0ϕ1k.

Proof. Let’s consider an AR(1) model without intercept so that E{Yt}=0, i.e. Yt=ϕ1Yt1+ut. The proof of the covariance function is divided in two parts. Firstly, we compute the variance and covariances of the AR(1) model. Then, we set the system and we solve it. Notably, the variance of Yt reads: (19.4)γ0=V{Yt}=E{Yt2}E{Yt}2=E{Yt2}==E{Yt(ϕ1Yt1+ut)}==ϕ1E{YtYt1}+E{Ytut}==ϕ1γ1+σu2 remembering that E{Ytut}=E{ut2}. The covariance with first lag, namely γ1 is computed as: (19.5)γ1=Cv{Yt,Yt1}=E{(ϕ1Yt1+ut)Yt1}==ϕ1E{Yt12}=ϕ1γ0 The Yule-Walker system is given by , , i.e.  {γ0=ϕ1γ1+σu2(L0)γ1=ϕ1γ0(L1) In order to solve the system, let’s substitute γ1 () in γ0 () and solve for γ0, i.e.  {γ0=ϕ12γ0+σu2=σu21ϕ12(L0)γ1=ϕ1σu21ϕ12(L1) Hence, by the relation γk=ϕ1γk1 the covariance reads explicitly: γk=σu21ϕ12ϕ1k=γ0ϕ1k.

Proposition 19.6 (Variance of an AR(2))
Let’s consider an AR(2) model without intercept so that E{Yt}=0. Then, its covariance function reads: γk={γ0=ψ0σu2k=0γ1=ψ1γ0k=1γ2=ψ2γ0k=2γk=ϕ1γk1+ϕ2γk2k3 where ψ0=(1ϕ1ψ1ϕ2ψ2)1ψ1=ϕ1(1ϕ2)1ψ2=ψ1ϕ1+ϕ2

Proof. Let’s consider an AR(2) model without intercept so that E{Yt}=0, i.e. Yt=ϕ1Yt1+ϕ2Yt2+ut. The proof of the covariance function is divided in two parts. Firstly, we compute the variance and covariances of the AR(2) model. Then, we set the system and we solve it. Notably, the variance of Yt reads: (19.6)γ0=V{Yt}=E{Yt2}E{Yt}2=E{Yt2}==E{Yt(ϕ1Yt1+ϕ2Yt2+ut)}==ϕ1E{YtYt1}+ϕ2E{YtYt2}+E{Ytut}==ϕ1γ1+ϕ2γ2+σu2 remembering that E{Ytut}=E{ut2}. The covariance with first lag, namely γ1 is computed as: (19.7)γ1=Cv{Yt,Yt1}=E{(ϕ1Yt1+ϕ2Yt2+ut)Yt1}==ϕ1E{Yt12}+ϕ2E{Yt2Yt1}==ϕ1γ0+ϕ2γ1 The covariance with second lag, namely γ2 is computed as: (19.8)γ2=Cv{Yt,Yt1}=E{(ϕ1Yt1+ϕ2Yt2+ut)Yt2}==ϕ1E{Yt1Yt2}+ϕ2E{Yt22}==ϕ1γ1+ϕ2γ0

The Yule-Walker system is given by , and , i.e.  {γ0=ϕ1γ1+ϕ2γ2+σu2(L0)γ1=ϕ1γ0+ϕ2γ1(L1)γ2=ϕ1γ1+ϕ2γ0(L2) In order to solve the system, let’s start by solving for γ1 () in terms of γ0 in L1, i.e. (19.9)γ1=(ϕ11ϕ2)ψ1γ0=ψ1γ0. Substituting γ1 from into γ2 () in L2 gives: (19.10)γ2=(ψ1ϕ1+ϕ2)ψ2γ0=ψ2γ0. Finally, substituting γ1 () and γ2 () into γ0 () in L0 gives an explicit expression of the variance of Yt, i.e.  γ0=11ϕ1ψ1ϕ2ψ2ψ0σu2=ψ0σu2.

AR(2) functions
# Formula for the variance 
AR2_variance <- function(phi, sigma2){
  psi_1 <- phi[1] / (1 - phi[2])
  psi_2 <- psi_1 * phi[1] + phi[2]
  psi_0 <- 1 / (1 - phi[1] * psi_1 - phi[2]*psi_2)
  sigma2 * psi_0
}
# Formula for the covariance
AR2_covariance <- function(phi, sigma2, lags = 1){
  # Reparametrize coefficients 
  psi1 = phi[1] / (1 - phi[2])
  psi2 = phi[1] * psi1 + phi[2]
  # Initialize variance-covariance
  gamma <- c()
  # Variance 
  gamma[1] <- sigma2/(1 - phi[1]*psi1 - phi[2]*psi2)
  # Covariance first lag 
  gamma[2] <- psi1 * gamma[1]
  # Covariance second lag 
  gamma[3] <- psi2 * gamma[1]
  # Iterative formula for lags > 2
  covariance <- function(gamma, phi, k = 1){
    if (k <= 2){
      return(gamma[k+1])
    }
    gamma <- c(gamma)
    for(i in 3:k){
      gamma[i+1] <- phi[1]*gamma[i] + phi[2]*gamma[i-1]
    }
    return(gamma[k+1])
  }
  cv <- c(gamma[1])
  if(lags == 0){
    return(cv)
  }
  for(lag in 1:lags) {
    cv[lag+1] <- covariance(gamma, phi, k = lag)
  }
  return(cv)
}
# Simulation of a Gaussian AR(2)
AR2_simulate <- function(n, phi = c(0.5, 0.2), sigma2 = 1){
  eps <- rnorm(n, sd = sqrt(sigma2))
  x <- eps
  for(t in 3:n){
    x[t] <- phi[1]*x[t-1] + phi[2]*x[t-2] + eps[t]
  }
  return(x)
}
AR(2) variance
# *************************************************
#                      Inputs 
# *************************************************
# Numer of simulations
nsim <- 500
# Horizon for each simulation 
t_bar <- 100000
# AR(2) parameters
phi <- c(0.3, 0.1)
# Variance of the residuals 
sigma2 <- 1
# *************************************************
scenarios <- purrr::map(1:nsim, ~AR2_simulate(t_bar, phi, sigma2))
# Compute variance and covariances with Monte Carlo
v_mc <- mean(purrr::map_dbl(scenarios, ~var(.x)))
cv_mc_L1 <- mean(purrr::map_dbl(scenarios, ~cov(.x[-1], lag(.x, 1)[-1])))
cv_mc_L2 <- mean(purrr::map_dbl(scenarios, ~cov(.x[-c(1,2)], lag(.x, 2)[-c(1,2)])))
cv_mc_L3 <- mean(purrr::map_dbl(scenarios, ~cov(.x[-c(1:3)], lag(.x, 3)[-c(1:3)])))
cv_mc_L4 <- mean(purrr::map_dbl(scenarios, ~cov(.x[-c(1:4)], lag(.x, 4)[-c(1:4)])))
cv_mc_L5 <- mean(purrr::map_dbl(scenarios, ~cov(.x[-c(1:5)], lag(.x, 5)[-c(1:5)])))
# Compute variance and covariances with Formulas 
v_mod <- AR2_variance(phi, sigma2)
cv_mod_L1 <- AR2_covariance(phi, sigma2, lags = 1)[2]
cv_mod_L2 <- AR2_covariance(phi, sigma2, lags = 2)[3]
cv_mod_L3 <- AR2_covariance(phi, sigma2, lags = 3)[4]
cv_mod_L4 <- AR2_covariance(phi, sigma2, lags = 4)[5]
cv_mod_L5 <- AR2_covariance(phi, sigma2, lags = 5)[6]
Covariance Formula MonteCarlo
V{Yt} 1.1363636 1.1366947
Cv{Yt,Yt1} 0.3787879 0.3790032
Cv{Yt,Yt2} 0.2272727 0.2274017
Cv{Yt,Yt3} 0.1060606 0.1064418
Cv{Yt,Yt4} 0.0545455 0.0548409
Cv{Yt,Yt5} 0.0269697 0.0271296
Table 19.2: Theoric long term variance and covariances computed on 500 Monte Carlo simulations (t = 100000).

Proposition 19.7 (Variance of an AR(3))
Let’s consider an AR(3) model without intercept so that E{Yt}=0. Then, its covariance function reads: γk={γ0=ψ0σu2k=0γ1=ψ1γ0k=1γ2=ψ2γ0k=2γ3=ψ3γ0k=3γk=ϕ1γk1+ϕ2γk2+ϕ3γk3k4 where ψ0=(1ϕ1ψ1ϕ2ψ2ϕ3ψ3)1ψ1=(ϕ1+ϕ2ϕ3)(1ϕ2ϕ32ϕ1ϕ3)1ψ2=ϕ1ψ1+ϕ3ψ1+ϕ2ψ3=ϕ1ψ2+ϕ2ψ1+ϕ3

Proof. Let’s consider an AR(3) model without intercept so that E{Yt}=0, i.e. Yt=ϕ1Yt1+ϕ2Yt2+ϕ3Yt3+ut, where utWN(0,σu2). Notably, the variance is computed as: (19.11)γ0=V{Yt}=E{Yt2}E{Yt}2==E{Yt2}==E{Yt(ϕ1Yt1+ϕ2Yt2+ϕ3Yt3+ut)}==ϕ1E{YtYt1}+ϕ2E{YtYt2}+ϕ3E{YtYt3}+E{Ytut}==ϕ1γ1+ϕ2γ2+ϕ3γ3+σu2 remembering that E{Ytut}=E{ut2}. The covariance with first lag, namely γ1 is computed as: (19.12)γ1=Cv{Yt,Yt1}=E{(ϕ1Yt1+ϕ2Yt2+ϕ3Yt3+ut)Yt1}==ϕ1E{Yt12}+ϕ2E{Yt2Yt1}+ϕ3E{Yt3Yt1}==ϕ1γ0+ϕ2γ1+ϕ2γ2 The covariance with second lag, namely γ2 is computed as: (19.13)γ2=Cv{Yt,Yt2}=E{(ϕ1Yt1+ϕ2Yt2+ϕ3Yt3+ut)Yt2}==ϕ1E{Yt1Yt2}+ϕ2E{Yt22}+ϕ3E{Yt3Yt2}==ϕ1γ1+ϕ2γ0+ϕ3γ1 The covariance with third lag, namely γ3 is computed as: (19.14)γ3=Cv{Yt,Yt3}=E{(ϕ1Yt1+ϕ2Yt2+ϕ3Yt3+ut)Yt3}==ϕ1E{Yt1Yt3}+ϕ2E{Yt2Yt3}+ϕ3E{Yt32}==ϕ1γ2+ϕ2γ1+ϕ3γ0 The Yule-Walker system is given by , , and reads {γ0=ϕ1γ1+ϕ2γ2+ϕ3γ3+σu2(L0)γ1=ϕ1γ0+ϕ2γ1+ϕ3γ2(L1)γ2=ϕ1γ1+ϕ2γ0+ϕ3γ1(L2)γ3=ϕ1γ2+ϕ2γ1+ϕ3γ0(L3) Let’s start by expressing γ2 () in terms of γ1 and γ0 from L2, i.e.  (19.15)γ2=(ϕ1+ϕ3)γ1+ϕ2γ0, and let’s substitute the above expression of γ2 () in γ1 () from L1, i.e.  γ1=ϕ1γ0+ϕ2γ1+ϕ3(ϕ1+ϕ3)γ1+ϕ3ϕ2γ0. At this point γ1 depends only on γ0, hence we can solve it: (19.16)γ1=ϕ1+ϕ2ϕ31ϕ2ϕ32ϕ1ϕ3ψ1γ0=ψ1γ0. With γ1 solved, one can come back to the expression of γ2 () and substitute the result in γ1 () obtaining an explicit expression for γ2, i.e. (19.17)γ2=(ϕ1ψ1+ϕ3ψ1+ϕ2)ψ2γ0=ψ2γ0. Substituting the explicit expressions of γ2 () and γ1 () into γ3 () completes the system, i.e.  (19.18)γ3=(ϕ1ψ2+ϕ2ψ1+ϕ3)ψ3γ0=ψ3γ0. Finally, substituting γ1 (), γ2 () and γ3 () in γ0 () gives the variance, i.e.  (19.19)γ0=11ϕ1ψ1ϕ2ψ2ϕ3ψ3ψ0σu2. and ψ0=(1ϕ1ψ1ϕ2ψ2ϕ3ψ3)1ψ1=(ϕ1+ϕ2ϕ3)(1ϕ2ϕ32ϕ1ϕ3)1ψ2=ϕ1ψ1+ϕ3ψ1+ϕ2ψ3=ϕ1ψ2+ϕ2ψ1+ϕ3

AR(3) functions
# Formula for the variance 
AR3_variance <- function(phi, sigma2){
  psi1 <- (phi[1] + phi[2]*phi[3]) / (1 - phi[2] - phi[3]^2 - phi[1]*phi[3])
  psi2 <- phi[1] * psi1 + phi[3] * psi1 + phi[2]
  psi3 <- phi[1] * psi2 + phi[2] * psi1 + phi[3]
  psi0 <- 1 / (1 - phi[1]*psi1 - phi[2]*psi2 - phi[3]*psi3)
  sigma2 * psi0
}
# Formula for the covariance
AR3_covariance <- function(phi, sigma2, lags = 1){
  # Reparametrize coefficients 
  psi1 <- (phi[1] + phi[2]*phi[3]) / (1 - phi[2] - phi[3]^2 - phi[1]*phi[3])
  psi2 <- phi[1] * psi1 + phi[3] * psi1 + phi[2]
  psi3 <- phi[1] * psi2 + phi[2] * psi1 + phi[3]
  psi0 <- 1 / (1 - phi[1]*psi1 - phi[2]*psi2 - phi[3]*psi3)
  # Initialize variance-covariance
  gamma <- c()
  # Variance 
  gamma[1] <- sigma2 * psi0
  # Covariance first lag 
  gamma[2] <- psi1 * gamma[1]
  # Covariance second lag 
  gamma[3] <- psi2 * gamma[1]
  # Covariance third lag 
  gamma[4] <- psi3 * gamma[1]
  # Iterative formula for lags > 2
  covariance <- function(gamma, phi, k = 1){
    if (k <= 3){
      return(gamma[k+1])
    }
    gamma <- c(gamma)
    for(i in 4:k){
      gamma[i+1] <- phi[1]*gamma[i] + phi[2]*gamma[i-1] + phi[3]*gamma[i-2]
    }
    return(gamma[k+1])
  }
  cv <- c(gamma[1])
  if(lags == 0){
    return(cv)
  }
  for(lag in 1:lags) {
    cv[lag+1] <- covariance(gamma, phi, k = lag)
  }
  return(cv)
}
# Simulation of a Gaussian AR(3)
AR3_simulate <- function(n, phi = c(0.5, 0.2, 0.1), sigma2 = 1){
  eps <- rnorm(n, sd = sqrt(sigma2))
  x <- eps
  for(t in 4:n){
    x[t] <- phi[1]*x[t-1] + phi[2]*x[t-2] + phi[3]*x[t-3] + eps[t]
  }
  return(x)
}
AR(3) variance
# *************************************************
#                      Inputs 
# *************************************************
# Numer of simulations
nsim <- 500
# Horizon for each simulation 
t_bar <- 100000
# AR(2) parameters
phi <- c(0.3, 0.1, 0.05)
# Variance of the residuals 
sigma2 <- 1
# *************************************************
scenarios <- purrr::map(1:nsim, ~AR3_simulate(t_bar, phi, sigma2))
# Compute variance and covariances with Monte Carlo
v_mc <- mean(purrr::map_dbl(scenarios, ~var(.x)))
cv_mc_L1 <- mean(purrr::map_dbl(scenarios, ~cov(.x[-1], lag(.x, 1)[-1])))
cv_mc_L2 <- mean(purrr::map_dbl(scenarios, ~cov(.x[-c(1,2)], lag(.x, 2)[-c(1,2)])))
cv_mc_L3 <- mean(purrr::map_dbl(scenarios, ~cov(.x[-c(1:3)], lag(.x, 3)[-c(1:3)])))
cv_mc_L4 <- mean(purrr::map_dbl(scenarios, ~cov(.x[-c(1:4)], lag(.x, 4)[-c(1:4)])))
cv_mc_L5 <- mean(purrr::map_dbl(scenarios, ~cov(.x[-c(1:5)], lag(.x, 5)[-c(1:5)])))
# Compute variance and covariances with Formulas 
v_mod <- AR3_variance(phi, sigma2)
cv_mod_L1 <- AR3_covariance(phi, sigma2, lags = 1)[2]
cv_mod_L2 <- AR3_covariance(phi, sigma2, lags = 2)[3]
cv_mod_L3 <- AR3_covariance(phi, sigma2, lags = 3)[4]
cv_mod_L4 <- AR3_covariance(phi, sigma2, lags = 4)[5]
cv_mod_L5 <- AR3_covariance(phi, sigma2, lags = 5)[6]
Covariance Formula MonteCarlo
V{Yt} 1.1538304 1.1539698
Cv{Yt,Yt1} 0.3987743 0.3987340
Cv{Yt,Yt2} 0.2549540 0.2548339
Cv{Yt,Yt3} 0.1740552 0.1740382
Cv{Yt,Yt4} 0.0976507 0.0976318
Cv{Yt,Yt5} 0.0594484 0.0594063
Table 19.3: Theoric long term variance and covariances computed on 500 Monte Carlo simulations (t = 100000).

Proposition 19.8 (Variance of an AR(4))
Let’s consider an AR(4) model without intercept so that E{Yt}=0. Then, its covariance function reads: γk={γ0=ψ0σu2k=0γ1=ψ1γ0k=1γ2=ψ2γ0k=2γ3=ψ3γ0k=3γ4=ψ4γ0k=4γk=ϕ1γk1+ϕ2γk2+ϕ3γk3+ϕ4γk4k5 where ψ1=(ϕ3ϕ21ϕ4+ϕ1ϕ4ϕ21ϕ4+ϕ1+ϕ3ϕ4)(1ϕ3(ϕ1+ϕ3)1ϕ4ϕ1ϕ4(ϕ1+ϕ3)1ϕ4ϕ2ϕ2ϕ4ϕ42)1ψ2=ψ1(ϕ1+ϕ3)1ϕ4+ϕ21ϕ4ψ3=ϕ1ψ2+ϕ2ψ1+ϕ3+ϕ4ψ1ψ4=ϕ1ψ3+ϕ2ψ2+ϕ3ψ1+ϕ4ψ0=(1ϕ1ψ1ϕ2ψ2ϕ3ψ3ϕ4ψ4)1

Proof. Let’s consider an AR(4) model without intercept so that E{Yt}=0, i.e. Yt=ϕ1Yt1+ϕ2Yt2+ϕ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3ψ1+ϕ4)ψ4γ0=ψ4γ0. Finally, substituting γ1 (), γ2 () and γ3 () and γ4 () into γ0 () gives the variance, i.e. (19.31)γ0=11ϕ1ψ1ϕ2ψ2ϕ3ψ3ϕ4ψ4ψ0σu2.

AR(4) functions
# Formula for the covariance
AR4_covariance <- function(phi, sigma2, lags = 1){
  phi1_ast <- (phi[1] + phi[3]) / (1-phi[4])
  phi2_ast <- phi[2] / (1-phi[4])
  # Reparametrization psi
  psi1 <- (phi[3]*phi2_ast + phi2_ast*phi[1]*phi[4] + phi[1] + phi[3]*phi[4]) /
    (1 - phi[3]*phi1_ast - phi[1]*phi[4] * phi1_ast - phi[2] - phi[2]*phi[3] - phi[4]^2)
  psi2 <- psi1 * phi1_ast + phi2_ast
  psi3 <- phi[1]*psi2 + phi[2]*psi1 + phi[3] + phi[4]*psi1
  psi4 <- phi[1]*psi3 + phi[2]*psi2 + phi[3]*psi1 + phi[4]
  psi0 <- 1 / (1 - phi[1]*psi1 - phi[2]*psi2 - phi[3]*psi3 - phi[4]*psi4)
  # Initialize variance-covariance
  gamma <- c()
  # Variance 
  gamma[1] <- sigma2 * psi0
  # Covariance first lag 
  gamma[2] <- psi1 * gamma[1]
  # Covariance second lag 
  gamma[3] <- psi2 * gamma[1]
  # Covariance third lag 
  gamma[4] <- psi3 * gamma[1]
   # Covariance fourth lag 
  gamma[5] <- psi4 * gamma[1]
  # Iterative formula for lags > 2
  covariance <- function(gamma, phi, k = 1){
    if (k <= 4){
      return(gamma[k+1])
    }
    gamma <- c(gamma)
    for(i in 5:k){
      gamma[i+1] <- phi[1]*gamma[i] + phi[2]*gamma[i-1] + phi[3]*gamma[i-2] + phi[4]*gamma[i-3]
    }
    return(gamma[k+1])
  }
  cv <- c(gamma[1])
  if(lags == 0){
    return(cv)
  }
  for(lag in 1:lags) {
    cv[lag+1] <- covariance(gamma, phi, k = lag)
  }
  return(cv)
}
# Simulation of a Gaussian AR(3)
AR4_simulate <- function(n, phi = c(0.5, 0.2, 0.1, 0.04), sigma2 = 1){
  eps <- rnorm(n, sd = sqrt(sigma2))
  x <- eps
  for(t in 5:n){
    x[t] <- phi[1]*x[t-1] + phi[2]*x[t-2] + phi[3]*x[t-3] + phi[4]*x[t-4] + eps[t]
  }
  return(x)
}
AR(4) variance
# *************************************************
#                      Inputs 
# *************************************************
# Numer of simulations
nsim <- 500
# Horizon for each simulation 
t_bar <- 100000
# AR(2) parameters
phi <- c(0.3, 0.15, 0.1, 0.03)
# Variance of the residuals 
sigma2 <- 1
# *************************************************
#                      Simulations  
# *************************************************
scenarios <- purrr::map(1:nsim, ~AR4_simulate(t_bar, phi, sigma2))
# *************************************************
#                      Moments 
# *************************************************
# Compute variance and covariances with Monte Carlo
v_mc <- mean(purrr::map_dbl(scenarios, ~var(.x)))
cv_mc_L1 <- mean(purrr::map_dbl(scenarios, ~cov(.x[-1], lag(.x, 1)[-1])))
cv_mc_L2 <- mean(purrr::map_dbl(scenarios, ~cov(.x[-c(1,2)], lag(.x, 2)[-c(1,2)])))
cv_mc_L3 <- mean(purrr::map_dbl(scenarios, ~cov(.x[-c(1:3)], lag(.x, 3)[-c(1:3)])))
cv_mc_L4 <- mean(purrr::map_dbl(scenarios, ~cov(.x[-c(1:4)], lag(.x, 4)[-c(1:4)])))
cv_mc_L5 <- mean(purrr::map_dbl(scenarios, ~cov(.x[-c(1:5)], lag(.x, 5)[-c(1:5)])))
# Compute variance and covariances with Formulas 
v_mod <- AR4_covariance(phi, sigma2, lags = 0)[1]
cv_mod_L1 <- AR4_covariance(phi, sigma2, lags = 1)[2]
cv_mod_L2 <- AR4_covariance(phi, sigma2, lags = 2)[3]
cv_mod_L3 <- AR4_covariance(phi, sigma2, lags = 3)[4]
cv_mod_L4 <- AR4_covariance(phi, sigma2, lags = 4)[5]
cv_mod_L5 <- AR4_covariance(phi, sigma2, lags = 5)[6]
Covariance Formula MonteCarlo
V{Yt} 1.2543907 1.2509967
Cv{Yt,Yt1} 0.5084245 0.5002624
Cv{Yt,Yt2} 0.4036375 0.3996579
Cv{Yt,Yt3} 0.3380467 0.3349660
Cv{Yt,Yt4} 0.2504338 0.2482111
Cv{Yt,Yt5} 0.1814536 0.1797934
Table 19.4: Theoric long term variance and covariances computed on 500 Monte Carlo simulations (t = 100000).

19.2.1 Stationary AR(1)

Let’s consider an AR(1) process, i.e. Yt=μ+ϕ1Yt1+ut. Through recursion up to time 0 it is possible to express an AR(1) model as an MA(), i.e.  Yt=ϕ1tY0+μj=0t1ϕ1j+j=0t1ϕ1jutj, where the process is stationary if and only if |ϕ1|<1. In fact, independently from the specific distribution of the residuals ut, the unconditional expectation of an AR(1) converges if and only if |ϕ1|<1, i.e.  E{Yt}=ϕ1tY0+μj=0t1ϕ1j=μ1ϕ1. The variance instead is computed as: γ0=V{Yt}=σu2j=0t1ϕ12j=σu21ϕ12. The auto covariance decays exponentially fast depending on the parameter ϕ1, i.e.  γ1=Cv{Yt,Yt1}=ϕ1σu21ϕ12=ϕ1γ0, where in general for the lag k γk=Cv{Yt,Ytk}=ϕ1|k|γ0. Finally, the autocorrelation function ρ1=Cr{Yt,Yt1}=γ1γ0=ϕ1, where in general for the lag k ρk=Cr{Yt,Ytk}=ϕ1|k|. An example of a simulated AR(1) process (ϕ1=0.95, μ=0.5 and σu2=1 and Normally distributed residuals) with its covariance function is shown in .

Figure 19.2: AR(1) simulation and expected value (red) on the top. Empirical autocovariance (gray) and fitted exponential decay (blue) at the bottom.
AR(1) simulation
set.seed(1) # random seed
# *************************************************
#                      Inputs 
# *************************************************
# Number of steps 
t_bar <- 100000 
# Long term mean
mu <- 0.5
# AR parameters
phi <- c(phi1 = 0.95)
# Variance residuals 
sigma2_u <- 1 
# *************************************************
#                    Simulation 
# *************************************************
# Initialization
Yt <- c(mu) 
# Simulated residuals
u_t <- rnorm(t_bar, 0, sqrt(sigma2_u)) 
# Simulated MA(1) process
for(t in 2:t_bar){
  Yt[t] <- mu + phi[1]*Yt[t-1] + u_t[t]
}

Example 19.2 Sampling the process for different t we expect that, on a large number of simulations, the distribution will be normal with stationary moments, i.e. for all t YtN(μ1ϕ1,σu21ϕ12).

Simulate and sample a stationary AR(1)
# *************************************************
#                      Inputs 
# *************************************************
set.seed(3) # random seed
t_bar <- 120 # number of steps ahead  
j_bar <- 1000 # number of simulations 
sigma <- 1 # standard deviation of epsilon 
par <- c(mu = 0, phi1 = 0.7) # parameters
y0 <- par[1]/(1-par[2]) # initial point 
t_sample <- c(30,60,90) # sample times 
# *************************************************
#                   Simulations 
# *************************************************
# Process Simulations 
scenarios <- list()
for(j in 1:j_bar){
  Y <- c(y0)
  # Simulated residuals 
  eps <- rnorm(t_bar, 0, sigma) 
  # Simulated AR(1) 
  for(t in 2:t_bar){
    Y[t] <- par[1] + par[2]*Y[t-1] + eps[t]
  }
  scenarios[[j]] <- dplyr::tibble(j = rep(j, t_bar), 
                                  t = 1:t_bar, 
                                  y = Y, 
                                  eps = eps)
}
scenarios <- dplyr::bind_rows(scenarios) 
# Trajectory j 
df_j <- dplyr::filter(scenarios, j == 6)
# Sample at different times 
df_t1 <- dplyr::filter(scenarios, t == t_sample[1])
df_t2 <- dplyr::filter(scenarios, t == t_sample[2])
df_t3 <- dplyr::filter(scenarios, t == t_sample[3])
Figure 19.3: Stationary AR(1) simulation with expected value, one possible trajectory and samples at different times.
Figure 19.4: Stationary AR(1) histograms for different sampled times with normal pdf from empiric moments and normal pdf with theoric moments.
Statistic Theoric Empiric
E{Yt} 0.000000 -0.0027287
V{Yt} 1.960784 1.9530614
Cv{Yt,Yt1} 1.372549 1.3586670
Cr{Yt,Yt1} 0.700000 0.6956660
Table 19.5: Empiric and theoric expectation, variance, covariance and correlation (first lag) for a stationary AR(1) process.

19.2.2 Non-stationary AR(1): random walk

A non-stationary process has expectation and/or variance that changes over time. Considering the setup of an AR(1), if ϕ1=1 the process degenerates into a so called random walk process. Formally, if μ0 it is called random walk with drift, i.e. Yt=μ+Yt1+ut. Considering its MA() representation Yt=Y0+μt+j=0t1utj, it is easy to see that the expectation depends on the starting point and on time t and the shocks uti never decays. In fact, computing the expectation and variance of a random walk process it emerges a clear dependence on time, i.e.  E{YtF0}=Y0+μtV{YtF0}=tσu2Cv{Yt,Yth}=(th)σu2Cr{Yt,Yth}=1ht, and the variance tends to explode to as t.

Stochastic trend of a Random walk

Let’s define the stochastic trend St as St=i=0t1uti, then

  1. The expectation of St if ut are all martingale difference sequences, is zero, i.e.  E{St}=j=0t1E{utj}=j=0t1E{E{utjFt1}}=0, and therefore E{Yt}=Y0+μt+E{St}=Y0+μt.

  2. The variance of St, if ut are all martingale difference sequences, is time-dependent, i.e.  V{Yt}=V{St}=j=0t1V{utj}=j=0t1σu2=tσu2. while the covariance between two times t and tk depends on the lag, i.e. Cv{St,Sth}=E{StSth}E{St}E{Sth}==E{i=0t1utij=0th1uthj}=j=0th1E{utj2}=j=0th1V{utj}==(th)σu2 and so the correlation Cr{St,Sth}=Cv{St,Sth}V{St}V{Sth}==(th)σu2σu2t(th)==(th)t(th)==tht==1ht tends to one as t, in fact: limtCr{St,Sth}=limt1ht=1

Random Walk simulation
set.seed(1) # random seed
# *************************************************
#                      Inputs 
# *************************************************
# Number of steps 
t_bar <- 100000 
# Long term mean
mu <- 0.02
# Variance residuals 
sigma2_u <- 1 
# *************************************************
#                    Simulation 
# *************************************************
# Initialization
Yt <- c(mu) 
# Simulated residuals
u_t <- rnorm(t_bar, 0, sqrt(sigma2_u)) 
# Simulated MA(1) process
for(t in 2:t_bar){
  Yt[t] <- mu + Yt[t-1] + u_t[t]  
}
Figure 19.5: Random walk simulation and expected value (red) on the top. Empirical autocorrelation (gray).

Example 19.3 Let’s simulate an random walk process with drift with a Gaussian error, namely utN(0,σu2).

Simulation of a non-stationary AR(1)
# *************************************************
#                      Inputs 
# *************************************************
set.seed(5) # random seed
t_bar <- 100 # number of steps ahead  
j_bar <- 2000 # number of simulations 
sigma0 <- 1 # standard deviation of epsilon 
par <- c(mu = 0.3, phi1 = 1) # parameters
X0 <- c(0) # initial point 
t_sample <- c(30,60,90) # sample times 
# *************************************************
#                   Simulation 
# *************************************************
# Process Simulations 
scenarios <- tibble()
for(j in 1:j_bar){
  # Initialize X0 and variance
  Xt <- rep(X0, t_bar)
  sigma <- rep(sigma0, t_bar)
  # Simulated residuals 
  eps <- rnorm(t_bar, mean=0, sd=sigma0)
  for(t in 2:t_bar){
    sigma[t] <- sigma0*sqrt(t)
    Xt[t] <- par[1] + par[2]*Xt[t-1] + eps[t]
  }
  df <- tibble(j = rep(j, t_bar), t = 1:t_bar, Xt = Xt, 
               sigma = sigma, eps = eps)
  scenarios <- dplyr::bind_rows(scenarios, df)
}
# Compute simulated moments 
scenarios <- scenarios %>%
  group_by(t) %>%
  mutate(e_x = mean(Xt), sd_x = sd(Xt))
# Trajectory j 
df_j <- dplyr::filter(scenarios, j == 6)
# Sample at different times 
df_t1 <- dplyr::filter(scenarios, t == t_sample[1])
df_t2 <- dplyr::filter(scenarios, t == t_sample[2])
df_t3 <- dplyr::filter(scenarios, t == t_sample[3])
Figure 19.6: Non stationary AR(1) simulation with expected value (red), a possible trajectory (green) and samples for different times (magenta) on the top. Theoretic (blue) and empiric (black) std. deviation at the bottom.

Sampling the process for different t we expect that, on a large number of simulations, the distribution will be still normal but with non-stationary moments, i.e.  XtN(μt,σu2t).

Figure 19.7: Non-stationary AR(1) histograms for different sampled times with normal pdf with empiric moments (blue) and normal pdf with theoretic moments (magenta).
t E{Yt} Emc{Yt} V{Yt} Vmc{Yt}
30 8.7 8.698311 29 29.69114
60 17.7 17.517805 59 60.09756
90 26.7 26.268452 89 90.05715
Table 19.6: Empiric and theoric expectation, variance, covariance and correlation (first lag) for a stationary AR(1) process.