gpt4 book ai didi

python - 后验概率 python 示例

转载 作者:行者123 更新时间:2023-12-01 09:05:12 25 4
gpt4 key购买 nike

我一直在构建一个简单的近似贝叶斯计算应用程序,但遇到了问题。我不知道如何正确实现posterior probability .

我的先前:非信息性(均匀分布)

生成模型:使用 numpy 二项式分布实现随机是/否猜测

这是代码:

import numpy as np
import pandas as pd

def pprob():
pass

def generative_model(n_events, p):
return np.random.binomial(n_events, p)

def ABC(n_occured, n_events, n_draws=100000):
prior = pd.Series(np.random.uniform(0, 1, size=n_draws))
sim_data = [generative_model(n_events, p) for p in prior]
posterior = prior[list(map(lambda x: x == n_occured, sim_data))]
posterior_probability = pprob()

ABC(10, 16)

提前致谢!

最佳答案

谢谢:this site

我的解决方案的完整代码是:

import math
from scipy import stats
from scipy.special import factorial
from matplotlib import pyplot as plt

def likelihood(theta, n, x):
return (factorial(n) / (factorial(x) * factorial(n - x))) * (theta x) * ((1 - theta) (n - x))

def pprob(prior, posterior, n_occured, n_events):
return pd.Series(map(lambda theta: likelihood(theta, n_events, n_occured), prior))

def generative_model(n_events, p):
return np.random.binomial(n_events, p)

def ABC(n_occured, n_events, n_draws=1000):
prior = pd.Series(sorted(np.random.uniform(0, 1, size=n_draws)))
sim_data = [generative_model(n_events ,p) for p in prior]
posterior = prior[list(map(lambda x: x == n_occured, sim_data))]
posterior_probability = pprob(prior, posterior, n_occured, n_events)

# let's see what we got
f, ax = plt.subplots(1)
ax.plot(prior, posterior_probability)
ax.set_xlabel("Theta")
ax.set_ylabel("Likelihood")
ax.grid()
ax.set_title("Likelihood of Theta for New Campaign")
plt.show()

ABC(10, 16)

给我带来了这个可爱的可能性:

[a ]

关于python - 后验概率 python 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52118510/

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