1

I am following the example code using Stan in Python but it is not compiled because of the syntax error:

ValueError: Failed to parse Stan model 'anon_model_ad43313e8b4b0b001a1a5d32da64a34e'. Error message:
SYNTAX ERROR, MESSAGE(S) FROM PARSER:

variable "real" does not exist.
  error in 'unknown file name' at line 20, column 9
  -------------------------------------------------
    18: }
    19: model{
    20:     real l1;
                ^
    21:     real l2;   
  -------------------------------------------------

And this is the stan code:

import numpy as np
import statsmodels.api as sm
import pystan
from scipy.stats import uniform, invgauss

stan_code = """
data{
    int<lower=0> N;
    vector[N] Y;
    vector[N] x1;
}
parameters{
    real beta0;
    real beta1;
    real<lower=0> lambda;
}
transformed parameters{
    vector[N] exb;
    vector[N] xb;

    for (i in 1:N) xb[i] = beta0 + beta1 * x1[i];
    for (i in 1:N) exb[i] = exp(xb[i]);
}
model{
    real l1;
    real l2;   
    vector[N] loglike;

    lambda ~ uniform(0.0001, 100);

    for (i in 1:N){
        l1 = 0.5 * (log(lambda) - log(2 * pi() * pow(Y[i], 3)));
        l2 = -lambda*pow(Y[i] - exb[i], 2)/(2 * pow(exb[i], 2) * Y[i]);
        loglike[i] = l1 + l2;
    }
    target += loglike;
}
"""

fit = pystan.stan(model_code=stan_code, data= stan_data, iter=5000, chains=3, warmup=2500, n_jobs=3)

Is there any something wrong? I also defined stan_data before implementing stan function. I read the Stan manual about the model block and it says it is allowed to declare local variables in the model block... Please let me know the problems above.

Eunji Lee
  • 33
  • 4

0 Answers0