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](https://miro.medium.com/max/9999/1*qrMIefB_Jzx-9Si5iO837g.png)
But…could we have calculated this distribution analytically without PyMC3’s help?
但是……在没有 PyMC3的帮助下,我们可以分析地计算此分布吗?
贝叶斯定理 (Bayes’ theorem)
From Bayes’ theorem, we have:
根据贝叶斯定理 ,我们有:
![Image for post](https://miro.medium.com/max/9999/0*09AZ3KIJe6IwGxUA.png)
Applying this to our problem, we obtain
将其应用于我们的问题,我们获得
![Image for post](https://miro.medium.com/max/9999/0*kXG0xLOUWGdsL5jy.png)
Substituting the definitions of the two distributions, we can re-write the right-hand-side as
替换两个分布的定义,我们可以将右侧重写为
![Image for post](https://miro.medium.com/max/9999/0*R1-EoEmQOY09WREY.png)
Removing constants (that don’t depend on theta), we can substitute this into the equation above to obtain
删除常量(不依赖于theta),我们可以将其代入上面的等式以获得
![Image for post](https://miro.medium.com/max/9999/0*ccSKYIPsyexl7r15.png)
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](https://miro.medium.com/max/9999/1*Rlw0RE7sS4kTjyOShhtCRA.png)
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