了解后验和共轭先验

Suppose you’ve tossed a coin 1,000 times and obtained 292. You’d like to know what the probability of obtaining heads is from a single coin toss — but you don’t just want a single estimate, you want an entire distribution. If you define

假设您抛硬币1,000次并获得292。您想知道一次抛硬币获得正面的概率是多少–但您不仅需要一个估计,还需要一个完整的分布。 如果您定义

  • y: the number of heads you obtain

    y :获得的头数

  • θ: the probability of obtaining heads from a single coin toss

    θ :从一次抛硬币获得正面的概率

and then model y as a binomial distribution with n=1,000, then the posterior distribution is very easy to obtain with just a few lines of code:

然后将y建模为n = 1,000的二项式分布 那么只需几行代码就很容易获得后验分布:

import arviz as az
import pymc3 as pm


N = 1000
N_HEADS = 292


with pm.Model() as model:
    theta = pm.Beta("θ", alpha=1.0, beta=1.0)
    y = pm.Binomial("y", n=N, p=theta, observed=N_HEADS)
    trace = pm.sample(return_inferencedata=True)


az.plot_posterior(trace.posterior)
Image for post
Image by author
图片作者

But…could we have calculated this distribution analytically without PyMC3’s help?

但是……在没有 PyMC3的帮助下,我们可以分析地计算此分布吗?

贝叶斯定理 (Bayes’ theorem)

From Bayes’ theorem, we have:

根据贝叶斯定理 ,我们有:

Image for post
Image by author
图片作者

Applying this to our problem, we obtain

将其应用于我们的问题,我们获得

Image for post
Image by author
图片作者

Substituting the definitions of the two distributions, we can re-write the right-hand-side as

替换两个分布的定义,我们可以将右侧重写为

Image for post
Image by author
图片作者

Removing constants (that don’t depend on theta), we can substitute this into the equation above to obtain

删除常量(不依赖于theta),我们可以将其代入上面的等式以获得

Image for post
Image by author
图片作者

which we can recognise (by inspection) as a Beta(292+1, 1000–292+1) distribution. Because our posterior and our prior are from the same family, the Beta distribution is known as a conjugate prior for the Binomial distribution.

我们可以通过检查将其识别为Beta (292 + 1,1000–292 + 1)分布。 因为我们的后验和我们的先验来自同一家族,所以Beta分布被称为二项分布的共轭先验

If we try plotting this using scipy, we’ll get the same distribution that PyMC3 has calculated for us:

如果我们尝试使用scipy进行绘制,我们将获得与PyMC3为我们计算出的分布相同的分布:

import scipy.stats
import matplotlib.pyplot as plt
import seaborn as sns


fig, ax = plt.subplots(figsize=(14, 8))
sns.kdeplot(scipy.stats.beta(292 + 1, 1000 - 292 + 1).rvs(1000), ax=ax)
Image for post
Image by author
图片作者

That’s the nice thing about conjugate priors: they make posterior probabilities analytically tractable.

关于共轭先验是一件好事:它们使后验概率在分析上易于处理。

Calculating posteriors by hand is an excellent exercise in deepening your understanding. However, in general, we will not find ourselves in such fortunate situations — that’s when we need tools such as PyMC3.

手工计算后验是加深理解的极好练习。 但是,总的来说,我们不会在如此幸运的情况下发现自己—那时我们需要诸如PyMC3之类的工具。

进一步阅读 (Further reading)

For an excellent and practical guide to Bayesian statistics, I recommend reading Bayesian Analysis with Python and listening to the Learning Bayesian Statistics podcast.

要获得有关贝叶斯统计的出色且实用的指南,我建议阅读使用Python进行的贝叶斯分析,并收听学习贝叶斯统计播客

翻译自: https://towardsdatascience.com/making-sense-of-posteriors-and-conjugate-priors-17f01e16c270

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值