forked from probml/pyprobml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirichlet_samples_plot.py
More file actions
41 lines (31 loc) · 1.07 KB
/
dirichlet_samples_plot.py
File metadata and controls
41 lines (31 loc) · 1.07 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
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)
np.random.seed(0)
NSamples = 5
X = np.arange(1, 6)
# This function generators a few bar graphs showing samples generated from a
# Dirichlet distribution with a param vector with elements = alpha.
def MakeDirSampleFig(alpha):
AlphaVec = np.repeat(alpha, NSamples)
samps = np.random.dirichlet(AlphaVec, NSamples)
fig, ax = plt.subplots(NSamples)
fig.suptitle('Samples from Dir (alpha=' + str(alpha) +')', y=1)
fig.tight_layout()
for i in range(NSamples):
ax[i].bar(X, samps[i, :], align='center')
ax[i].set_ylim([0, 1])
ax[i].yaxis.set_ticks([0, .5, 1])
ax[i].set_xlim([min(X) - .5, max(X) + .5])
plt.draw()
SaveN = "dirSample" + str(int(np.round(10*alpha))) + ".pdf"
save_fig(SaveN)
MakeDirSampleFig(.1)
MakeDirSampleFig(1.0)
plt.show()