numpy.random.Generator.logistic#

方法

random.Generator.logistic(loc=0.0, scale=1.0, size=None)#

從 Logistic 分佈中抽取樣本。

樣本是從具有指定參數 loc(位置或平均值,也稱為中位數)和 scale(>0)的 Logistic 分佈中抽取的。

參數:
locfloat 或 float 的類陣列,可選

分佈的參數。預設值為 0。

scalefloat 或 float 的類陣列,可選

分佈的參數。必須是非負數。預設值為 1。

sizeint 或 int 元組,可選

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

返回:
outndarray 或 純量

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

參見

scipy.stats.logistic

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

註解

Logistic 分佈的機率密度為

\[P(x) = \frac{e^{-(x-\mu)/s}}{s(1+e^{-(x-\mu)/s})^2},\]

其中 \(\mu\) = 位置,\(s\) = 尺度。

Logistic 分佈用於極值問題中,它可以作為 Gumbel 分佈的混合,也用於流行病學,以及世界西洋棋總會(FIDE),它在 Elo 排名系統中使用,假設每位棋手的表現都是邏輯分佈的隨機變數。

參考文獻

[1]

Reiss, R.-D. and Thomas M. (2001), “Statistical Analysis of Extreme Values, from Insurance, Finance, Hydrology and Other Fields,” Birkhauser Verlag, Basel, pp 132-133.

[2]

Weisstein, Eric W. “Logistic Distribution.” From MathWorld–A Wolfram Web Resource. https://mathworld.wolfram.com/LogisticDistribution.html

[3]

Wikipedia, “Logistic-distribution”, https://en.wikipedia.org/wiki/Logistic_distribution

範例

從分佈中抽取樣本

>>> loc, scale = 10, 1
>>> rng = np.random.default_rng()
>>> s = rng.logistic(loc, scale, 10000)
>>> import matplotlib.pyplot as plt
>>> count, bins, _ = plt.hist(s, bins=50, label='Sampled data')

# 繪製採樣資料與精確分佈的對比圖

>>> def logistic(x, loc, scale):
...     return np.exp((loc-x)/scale)/(scale*(1+np.exp((loc-x)/scale))**2)
>>> logistic_values  = logistic(bins, loc, scale)
>>> bin_spacing = np.mean(np.diff(bins))
>>> plt.plot(bins, logistic_values  * bin_spacing * s.size, label='Logistic PDF')
>>> plt.legend()
>>> plt.show()
../../../_images/numpy-random-Generator-logistic-1.png