# 機率分佈

## Binomial distribution



In [1]:

# 請把程式碼填在此



## Poisson distribution



In [2]:

# 請把程式碼填在此



## Gaussian (Normal) distribution



In [3]:

# 請把程式碼填在此



## Gamma distribution



In [4]:

import matplotlib.pyplot as plt
import scipy.special as sps
import numpy as np
import pandas as pd
from scipy.stats import gamma

# 創建 list 來裝數據
gamma_shape_scale = []
gamma_mean = []
gamma_variance = []

def gammahist(shape, scale):
# 產生 gamma distribution 隨機數據並排序
x = np.sort(np.random.gamma(shape, scale, 5000))

# 繪製直方圖
plt.hist(x, bins=40, normed=True)

# 將 shape, scale, mean, variance 附加至 list 裡面
gamma_shape_scale.append("(%s, %s)" % (shape, scale))
gamma_mean.append(round(np.mean(x), 3))
gamma_variance.append(round(np.var(x), 3))

# 產生一組從 0 到 25(包含) 切 999等分 (共1000個) 的數據
x = np.linspace(0, 25, 1000)
# 將產生的數據 x 帶入 gamma distribution 公式
y = x ** (shape - 1) * (np.exp(-x / scale) / (sps.gamma(shape) * scale ** shape))

# 將 x, y 繪製出來
plt.plot(x, y, linewidth=1, label=r"k=%s, $\theta$=%s" % (shape, scale))




In [5]:

# 設定不同 shape, scale 參數
para = [(0.5, 1.0), (1.0, 2.0), (2.0, 2.0), (3.0, 2.0), (4.0, 2.0), (5.0, 1.0), (7.5, 1.0), (9.0, 0.5)]

# 繪製 histogram (只畫其中兩組)
for i in [(2.0, 2.0), (7.5, 1.0)]:
shape, scale = i
gammahist(shape, scale)
plt.legend()
plt.show()

# 繪製不同 shape, scale 參數下的 gamma distribution
for i in para:
shape, scale = i

# -----以下為 gamma distribution 圖的設定-----
# 在圖中放入公式
plt.text(3, 0.35, r'$p\left ( x \right )=x^{k-1}\frac{e^{-x/\theta }}{\theta ^{k}\Gamma \left ( k \right )}$', size=20)
# 設定 y 軸 limit
plt.ylim(0, 0.5)
# 開啟 x, y 軸小格
plt.minorticks_on()
# 開啟圖例
plt.legend()
# show 出圖
plt.show()




C:\ProgramData\Anaconda2\lib\site-packages\ipykernel\__main__.py:30: RuntimeWarning: divide by zero encountered in power




In [6]:

# 創建一個表格放入 shape, scale, mean, variance
df = pd.DataFrame({"(k, θ)": gamma_shape_scale, "Mean": gamma_mean, "Var": gamma_variance})
# show 出表格
df




Out[6]:

(k, θ)
Mean
Var

0
(2.0, 2.0)
3.998
7.824

1
(7.5, 1.0)
7.555
7.537



## Chi-squared distribution



In [1]:

%matplotlib inline
import numpy as np
from scipy.stats import chi2
from matplotlib import pyplot as plt
import random
import numpy as np
import scipy.stats as stats




In [2]:

#k_values = [1, 3, 5, 7, 9]
k_values = [1, 2, 3, 4, 5] #各種K值得chisqure分布
linestyles = ['-', '--', ':', '-.','']
linecolors = ['r','b','g','k','c']
mu = 0
x = np.linspace(-1, 20, 10000)




In [3]:

fig, ax = plt.subplots(figsize=(5, 3.75))

#for k, ls in zip(k_values, linestyles):
#for k, ls in zip(len(k_values), len(linecolors)):
for k in range(len(k_values)):
dist = chi2(k_values[k], mu)

plt.plot(x, dist.pdf(x), linestyles[k] ,c=linecolors[k],
label=r'$k={}$'.format(k_values[k]))
plt.xlim(0, 10)
plt.ylim(0, 0.5)

plt.xlabel('$Q$')
plt.ylabel(r'$p(Q|k)$')
plt.title(r'$\chi^2\ \mathrm{Distribution}$')

plt.minorticks_on()

plt.legend()

plt.show()







In [4]:

#亂數數據與chisqure distribution
norm = stats.norm(0, 1)

x1 = norm.rvs(size=100000)**2
x2 = norm.rvs(size=100000)**2
x3 = norm.rvs(size=100000)**2

f = x1 + x2 + x3

plt.hist(f, 60, normed=True)

x = np.arange(0, 30, .05)
plt.plot(x, stats.chi2.pdf(x, df=3), color='r', lw=2)
plt.show()






## Beta distribution



In [7]:

# 請把程式碼填在此



## Exponential distribution



In [8]:

# 請把程式碼填在此



## Uniform distribution



In [9]:

# 請把程式碼填在此