from scipy import stats
p = 0.6
bernoulli_dist = stats.bernoulli(p)
p_tail = bernoulli_dist.pmf(0)
p_head = bernoulli_dist.pmf(1)
print('Prob of tail:', p_tail)
print('Prob of head:', p_head)
Prob of tail: 0.4 Prob of head: 0.6
trials = bernoulli_dist.rvs(10) # 베르누이 분포를 따르는 10개의 샘플
trials
array([1, 1, 0, 1, 1, 1, 1, 0, 1, 1])
n = 10 # 주사위를 10번 던진다.
p = 1/6 # 3의 눈이 나올 확률 1/6
binom_dist = stats.binom(n, p)
import numpy as np
# 3의 눈이 1번, 2번, 3번, ..., 10번 나올 확률
trials = binom_dist.pmf(np.arange(10))
trials
array([1.61505583e-01, 3.23011166e-01, 2.90710049e-01, 1.55045360e-01, 5.42658759e-02, 1.30238102e-02, 2.17063503e-03, 2.48072575e-04, 1.86054431e-05, 8.26908584e-07])
# Float formatting 변경
[round(x, 5) for x in trials]
[0.16151, 0.32301, 0.29071, 0.15505, 0.05427, 0.01302, 0.00217, 0.00025, 2e-05, 0.0]
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(font_scale=1.5)
binom_dist1 = stats.binom(20, 0.5) # n = 20
binom_dist2 = stats.binom(20, 0.7) # n = 20
binor_dist3 = stats.binom(40, 0.5) # n = 40
k = np.arange(40)
plt.plot(k, binom_dist1.pmf(k), 'o-b')
plt.plot(k, binom_dist2.pmf(k), 'd-r')
plt.plot(k, binor_dist3.pmf(k), 's-g')
plt.title('Binomial distribition')
plt.legend(['p=0.5 and n=20', 'p=0.7 and n=20', 'p=0.5 and n=40'])
plt.xlabel('X')
plt.ylabel('P(X)')
Text(0,0.5,'P(X)')
poisson_dist1 = stats.poisson(5)
poisson_dist2 = stats.poisson(10)
poisson_dist3 = stats.poisson(25)
n = np.arange(50)
plt.plot(n, poisson_dist1.pmf(n), 'o-b')
plt.plot(n, poisson_dist2.pmf(n), 'd-r')
plt.plot(n, poisson_dist3.pmf(n), 's-g')
plt.title('Poisson distribution - PMF')
plt.legend(['$\lambda$=5', '$\lambda$=30', '$\lambda$=50'])
plt.xlabel('X')
plt.ylabel('P(X)')
Text(0,0.5,'P(X)')
x = np.arange(-10, 10, 0.1)
mu = -2
sigma = 0.7
norm_dist1 = stats.norm(mu, sigma) # 정규 분포
norm_dist2 = stats.norm() #표준 정규 분포
y1 = norm_dist1.pdf(x)
y2 = norm_dist2.pdf(x)
plt.plot(x, y1)
plt.plot(x, y2)
plt.title('Normal Distribution - PDF')
plt.legend(['$\mu$=-2, $\sigma$=0.7', '$\mu$=0, $\sigma$=1'])
plt.xlabel('X')
plt.ylabel('pdf(X)')
Text(0,0.5,'pdf(X)')
x = np.arange(0, 3, 0.1)
exp_dist1 = stats.expon(scale=0.5)
exp_dist2 = stats.expon(scale=1)
exp_dist3 = stats.expon(scale=1.5)
y1 = exp_dist1.pdf(x)
y2 = exp_dist2.pdf(x)
y3 = exp_dist3.pdf(x)
plt.plot(x, y1)
plt.plot(x, y2)
plt.plot(x, y3)
plt.title('Exponential Distribution - PDF')
plt.legend(['$\lambda$=0.5', '$\lambda$=1.0', '$\lambda$=1.5'])
plt.xlabel('X')
plt.ylabel('pdf(X)')
Text(0,0.5,'pdf(X)')
Probability density function for lognormal : $$f(x,s)=\frac{1}{sx\sqrt{2\pi}}\exp(-\frac{\log^2(x)}{2s^2})$$ $s$는 shape parameter이다.
x = np.arange(0, 3, 0.1)
lognorm_dist1 = stats.lognorm(1)
lognorm_dist2 = stats.lognorm(2)
lognorm_dist3 = stats.lognorm(3)
y1 = lognorm_dist1.pdf(x)
y2 = lognorm_dist2.pdf(x)
y3 = lognorm_dist3.pdf(x)
plt.plot(x, y1)
plt.plot(x, y2)
plt.plot(x, y3)
plt.title('Lognormal Distribution - PDF')
plt.legend(['$s$=1', '$s$=2', '$s$=3'])
plt.xlabel('X')
plt.ylabel('pdf(X)')
Text(0,0.5,'pdf(X)')
import matplotlib as mpl
mpl.rcParams.update({'font.size': 22})
x = np.linspace(0, 1, 1000) # Beta 분포는 0 ~ 1 사이의 값만 가진다.
plt.figure(figsize=(10, 10))
plt.subplot(221)
plt.fill(x, stats.beta(1.0001, 1.0001).pdf(x))
plt.ylim(0, 6)
plt.title('a = 1, b = 1')
plt.subplot(222)
plt.fill(x, stats.beta(4, 2).pdf(x))
plt.ylim(0, 6)
plt.title('a = 4, b = 2, mode={}'.format((4-1)/(4+2-2)))
plt.subplot(223)
plt.fill(x, stats.beta(8, 4).pdf(x))
plt.ylim(0, 6)
plt.title('a = 8, b = 4, mode={}'.format((8-1)/(8+4-2)))
plt.subplot(224)
plt.fill(x, stats.beta(30, 12).pdf(x))
plt.ylim(0, 6)
plt.title('a = 30, b = 12, mode={}'.format((30-1)/(30+12-2)))
Text(0.5,1,'a = 30, b = 12, mode=0.725')
x = np.linspace(0, 10, 100) # Gamma 분포는 0 ~ 무한대 사이의 값을 가진다.
plt.figure(figsize=(10, 10))
plt.subplot(221)
plt.plot(x, stats.gamma(9).pdf(x))
plt.ylim(0, 0.4)
plt.title('a = 9, b = 1, mode={}'.format((9-1)/(1)))
plt.subplot(222)
plt.plot(x, stats.gamma(6).pdf(x))
plt.ylim(0, 0.4)
plt.title('a = 6, b = 1, mode={}'.format((6-1)/(1)))
plt.subplot(223)
plt.plot(x, stats.gamma(3).pdf(x))
plt.ylim(0, 0.4)
plt.title('a = 3, b = 1, mode={}'.format((3-1)/(1)))
plt.subplot(224)
plt.plot(x, stats.gamma(2).pdf(x))
plt.ylim(0, 0.4)
plt.title('a = 2, b = 1, mode={}'.format((2-1)/(1)))
Text(0.5,1,'a = 2, b = 1, mode=1.0')