Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
probml
GitHub Repository: probml/pyprobml
Path: blob/master/deprecated/scripts/beta_binom_post_pred_plot.py
1192 views
1
2
# Plots the posterior and plugin predictives for the Beta-Binomial distribution.
3
4
5
import superimport
6
7
import numpy as np
8
import matplotlib.pyplot as plt
9
import pyprobml_utils as pml
10
11
from scipy.special import comb, beta
12
from scipy.stats import binom
13
14
15
N = 10 # Future sample size M
16
# Hyperparameters
17
a = 1
18
b = 1
19
N1 = 4
20
N0 = 1
21
22
ind = np.arange(N+1)
23
post_a = a + N1
24
post_b = b + N0
25
26
# Compound beta-binomial distribution
27
distribution = []
28
for k in range(N+1):
29
distribution.append(comb(N,k) * beta(k+post_a, N-k+post_b) / beta(post_a, post_b))
30
31
fig,ax = plt.subplots()
32
rects = ax.bar(ind, distribution, align='center')
33
ax.set_title('posterior predictive')
34
ax.set_xticks(list(range(N+1)))
35
ax.set_xticklabels(list(range(N+1)))
36
pml.savefig('BBpostpred.pdf')
37
plt.show()
38
39
# Plugin binomial distribution
40
mu = (post_a - 1) / float(post_a + post_b - 2) # MAP estimate
41
distribution = []
42
rv = binom(N, mu)
43
for k in range(N+1):
44
distribution.append(rv.pmf(k))
45
46
fig,ax = plt.subplots()
47
rects = ax.bar(ind, distribution, align='center')
48
ax.set_title('plugin predictive')
49
ax.set_xticks(list(range(N+1)))
50
ax.set_xticklabels(list(range(N+1)))
51
pml.savefig('BBpluginpred.pdf')
52
plt.show()
53
54
55
56