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]
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
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
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]:
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()
Out[18]:
In [ ]: