scipy.stats.
log#
- scipy.stats.log(X, /)[source]#
非負隨機變數的自然對數
- 參數:
- XContinuousDistribution
具有正支撐的隨機變數 \(X\)。
- 返回:
- YContinuousDistribution
隨機變數 \(Y = \exp(X)\)。
範例
假設我們有一個伽瑪分佈的隨機變數 \(X\)
>>> import numpy as np >>> from scipy import stats >>> Gamma = stats.make_distribution(stats.gamma) >>> X = Gamma(a=1.0)
我們希望有一個 exp-伽瑪分佈的隨機變數 \(Y\),這是一個自然指數為 \(X\) 的隨機變數。如果 \(X\) 是 \(Y\) 的自然指數,那麼我們必須取 \(Y\) 為 \(X\) 的自然對數。
>>> Y = stats.log(X)
為了展示
X
代表Y
的指數,我們繪製了Y
觀測值指數的標準化直方圖,對照X
的底層 PDF。>>> import matplotlib.pyplot as plt >>> rng = np.random.default_rng() >>> y = Y.sample(shape=10000, rng=rng) >>> ax = plt.gca() >>> ax.hist(np.exp(y), bins=50, density=True) >>> X.plot(ax=ax) >>> plt.legend(('PDF of `X`', 'histogram of `exp(y)`')) >>> plt.show()