In [22]:
%matplotlib inline 
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
x_axis = np.arange(-10, 10, 0.001)

In [23]:
# Mean = 0, SD = 2.
dist_a = stats.norm.pdf(x_axis,0,2)

In [24]:
dist_a[10000]


Out[24]:
0.19947114020071635

In [25]:
# Mean = 1, SD = 2
plt.plot(x_axis, dist_a)
# plt.plot(x_axis, dist_b)
# plt.fill_between(x_axis, dist_a, dist_b, where=dist_b>=dist_a, facecolor='green', interpolate=True)
# plt.fill_between(x_axis, dist_a, dist_b, where=dist_b<=dist_a, facecolor='blue', interpolate=True)
plt.show()



In [27]:
dist_b = stats.norm.pdf(x_axis,1,2)
plt.plot(x_axis, dist_b)


Out[27]:
[<matplotlib.lines.Line2D at 0x7fd544902e10>]

In [28]:
actual = np.array([0.4, 0.6]) # actual number of red and blue balls
model1 = np.array([0.2, 0.8]) # numbers we got above in 10 samples
model2 = np.array([0.35, 0.65]) # numbers we got above for 100 samples

kl1 = (model1 * np.log(model1/actual)).sum()
print "Model 1: ", kl1


Model 1:  0.0915162218494

In [30]:
kl2 = (model2 * np.log(model2/actual)).sum()
print "Model 2: ", kl2


Model 2:  0.00529177256922

如果要使弿