當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python SciPy special.fdtri用法及代碼示例

本文簡要介紹 python 語言中 scipy.special.fdtri 的用法。

用法:

scipy.special.fdtri(dfn, dfd, p, out=None) = <ufunc 'fdtri'>#

F-distribution 的 p-th 分位數。

該函數是 F-distribution CDF 的逆函數,fdtr,返回x這樣fdtr(dfn, dfd, x) = p.

參數

dfn array_like

第一個參數(正浮點數)。

dfd array_like

第二個參數(正浮點數)。

p array_like

累積概率,在 [0, 1] 中。

out ndarray,可選

函數值的可選輸出數組

返回

x 標量或 ndarray

p 對應的分位數。

注意

使用與反正則化 beta 函數 的關係進行計算。讓 然後,

如果p是這樣的\(x < 0.5\) ,使用以下關係來提高穩定性:讓\(z' = I^{-1}_{1 - p}(d_n/2, d_d/2).\) 然後,

Cephes [1] 例程的包裝器 fdtri

F 分布也可用作 scipy.stats.f 。與 scipy.stats.f ppf 方法相比,直接調用 fdtri 可以提高性能(請參見下麵的最後一個示例)。

參考

[1]

Cephes 數學函數庫,http://www.netlib.org/cephes/

例子

fdtri表示 F 分布 CDF 的倒數,可表示為fdtr。在這裏,我們計算 CDFdf1=1,df2=2x=3.fdtri然後返回3給定相同的值df1,df2和計算的 CDF 值。

>>> import numpy as np
>>> from scipy.special import fdtri, fdtr
>>> df1, df2 = 1, 2
>>> x = 3
>>> cdf_value =  fdtr(df1, df2, x)
>>> fdtri(df1, df2, cdf_value)
3.000000000000006

通過為 x 提供 NumPy 數組來計算多個點的函數。

>>> x = np.array([0.1, 0.4, 0.7])
>>> fdtri(1, 2, x)
array([0.02020202, 0.38095238, 1.92156863])

繪製多個參數集的函數。

>>> import matplotlib.pyplot as plt
>>> dfn_parameters = [50, 10, 1, 50]
>>> dfd_parameters = [0.5, 1, 1, 5]
>>> linestyles = ['solid', 'dashed', 'dotted', 'dashdot']
>>> parameters_list = list(zip(dfn_parameters, dfd_parameters,
...                            linestyles))
>>> x = np.linspace(0, 1, 1000)
>>> fig, ax = plt.subplots()
>>> for parameter_set in parameters_list:
...     dfn, dfd, style = parameter_set
...     fdtri_vals = fdtri(dfn, dfd, x)
...     ax.plot(x, fdtri_vals, label=rf"$d_n={dfn},\, d_d={dfd}$",
...             ls=style)
>>> ax.legend()
>>> ax.set_xlabel("$x$")
>>> title = "F distribution inverse cumulative distribution function"
>>> ax.set_title(title)
>>> ax.set_ylim(0, 30)
>>> plt.show()
scipy-special-fdtri-1_00_00.png

F 分布也可用作 scipy.stats.f 。直接使用 fdtri 比調用 scipy.stats.f ppf 方法要快得多,特別是對於小型數組或單個值。為了獲得相同的結果,必須使用以下參數化:stats.f(dfn, dfd).ppf(x)=fdtri(dfn, dfd, x)

>>> from scipy.stats import f
>>> dfn, dfd = 1, 2
>>> x = 0.7
>>> fdtri_res = fdtri(dfn, dfd, x)  # this will often be faster than below
>>> f_dist_res = f(dfn, dfd).ppf(x)
>>> f_dist_res == fdtri_res  # test that results are equal
True

相關用法


注:本文由純淨天空篩選整理自scipy.org大神的英文原創作品 scipy.special.fdtri。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。