如何在 Python 中获得 R 的 ccf?

How do I get R's ccf in Python?

我不是统计学家,我只是将一些 R 代码翻译成 Python。

回复:

a = 1:1000
b = 1000:1
ccf(a, b, max.lag=100, plot=FALSE)

Autocorrelations of series ‘X’, by lag

   -26    -25    -24    -23    -22    -21    -20    -19    -18    -17    -16
-0.922 -0.925 -0.928 -0.931 -0.934 -0.937 -0.940 -0.943 -0.946 -0.949 -0.952
   -15    -14    -13    -12    -11    -10     -9     -8     -7     -6     -5
-0.955 -0.958 -0.961 -0.964 -0.967 -0.970 -0.973 -0.976 -0.979 -0.982 -0.985
    -4     -3     -2     -1      0      1      2      3      4      5      6
-0.988 -0.991 -0.994 -0.997 -1.000 -0.997 -0.994 -0.991 -0.988 -0.985 -0.982
     7      8      9     10     11     12     13     14     15     16     17
-0.979 -0.976 -0.973 -0.970 -0.967 -0.964 -0.961 -0.958 -0.955 -0.952 -0.949
    18     19     20     21     22     23     24     25     26
-0.946 -0.943 -0.940 -0.937 -0.934 -0.931 -0.928 -0.925 -0.922

Python:

import scipy.signal as ss
import numpy as np

x = np.array(range(1, 1001))
y = np.array(range(1000, 0, -1))

ss.correlate(x, y)
# array([      1,       4,      10, ..., 2994001, 1998000, 1000000])
ss.correlate(x - np.mean(x), y - np.mean(y), method='direct')/(np.std(x)*np.std(y)*len(x))
# array([0.00299401, 0.00597602, 0.00894607, ..., 0.00894607, 0.00597602,
       0.00299401])

None 这些答案都接近 R 结果。如何在 Python 中获得相同的结果?

这是一个功能相同的函数:

import scipy.signal as ss

def ccf(x, y, lag_max = 100):

    result = ss.correlate(y - np.mean(y), x - np.mean(x), method='direct') / (np.std(y) * np.std(x) * len(y))
    length = (len(result) - 1) // 2
    lo = length - lag_max
    hi = length + (lag_max + 1)

    return result[lo:hi]