如何用Python检查时间序列数据是否平稳?

时间序列数据通常以其时间性质为特征。这种时间性质为数据增加了趋势或季节性,使其与时间序列分析和预测兼容。如果时间序列数据不随时间变化或没有时间结构,则称其为静态数据。因此,检查数据是否平稳是非常必要的。在时间序列预测中,如果数据是平稳的,我们就无法从数据中获得有价值的见解。

静态数据的示例图:

在这里插入图片描述

平稳性的类型

当涉及到识别数据是否是平稳的时,这意味着识别数据中平稳性的细粒度概念。在时间序列数据中观察到的平稳性类型包括:

  • 趋势平稳 :不显示趋势的时间序列。
  • 季节性平稳(Seasonal Stationary):不显示季节性变化的时间序列。
  • 严格平稳:观测值的联合分布不随时间变化。

实现方法及步骤

下面的步骤将让用户容易地理解检查给定时间序列数据是否平稳的方法。

步骤1:绘制时间序列数据

# import python pandas library
import pandas as pd# import python matplotlib library for plotting
import matplotlib.pyplot as plt# read the dataset using pandas read_csv() 
# function
data = pd.read_csv("daily-total-female-births-IN.csv",header=0, index_col=0)# use simple line plot to see the distribution 
# of the data
plt.plot(data)

输出
在这里插入图片描述
步骤2:评估描述性统计量

这通常是通过将数据分成两个或多个分区并计算每组的均值和方差来完成的。如果这些一阶矩在这些分区之间是一致的,那么我们可以假设数据是平稳的。让我们使用1949 - 1960年之间的航空公司乘客计数数据集。

# import python pandas library
import pandas as pd# import python matplotlib library for
# plotting
import matplotlib.pyplot as plt# read the dataset using pandas read_csv() 
# function
data = pd.read_csv("AirPassengers.csv",header=0, index_col=0)# print the first 6 rows of data
print(data.head(10))# use simple line plot to understand the 
# data distribution
plt.plot(data)

在这里插入图片描述
现在,让我们将这些数据划分为不同的组,计算不同组的均值和方差,并检查一致性。

# import the python pandas library
import pandas as pd# use pandas read_csv() function to read the dataset.
data = pd.read_csv("AirPassengers.csv", header=0, index_col=0)# extracting only the air passengers count from
# the dataset using values function
values = data.values# getting the count to split the dataset into 3
parts = int(len(values)/3)# splitting the data into three parts
part_1, part_2, part_3 = values[0:parts], values[parts:(parts*2)], values[(parts*2):(parts*3)]# calculating the mean of the separated three 
# parts of data individually.
mean_1, mean_2, mean_3 = part_1.mean(), part_2.mean(), part_3.mean()# calculating the variance of the separated 
# three parts of data individually.
var_1, var_2, var_3 = part_1.var(), part_2.var(), part_3.var()# printing the mean of three groups
print('mean1=%f, mean2=%f, mean2=%f' % (mean_1, mean_2, mean_3))# printing the variance of three groups
print('variance1=%f, variance2=%f, variance2=%f' % (var_1, var_2, var_3))

输出
在这里插入图片描述
输出清楚地表明,三组的平均值和方差彼此差异很大,说明数据是非平稳的。例如,如果平均值mean_1 = 150,mean_2 = 160,mean_3 = 155和variance_1 = 33,variance_2 = 35,variance_3 = 37,那么我们可以得出结论,数据是平稳的。有时这种方法可能会对某些分布失败,如对数范数分布。

让我们尝试与上面相同的示例,但使用NumPy的log()函数获取乘客计数的日志并检查结果。

# import python pandas library
import pandas as pd# import python matplotlib library for plotting
import matplotlib.pyplot as plt# import python numpy library
import numpy as np# read the dataset using pandas read_csv()
# function
data = pd.read_csv("AirPassengers.csv", header=0, index_col=0)# extracting only the air passengers count 
# from the dataset using values function
values = log(data.values)# printing the first 15 passenger count values
print(values[0:15])# using simple line plot to understand the 
# data distribution
plt.plot(values)

