几年前我学习过斯坦,当时的教程模型是8schools.stan,几年前我忙着做其他事情。我现在回来试着重新学习斯坦。现在,教程模型只是schools.stan。我运行了这两个版本的相同的基本模型,将种子设置为相同的值。我得到了两个结果,它们非常相似,但不完全相同,但lp__的值却有很大的不同。
8schools.stan和schools.stan之间唯一的区别是在模型部分。这两个文件的区别是:
[c:\Larry\R-Spaces\STAN]# diff 8schools.stan school.stan
7,18c17,18
eta ~ normal(0, 1);
y ~ normal(theta, sigma);
--
target += normal_lpdf(eta | 0, 1);
target += normal_lpdf(y | theta, sigma);
9a20据我所知,这两个模型语句是等价的。我使用教程中给出的相同的schools_dat数据集运行这两个模型,使用以下STAN调用,只将fit1更改为fit2,并在两次运行中将STAN文件从8schools.stan更改为schools.stan。
fit2 <- stan(
file = "schools.stan", # Stan program
data = schools_dat, # named list of data
chains = 4, # number of Markov chains
warmup = 1000, # number of warmup iterations per chain
iter = 2000, # total number of iterations per chain
cores = 4, # number of cores (using 2 just for the vignette)
refresh = 1000, # show progress every 'refresh' iterations
seed = 5
)8所学校的成绩:
mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
mu 8.07 0.12 5.12 -1.57 4.73 7.92 11.25 19.01 1839 1
tau 6.54 0.14 5.55 0.20 2.45 5.19 9.06 21.00 1491 1
eta[1] 0.37 0.01 0.92 -1.45 -0.24 0.39 0.98 2.12 4000 1
...
...
theta[8] 8.68 0.14 8.03 -5.68 3.79 8.15 12.92 26.57 3403 1
lp__ -4.79 0.07 2.51 -10.25 -6.37 -4.57 -3.04 -0.41 1202 1对于schools.stan:
mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
mu 8.04 0.19 5.25 -2.05 4.76 7.84 11.20 18.65 730 1.00
tau 6.34 0.20 5.46 0.22 2.33 5.00 8.86 21.39 724 1.01
eta[1] 0.35 0.02 0.94 -1.56 -0.28 0.38 0.99 2.12 3071 1.00
...
...
theta[8]8.43 0.15 7.63 -6.59 3.78 8.13 12.63 25.05 2742 1.00
lp__ -39.67 0.07 2.60 -45.31 -41.23 -39.45 -37.81 -35.25 1336 1.00这两个模型的结果非常接近,但不完全相同,除了lp__,这是非常不同的。我怀疑这两个模型的编译方式略有不同,因此种子没有给出相同的值。但是,这两个模型语句真的是相同的吗?除了估计参数的细微差异--在抽样预期的变异范围内(但请注意相同的种子),显著的差异是lp__的值。
这里发生了什么事?预先感谢任何能为我澄清这个问题的人。
发布于 2017-01-05 04:43:06
现在,带有~的版本会在密度函数中删除任何常数(在正常情况下是两个pi的平方根),而使用+=的版本则会保留它们。参数估计不应该有任何系统上的差异,尽管如果不设置伪随机数生成器种子,它们将不完全相同。
https://stackoverflow.com/questions/41475908
复制相似问题