# Simulate the data of ARMA

**URL:** <https://discourse.mc-stan.org/t/simulate-the-data-of-arma/13108>\
**Category:** Modeling\
**Created:** [February 13, 2020, 1:29pm UTC](https://discourse.mc-stan.org/t/simulate-the-data-of-arma/13108 "2020-02-13T13:29:39Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![S\_mengyuan](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/s_mengyuan/32/7125_2.png) [@S\_mengyuan](https://discourse.mc-stan.org/u/S_mengyuan)\
**Post date:** [February 13, 2020, 1:29pm UTC](https://discourse.mc-stan.org/t/simulate-the-data-of-arma/13108/1 "2020-02-13T13:29:39Z")

</div>

I’m new to Stan and met a problem of my first try. I’m using the ARMA from the book to simulate a few data of the future. but it showed me errors in the block. I think the generated quantities block is correct but it has problems.  
the code is:

```
ARMA_code ="""
data {
  int<lower=1> T;
  real y[T];
  int<lower=0> n_new;
}
parameters {
  real mu;
  real phi;
  real theta;
  real<lower=0> sigma;
} 

model {
   // real err;
    vector[T] nu;
    vector[T] err;
    nu[1] <- mu + phi * mu;
    err[1] <- y[1] - nu[1];
    for (t in 2:T) {
        nu[t] <- mu + phi * y[t-1] + theta * err[t-1];
        err[t] <- y[t] - nu[t];
        }
    err ~ normal(0,sigma);
    mu ~ normal(0,10);
    phi ~ normal(0,2);
    theta ~ normal(0,2);
    sigma ~ cauchy(0,5);
 }

generated quantities{
   vector[n_new+T] y_new;
  // y_new[1:T] <- mu;
   vector[n_new] x_pred;   
   y_new[T+1] = mu+phi*y[T]+theta*err+sigma;
   for (i in 2:n_new){
       y_new[T+i] = normal_rng(mu+phi*y_new[T+i-1]+theta*err, sigma);
    }
}
"""

```

Here is the error in detail:

```
ValueError: Failed to parse Stan model 'anon_model_983af5eff8a1020ec870838c279bf070'. Error message:
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Info: assignment operator <- deprecated in the Stan language; use = instead.
Info: assignment operator <- deprecated in the Stan language; use = instead.
Info: assignment operator <- deprecated in the Stan language; use = instead.
Info: assignment operator <- deprecated in the Stan language; use = instead.
Variable "err" does not exist.
 error in 'unknown file name' at line 35, column 37
  -------------------------------------------------
    33: // y_new[1:T] <- mu;
    34: vector[n_new] x_pred;   
    35: y_new[T+1] = mu+phi*y[T]+theta*err+sigma;
                                            ^
    36: for (i in 2:n_new){
  ------------------------------------------------

```

Additive question:  
Can anyone explain what is the process inner of stan when fitting? I already read papers but still confuse.

---

<div class="post-metadata">

**Author:** ![Ara\_Winter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ara_winter/32/4284_2.png) [@Ara\_Winter](https://discourse.mc-stan.org/u/Ara_Winter)\
**Post date:** [February 13, 2020, 1:57pm UTC](https://discourse.mc-stan.org/t/simulate-the-data-of-arma/13108/2 "2020-02-13T13:57:27Z")

</div>

Morning and welcome! First off all your \<- need to be changed to = . And it’s a bit hard to read you model code so let me see if I can reformat that for you.

---

<div class="post-metadata">

**Author:** ![Ara\_Winter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ara_winter/32/4284_2.png) [@Ara\_Winter](https://discourse.mc-stan.org/u/Ara_Winter)\
**Post date:** [February 13, 2020, 1:59pm UTC](https://discourse.mc-stan.org/t/simulate-the-data-of-arma/13108/3 "2020-02-13T13:59:32Z")

</div>

So the code should look more like this

```
data {
  int<lower=1> T; // num observations
  real y[T]; // observed outputs
}
parameters {
  real mu; // mean coeff
  real phi; // autoregression coeff
  real theta; // moving avg coeff
  real<lower=0> sigma; // noise scale
}
model {
  vector[T] nu; // prediction for time t
  vector[T] err; // error for time t
  nu[1] = mu + phi * mu; // assume err[0] == 0
  err[1] = y[1] - nu[1];
  for (t in 2:T) {
    nu[t] = mu + phi * y[t-1] + theta * err[t-1];
    err[t] = y[t] - nu[t];
  }
  mu ~ normal(0, 10); // priors
  phi ~ normal(0, 2);
  theta ~ normal(0, 2);
  sigma ~ cauchy(0, 5);
  err ~ normal(0, sigma); // likelihood
}

```

but add in the generate block. This is from the manual here: [https://mc-stan.org/docs/2\_19/stan-users-guide/autoregressive-moving-average-models.html](https://mc-stan.org/docs/2_19/stan-users-guide/autoregressive-moving-average-models.html)

---

<div class="post-metadata">

**Author:** ![S\_mengyuan](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/s_mengyuan/32/7125_2.png) [@S\_mengyuan](https://discourse.mc-stan.org/u/S_mengyuan)\
**Post date:** [February 13, 2020, 2:02pm UTC](https://discourse.mc-stan.org/t/simulate-the-data-of-arma/13108/4 "2020-02-13T14:02:33Z")

</div>

thanks for the suggestion.  
I want to predict the future data in generated quantities block. confuse how to fix it?

---

<div class="post-metadata">

**Author:** ![mitzimorris](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mitzimorris/32/29_2.png) [@mitzimorris](https://discourse.mc-stan.org/u/mitzimorris)\
**Post date:** [February 13, 2020, 2:23pm UTC](https://discourse.mc-stan.org/t/simulate-the-data-of-arma/13108/5 "2020-02-13T14:23:37Z")

</div>

[quote=“S\_mengyuan, post:1, topic:13108”]

```
generated quantities{
   vector[n_new+T] y_new;
   vector[n_new] x_pred;   
   y_new[1:T] = mu;
   y_new[T+1] = mu+phi*y[T]+theta*err+sigma;
   for (i in 2:n_new){
       y_new[T+i] = normal_rng(mu+phi*y_new[T+i-1]+theta*err, sigma);
    }
}

```

not sure what you’re trying to do, but your generated quantities block should look something like the above.  
your original program had some lines commented out - I uncommented them, and put the data declarations before assignments.  
try using CmdStanR or CmdStanPy to get the new Stan3 parser which has better error messages.

---

<div class="post-metadata">

**Author:** ![S\_mengyuan](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/s_mengyuan/32/7125_2.png) [@S\_mengyuan](https://discourse.mc-stan.org/u/S_mengyuan)\
**Post date:** [February 13, 2020, 2:32pm UTC](https://discourse.mc-stan.org/t/simulate-the-data-of-arma/13108/6 "2020-02-13T14:32:17Z")

</div>

I want to predict the future n\_new data.  
I tried your block.

```
 error in 'unknown file name' at line 32, column 3
  -------------------------------------------------
    30: vector[n_new+T] y_new;
    31: vector[n_new] x_pred;   
    32: y_new[1:T] = mu;
          ^
    33: y_new[T+1] = mu+phi*y[T]+theta*err+sigma;
  -------------------------------------------------

PARSER EXPECTED: <one of the following:
  a variable declaration, beginning with type
      (int, real, vector, row_vector, matrix, unit_vector,
       simplex, ordered, positive_ordered,
       corr_matrix, cov_matrix,
       cholesky_corr, cholesky_cov
  or a <statement>
  or '}' to close variable declarations and definitions>

```

---

<div class="post-metadata">

**Author:** ![mitzimorris](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mitzimorris/32/29_2.png) [@mitzimorris](https://discourse.mc-stan.org/u/mitzimorris)\
**Post date:** [February 13, 2020, 2:51pm UTC](https://discourse.mc-stan.org/t/simulate-the-data-of-arma/13108/7 "2020-02-13T14:51:19Z")

</div>

hi S, in the Forums posts, use 3 backticks for blockquotes.

I would look in the language ref manual or the users manual for more information on the generated quanitites block. the error message indicates that the kind of vectorized assigment you’re trying to do isn’t allowed.

---

<div class="post-metadata">

**Author:** ![S\_mengyuan](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/s_mengyuan/32/7125_2.png) [@S\_mengyuan](https://discourse.mc-stan.org/u/S_mengyuan)\
**Post date:** [February 13, 2020, 3:19pm UTC](https://discourse.mc-stan.org/t/simulate-the-data-of-arma/13108/8 "2020-02-13T15:19:54Z")

</div>

> [@mitzimorris](#):
>
> language

do you think the logic of genertated quantities block is corrext?

---

<div class="post-metadata">

**Author:** ![S\_mengyuan](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/s_mengyuan/32/7125_2.png) [@S\_mengyuan](https://discourse.mc-stan.org/u/S_mengyuan)\
**Post date:** [February 13, 2020, 3:29pm UTC](https://discourse.mc-stan.org/t/simulate-the-data-of-arma/13108/9 "2020-02-13T15:29:27Z")

</div>

```
ARMA_code ="""

data {
  int<lower=1> T; // num observations
  real y[T]; // observed outputs
  int<lower=0> n_new;
}
parameters {
  real mu; // mean coeff
  real phi; // autoregression coeff
  real theta; // moving avg coeff
  real<lower=0> sigma; // noise scale
  vector[T] output;
}
model {

  for (x in 1:T) {
      output[x] = y[x];
      }
  
  vector[T] nu; // prediction for time t
  vector[T] err; // error for time t
  nu[1] = mu + phi * mu; // assume err[0] == 0
  err[1] = y[1] - nu[1];
  for (t in 2:T) {
    nu[t] = mu + phi * y[t-1] + theta * err[t-1];
    err[t] = y[t] - nu[t];
  }
  mu ~ normal(0, 10); // priors
  phi ~ normal(0, 2);
  theta ~ normal(0, 2);
  sigma ~ cauchy(0, 5);
  err ~ normal(0, sigma); // likelihood
}

generated quantities{
   vector[n_new+T] y_new;
   vector[n_new] x_pred;   
   y_new[1:T] = output;
   y_new[T+1] = mu+phi*y[T]+theta*err+sigma;
   for (i in 2:n_new){
       y_new[T+i] = normal_rng(mu+phi*y_new[T+i-1]+theta*err, sigma);
    }
}

```

here is the full code? can you correct where is wrong?

---

<div class="post-metadata">

**Author:** ![martinmodrak](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/martinmodrak/32/133_2.png) [@martinmodrak](https://discourse.mc-stan.org/u/martinmodrak)\
**Post date:** [February 20, 2020, 4:53pm UTC](https://discourse.mc-stan.org/t/simulate-the-data-of-arma/13108/10 "2020-02-20T16:53:10Z")

</div>

Did you manage to resolve this? Also note that if your goal is to just run the model (as opposed to learning Stan to be able to do advanced modelling), you should be IMHO able to setup an ARMA model using `brms`, which has much friendlier learning curve and has facilities to predict and other common operations out of the box.

Best of luck with your model!

---

<div class="post-metadata">

**Author:** ![S\_mengyuan](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/s_mengyuan/32/7125_2.png) [@S\_mengyuan](https://discourse.mc-stan.org/u/S_mengyuan)\
**Post date:** [February 27, 2020, 5:37pm UTC](https://discourse.mc-stan.org/t/simulate-the-data-of-arma/13108/11 "2020-02-27T17:37:55Z")

</div>

thanks, I just want to learn it and the problem was solved.

---

<div class="post-metadata">

**Author:** ![martinmodrak](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/martinmodrak/32/133_2.png) [@martinmodrak](https://discourse.mc-stan.org/u/martinmodrak)\
**Post date:** [February 27, 2020, 8:46pm UTC](https://discourse.mc-stan.org/t/simulate-the-data-of-arma/13108/12 "2020-02-27T20:46:00Z")

</div>

If you could share your final code here, for possible future inquirers, it would be great! Thanks!

---

<div class="post-metadata">

**Author:** ![S\_mengyuan](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/s_mengyuan/32/7125_2.png) [@S\_mengyuan](https://discourse.mc-stan.org/u/S_mengyuan)\
**Post date:** [March 3, 2020, 9:25am UTC](https://discourse.mc-stan.org/t/simulate-the-data-of-arma/13108/13 "2020-03-03T09:25:08Z")

</div>

```stan
data {
  int<lower=1> T; // num observations
  real y[T]; // observed outputs
  int<lower=0> n_new;
}
transformed data{
    int<lower=0> x=T+n_new;
}
parameters {
  real mu; // mean coeff
  real phi; // autoregression coeff
  real theta; // moving avg coeff
  real<lower=0> sigma; // noise scale
}
model { 
  vector[x] nu; // prediction for time t
  vector[T] err; // error for time t
  nu[1] = mu + phi * mu; // assume err[0] == 0
  err[1] = y[1] - nu[1];
  for (t in 2:T) {
    nu[t] = mu + phi * y[t-1] + theta * err[t-1];
    err[t] = y[t] - nu[t];
    }
  for(t in (T+1):x) {
     nu[t] = mu + phi * nu[t-1] + theta*(nu[t-1]-nu[t-2]);
      }  
  mu ~ normal(0, 10); // priors
  phi ~ normal(0, 2);
  theta ~ normal(0, 2);
  sigma ~ cauchy(0, 5);
  err ~ normal(0, sigma); // likelihood
}
generated quantities{
 vector[x] prediction;
 for(t in 1:T){
    prediction[t] = y[t];
 }
 for(t in T:x){ 
    prediction[t] = mu+phi*prediction[t-1]+theta*(prediction[t-1]-prediction[t-2]); 
 } 
}

```

---

<div class="post-metadata">

**Author:** ![S\_mengyuan](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/s_mengyuan/32/7125_2.png) [@S\_mengyuan](https://discourse.mc-stan.org/u/S_mengyuan)\
**Post date:** [March 3, 2020, 9:27am UTC](https://discourse.mc-stan.org/t/simulate-the-data-of-arma/13108/14 "2020-03-03T09:27:07Z")

</div>

here is the code, but I’m not sure how to do MA part after existing series. alternatively, i just used random walk as error term.  
hope it is helpful!

---

<div class="post-metadata">

**Author:** ![andre.pfeuffer](https://avatars.discourse-cdn.com/v4/letter/a/e480ec/32.png) [@andre.pfeuffer](https://discourse.mc-stan.org/u/andre.pfeuffer)\
**Post date:** [March 3, 2020, 10:00am UTC](https://discourse.mc-stan.org/t/simulate-the-data-of-arma/13108/15 "2020-03-03T10:00:40Z")

</div>

According to:

[http://www-stat.wharton.upenn.edu/~stine/insr260\_2009/lectures/arma\_forc.pdf](http://www-stat.wharton.upenn.edu/~stine/insr260_2009/lectures/arma_forc.pdf)

for unknown error, the error is 0 . Thus err[T] is your last error, err[T+1:x] is 0.

```
for(t in 1:T){
prediction[t] = y[t];
}

```

shouldn’t that be `nu[t]` instead of `y[t]`?

---

<div class="post-metadata">

**Author:** ![S\_mengyuan](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/s_mengyuan/32/7125_2.png) [@S\_mengyuan](https://discourse.mc-stan.org/u/S_mengyuan)\
**Post date:** [March 3, 2020, 11:00am UTC](https://discourse.mc-stan.org/t/simulate-the-data-of-arma/13108/16 "2020-03-03T11:00:04Z")

</div>

> [@andre.pfeuffer](#):
>
> err[T+1:x] is 0

Thanks, I’m exploring the unknown error right now and try to use a acceptable to represent it. As you said err[T+1:x]=0 is one way, but I used the random walk to represent the error after T, maybe it’s not good. but I will try to using statistical model to simulate the errors after T.  
it should be nu[t] in stead of y[t], I just want to plot the original value so that I used y[t].

---

<div class="post-metadata">

**Author:** ![asael\_am](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/asael_am/32/5377_2.png) [@asael\_am](https://discourse.mc-stan.org/u/asael_am)\
**Post date:** [March 3, 2020, 12:04pm UTC](https://discourse.mc-stan.org/t/simulate-the-data-of-arma/13108/17 "2020-03-03T12:04:09Z")

</div>

Hi if you still have some struggles you can see my varstan package for time series models

I have implemented, SARIMA, GARCH VARMA, BEKK, and Dynamic Regression

> **[asael697/varstan](https://github.com/asael697/varstan)**
>
> Contribute to asael697/varstan development by creating an account on GitHub.

Is still on developing so installing it could be a little bit hard but you can try it or I can tell you where you can find what you are looking at

---

<div class="post-metadata">

**Author:** ![S\_mengyuan](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/s_mengyuan/32/7125_2.png) [@S\_mengyuan](https://discourse.mc-stan.org/u/S_mengyuan)\
**Post date:** [March 3, 2020, 12:06pm UTC](https://discourse.mc-stan.org/t/simulate-the-data-of-arma/13108/18 "2020-03-03T12:06:17Z")

</div>

thanks, that’s good.

If I want to use arma to predict the future value. do you think my model works or I should do something elso? I’m new to stan.

---

<div class="post-metadata">

**Author:** ![asael\_am](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/asael_am/32/5377_2.png) [@asael\_am](https://discourse.mc-stan.org/u/asael_am)\
**Post date:** [March 3, 2020, 12:11pm UTC](https://discourse.mc-stan.org/t/simulate-the-data-of-arma/13108/19 "2020-03-03T12:11:10Z")

</div>

Instead of this

```
for(t in T:x){
prediction[t] = mu+phiprediction[t-1]+theta(prediction[t-1]-prediction[t-2]);
}

```

Put:

```
for(t in T:x){
prediction[t] = normal_rng(mu+phiprediction[t-1]+theta(prediction[t-1]-prediction[t-2]) ,sigma) ;
}

```

Because you wanna have a sample of the 1-step ahead predictivie distribution.  
Actually a forecast is a probability distribution.

---

<div class="post-metadata">

**Author:** ![S\_mengyuan](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/s_mengyuan/32/7125_2.png) [@S\_mengyuan](https://discourse.mc-stan.org/u/S_mengyuan)\
**Post date:** [March 3, 2020, 12:12pm UTC](https://discourse.mc-stan.org/t/simulate-the-data-of-arma/13108/20 "2020-03-03T12:12:56Z")

</div>

> [@asael\_am](#):
>
> theta(prediction[t-1]-prediction[t-2])

do you think I should drop theta(prediction[t-1]-prediction[t-2]) out instead, because I’m doing the test, I should make the model as simple as possible first.

[Next page](https://discourse.mc-stan.org/t/simulate-the-data-of-arma/13108.md?page=2)
