PYSTAN:无法解析模型[如何使用未采样而计算出的数据]

时间:2018-08-20 10:44:24

标签: python statistics bayesian pystan

对于以下模型,出现以下错误:

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

Cannot assign to variable outside of declaration block; left-hand-side variable origin=data
  error in 'unknown file name' at line 21, column 26
  -------------------------------------------------
    19:     angular_speed ~ uniform(0, pi());
    20:     radius ~ normal(radius_mu, radius_sigma);
    21:     for (n in 1:N) x[n] = radius_mu*cos(alpha[n]);
                                 ^
    22:     for (n in 1:N) u[n] = 2*pi()*angular_speed*cos(pi()/2*x[n]/radius[n]);
  -------------------------------------------------

PARSER EXPECTED: <expression assignable to left-hand side>

我的直觉是,当根据数据块中定义的输出数据是从采样值(在这种情况下为半径)“计算”而不是自身采样时,pystan不喜欢它。我注意到这样做的时候它消失了:

real x_bis[N]
real u_bis[N]
for (n in 1:N) x_bis[n] = radius_mu*cos(alpha[n]);
for (n in 1:N) u_bis[n] = 2*pi()*angular_speed*cos(pi()/2*x[n]/radius[n]);
x ~ normal(x_bis, 0.0001);
u ~ normal(u_bis, 0.0001);

但是,这不是我希望此模型起作用的方式。因此,我的问题是:“是否有一种方法不对数据进行采样,而仅根据采样值/参数计算?”您可以在下面找到完整的模型代码:

code = """
data {
    int<lower=0> N;
    real x[N];
    real<lower=0> u[N];
}
parameters {
    real<lower=0> radius_mu; 
    real radius_sigma;
    real<lower=0> angular_speed;
}
transformed parameters {
}
model {
    real alpha[N];
    real radius[N];

    alpha ~ uniform(0, pi());
    angular_speed ~ uniform(0, pi());
    radius ~ normal(radius_mu, radius_sigma);
    for (n in 1:N) x[n] = radius_mu*cos(alpha[n]);
    for (n in 1:N) u[n] = 2*pi()*angular_speed*cos(pi()/2*x[n]/radius[n]);
}
generated quantities{
}
"""

0 个答案:

没有答案