-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgammaDistribution.py
More file actions
37 lines (24 loc) · 947 Bytes
/
gammaDistribution.py
File metadata and controls
37 lines (24 loc) · 947 Bytes
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
import numpy as np
import matplotlib.pyplot as plt
import scipy.special as sps
from scipy.optimize import fsolve
from math import exp, log
##########################################
# use any positive numbers that you want #
shape, scale = 5., 1.73 #
##########################################
s = np.random.gamma(shape, scale, 1000)
def findParameters(x):
x_a = sum(x) / len(x)
x_g = exp(sum([log(i) for i in x]) / len(x))
t = x_a / x_g
f = lambda x : (x - (t*exp(sps.digamma(x))))
k = fsolve(f, 3)[0]
return k, x_a / k
my_k, my_theta = findParameters(s)
count, bins, ignored = plt.hist(s, 50, normed=True)
real_y = bins**(shape-1)*(np.exp(-bins/scale) / (sps.gamma(shape)*scale**shape))
my_y = bins**(my_k-1)*(np.exp(-bins/my_theta) / (sps.gamma(my_k)*my_theta**my_k))
plt.plot(bins, real_y, linewidth=2, color='r')
plt.plot(bins, my_y, linewidth=2, color='g')
plt.show()