且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用 numpy 估计周期性的自相关

更新时间:2021-09-12 23:46:46

我会使用 mode='same' 而不是 mode='full' 因为使用 mode='full' 我们可以获得极端变化的协方差,其中只有 1 个数组元素与自身重叠,其余为零.这些不会很有趣.使用 mode='same' 至少一半的移位数组与原始数组重叠.

I would use mode='same' instead of mode='full' because with mode='full' we get covariances for extreme shifts, where just 1 array element overlaps self, the rest being zeros. Those are not going to be interesting. With mode='same' at least half of the shifted array overlaps the original one.

此外,要获得真正的相关系数 (r),您需要除以重叠的大小,而不是原始 x 的大小.(在我的代码中,这些是 np.arange(n-1, n//2, -1)).那么每个输出将在 -1 和 1 之间.

Also, to have the true correlation coefficient (r) you need to divide by the size of the overlap, not by the size of the original x. (in my code these are np.arange(n-1, n//2, -1)). Then each of the outputs will be between -1 and 1.

一目了然Durbin–Watson statistic,类似于2(1-r) 表明人们认为其值低于 1 是自相关的重要指示,对应于 r > 0.5.所以这就是我在下面使用的.有关自相关重要性的统计合理处理,请参阅统计文献;一个起点是为您的时间序列建立一个模型.

A glance at Durbin–Watson statistic, which is similar to 2(1-r), suggests that people consider its values below 1 to be a significant indication of autocorrelation, which corresponds to r > 0.5. So this is what I use below. For a statistically sound treatment of the significance of autocorrelation refer to statistics literature; a starting point would be to have a model for your time series.

def autocorr(x):
    n = x.size
    norm = (x - np.mean(x))
    result = np.correlate(norm, norm, mode='same')
    acorr = result[n//2 + 1:] / (x.var() * np.arange(n-1, n//2, -1))
    lag = np.abs(acorr).argmax() + 1
    r = acorr[lag-1]        
    if np.abs(r) > 0.5:
      print('Appears to be autocorrelated with r = {}, lag = {}'. format(r, lag))
    else: 
      print('Appears to be not autocorrelated')
    return r, lag

您的两个玩具示例的输出:

Output for your two toy examples:

似乎不是自相关
似乎与 r = 1.0, lag = 4 自相关

Appears to be not autocorrelated
Appears to be autocorrelated with r = 1.0, lag = 4