我正在使用 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;
-------------------------------------------------
这是斯坦代码:
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 手册,它说允许在模型块中声明局部变量......请让我知道上面的问题。