Chapter 2
Chapter 2
Chapter 2
hood
B AY E S I A N D ATA A N A LY S I S I N P Y T H O N
Michal Oleszak
Machine Learning Engineer
Bayes' Theorem revisited
P (B∣A) ∗ P (A)
P (A∣B) =
P (B)
P (data∣parameters) ∗ P (parameters)
P (parameters∣data) =
P (data)
P(parameters) → prior distribution: what we know about the parameters before seeing
any data
num_heads head_prob
0 0 0.00
1 0 0.01
2 0 0.02
... ...
10199 100 0.99
10200 100 1.00
[10201 rows x 2 columns]
num_heads head_prob
0 0 0.00
1 0 0.01
2 0 0.02
... ...
10199 100 0.99
10200 100 1.00
[10201 rows x 2 columns]
coin["prior"] = uniform.pdf(coin["head_prob"])
coin["likelihood"] = binom.pmf(coin["num_heads"], 100, coin["head_prob"])
sns.lineplot(heads75["head_prob"], heads75["posterior_prob"])
plt.show()
Michal Oleszak
Machine Learning Engineer
Prior distribution
Prior distribution re ects what we know about the parameter before observing any data:
nothing → uniform distribution (all values equally likely)
One can choose any probability distribution as a prior to include external info in the model:
expert opinion
common knowledge
previous research
subjective belief
def get_heads_prob(tosses):
num_heads = np.sum(tosses)
# prior: Beta(1,1)
return np.random.beta(num_heads + 1, len(tosses) - num_heads + 1, 1000)
If posterior is known, we can sample from it If posterior is not known, we can calculate
using numpy : it using grid approximation.
Michal Oleszak
Machine Learning Engineer
The honest way
Report the prior and the posterior of each parameter
posterior_draws
posterior_mean = np.mean(posterior_draws)
posterior_mean = np.mean(posterior_draws)
posterior_median = np.median(posterior_draws)
posterior_mean = np.mean(posterior_draws)
posterior_median = np.median(posterior_draws)
posterior_p75 = np.percentile(posterior_draws, 75)
hpd = pm.hpd(posterior_draws,
hdi_prob=0.9)
print(hpd)
[-4.86840193 4.96075498]