numpy.random.Generator.chisquare#
方法
- random.Generator.chisquare(df, size=None)#
從卡方分佈中抽取樣本。
當 df 個獨立的隨機變數(每個都具有標準常態分佈,平均值為 0,變異數為 1)被平方並加總時,結果的分佈是卡方分佈(請參閱「註解」)。此分佈常在假設檢定中使用。
- 參數:
- dffloat 或 float 的類陣列 (array_like)
自由度,必須 > 0。
- sizeint 或 int 元組,選用
輸出形狀。如果給定的形狀是,例如,
(m, n, k)
,則會抽取m * n * k
個樣本。如果 size 為None
(預設),則如果df
是純量,則會傳回單一值。否則,會抽取np.array(df).size
個樣本。
- 傳回值:
- outndarray 或 純量
從參數化的卡方分佈中抽取的樣本。
- 引發:
- ValueError
當 df <= 0 或給定不適當的
size
(例如size=-1
) 時。
註解
透過將 df 個獨立、標準常態分佈的隨機變數的平方加總所獲得的變數
\[Q = \sum_{i=1}^{\mathtt{df}} X^2_i\]是卡方分佈,表示為
\[Q \sim \chi^2_k.\]卡方分佈的機率密度函數為
\[p(x) = \frac{(1/2)^{k/2}}{\Gamma(k/2)} x^{k/2 - 1} e^{-x/2},\]其中 \(\Gamma\) 是 gamma 函數,
\[\Gamma(x) = \int_0^{-\infty} t^{x - 1} e^{-t} dt.\]參考文獻
[1]NIST “工程統計手冊” https://www.itl.nist.gov/div898/handbook/eda/section3/eda3666.htm
範例
>>> rng = np.random.default_rng() >>> rng.chisquare(2,4) array([ 1.89920014, 9.00867716, 3.13710533, 5.62318272]) # random
具有 20 個自由度的卡方隨機變數的分佈如下所示
>>> import matplotlib.pyplot as plt >>> import scipy.stats as stats >>> s = rng.chisquare(20, 10000) >>> count, bins, _ = plt.hist(s, 30, density=True) >>> x = np.linspace(0, 60, 1000) >>> plt.plot(x, stats.chi2.pdf(x, df=20)) >>> plt.xlim([0, 60]) >>> plt.show()