forked from aloctavodia/Doing_bayesian_data_analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
09_BernBetaMuKappaPyMC.py
159 lines (126 loc) · 4.74 KB
/
09_BernBetaMuKappaPyMC.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
"""
Bernoulli Likelihood with Hierarchical Prior!
"""
import numpy as np
import pymc3 as pm
import sys
from scipy.stats import beta, binom
import matplotlib.pyplot as plt
plt.style.use('seaborn-darkgrid')
# Data for figure 9.11
N = [10, 10, 10] # Number of flips per coin
z = [5, 5, 5] # Number of heads per coin
## Data for figure 9.12
#N = [10, 10, 10] # Number of flips per coin
#z = [1, 5, 9] # Number of heads per coin
## Data for exercise 9.1
#ncoins = 50
#nflipspercoin = 5
#mu_act = .7
#kappa_act = 20
#theta_act = beta.rvs(mu_act*kappa_act+1, (1-mu_act)*kappa_act+1, size=ncoins)
#z = binom.rvs(n=nflipspercoin, p=theta_act, size=ncoins)
#N = [nflipspercoin] * ncoins
# Arrange the data into a more convenient way to feed the PyMC model.
coin = [] # list/vector index for each coins (from 0 to number of coins)
y = [] # list/vector with head (1) or tails (0) for each flip.
for i, flips in enumerate(N):
heads = z[i]
if heads > flips:
sys.exit("The number of heads can't be greater than the number of flips")
else:
y = y + [1] * heads + [0] * (flips-heads)
coin = coin + [i] * flips
# Specify the model in PyMC
with pm.Model() as model:
# define the hyperparameters
mu = pm.Beta('mu', 2, 2)
kappa = pm.Gamma('kappa', 1, 0.1)
# define the prior
theta = pm.Beta('theta', mu * kappa, (1 - mu) * kappa, shape=len(N))
# define the likelihood
y = pm.Bernoulli('y', p=theta[coin], observed=y)
# Generate a MCMC chain
trace = pm.sample(1000, progressbar=False)
## Check the results.
## Print summary for each trace
#pm.df_summary(trace)
#pm.df_summary(trace)
## Check for mixing and autocorrelation
pm.autocorrplot(trace, varnames=['mu', 'kappa'])
#pm.autocorrplot(trace, varnames =[mu, kappa])
## Plot KDE and sampled values for each parameter.
pm.traceplot(trace)
#pm.traceplot(trace)
# Create arrays with the posterior sample
theta1_sample = trace['theta'][:,0]
theta2_sample = trace['theta'][:,1]
theta3_sample = trace['theta'][:,2]
mu_sample = trace['mu']
kappa_sample = trace['kappa']
# Scatter plot hyper-parameters
fig, ax = plt.subplots(4, 3, figsize=(12,12))
ax[0, 0].scatter(mu_sample, kappa_sample, marker='o', color='skyblue')
ax[0, 0].set_xlim(0,1)
ax[0, 0].set_xlabel(r'$\mu$')
ax[0, 0].set_ylabel(r'$\kappa$')
# Plot mu histogram
#plot_post(mu_sample, xlab=r'$\mu$', show_mode=False, labelsize=9, framealpha=0.5)
pm.plot_posterior(mu_sample, ax=ax[0, 1], color='skyblue')
ax[0, 1].set_xlabel(r'$\mu$')
ax[0, 1].set_xlim(0,1)
# Plot kappa histogram
#plot_post(kappa_sample, xlab=r'$\kappa$', show_mode=False, labelsize=9, framealpha=0.5)
pm.plot_posterior(kappa_sample, ax=ax[0, 2], color='skyblue')
ax[0, 2].set_xlabel(r'$\kappa$')
# Plot theta 1
#plot_post(theta1_sample, xlab=r'$\theta1$', show_mode=False, labelsize=9, framealpha=0.5)
pm.plot_posterior(theta1_sample, ax=ax[1, 0], color='skyblue')
ax[1, 0].set_xlabel(r'$\theta1$')
ax[1, 0].set_xlim(0,1)
# Scatter theta 1 vs mu
ax[1, 1].scatter(theta1_sample, mu_sample, marker='o', color='skyblue')
ax[1, 1].set_xlim(0,1)
ax[1, 1].set_ylim(0,1)
ax[1, 1].set_xlabel(r'$\theta1$')
ax[1, 1].set_ylabel(r'$\mu$')
# Scatter theta 1 vs kappa
ax[1, 2].scatter(theta1_sample, kappa_sample, marker='o', color='skyblue')
ax[1, 2].set_xlim(0,1)
ax[1, 2].set_xlabel(r'$\theta1$')
ax[1, 2].set_ylabel(r'$\kappa$')
# Plot theta 2
#plot_post(theta2_sample, xlab=r'$\theta2$', show_mode=False, labelsize=9, framealpha=0.5)
pm.plot_posterior(theta2_sample, ax=ax[2, 0], color='skyblue')
ax[2, 0].set_xlabel(r'$\theta2$')
ax[2, 0].set_xlim(0,1)
# Scatter theta 2 vs mu
ax[2, 1].scatter(theta2_sample, mu_sample, marker='o', color='skyblue')
ax[2, 1].set_xlim(0,1)
ax[2, 1].set_ylim(0,1)
ax[2, 1].set_xlabel(r'$\theta2$')
ax[2, 1].set_ylabel(r'$\mu$')
# Scatter theta 2 vs kappa
ax[2, 2].scatter(theta2_sample, kappa_sample, marker='o', color='skyblue')
ax[2, 2].set_xlim(0,1)
ax[2, 2].set_xlabel(r'$\theta2$')
ax[2, 2].set_ylabel(r'$\kappa$')
# Plot theta 3
#plot_post(theta3_sample, xlab=r'$\theta3$', show_mode=False, labelsize=9, framealpha=0.5)
pm.plot_posterior(theta3_sample, ax=ax[3, 0], color='skyblue')
ax[3, 0].set_xlabel(r'$\theta3$')
ax[3, 0].set_xlim(0,1)
# Scatter theta 3 vs mu
ax[3, 1].scatter(theta3_sample, mu_sample, marker='o', color='skyblue')
ax[3, 1].set_xlim(0,1)
ax[3, 1].set_ylim(0,1)
ax[3, 1].set_xlabel(r'$\theta3$')
ax[3, 1].set_ylabel(r'$\mu$')
# Scatter theta 3 vs kappa
ax[3, 2].scatter(theta3_sample, kappa_sample, marker='o', color='skyblue')
ax[3, 2].set_xlim(0,1)
ax[3, 2].set_xlabel(r'$\theta3$')
ax[3, 2].set_ylabel(r'$\kappa$')
plt.tight_layout()
plt.savefig('Figure_9.11.png')
plt.show()