Stan中的“错误:变量“真实”在模型块中不存在”

时间:2018-08-16 01:43:37

标签: python stan pystan

我正在Python中使用Stan跟踪示例代码,但由于语法错误而未编译:

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;   
  -------------------------------------------------

这是stan代码:

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)

有什么问题吗?在实现stan_data函数之前,我还定义了stan。 我阅读了有关模型块的Stan手册,并说允许在模型块中声明局部变量... 请让我知道上述问题。

0 个答案:

没有答案