时间序列之间的相关性

时间:2019-06-20 09:03:14

标签: machine-learning time-series data-science

我有一个数据集,其中一个过程被描述为一个由〜2000点和1500维构成的时间序列。

我想量化每个维度与通过另一种方法测得的另一个时间序列的关联程度。

执行此操作的适当方法是什么(最终在python中完成)?我听说Pearson不适用于此任务,至少在没有数据准备的情况下。您对此有何看法?

非常感谢!

1 个答案:

答案 0 :(得分:1)

数据科学中的一个普遍的好规则是首先尝试简单的事情。只有当简单的事情失败时,您才应该转向更复杂的事情。考虑到这一点,这是您如何计算每个维度与其他一些时间序列之间的皮尔逊相关性。这里的关键功能是pearsonr

import numpy as np
from scipy.stats import pearsonr

# Generate a random dataset using 2000 points and 1500 dimensions
n_times = 2000
n_dimensions = 1500
data = np.random.rand(n_times, n_dimensions)

# Generate another time series, also using 2000 points
other_time_series = np.random.rand(n_times)

# Compute correlation between each dimension and the other time series
correlations = np.zeros(n_dimensions)
for dimension in range(n_dimensions):
    # The Pearson correlation function gives us both the correlation
    # coefficient (r) and a p-value (p). Here, we only use the coefficient.
    r, p = pearsonr(data[:, dimension], other_time_series)
    correlations[dimension] = r

# Now we have, for each dimension, the Pearson correlation with the other time
# series!
len(correlations)

# Print the first 5 correlation coefficients
print(correlations[:5])

如果Pearson相关性不适用于您,则可以尝试将pearsonr函数换成其他函数,例如:

  • spearmanr Spearman等级相关系数。
  • kendalltau Kendall的tau,一种序数数据的相关度量。