gpt4 book ai didi

python - 将 pyMC2 贝叶斯 A/B 测试示例移植到 pyMC3

转载 作者:太空狗 更新时间:2023-10-30 00:02:56 31 4
gpt4 key购买 nike

我正在努力学习 pyMC 3,但遇到了一些麻烦。由于 pyMC3 的教程有限,我从 Bayesian Methods for Hackers 开始工作.我正在尝试将 pyMC 2 代码移植到 Bayesian A/B testing 中的 pyMC 3例如,没有成功。据我所知,该模型根本没有考虑观察结果。

我不得不对示例进行一些更改,因为 pyMC 3 完全不同,所以看起来应该像这样: 将 pymc 作为 pm 导入

# The parameters are the bounds of the Uniform.
p = pm.Uniform('p', lower=0, upper=1)

# set constants
p_true = 0.05 # remember, this is unknown.
N = 1500

# sample N Bernoulli random variables from Ber(0.05).
# each random variable has a 0.05 chance of being a 1.
# this is the data-generation step
occurrences = pm.rbernoulli(p_true, N)

print occurrences # Remember: Python treats True == 1, and False == 0
print occurrences.sum()

# Occurrences.mean is equal to n/N.
print "What is the observed frequency in Group A? %.4f" % occurrences.mean()
print "Does this equal the true frequency? %s" % (occurrences.mean() == p_true)

# include the observations, which are Bernoulli
obs = pm.Bernoulli("obs", p, value=occurrences, observed=True)

# To be explained in chapter 3
mcmc = pm.MCMC([p, obs])
mcmc.sample(18000, 1000)

figsize(12.5, 4)
plt.title("Posterior distribution of $p_A$, the true effectiveness of site A")
plt.vlines(p_true, 0, 90, linestyle="--", label="true $p_A$ (unknown)")
plt.hist(mcmc.trace("p")[:], bins=25, histtype="stepfilled", normed=True)
plt.legend()

相反看起来像:

import pymc as pm

import random
import numpy as np
import matplotlib.pyplot as plt

with pm.Model() as model:
# Prior is uniform: all cases are equally likely
p = pm.Uniform('p', lower=0, upper=1)

# set constants
p_true = 0.05 # remember, this is unknown.
N = 1500

# sample N Bernoulli random variables from Ber(0.05).
# each random variable has a 0.05 chance of being a 1.
# this is the data-generation step
occurrences = [] # pm.rbernoulli(p_true, N)
for i in xrange(N):
occurrences.append((random.uniform(0.0, 1.0) <= p_true))
occurrences = np.array(occurrences)
obs = pm.Bernoulli('obs', p_true, observed=occurrences)

start = pm.find_MAP()
step = pm.Metropolis()
trace = pm.sample(18000, step, start)
pm.traceplot(trace);
plt.show()

为冗长的帖子道歉,但在我的改编中有一些小的变化,例如手动生成观察结果,因为 pm.rbernoulli 不再存在。我也不确定我是否应该在运行跟踪之前找到起点。我应该如何更改我的实现才能正确运行?

最佳答案

你确实很接近。然而,这一行:

obs = pm.Bernoulli('obs', p_true, observed=occurrences)

是错误的,因为您只是为 p (p_true == 0.05) 设置了一个常量值。因此,上面定义的具有统一先验的随机变量 p 不受可能性的约束,并且您的图显示您只是从先验中抽样。如果您在代码中将 p_true 替换为 p,它应该可以工作。这是固定版本:

import pymc as pm

import random
import numpy as np
import matplotlib.pyplot as plt

with pm.Model() as model:
# Prior is uniform: all cases are equally likely
p = pm.Uniform('p', lower=0, upper=1)

# set constants
p_true = 0.05 # remember, this is unknown.
N = 1500

# sample N Bernoulli random variables from Ber(0.05).
# each random variable has a 0.05 chance of being a 1.
# this is the data-generation step
occurrences = [] # pm.rbernoulli(p_true, N)
for i in xrange(N):
occurrences.append((random.uniform(0.0, 1.0) <= p_true))
occurrences = np.array(occurrences)
obs = pm.Bernoulli('obs', p, observed=occurrences)

start = pm.find_MAP()
step = pm.Metropolis()
trace = pm.sample(18000, step, start)

pm.traceplot(trace);

关于python - 将 pyMC2 贝叶斯 A/B 测试示例移植到 pyMC3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22708513/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com