In [1]:
%matplotlib inline
import numpy as np
from scipy.stats import gamma
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_context("poster")
sns.set_style("ticks")

In [3]:
x = np.arange(0.1,3, 0.01)
y = gamma.pdf(x, 1.0, scale=1.0)
print y[:10]


[ 0.90483742  0.89583414  0.88692044  0.87809543  0.86935824  0.86070798
  0.85214379  0.84366482  0.83527021  0.82695913]

In [4]:
for k in np.array([0.5,1.0, 2.0]):
    for theta in np.array([0.5, 1.0, 2.0]):
        y = gamma.pdf(x, k, scale=theta)
        plt.plot(x,y, label=r'$k= %s, \theta = %s$' % (k, theta))

plt.xlabel("X")
plt.ylabel("Y")
plt.title("Gamma Distribution")
plt.legend()
plt.show()



In [6]:
fit_alpha, fit_loc, fit_beta= gamma.fit(x)
print fit_alpha, fit_loc, fit_beta


400000006.305 -16741.5148762 4.18576490337e-05

In [7]:
x = [1,1,3, 15, 4, 5]
fit_alpha, fit_loc, fit_beta= gamma.fit(x)
print fit_alpha, fit_loc, fit_beta


0.535656012133 1.0 4.1320527502

In [9]:
plt.plot(x, gamma.pdf(x,fit_alpha, loc=fit_loc, scale=fit_beta), label=r'$k=%s, loc=%s, \theta = %s$'
         % (fit_alpha, fit_loc, fit_beta))
plt.hist(x)


Out[9]:
(array([ 2.,  1.,  2.,  0.,  0.,  0.,  0.,  0.,  0.,  1.]),
 array([  1. ,   2.4,   3.8,   5.2,   6.6,   8. ,   9.4,  10.8,  12.2,
         13.6,  15. ]),
 <a list of 10 Patch objects>)

In [14]:
x = np.concatenate((np.random.randint(1,5, 5), np.random.randint(6,15, 5), np.random.randint(16,50, 2)))

In [18]:
x = np.concatenate((np.random.randint(1,5, 5), np.random.randint(6,15, 5), np.random.randint(16,50, 2)))
print "Dist 1", x, x.mean(), np.median(x)
plt.hist(x, alpha=0.5, facecolor=sns.xkcd_rgb["blue"], label="Dist 1")
x = np.concatenate((np.random.randint(6,15, 1), np.random.randint(16,50, 3)))
plt.hist(x, alpha=0.5, facecolor=sns.xkcd_rgb["red"], label="Dist 2")
print "Dist 2", x, x.mean(), np.median(x)
plt.legend()


Dist 1 [ 2  1  3  2  2 14 11  6 10  8 21 45] 10.4166666667 7.0
Dist 2 [ 9 36 46 20] 27.75 28.0
Out[18]:
<matplotlib.legend.Legend at 0x20156f60>

In [ ]: