forked from probml/pyprobml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbeta_binom_post_plot.py
More file actions
65 lines (52 loc) · 1.7 KB
/
beta_binom_post_plot.py
File metadata and controls
65 lines (52 loc) · 1.7 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
53
54
55
56
57
58
59
60
61
62
63
64
65
import numpy as np
import matplotlib.pyplot as plt
import os
def save_fig(fname):
figdir = os.path.join(os.environ["PYPROBML"], "figures")
plt.tight_layout()
fullname = os.path.join(figdir, fname)
print('saving to {}'.format(fullname))
plt.savefig(fullname)
from scipy.stats import dirichlet
#Points where we evaluate the pdf
x = np.linspace(0.001, .999, 100)
#Given an alpha parameter, this returns a pdf function
def MakeBeta(alpha):
def Beta(y):
return dirichlet.pdf([y, 1 - y], alpha)
Beta = np.vectorize(Beta)
return Beta
#Makes strings for the legend:
def MakeLabel(Data,which):
alpha = Data[which]
lab = which + " Be(" + str(alpha[0]) + ", " + str(alpha[1]) + ")"
return lab
#Forms graph give the parameters of the prior, likelihood and posterior:
def MakeGraph(Data,SaveName):
prior = MakeBeta(Data['prior'])(x)
likelihood = MakeBeta(Data['lik'])(x)
posterior = MakeBeta(Data['post'])(x)
fig, ax = plt.subplots()
ax.plot(x, prior, 'r', label=MakeLabel(Data, "prior"), linewidth=2.0)
ax.plot(x, likelihood, 'k--', label=MakeLabel(Data, "lik"), linewidth=2.0)
ax.plot(x, posterior, 'b--', label=MakeLabel(Data, "post"), linewidth=2.0)
ax.legend(loc='upper left', shadow=True)
save_fig(SaveName)
plt.show()
Data1 = {'prior': [1, 1],
'lik': [5, 2],
'post': [5, 2]}
Data2 = {'prior': [1, 1],
'lik': [41, 11],
'post': [41, 11]}
Data3 = {'prior': [2, 2],
'lik': [5, 2],
'post': [6, 3]}
Data4 = {'prior': [2, 2],
'lik': [41, 11],
'post': [42, 12]}
MakeGraph(Data1, "betaPost1")
MakeGraph(Data2, "betaPost2")
MakeGraph(Data3, "betaPost3")
MakeGraph(Data4, "betaPost4")
plt.show()