输出
在这里插入图片描述
输出表示有一些趋势,但不像前面的情况那样非常陡峭,现在让我们计算分区均值和方差。

# getting the count to split the dataset
# into 3 parts
parts = int(len(values)/3)# splitting the data into three parts.
part_1, part_2, part_3 = values[0:parts], values[parts:(parts*2)], values[(parts*2):(parts*3)]# calculating the mean of the separated three 
# parts of data individually.
mean_1, mean_2, mean_3 = part_1.mean(), part_2.mean(), part_3.mean()# calculating the variance of the separated three 
# parts of data individually.
var_1, var_2, var_3 = part_1.var(), part_2.var(), part_3.var()# printing the mean of three groups
print('mean1=%f, mean2=%f, mean2=%f' % (mean_1, mean_2, mean_3))# printing the variance of three groups
print('variance1=%f, variance2=%f, variance2=%f' % (var_1, var_2, var_3))

输出
在这里插入图片描述
理想情况下,我们会期望均值和方差非常不同,但它们是相同的,在这种情况下,这种方法可能会非常失败。为了避免这种情况,我们有另一个统计测试,下面讨论。

步骤3:增强的Dickey-Fuller检验

这是一个统计测试,专门用于测试单变量时间序列数据是否平稳。这个测试是基于一个假设,可以告诉我们它可以被接受的概率程度。它通常被归类为单位根检验之一,它决定了一个单变量时间序列数据遵循趋势的强度。我们来定义零假设和替代假设,

  • Ho(假设):时间序列数据是非平稳的
  • H1(替代假设):时间序列数据是平稳的

假设α = 0.05,表示(95%置信度)。如果p > 0.05不能拒绝零假设,则用p值解释检验结果,否则如果p <= 0.05则拒绝零假设。现在,让我们使用相同的航空乘客数据集,并使用stats model包提供的adfuller()统计函数对其进行测试,以检查数据是否稳定。

# import python pandas package
import pandas as pd# import the adfuller function from statsmodel 
# package to perform ADF test
from statsmodels.tsa.stattools import adfuller# read the dataset using pandas read_csv() function
data = pd.read_csv("AirPassengers.csv", header=0, index_col=0)# extracting only the passengers count using values function
values = data.values# passing the extracted passengers count to adfuller function.
# result of adfuller function is stored in a res variable
res = adfuller(values)# Printing the statistical result of the adfuller test
print('Augmneted Dickey_fuller Statistic: %f' % res[0])
print('p-value: %f' % res[1])# printing the critical values at different alpha levels.
print('critical values at different levels:')
for k, v in res[4].items():print('\t%s: %.3f' % (k, v))

输出
在这里插入图片描述
根据我们的假设,ADF统计量远远大于不同水平的临界值,并且p值也大于0.05,这意味着我们无法在90%,95%和99%的置信度下拒绝零假设,这意味着时间序列数据是强非平稳的。

现在,让我们尝试对log normed值运行ADF测试,并交叉检查我们的结果。

# import python pandas package
import pandas as pd# import the adfuller function from statsmodel
# package to perform ADF test
from statsmodels.tsa.stattools import adfuller# import python numpy package
import numpy as np# read the dataset using pandas read_csv() function
data = pd.read_csv("AirPassengers.csv", header=0, index_col=0)# extracting only the passengers count using 
# values function and applying log transform on it.
values = log(data.values)# passing the extracted passengers count to adfuller function.
# result of adfuller function is stored in a res variable
res = adfuller(values)# Printing the statistical result of the adfuller test
print('Augmneted Dickey_fuller Statistic: %f' % res[0])
print('p-value: %f' % res[1])# printing the critical values at different alpha levels.
print('critical values at different levels:')
for k, v in res[4].items():print('\t%s: %.3f' % (k, v))

