当前位置:首页 > 科技  > 软件

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

来源: 责编: 时间:2023-11-10 17:07:16 335观看
导读在本文中,我将展示如何使用递归图 Recurrence Plots 来描述不同类型的时间序列。我们将查看具有500个数据点的各种模拟时间序列。我们可以通过可视化时间序列的递归图并将其与其他已知的不同时间序列的递归图进行比较,

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

H6128资讯网——每日最新资讯28at.com

递归图

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

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

import numpy as np import matplotlib.pyplot as plt  def recurrence_plot(data, threshold=0.1):    """    Generate a recurrence plot from a time series.     :param data: Time series data    :param threshold: Threshold to determine recurrence    :return: Recurrence plot    """    # Calculate the distance matrix    N = len(data)    distance_matrix = np.zeros((N, N))    for i in range(N):        for j in range(N):            distance_matrix[i, j] = np.abs(data[i] - data[j])     # Create the recurrence plot    recurrence_plot = np.where(distance_matrix <= threshold, 1, 0)     return recurrence_plot

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

白噪声

接下来我们将可视化白噪声。首先,我们需要创建一系列模拟的白噪声:H6128资讯网——每日最新资讯28at.com

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

图片H6128资讯网——每日最新资讯28at.com

递归图为这种白噪声提供了有趣的可视化效果。对于任何一种白噪声,图看起来都是一样的:H6128资讯网——每日最新资讯28at.com

# Generate and plot the recurrence plot recurrence = recurrence_plot(white_noise, threshold=0.1)  plt.figure(figsize=(8, 8)) plt.imshow(recurrence, cmap='binary', origin='lower') plt.title('Recurrence Plot') plt.xlabel('Time') plt.ylabel('Time') plt.colorbar(label='Recurrence') plt.show()

H6128资讯网——每日最新资讯28at.com

可以直观地看到一个嘈杂的过程。可以看到图中对角线总是黑色的。H6128资讯网——每日最新资讯28at.com

随机游走

接下来让我们看看随机游走(Random Walk)是什么样子的:H6128资讯网——每日最新资讯28at.com

# Generate 500 data points of a random walk steps = np.random.choice([-1, 1], size=500) # Generate random steps: -1 or 1 random_walk = np.cumsum(steps) # Cumulative sum to generate the random walk  # Plot the random walk time series plt.figure(figsize=(10, 6)) plt.plot(random_walk, label='Random Walk') plt.title('Random Walk Time Series') plt.xlabel('Time') plt.ylabel('Value') plt.legend() plt.grid(True) plt.show()

H6128资讯网——每日最新资讯28at.com

# Generate and plot the recurrence plot recurrence = recurrence_plot(random_walk, threshold=0.1)  plt.figure(figsize=(8, 8)) plt.imshow(recurrence, cmap='binary', origin='lower') plt.title('Recurrence Plot') plt.xlabel('Time') plt.ylabel('Time') plt.colorbar(label='Recurrence') plt.show()

H6128资讯网——每日最新资讯28at.com

SARIMA

SARIMA(4,1,4)(1,0,0,12)的模拟数据H6128资讯网——每日最新资讯28at.com

from statsmodels.tsa.statespace.sarimax import SARIMAX  # Define SARIMA parameters p, d, q = 4, 1, 4 # Non-seasonal order P, D, Q, s = 1, 0, 0, 12 # Seasonal order  # Simulate data model = SARIMAX(np.random.randn(100), order=(p, d, q), seasonal_order=(P, D, Q, s), trend='ct') fit = model.fit(disp=False) # Fit the model to random data to get parameters simulated_data = fit.simulate(nsimulatinotallow=500)  # Plot the simulated time series plt.figure(figsize=(10, 6)) plt.plot(simulated_data, label=f'SARIMA({p},{d},{q})({P},{D},{Q},{s})') plt.title('Simulated Time Series from SARIMA Model') plt.xlabel('Time') plt.ylabel('Value') plt.legend() plt.grid(True) plt.show()

H6128资讯网——每日最新资讯28at.com

recurrence = recurrence_plot(simulated_data, threshold=0.1)  plt.figure(figsize=(8, 8)) plt.imshow(recurrence, cmap='binary', origin='lower') plt.title('Recurrence Plot') plt.xlabel('Time') plt.ylabel('Time') plt.colorbar(label='Recurrence') plt.show()

H6128资讯网——每日最新资讯28at.com

混沌的数据

def logistic_map(x, r):    """Logistic map function."""    return r * x * (1 - x)  # Initialize parameters N = 500         # Number of data points r = 3.9         # Parameter r, set to a value that causes chaotic behavior x0 = np.random.rand() # Initial value  # Generate chaotic time series data chaotic_data = [x0] for _ in range(1, N):    x_next = logistic_map(chaotic_data[-1], r)    chaotic_data.append(x_next)  # Plot the chaotic time series plt.figure(figsize=(10, 6)) plt.plot(chaotic_data, label=f'Logistic Map (r={r})') plt.title('Chaotic Time Series') plt.xlabel('Time') plt.ylabel('Value') plt.legend() plt.grid(True) plt.show()

H6128资讯网——每日最新资讯28at.com

recurrence = recurrence_plot(chaotic_data, threshold=0.1)  plt.figure(figsize=(8, 8)) plt.imshow(recurrence, cmap='binary', origin='lower') plt.title('Recurrence Plot') plt.xlabel('Time') plt.ylabel('Time') plt.colorbar(label='Recurrence') plt.show()

