PyMC3 (Metropolis-Hastings) 的录取率
Acceptance-rate in PyMC3 (Metropolis-Hastings)
有谁知道我如何在 PyMC3 (Metropolis-Hastings) 中查看最终接受率?或者一般来说,我怎样才能看到 pymc3.sample() returns 的所有信息?
谢谢
让step = pymc3.Metropolis()作为我们的采样器,我们可以通过
得到最终的接受率
"step.accepted"
仅供像我这样的初学者 (pymc3),在每个 variable/obj 之后。放一个“。”并按下 Tab 键;你会看到一些有趣的建议 ;)
举个例子,首先建立模型:
import pymc3 as pm3
sigma = 3 # Note this is the std of our data
data = norm(10,sigma).rvs(100)
mu_prior = 8
sigma_prior = 1.5 # Note this is our prior on the std of mu
plt.hist(data,bins=20)
plt.show()
basic_model = pm3.Model()
with basic_model:
# Priors for unknown model parameters
mu = pm3.Normal('Mean of Data',mu_prior,sigma_prior)
# Likelihood (sampling distribution) of observations
data_in = pm3.Normal('Y_obs', mu=mu, sd=sigma, observed=data)
其次,进行模拟:
chain_length = 10000
with basic_model:
# obtain starting values via MAP
startvals = pm3.find_MAP(model=basic_model)
# instantiate sampler
step = pm3.Metropolis()
# draw 5000 posterior samples
trace = pm3.sample(chain_length, step=step, start=startvals)
利用上面的例子,可以这样计算录取率:
accept = np.sum(trace['Mean of Data'][1:] != trace['Mean of Data'][:-1])
print("Acceptance Rate: ", accept/trace['Mean of Data'].shape[0])
(在网上的教程中找到了这个解决方案,但不是很懂。)
我查了一下NUTS算法,从这里找到了解决方案pymc3 forum。
trace.mean_tree_accept.mean()
有谁知道我如何在 PyMC3 (Metropolis-Hastings) 中查看最终接受率?或者一般来说,我怎样才能看到 pymc3.sample() returns 的所有信息?
谢谢
让step = pymc3.Metropolis()作为我们的采样器,我们可以通过
得到最终的接受率"step.accepted"
仅供像我这样的初学者 (pymc3),在每个 variable/obj 之后。放一个“。”并按下 Tab 键;你会看到一些有趣的建议 ;)
举个例子,首先建立模型:
import pymc3 as pm3
sigma = 3 # Note this is the std of our data
data = norm(10,sigma).rvs(100)
mu_prior = 8
sigma_prior = 1.5 # Note this is our prior on the std of mu
plt.hist(data,bins=20)
plt.show()
basic_model = pm3.Model()
with basic_model:
# Priors for unknown model parameters
mu = pm3.Normal('Mean of Data',mu_prior,sigma_prior)
# Likelihood (sampling distribution) of observations
data_in = pm3.Normal('Y_obs', mu=mu, sd=sigma, observed=data)
其次,进行模拟:
chain_length = 10000
with basic_model:
# obtain starting values via MAP
startvals = pm3.find_MAP(model=basic_model)
# instantiate sampler
step = pm3.Metropolis()
# draw 5000 posterior samples
trace = pm3.sample(chain_length, step=step, start=startvals)
利用上面的例子,可以这样计算录取率:
accept = np.sum(trace['Mean of Data'][1:] != trace['Mean of Data'][:-1])
print("Acceptance Rate: ", accept/trace['Mean of Data'].shape[0])
(在网上的教程中找到了这个解决方案,但不是很懂。)
我查了一下NUTS算法,从这里找到了解决方案pymc3 forum。
trace.mean_tree_accept.mean()