输出
在这里插入图片描述
正如你所看到的,ADF测试再次显示ADF统计量在不同水平上远远大于临界值,并且p值也远远大于0.05,这意味着我们无法在90%,95%和99%的置信度下拒绝零假设,这意味着时间序列数据是强非平稳的。

因此,ADF单位根检验是检查时间序列数据是否平稳的鲁棒性检验。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/712168.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

用HTML5的<canvas>元素实现刮刮乐游戏

用HTML5的<canvas>元素实现刮刮乐 用HTML5的<canvas>元素实现刮刮乐&#xff0c;要求&#xff1a;将上面的“图层”的图像可用鼠标刮去&#xff0c;露出下面的“图层”的图像。 示例从简单到复杂。 简单示例 准备两张图像&#xff0c;我这里上面的图像top_imag…

node express实现Excel文档转json文件

有些场景我们需要将Excel文档中的内容抽取出来生成别的文件&#xff0c;作为一个前端&#xff0c;服务框架最应该熟悉的就是node了&#xff0c;以下是基于多语言转换实现代码&#xff0c;看明白原理自己改一改就能用了 1.安装node环境 2.创建一个文件夹&#xff0c;文件夹中创建…

7、Redis-事务、持久化、内存淘汰机制和过期key处理

目录 一、事务 二、持久化 三、内存淘汰机制 四、过期key处理 一、事务 Redis的事务本质上就是一个批量执行命令的操作。分为三个步骤&#xff1a; 开始事务&#xff1a;multi命令入队&#xff1a;正常输入命令即可执行事务&#xff08;依次执行命令&#xff09;&#xf…

掌握java模板方法模式,提升代码复用与扩展的艺术

Java 模板方法模式是一种行为型设计模式&#xff0c;它定义了一个算法的骨架&#xff0c;并将一些步骤延迟到子类中实现。模板方法模式使得子类可以在不改变算法结构的情况下重定义算法中的某些步骤。 使用场景 算法骨架固定&#xff1a;如果一个算法的基本结构已经固定&#…

跨专业考研难度大吗?听听过来人的真实经历

在考研的大潮中&#xff0c;跨专业考研成为了一个不可忽视的现象。许多考生因为对原专业失去兴趣、追求职业梦想或其他原因&#xff0c;选择了跨专业报考。那么&#xff0c;跨专业考研的难度究竟有多大呢&#xff1f;今天&#xff0c;我们就来聊聊这个话题&#xff0c;听听过来…

不是我吹,这8道HashMap面试题让你面试时对答如流

前言 又到了一年一度的金三银四面试季&#xff0c;我们拿着自己的面试秘籍去面试&#xff0c;但是面试官的问题五花八门&#xff0c;让我们摸不清他们的套路。今天我就总结了面试时必问的hashmap面试题&#xff0c;无论面试官怎么问&#xff0c;我们都对答如流。 另外本人整理了…

java小记(2)

IS-A&#xff1a;类的父子继承关系。 default&#xff1a;关键字&#xff0c;与Java中的public&#xff0c;private等关键字一样&#xff0c;都属于修饰符关键字&#xff0c;可以用来修饰属性、方法以及类&#xff0c;但是default一般用来修饰接口中的方法。 接口与抽象类的区…

代码随想录算法训练营第二十四天 | 77. 组合

回溯算法理论基础 https://programmercarl.com/%E5%9B%9E%E6%BA%AF%E7%AE%97%E6%B3%95%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.html 回溯法也可以叫做回溯搜索法&#xff0c;它是一种搜索的方式。 回溯是递归的副产品&#xff0c;只要有递归就会有回溯。 回溯法并不是什么高效的…

马斯克正式起诉OpenAI和奥特曼!

就在刚刚&#xff0c;马斯克闹出来一件大事——正式起诉OpenAI和Sam Altman&#xff0c;并要求OpenAI 恢复开源GPT-4等模型&#xff01; 众所周知&#xff0c;马斯克这两年一只在推特上指责 OpenAI是CloseAI(不开源)&#xff0c;但都只是停留在口头上。 而这次马斯克动了真格。…

