1

我有这两个时间序列,我想测试它们是否来自同一个分布。所以我应用了scipy.stats.ks_2samp()测试。但是测试返回的 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)
4

1 回答 1

2

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 检验在这里不合适,因为它假设两个随机样本,而您所拥有的是时间序列,而时间显然是一个重要因素。

于 2021-04-12T22:52:55.890 回答