Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
32 lines (26 sloc) 821 Bytes
"""
Code by Tae-Hwan Hung(@graykode)
https://en.wikipedia.org/wiki/Beta_distribution
"""
import numpy as np
from matplotlib import pyplot as plt
def gamma_function(n):
cal = 1
for i in range(2, n):
cal *= i
return cal
def beta(x, a, b):
gamma = gamma_function(a + b) / \
(gamma_function(a) * gamma_function(b))
y = gamma * (x ** (a - 1)) * ((1 - x) ** (b - 1))
return x, y, np.mean(y), np.std(y)
for ls in [(1, 3), (5, 1), (2, 2), (2, 5)]:
a, b = ls[0], ls[1]
# x in [0, 1], trial is 1/0.001 = 1000
x = np.arange(0, 1, 0.001, dtype=np.float)
x, y, u, s = beta(x, a=a, b=b)
plt.plot(x, y, label=r'$\mu=%.2f,\ \sigma=%.2f,'
r'\ \alpha=%d,\ \beta=%d$' % (u, s, a, b))
plt.legend()
plt.savefig('graph/beta.png')
plt.show()
You can’t perform that action at this time.