nginx if 指令

目录 nginx if 指令直接判断变量判断是否等于字符串判断变量是否匹配正则表达式文件及目录判断示例1&#xff1a;判断index.html是否存在示例2&#xff1a;判断URL中是否存在某个参数Parameter示例3&#xff1a;判断URI中是否为某个特定路径示例4&#xff1a;开放白名单内的功能…

从0开始python学习-53.python中flask创建简单接口

目录 1. 创建一个简单的请求,没有写方法时默认为get 2. 创建一个get请求 3. 创建一个post请求&#xff0c;默认可以使用params和表单传参 4. 带有参数的post请求 1. 创建一个简单的请求,没有写方法时默认为get from flask import Flask, request# 初始化一个flask的对象 ap…

RK3566 linux iperf网络测试

一、开发环境 系统:buildroot&#xff1b; 在Linux目标板和Windows PC上运行iperf进行测试&#xff1b; 二、调试 1、查询目标板上的iperf 使用终端助手连接目标板&#xff0c;然后输入命令查询iperf的版本&#xff1a; rootrk3566-buildroot:~# iperf -v iperf version …

图数据库 之 Neo4j - 应用场景3 - 知识图谱(8)

背景 知识图谱的复杂性:知识图谱通常包含大量的实体、关系和属性,以及它们之间的复杂关联。传统的关系型数据库在处理这种复杂性时可能面临性能和灵活性的挑战。 图数据库的优势:图数据库是一种专门用于存储和处理图结构数据的数据库。它们使用节点和边来表示实体和关系,并…

USB - Battery Charing

Getting to the bottom of USB Battery Charging (了解 USB 电池充电的真相) 如今&#xff0c;几乎所有带电池的产品都被期望支持 BC1.2 USB 充电标准。 Today, almost every product with a battery is expected to support the BC1.2 standard for USB charging. 这对消费者来…

详解字符串函数<string.h>(上)

1. strlen函数的使用和模拟实现 size_t strlen(const char* str); 1.1 函数功能以及用法 字符串长度 strlen函数的功能是计算字符串的长度。在使用时&#xff0c;要求用户传入需要计算长度的字符串的起始位置&#xff0c;并返回字符串的长度。 #include <stdio.h> #…

基于SSM医院电子病历管理系统的设计与实现(源代码+数据库脚本+万字文档+PPT)

系统介绍 医院电子病历管理系统主要是借助计算机&#xff0c;通过对医院电子病历管理系统所需的信息管理&#xff0c;增加用户的选择&#xff0c;同时也方便对广大用户信息的及时查询、修改以及对用户信息的及时了解。医院电子病历管理系统 对用户带来了更多的便利&#xff0c…

Python GUI自动化定位代码参考

一、pyautogui原始逻辑 import pyautogui # 获取指定图片在屏幕上的位置 image_path path/to/image.png target_position pyautogui.locateCenterOnScreen(image_path) if target_position is not None: # 获取偏移量 offset_x 10 offset_y 10 # 计算实际点…

一文读懂ZKFair PFP-CyberArmy的参与价值与潜力

3月2日&#xff0c;ZKFair PFP-CyberArmy 将在 Element 上正式开始Public Sale。

文件基础和文件fd

文章目录 预备知识C语言的文件接口系统调用文件fd 正文开始前给大家推荐个网站&#xff0c;前些天发现了一个巨牛的 人工智能学习网站&#xff0c; 通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家。 点击跳转到网站。 预备知识 我们平时说文件就是说文件里…

1_Springboot(一)入门

Springboot&#xff08;一&#xff09;——入门 本章重点&#xff1a; 1.什么是Springboot; 2.使用Springboot搭建web项目&#xff1b; 一、Springboot 1.Springboot产生的背景 Servlet->Struts2->Spring->SpringMVC&#xff0c;技术发展过程中&#xff0c;对使…