2
$\begingroup$

I'm trying to simulate an MA(1) model in R from scratch in order to obtain more insight into the theoretical underpinnings of the model.

The moving average is defined as follows:

$X_t = \mu + \varepsilon_t + \theta_1 \varepsilon_{t-1} + \cdots + \theta_q \varepsilon_{t-q}$

Where $\epsilon_{t}$ is defined as white noise terms.

This MA(1) model has expected value and autocovariances as written down below:

$E(X_t) = 0$

$Cov(Y_{t} , Y_{t - 1}) = \gamma_{1} = -\theta \sigma_{e}^{2}$

$Cov(Y_{t} , Y_{t - k}) = \gamma_{k} = 0$

I will post my R code below.

ma.sim = function( ma_parameter = c(0.4 , 0.5) , number){


ma_mod_vals = rep(0 , times = number)


# Obtain the sequence of coefficients.

for(i in 1:number){

# create two white noise terms:


white_noise = arima.sim(model = list() , n = 2)


ma_mod_vals[i] = white_noise[1] + ma_parameter[1] * white_noise[2]


}

lister = list(output = ma_mod_vals)


}

I used the code below to generate the 2 white noise terms present in the MA(1) model.

white_noise = arima.sim(model = list() , n = 2)

What I don't understand is why I don't obtain a similar acf plot to the arima.sim function which is used to generate arima models.

For example values simulated using my function yields the following acf plot:

Acf plot of my MA(1) model

It is clear according to the theory that the acf plot of a MA(1) model should be significant at lag 1. But we see in this plot that the acf value at lag 1 is not significant at all. I suspect that the white noise terms that I am generating are not correct. I think that they should be correlated in some way, but I do not know how to do this.

$\endgroup$

1 Answer 1

2
$\begingroup$

You're right that you the issue is with correlation. The problem is that your code is generating two new white noise points each time it calculates an $x_t$ value, when it should be reusing the previous white noise points.

For example, your code will generate white noise points $w_1$ and $w_0$ and calculate $x_1 = w_1 + \theta w_0$. Then it generates two new points $w_2$ and $w_3$ to calculate $x_2 = w_3 + \theta w_2$. Your $x_t$ won't be correlated because they're not using the same $w_t$. Instead, for $x_2$ you need $x_2 = w_2 + \theta w_1$

To amend the code I've done:

ma.sim = function( ma_parameter = c(0.4 , 0.5) , number){
  
  
  ma_mod_vals = rep(0 , times = number)
  
  
  # Generate the white noise sequence.
  
  white_noise = arima.sim(model = list() , n = number)
  
  for(i in 2:number){
    
    # Calculate x values
    
    ma_mod_vals[i] = white_noise[i] + ma_parameter[1] * white_noise[i-1]
    
    
  }
  
  lister = list(output = ma_mod_vals)
  
  return(lister)
  
}

You'll notice that the for loop starts at 2 instead of 1. The code will through an error otherwise. Running the above should get your desired ACF.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.