In [1]:
# 請把程式碼填在此
In [2]:
# 請把程式碼填在此
In [3]:
# 請把程式碼填在此
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)
gammadist(shape, scale)
# 將 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))
def gammadist(shape, scale):
# 產生一組從 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
gammadist(shape, scale)
# -----以下為 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()
In [6]:
# 創建一個表格放入 shape, scale, mean, variance
df = pd.DataFrame({"(k, θ)": gamma_shape_scale, "Mean": gamma_mean, "Var": gamma_variance})
# show 出表格
df
Out[6]:
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))
fig.subplots_adjust(bottom=0.12)
#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()
In [7]:
# 請把程式碼填在此
In [8]:
# 請把程式碼填在此
In [9]:
# 請把程式碼填在此