我已经看到有关PyMC重播的问题的相关问题(例如https://stats.stackexchange.com/questions/95514/pymc-random-seed-doesnt-guarantee-the-same-posterior-samples),但我看到的答案并没有完全解决我的问题。
我已经设置了一个简单的贝叶斯线性回归模型,并运行了5个链,每次重新播种numpy随机数发生器,其标称值相同,为123456.我预计这些跟踪将是相同的,但这是不是这样的。四条输出轨迹相同,但其中一条是不同的。如果我重新运行代码,就会看到类似的行为。
我正在运行PyMC版本2.3.2。
代码:
import numpy as np
import pymc
import matplotlib.pyplot as plt
# Number of traces to run
n_trace = 5
itr = 50000
brn = 5000
thn = 5
# Generate artificial data
N = 100
intercept = 4
slope = 1.5
true_x = np.random.uniform(0, 50, N)
true_y = slope*true_x + intercept
data_y = true_y + np.random.normal(0, 5, size=len(true_y))
for i in xrange(n_trace):
print '\nTrace {0} of {1}'.format(i+1, n_trace)
# Re-seed random number generator
pymc.numpy.random.seed(123456)
# MODEL ===========================================================================
# Define priors on "orphan" random variables
sig_y = pymc.Uniform('sig_y', 0, 10)
th0 = pymc.Normal('th0', mu=0, tau=0.0001)
th1 = pymc.Normal('th1', mu=0, tau=0.0001)
# Define deterministic linear predictor Xtrue -> Ytrue
@pymc.deterministic
def y_pred(x=true_x, th0=th0, th1=th1):
return th0 + th1*x
# Likelihood of observed y data
y_obs = pymc.Normal('y', mu=y_pred, tau=1/sig_y**2, value=data_y, observed=True)
M = pymc.MCMC(pymc.Model([th0, th1, sig_y, y_obs, y_pred, true_x]))
M.sample(iter=itr, burn=brn, thin=thn, verbose=0)
# MODEL ===========================================================================
print '\nTrace {0}'.format(i+1)
print('E[th0]:{0:8.3f}\tStd[th0]: {1:8.3f}'.format(M.trace(th0)[:].mean(), M.trace(th0)[:].std()))
print('E[th1]:{0:8.3f}\tStd[th1]: {1:8.3f}'.format(M.trace(th1)[:].mean(), M.trace(th1)[:].std()))
print('E[s_y]:{0:8.3f}\tStd[s_y]: {1:8.3f}'.format(M.trace(sig_y)[:].mean(), M.trace(sig_y)[:].std()))
输出(请注意,为清晰起见,我手动删除了进度条):
>>> import PyMC_Ex1_LinReg as lr
Trace 1 of 5
E[th0]: 4.176 Std[th0]: 1.008
E[th1]: 1.515 Std[th1]: 0.037
E[s_y]: 5.031 Std[s_y]: 0.364
Trace 2 of 5
E[th0]: 4.191 Std[th0]: 1.044
E[th1]: 1.514 Std[th1]: 0.038
E[s_y]: 5.040 Std[s_y]: 0.368
Trace 3 of 5
E[th0]: 4.176 Std[th0]: 1.008
E[th1]: 1.515 Std[th1]: 0.037
E[s_y]: 5.031 Std[s_y]: 0.364
Trace 4 of 5
E[th0]: 4.176 Std[th0]: 1.008
E[th1]: 1.515 Std[th1]: 0.037
E[s_y]: 5.031 Std[s_y]: 0.364
Trace 5 of 5
E[th0]: 4.176 Std[th0]: 1.008
E[th1]: 1.515 Std[th1]: 0.037
E[s_y]: 5.031 Std[s_y]: 0.364
>>>