如何尽可能快地计算互相关系数

2024-07-05 15:00:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我要计算很多对序列的互相关。事实上,我只关心前100个滞后

这是我的代码和一些编造的数据:

import numpy as np
import matplotlib.pyplot as plt
import statsmodels.tsa.stattools as stattools

def create(n):
    x = np.zeros(n)
    for i in range(1, n):
        if np.random.rand() < 0.9:
            if np.random.rand() < 0.5:
                x[i] = x[i-1] + 1
        else:
            x[i] = np.random.randint(0,100)
    return x
x = create(4000)
y = create(4000)
plt.plot(stattools.ccf(x, y)[:100])

如果我这样做了

%timeit stattools.ccf(x, y)[:100]

我明白了

100 loops, best of 3: 10.7 ms per loop

有没有可能再快一点


Tags: 数据代码importnumpyifascreatenp