PyStan模型描述



我试图建立一个简单的统计模型来研究随机模型和PyStan
这里,这是我在PyStan中的模型描述,不太好用。请帮帮我。

  • 模型是通过人的Age来预测Val。其公式是ValNormDist生成
  • 数据大小为NAge的最大值为N_age
  • CCD_ 8的作用取决于CCD_。在这个脚本中,a_age[N]代表它
  • CCD_ 11被归一化

stanexec= """
data {
int N;
int N_age;
vector[N] Val;
vector[N] Age;
}
parameters {
real mu;
vector[N_age] a_age;
real<lower=0> s_age;
real<lower=0> s;
}
model {
a_age[1] ~ normal(-sum(a_age[2:N_age]), 0.001);
a_age[2:N_age] ~ normal(a_age[1:(N_age-1)],s_age);    
for (i in 1:N){
Val[i] ~ normal(mu+a_age[Age[i]],s);
} 
}
"""

这个脚本出错了。其他型号运行良好,没有问题。所以,我希望miss存在于这个模型描述中。

ValueError                                Traceback (most recent call last)
<ipython-input-4-2e995ebf95f8> in <module>
11     thin=1,
12     warmup=100,
---> 13     seed=1
14 )
15 mcmc_sample  = fit.extract(permuted=True)
~Anaconda3libsite-packagespystanapi.py in stan(file, model_name, model_code, fit, data, pars, chains, iter, warmup, thin, init, seed, algorithm, control, sample_file, diagnostic_file, verbose, boost_lib, eigen_lib, include_paths, n_jobs, **kwargs)
426                       boost_lib=boost_lib, eigen_lib=eigen_lib,
427                       include_paths=include_paths,
--> 428                       obfuscate_model_name=obfuscate_model_name, verbose=verbose)
429     # check that arguments in kwargs are valid
430     valid_args = {"chain_id", "init_r", "test_grad", "append_samples", "enable_random_init",
~Anaconda3libsite-packagespystanmodel.py in __init__(self, file, charset, model_name, model_code, stanc_ret, include_paths, boost_lib, eigen_lib, verbose, obfuscate_model_name, extra_compile_args)
221                                          verbose=verbose,
222                                          include_paths=include_paths,
--> 223                                          obfuscate_model_name=obfuscate_model_name)
224 
225         if not isinstance(stanc_ret, dict):
~Anaconda3libsite-packagespystanapi.py in stanc(file, charset, model_code, model_name, include_paths, verbose, obfuscate_model_name)
165             msg = msg.encode('ascii', 'replace')
166         error_msg = "Failed to parse Stan model '{}'. Error message:n{}".format(model_name, msg)
--> 167         raise ValueError(error_msg)
168     elif result['status'] == 0:  # SUCCESS_RC is 0
169         logger.debug("Successfully parsed Stan model '{}'.".format(model_name))
ValueError: Failed to parse Stan model 'anon_model_57b2ca24f38f5d3aa2d8b8fac976c276'. Error message:
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Container index must be integer; found type=real
error in 'unknown file name' at line 21, column 35
-------------------------------------------------
19:     a_age[2:N_age] ~ normal(a_age[1:(N_age-1)],s_age);    
20:     for (i in 1:N){
21:         Val[i] ~ normal(mu+a_age[Age[i]],s_age);
^
22:     }
-------------------------------------------------
PARSER EXPECTED: <one or more container indexes followed by ']'>

我知道已经有一段时间了,但也许其他人仍然需要解决方案。您面临的问题是,您只能使用整数进行索引。STAN中的矢量属于实数类型。如果要使用Age元素(即向量(进行索引,则需要使用实数进行索引。这是不可能的。解决方案是将Age声明为整数数组,如下所示:

int Age[N];

这应该能解决你的问题。

最新更新