numpy.random.Generator.logseries#

方法

random.Generator.logseries(p, size=None)#

從對數級數分佈中抽取樣本。

樣本是從具有指定形狀參數(0 <= p < 1)的對數級數分佈中抽取的。

參數:
p浮點數或浮點數的類陣列

分佈的形狀參數。必須在 [0, 1) 範圍內。

size整數或整數元組,選填

輸出形狀。如果給定的形狀是,例如 (m, n, k),則會抽取 m * n * k 個樣本。如果 size 為 None(預設),則如果 p 是純量,則會傳回單一值。否則,會抽取 np.array(p).size 個樣本。

回傳值:
outndarray 或純量

從參數化對數級數分佈中抽取的樣本。

另請參閱

scipy.stats.logser

機率密度函數、分佈或累積密度函數等等。

註解

對數級數分佈的機率質量函數為

\[P(k) = \frac{-p^k}{k \ln(1-p)},\]

其中 p = 機率。

對數級數分佈經常被用於表示物種豐富度和發生率,最早由 Fisher、Corbet 和 Williams 於 1943 年提出 [2]。它也可用於建模汽車中看到的乘員數量 [3]。

參考文獻

[1]

Buzas, Martin A.; Culver, Stephen J., Understanding regional species diversity through the log series distribution of occurrences: BIODIVERSITY RESEARCH Diversity & Distributions, Volume 5, Number 5, September 1999 , pp. 187-195(9).

[2]

Fisher, R.A,, A.S. Corbet, and C.B. Williams. 1943. The relation between the number of species and the number of individuals in a random sample of an animal population. Journal of Animal Ecology, 12:42-58.

[3]

D. J. Hand, F. Daly, D. Lunn, E. Ostrowski, A Handbook of Small Data Sets, CRC Press, 1994.

[4]

Wikipedia, “Logarithmic distribution”, https://en.wikipedia.org/wiki/Logarithmic_distribution

範例

從分佈中抽取樣本

>>> a = .6
>>> rng = np.random.default_rng()
>>> s = rng.logseries(a, 10000)
>>> import matplotlib.pyplot as plt
>>> bins = np.arange(-.5, max(s) + .5 )
>>> count, bins, _ = plt.hist(s, bins=bins, label='Sample count')

# 繪製分佈圖

>>> def logseries(k, p):
...     return -p**k/(k*np.log(1-p))
>>> centres = np.arange(1, max(s) + 1)
>>> plt.plot(centres, logseries(centres, a) * s.size, 'r', label='logseries PMF')
>>> plt.legend()
>>> plt.show()
../../../_images/numpy-random-Generator-logseries-1.png