-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter 3
More file actions
53 lines (38 loc) · 2.14 KB
/
Chapter 3
File metadata and controls
53 lines (38 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
Chapter 3: Conjugate Priors & Posterior Distributions
In our first example, the Beta prior and Binomial likelihood combined to create a Beta posterior. This is an example of conjugacy.
3.1 The Concept of Conjugacy
A prior distribution is conjugate to a likelihood if the resulting posterior distribution belongs to the same family as the prior.
The primary benefit is algebraic convenience. It gives us a simple "update rule" to find the posterior without complex math.
3.2 Common Conjugate Pairs
|Likelihood|Parameter | conjugate prior| posterior distribtution update rule |
| :--- | :---: | :---: | ---: |
|Binomial| p (probability)| Beta| Beta(prior alpha + #successes, prior beta + #failures)|
|Poisson| λ (rate)| Gamma |Gamma(prior shape + sum of data, prior rate + #observations)|
|Normal (known var)| μ (mean)| Normal| A Normal distribution with updated mean and variance.|
|Multinomial| p (vector of probs)| Dirichlet | Dirichlet(prior alphas + counts in each category)|
3.3 Code Example: Conjugacy in PyMC
We can use PyMC to sample from our Beta-Binomial model. The MCMC sampler will produce draws that trace out the analytical posterior distribution, Beta(9, 5).
Python
import pymc as pm
import arviz as az
from scipy.stats import beta
import matplotlib.pyplot as plt
import numpy as np
# Analytical posterior parameters from Chapter 1
alpha_post = 2 + 7
beta_post = 2 + 3
# 1. Define the model in PyMC
with pm.Model() as coin_model:
# Prior: Beta(2, 2)
p = pm.Beta('p', alpha=2.0, beta=2.0)
# Likelihood: Binomial(n=10, p) with observed data k=7
y = pm.Binomial('y', n=10, p=p, observed=7)
# 2. Sample from the posterior using MCMC
trace = pm.sample(2000, tune=1000, chains=4)
# 3. Plot the results and compare with the analytical solution
az.plot_posterior(trace, var_names=['p'], kind='hist')
x = np.linspace(0, 1, 100)
plt.plot(x, beta.pdf(x, alpha_post, beta_post), 'r-', lw=3, label=f'Analytical Posterior: Beta({alpha_post},{beta_post})')
plt.legend()
plt.show()
The histogram of samples from PyMC will perfectly match the red curve of the analytical Beta(9, 5) distribution, showing that MCMC converges to the correct answer.