为什么在这种情况下 Kolmogorov-Smirnov 检验失败了?

Why does the Kolmogorov-Smirnov test fail in this case?

我有这两个时间序列,我想测试它们是否来自同一个分布。所以我应用了 scipy.stats.ks_2samp() 测试。但是测试 returns 的 p 值为 0.0028,而 describe() 给出了这些统计数据:

count   120.000000  120.000000
mean    0.785867    0.774267
std     0.323941    0.304894
min     0.610000    0.610000
25%     0.619000    0.610000
50%     0.619000    0.619000
75%     0.749000    0.769500
max     1.812000    1.742000

所以当均值和标准差非常相似时,我不明白为什么检验会拒绝原假设。此外,(累积)分布图看起来非常相似。

有人能帮帮我吗?

这是我的数据和测试调用:

from scipy import stats

df = pd.DataFrame(data=[[
    0.62, 0.61, 0.61, 0.619, 0.619, 0.619, 0.62, 0.619, 0.61,
    0.619, 0.62, 0.619, 0.619, 0.62, 0.611, 0.62, 0.62, 0.61,
    0.619, 0.61, 0.619, 0.62, 0.642, 0.67, 0.749, 0.838, 0.862,
    0.804, 0.89, 0.942, 1.012, 1.13, 1.14, 1.191, 1.201, 1.123,
    1.299, 1.359, 1.411, 1.362, 1.352, 1.44,1.451, 1.46, 1.557,
    1.491, 1.622, 1.639, 1.787, 1.812, 1.665, 1.612, 1.253, 0.936,
    0.704, 0.643, 0.62, 0.619, 0.62, 0.61, 0.619, 0.62, 0.619,
    0.62, 0.61, 0.619, 0.61, 0.619, 0.62, 0.619, 0.62, 0.62,
    0.619, 0.62, 0.62, 0.619, 0.62, 0.619, 0.619, 0.62, 0.619,
    0.619, 0.619, 0.619, 0.61, 0.61, 0.619, 0.619, 0.619, 0.62,
    0.619, 0.619, 0.619, 0.619, 0.61, 0.619, 0.619, 0.62, 0.619,
    0.61, 0.619, 0.619, 0.619, 0.619, 0.61, 0.619, 0.619, 0.62,
    0.619, 0.61, 0.619, 0.619, 0.62, 0.619, 0.749, 0.63, 0.62,
    0.61, 0.619, 0.619],
    [0.801, 0.644, 0.62, 0.62, 0.61, 0.61, 
    0.619, 0.62, 0.61, 0.61, 0.61, 0.61, 0.619, 0.619, 0.62,
    0.61, 0.619, 0.61, 0.619, 0.62, 0.62, 0.629, 0.689, 0.759,
    0.849, 0.84, 0.918, 1.019, 0.967, 0.92, 0.976, 1.089, 1.062,
    1.219, 1.202, 1.261, 1.387, 1.422, 1.39, 1.264, 1.281, 1.35,
    1.32, 1.419, 1.568, 1.554, 1.623, 1.592, 1.709, 1.742, 1.535,
    1.123, 0.84, 0.682, 0.63, 0.62, 0.61, 0.61, 0.619, 0.62,
    0.61, 0.61, 0.61, 0.61, 0.619, 0.62, 0.61, 0.619, 0.61,
    0.62, 0.61, 0.62, 0.61, 0.61, 0.619, 0.62, 0.62, 0.61,
    0.61, 0.61, 0.619, 0.62, 0.61, 0.619, 0.62, 0.61, 0.61,
    0.61, 0.61, 0.61, 0.619, 0.62, 0.62, 0.61, 0.61, 0.61,
    0.619, 0.619, 0.619, 0.61, 0.618, 0.61, 0.61, 0.619, 0.61,
    0.61, 0.61, 0.61, 0.619, 0.619, 0.62, 0.61, 0.619, 0.62,
    0.62, 0.61, 0.619, 0.61, 0.61, 0.61]]).T

print(stats.ks_2samp(df.iloc[:, 1], df.iloc[:, 0]).pvalue)

Kolmogorov-Smirnov 测试没有失败。两个系列看似平坦的尾巴,实则有本质的不同。我们可以通过放大尾部(从索引 60 开始)并对每个系列中的值进行排序以便于比较来看到这一点:

import matplotlib.pyplot as plt

plt.plot(df.iloc[60:, 0].sort_values(ignore_index=True))
plt.plot(df.iloc[60:, 1].sort_values(ignore_index=True), color='orange')
plt.ylim([0.605, 0.625]);

我不知道这是数据记录方式的人工制品,还是真实效果。无论如何,请注意 Kolmogorov-Smirnov 测试在这里不合适,因为它假设有两个随机样本,而你拥有的是时间序列,时间显然是一个重要因素。