スピアマンの順位相関係数(Spearman’s rho)#
順序尺度 × 順序尺度 あるいは 任意の変数を順位に置き換えて測る相関(順位ベースのピアソン相関)
線形性や正規生の仮定を必要としない
2つの変数\(X, Y\)の\(n\)個の観測値の各ペア\((x_i, y_i)\)の順位の差\(d_i\)を求め
\[
\rho=1-\frac{6 \sum d_i^2}{n\left(n^2-1\right)}
\]
x = [1, 1, 2, 2, 3, 3]
y = [0, 0, 0, 1, 1, 1]
from scipy.stats import spearmanr
print(f"{spearmanr(x, y).statistic=:.3f}")
from ordinalcorr import polychoric
print(f"{polychoric(x, y)=:.3f}")
spearmanr(x, y).statistic=0.816
polychoric(x, y)=0.999
x = np.linspace(-10, 10, 15)
y = 1 / (1 + np.exp(-x))
plt.scatter(x, y)
from scipy.stats import spearmanr, pearsonr
from ordinalcorr import polychoric
print(f"{pearsonr(x, y).statistic=:.3f}")
print(f"{spearmanr(x, y).statistic=:.3f}")
print(f"{polychoric(x, y)=:.3f}")
pearsonr(x, y).statistic=0.933
spearmanr(x, y).statistic=1.000
polychoric(x, y)=1.000