# -*- coding: utf-8 -*-

import numpy as np
from scipy import stats
import matplotlib.pyplot as plt

# 二项分布
n = 10
p = 0.3
k = np.arange(0, 21)
binomial = stats.binom.pmf(k, n, p)

plt.plot(k, binomial, 'o-')
plt.title('Binomial: n=%i, p=%.2f' %(n, p), fontsize = 15)
plt.xlabel('Number of successes')
plt.ylabel('Probability of successes', fontsize = 15)


# 泊松分布
rate = 2
n = np.arange(0, 10)
y = stats.poisson.pmf(n, rate)
data = stats.poisson.rvs(mu = 2, loc = 0, size = 1000)

plt.figure()
plt.hist(data, bins = 9, normed = True)
plt.xlim(0, 10)
plt.xlabel("Number of accidents")
plt.title("Simulating Poisson Random Variables")
plt.show()


# 正态分布
mu = 0
sigma = 1
x = np.arange(-5, 5, 0.1)
y = stats.norm.pdf(x, 0, 1)

plt.plot(x, y)
plt.title("Normal: $\mu$=%.1f, $\sigma^2$=%.1f" %(mu, sigma))
plt.xlabel("x")
plt.ylabel("Probability density")
plt.show()


# Beta分布
a = 0.5
b = 0.5
x = np.arange(0.01, 1, 0.01)
y = stats.beta.pdf(x, a, b)

plt.plot(x, y)
plt.title("Beta: a=%.1f, b=%.1f" %(a, b))
plt.xlabel("x")
plt.ylabel("Probability density")
plt.show()


# 指数分布
lambd = 0.5
x = np.arange(0, 15, 0.1)
y = lambd * np.exp(-lambd * x)

plt.plot(x, y)
plt.title("Exponential: $\lambda$=%.2f" %lambd)
plt.xlabel("x")
plt.ylabel("Probability density")
plt.show()

data = stats.expon.rvs(scale = 2, size = 1000)
plt.figure()
plt.hist(data, bins = 20, normed = True)
plt.xlim(0, 15)
plt.title("Simulating Exponential Random Variables")
plt.show()


