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

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

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

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

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

递归图

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

Recurrence Plots 的基本原理是测量时间序列中各点之间的相似性。如果两个时间点之间的距离小于某个给定的阈值,就会在 Recurrence Plot 中绘制一个点,表示这两个时间点之间存在重复性。这些点在二维平面上组成了一种图像。Mtd28资讯网——每日最新资讯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。得到的矩阵可以看作是一幅图像。Mtd28资讯网——每日最新资讯28at.com

白噪声

接下来我们将可视化白噪声。首先,我们需要创建一系列模拟的白噪声:Mtd28资讯网——每日最新资讯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()

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

递归图为这种白噪声提供了有趣的可视化效果。对于任何一种白噪声,图看起来都是一样的:Mtd28资讯网——每日最新资讯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()

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

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

随机游走

接下来让我们看看随机游走(Random Walk)是什么样子的:Mtd28资讯网——每日最新资讯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()

Mtd28资讯网——每日最新资讯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()

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

SARIMA

SARIMA(4,1,4)(1,0,0,12)的模拟数据Mtd28资讯网——每日最新资讯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()

Mtd28资讯网——每日最新资讯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()

Mtd28资讯网——每日最新资讯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()

Mtd28资讯网——每日最新资讯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()

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

标准普尔500指数

作为最后一个例子,让我们看看从2013年10月28日至2023年10月27日的标准普尔500指数真实数据:Mtd28资讯网——每日最新资讯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');

Mtd28资讯网——每日最新资讯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()

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

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

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

总结

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

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

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

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

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

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

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

标签:
  • 热门焦点
  • 一加Ace2 Pro真机揭晓 钛空灰配色质感拉满

    终于,在经过了几波预热之后,一加Ace2 Pro的外观真机图在网上出现了。还是博主数码闲聊站曝光的,这次的外观设计还是延续了一加11的方案,只是细节上有了调整,例如新加入了钛空灰
  • Mate60手机壳曝光 致敬自己的经典设计

    8月3日消息,今天下午博主数码闲聊站带来了华为Mate60的第三方手机壳图,可以让我们在真机发布之前看看这款华为全新旗舰的大致轮廓。从曝光的图片看,Mate 60背后摄像头面积依然
  • .NET 程序的 GDI 句柄泄露的再反思

    一、背景1. 讲故事上个月我写过一篇 如何洞察 C# 程序的 GDI 句柄泄露 文章,当时用的是 GDIView + WinDbg 把问题搞定,前者用来定位泄露资源,后者用来定位泄露代码,后面有朋友反
  • 从零到英雄:高并发与性能优化的神奇之旅

    作者 | 波哥审校 | 重楼作为公司的架构师或者程序员,你是否曾经为公司的系统在面对高并发和性能瓶颈时感到手足无措或者焦头烂额呢?笔者在出道那会为此是吃尽了苦头的,不过也得
  • Python异步IO编程的进程/线程通信实现

    这篇文章再讲3种方式,同时讲4中进程间通信的方式一、 Python 中线程间通信的实现方式共享变量共享变量是多个线程可以共同访问的变量。在Python中,可以使用threading模块中的L
  • 每天一道面试题-CPU伪共享

    前言:了不起:又到了每天一到面试题的时候了!学弟,最近学习的怎么样啊 了不起学弟:最近学习的还不错,每天都在学习,每天都在进步! 了不起:那你最近学习的什么呢? 了不起学弟:最近在学习C
  • 三星推出Galaxy Tab S9系列平板电脑以及Galaxy Watch6系列智能手表

    2023年7月26日,三星电子正式发布了Galaxy Z Flip5与Galaxy Z Fold5。除此之外,Galaxy Tab S9系列平板电脑以及三星Galaxy Watch6系列智能手表也同期
  • 三星Galaxy Z Fold/Flip 5国行售价曝光 :最低7499元/12999元起

    据官方此前宣布,三星将于7月26日也就是明天在韩国首尔举办Unpacked活动,届时将带来带来包括Galaxy Buds 3、Galaxy Watch 6、Galaxy Tab S9、Galaxy
  • 首发天玑9200+ iQOO Neo8系列发布首销售价2299元起

    2023年5月23日晚,iQOO Neo8系列正式发布。其中,Neo系列首款Pro之作——iQOO Neo8 Pro强悍登场,限时售价3099元起;价位段最强性能手机iQOO Neo8同期上市
Top