The function numpy.random.beta is used to create one Beta distributed sample of size N, which is 10000 in example.
The alpha (renamed a) is 2 and beta (renamed b) is 5, same as before.
The mean of sample is contrasted with the actual value.
# ex16.py
from __future__ import print_function, division
import matplotlib.pyplot as plt
from numpy.random import beta
N = 10000
a = 2
b = 5
x = beta(a, b, N)
plt.hist(x, bins = 50, color = 'pink')
plt.xlabel('x')
plt.ylabel('Frequency')
plt.title('Histogram of x - Beta Distribution')
m1 = x.mean()
m2 = a/(a+b)
error = (m1-m2)/m2
print("mean of sample = %s but should be %s" % (m1,m2))
print("The error = %s" % error)
plt.show()
# mean of sample = 0.286522609772 but should be 0.285714285714
# The error = 0.00282913420239
Output:
No comments:
Post a Comment