本文簡要介紹 python 語言中 scipy.stats.nbinom
的用法。
用法:
scipy.stats.nbinom = <scipy.stats._discrete_distns.nbinom_gen object>#
負二項式離散隨機變量。
作為
rv_discrete
類的實例,nbinom
對象從它繼承了一組通用方法(完整列表見下文),並用特定於此特定發行版的詳細信息來完成它們。注意:
負二項分布說明了 i.i.d 的序列。重複伯努利試驗,直到發生預定義的非隨機成功次數。
nbinom
的失敗次數的概率質量函數為:對於 ,
nbinom
將 和 作為形狀參數,其中 是成功次數, 是單次成功的概率, 是單次失敗的概率。負二項分布的另一個常見參數化是根據平均失敗次數 來實現 成功。平均值 與成功概率有關,因為
成功數量 也可以根據 “dispersion”, “heterogeneity” 或 “aggregation” 參數 來指定,它將平均值 與方差 相關,例如 。無論 使用的約定如何,
上麵的概率質量函數以“standardized” 形式定義。要轉移分布,請使用
loc
參數。具體來說,nbinom.pmf(k, n, p, loc)
等同於nbinom.pmf(k - loc, n, p)
。例子:
>>> import numpy as np >>> from scipy.stats import nbinom >>> import matplotlib.pyplot as plt >>> fig, ax = plt.subplots(1, 1)
計算前四個時刻:
>>> n, p = 5, 0.5 >>> mean, var, skew, kurt = nbinom.stats(n, p, moments='mvsk')
顯示概率質量函數(
pmf
):>>> x = np.arange(nbinom.ppf(0.01, n, p), ... nbinom.ppf(0.99, n, p)) >>> ax.plot(x, nbinom.pmf(x, n, p), 'bo', ms=8, label='nbinom pmf') >>> ax.vlines(x, 0, nbinom.pmf(x, n, p), colors='b', lw=5, alpha=0.5)
或者,可以調用分布對象(作為函數)來固定形狀和位置。這將返回一個 “frozen” RV 對象,其中包含固定的給定參數。
凍結分布並顯示凍結的
pmf
:>>> rv = nbinom(n, p) >>> ax.vlines(x, 0, rv.pmf(x), colors='k', linestyles='-', lw=1, ... label='frozen pmf') >>> ax.legend(loc='best', frameon=False) >>> plt.show()
檢查
cdf
和ppf
的準確性:>>> prob = nbinom.cdf(x, n, p) >>> np.allclose(x, nbinom.ppf(prob, n, p)) True
生成隨機數:
>>> r = nbinom.rvs(n, p, size=1000)
相關用法
- Python SciPy stats.norminvgauss用法及代碼示例
- Python SciPy stats.normaltest用法及代碼示例
- Python SciPy stats.nchypergeom_wallenius用法及代碼示例
- Python SciPy stats.nakagami用法及代碼示例
- Python SciPy stats.nct用法及代碼示例
- Python SciPy stats.ncf用法及代碼示例
- Python SciPy stats.ncx2用法及代碼示例
- Python SciPy stats.nchypergeom_fisher用法及代碼示例
- Python SciPy stats.norm用法及代碼示例
- Python SciPy stats.nhypergeom用法及代碼示例
- Python SciPy stats.anderson用法及代碼示例
- Python SciPy stats.iqr用法及代碼示例
- Python SciPy stats.genpareto用法及代碼示例
- Python SciPy stats.skewnorm用法及代碼示例
- Python SciPy stats.cosine用法及代碼示例
- Python SciPy stats.directional_stats用法及代碼示例
- Python SciPy stats.invwishart用法及代碼示例
- Python SciPy stats.bartlett用法及代碼示例
- Python SciPy stats.levy_stable用法及代碼示例
- Python SciPy stats.page_trend_test用法及代碼示例
- Python SciPy stats.itemfreq用法及代碼示例
- Python SciPy stats.exponpow用法及代碼示例
- Python SciPy stats.gumbel_l用法及代碼示例
- Python SciPy stats.chisquare用法及代碼示例
- Python SciPy stats.semicircular用法及代碼示例
注:本文由純淨天空篩選整理自scipy.org大神的英文原創作品 scipy.stats.nbinom。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。