kendalltau#
- scipy.stats.kendalltau(x, y, *, nan_policy='propagate', method='auto', variant='b', alternative='two-sided')[source]#
計算 Kendall's tau,一種用於序數資料的相關性量測。
Kendall's tau 是一種量測兩個排序之間對應程度的方法。接近 1 的值表示高度一致,而接近 -1 的值表示高度不一致。此函式實作了 Kendall's tau 的兩種變體:tau-b(預設)和 tau-c(也稱為 Stuart's tau-c)。它們僅在如何正規化以使其落在 -1 到 1 的範圍內有所不同;假設檢定(它們的 p 值)是相同的。Kendall 最初的 tau-a 沒有單獨實作,因為在沒有 ties 的情況下,tau-b 和 tau-c 都會簡化為 tau-a。
- 參數:
- x, yarray_like
排序的陣列,形狀相同。如果陣列不是 1 維的,它們將被展平為 1 維。
- nan_policy{‘propagate’, ‘raise’, ‘omit’}, optional
定義當輸入包含 nan 時如何處理。以下選項可用(預設為 ‘propagate’)
‘propagate’:傳回 nan
‘raise’:拋出錯誤
‘omit’:執行計算時忽略 nan 值
- method{‘auto’, ‘asymptotic’, ‘exact’}, optional
定義用於計算 p 值的[5]方法。以下選項可用(預設為 ‘auto’)
‘auto’:根據速度和準確性之間的權衡選擇適當的方法
‘asymptotic’:使用適用於大樣本的常態近似
‘exact’:計算精確的 p 值,但僅在沒有 ties 的情況下才能使用。隨著樣本數量的增加,‘exact’ 計算時間可能會增長,並且結果可能會損失一些精度。
- variant{‘b’, ‘c’}, optional
定義傳回 Kendall's tau 的哪個變體。預設為 ‘b’。
- alternative{‘two-sided’, ‘less’, ‘greater’}, optional
定義對立假設。預設為 ‘two-sided’。以下選項可用
‘two-sided’:等級相關性非零
‘less’:等級相關性為負(小於零)
‘greater’:等級相關性為正(大於零)
- 傳回值:
- resSignificanceResult
一個包含屬性的物件
- statisticfloat
tau 統計量。
- pvaluefloat
假設檢定的 p 值,其虛無假設為無關聯性,tau = 0。
- 引發:
- ValueError
如果 nan_policy 為 ‘omit’ 且 variant 不是 ‘b’,或者如果 method 為 ‘exact’ 且 x 和 y 之間存在 ties。
另請參閱
spearmanr
計算 Spearman 等級順序相關係數。
theilslopes
計算一組點 (x, y) 的 Theil-Sen 估計量。
weightedtau
計算 Kendall's tau 的加權版本。
- Kendall's tau 檢定
延伸範例
註解
使用的 Kendall's tau 定義是 [2]
tau_b = (P - Q) / sqrt((P + Q + T) * (P + Q + U)) tau_c = 2 (P - Q) / (n**2 * (m - 1) / m)
其中 P 是和諧對的數量,Q 是不和諧對的數量,T 是僅在 x 中的 ties 數量,而 U 是僅在 y 中的 ties 數量。如果同一個 pair 在 x 和 y 中都發生 tie,則不會將其添加到 T 或 U。n 是樣本總數,而 m 是 x 或 y 中唯一值的數量,以較小者為準。
參考文獻
[1]Maurice G. Kendall, “A New Measure of Rank Correlation”, Biometrika Vol. 30, No. 1/2, pp. 81-93, 1938.
[2]Maurice G. Kendall, “The treatment of ties in ranking problems”, Biometrika Vol. 33, No. 3, pp. 239-251. 1945.
[3]Gottfried E. Noether, “Elements of Nonparametric Statistics”, John Wiley & Sons, 1967.
[4]Peter M. Fenwick, “A new data structure for cumulative frequency tables”, Software: Practice and Experience, Vol. 24, No. 3, pp. 327-336, 1994.
[5]Maurice G. Kendall, “Rank Correlation Methods” (4th Edition), Charles Griffin & Co., 1970.
範例
>>> from scipy import stats >>> x1 = [12, 2, 1, 12, 2] >>> x2 = [1, 4, 7, 1, 0] >>> res = stats.kendalltau(x1, x2) >>> res.statistic -0.47140452079103173 >>> res.pvalue 0.2827454599327748
如需更詳細的範例,請參閱Kendall's tau 檢定。