为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 Python 中获得 R 的 ccf?

如何在 Python 中获得 R 的 ccf?

Qyouu 2021-09-28 13:26:34
我不是统计学家,我只是将一些 R 代码翻译成 Python。回复:a = 1:1000b = 1000:1ccf(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.922Python:import scipy.signal as ssimport numpy as npx = 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])这些答案都没有接近 R 结果。如何在 Python 中获得相同的结果?
查看完整描述

1 回答

?
慕斯709654

TA贡献1840条经验 获得超5个赞

这是一个执行相同操作的函数:


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]


查看完整回答
反对 回复 2021-09-28
  • 1 回答
  • 0 关注
  • 143 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信