使用递归图 recurrence plot 表征时间序列

发布时间 2023-11-09 15:03:52作者: deephub

在本文中,我将展示如何使用递归图 Recurrence Plots 来描述不同类型的时间序列。我们将查看具有500个数据点的各种模拟时间序列。我们可以通过可视化时间序列的递归图并将其与其他已知的不同时间序列的递归图进行比较,从而直观地表征时间序列。

递归图

Recurrence Plots(RP)是一种用于可视化和分析时间序列或动态系统的方法。它将时间序列转化为图形化的表示形式,以便分析时间序列中的重复模式和结构。Recurrence Plots 是非常有用的,尤其是在时间序列数据中存在周期性、重复事件或关联结构时。

Recurrence Plots 的基本原理是测量时间序列中各点之间的相似性。如果两个时间点之间的距离小于某个给定的阈值,就会在 Recurrence Plot 中绘制一个点,表示这两个时间点之间存在重复性。这些点在二维平面上组成了一种图像。

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. def recurrence_plot(data, threshold=0.1):
  4. """
  5. Generate a recurrence plot from a time series.
  6. :param data: Time series data
  7. :param threshold: Threshold to determine recurrence
  8. :return: Recurrence plot
  9. """
  10. # Calculate the distance matrix
  11. N = len(data)
  12. distance_matrix = np.zeros((N, N))
  13. for i in range(N):
  14. for j in range(N):
  15. distance_matrix[i, j] = np.abs(data[i] - data[j])
  16. # Create the recurrence plot
  17. recurrence_plot = np.where(distance_matrix <= threshold, 1, 0)
  18. return recurrence_plot

上面的代码创建了一个二进制距离矩阵,如果时间序列i和j的值相差在0.1以内(阈值),则它们的值为1,否则为0。得到的矩阵可以看作是一幅图像。

白噪声

接下来我们将可视化白噪声。首先,我们需要创建一系列模拟的白噪声:

  1. # Set a seed for reproducibility
  2. np.random.seed(0)
  3. # Generate 500 data points of white noise
  4. white_noise = np.random.normal(size=500)
  5. # Plot the white noise time series
  6. plt.figure(figsize=(10, 6))
  7. plt.plot(white_noise, label='White Noise')
  8. plt.title('White Noise Time Series')
  9. plt.xlabel('Time')
  10. plt.ylabel('Value')
  11. plt.legend()
  12. plt.grid(True)
  13. plt.show()

递归图为这种白噪声提供了有趣的可视化效果。对于任何一种白噪声,图看起来都是一样的:

 

https://avoid.overfit.cn/post/6b385fd6e8d64f2cb62d9caafd05389b