H6128资讯网——每日最新资讯28at.com

标准普尔500指数

作为最后一个例子,让我们看看从2013年10月28日至2023年10月27日的标准普尔500指数真实数据:H6128资讯网——每日最新资讯28at.com

import pandas as pd  df = pd.read_csv('standard_and_poors_500_idx.csv', parse_dates=True) df['Date'] = pd.to_datetime(df['Date']) df.set_index('Date', inplace = True) df.drop(columns = ['Open', 'High', 'Low'], inplace = True)  df.plot() plt.title('S&P 500 Index - 10/28/2013 to 10/27/2023') plt.ylabel('S&P 500 Index') plt.xlabel('Date');

H6128资讯网——每日最新资讯28at.com

recurrence = recurrence_plot(df['Close/Last'], threshold=10)  plt.figure(figsize=(8, 8)) plt.imshow(recurrence, cmap='binary', origin='lower') plt.title('Recurrence Plot') plt.xlabel('Time') plt.ylabel('Time') plt.colorbar(label='Recurrence') plt.show()

H6128资讯网——每日最新资讯28at.com

选择合适的相似性阈值是 递归图分析的一个关键步骤。较小的阈值会导致更多的重复模式,而较大的阈值会导致更少的重复模式。阈值的选择通常需要根据数据的特性和分析目标进行调整。H6128资讯网——每日最新资讯28at.com

这里我们不得不调整阈值,最终确得到的结果为10,这样可以获得更大的对比度。上面的递归图看起来很像随机游走递归图和无规则的混沌数据的混合体。H6128资讯网——每日最新资讯28at.com

总结

在本文中,我们介绍了递归图以及如何使用Python创建递归图。递归图给了我们一种直观表征时间序列图的方法。递归图是一种强大的工具,用于揭示时间序列中的结构和模式,特别适用于那些具有周期性、重复性或复杂结构的数据。通过可视化和特征提取,研究人员可以更好地理解时间序列数据并进行进一步的分析。H6128资讯网——每日最新资讯28at.com

从递归图中可以提取各种特征,以用于进一步的分析。这些特征可以包括重复点的分布、Lempel-Ziv复杂度、最长对角线长度等。H6128资讯网——每日最新资讯28at.com

递归图在多个领域中得到了广泛应用,包括时间序列分析、振动分析、地震学、生态学、金融分析、生物医学等。它可用于检测周期性、异常事件、相位同步等。H6128资讯网——每日最新资讯28at.com

本文链接:http://www.28at.com/showinfo-26-20045-0.html使用递归图 recurrence plot 表征时间序列

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。邮件:2376512515@qq.com

上一篇: 系统架构七个非功能性需求

下一篇: 线性回归,核技巧和线性核

标签:
  • 热门焦点
  • 红魔电竞平板评测:大屏幕硬实力

    前言:三年的疫情因为要上网课的原因激活了平板市场,如今网课的时代已经过去,大家的生活都恢复到了正轨,这也就意味着,真正考验平板电脑生存的环境来了。也就是面对着这种残酷的
  • 7月安卓手机性价比榜:努比亚+红魔两款新机入榜

    7月登场的新机有努比亚Z50S Pro和红魔8S Pro,除了三星之外目前唯二的两款搭载超频版骁龙8Gen2处理器的产品,而且努比亚和红魔也一贯有着不错的性价比,所以在本次的性价比榜单
  • 之家push系统迭代之路

    前言在这个信息爆炸的互联网时代,能够及时准确获取信息是当今社会要解决的关键问题之一。随着之家用户体量和内容规模的不断增大,传统的靠"主动拉"获取信息的方式已不能满足用
  • 使用LLM插件从命令行访问Llama 2

    最近的一个大新闻是Meta AI推出了新的开源授权的大型语言模型Llama 2。这是一项非常重要的进展:Llama 2可免费用于研究和商业用途。(几小时前,swyy发现它已从LLaMA 2更名为Lla
  • .NET 程序的 GDI 句柄泄露的再反思

    一、背景1. 讲故事上个月我写过一篇 如何洞察 C# 程序的 GDI 句柄泄露 文章,当时用的是 GDIView + WinDbg 把问题搞定,前者用来定位泄露资源,后者用来定位泄露代码,后面有朋友反
  • 由于成本持续增加,笔记本产品价格预计将明显上涨

    根据知情人士透露,由于材料、物流等成本持续增加,笔记本产品价格预计将在2021年下半年有明显上涨。进入6月下旬以来,全球半导体芯片缺货情况加剧,显卡、处理器
  • 上海举办人工智能大会活动,建设人工智能新高地

    人工智能大会在上海浦江两岸隆重拉开帷幕,人工智能新技术、新产品、新应用、新理念集中亮相。8月30日晚,作为大会的特色活动之一的上海人工智能发展盛典人工
  • 亲历马斯克血洗Twitter,硅谷的苦日子在后头

    文/刘哲铭  编辑/李薇  马斯克再次挥下裁员大刀。  美国时间11月14日,Twitter约4400名外包员工遭解雇,此次被解雇的员工的主要工作为内容审核等。此前,T
  • 荣耀Magic4 至臻版 首创智慧隐私通话 强劲影音系统

    2022年第一季度临近尾声,在该季度内,许多品牌陆续发布自己的最新产品,让大家从全新的角度来了解当今的手机技术。手机是电子设备中,更新迭代十分迅速的一款产品,基
Top