Simulate the data of ARMA

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.

1 Like

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.

1 Like

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

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

[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.

2 Likes

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>

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.

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

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?

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!