如何用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://xiahunao.cn/news/2822162.html

如若内容造成侵权/违法违规/事实不符,请联系瞎胡闹网进行投诉反馈,一经查实,立即删除!

相关文章

基于粒子群优化算法的图象聚类识别matlab仿真

目录 1.程序功能描述 2.测试软件版本以及运行结果展示 3.核心程序 4.本算法原理 5.完整程序 1.程序功能描述 基于粒子群优化算法的图象聚类识别。通过PSO优化方法&#xff0c;将数字图片的特征进行聚类&#xff0c;从而识别出数字0~9. 2.测试软件版本以及运行结果展示 M…

智能咖啡厅助手:人形机器人 +融合大模型,行为驱动的智能咖啡厅机器人(机器人大模型与具身智能挑战赛)

智能咖啡厅助手&#xff1a;人形机器人 融合大模型&#xff0c;行为驱动的智能咖啡厅机器人(机器人大模型与具身智能挑战赛) “机器人大模型与具身智能挑战赛”的参赛作品。的目标是结合前沿的大模型技术和具身智能技术&#xff0c;开发能在模拟的咖啡厅场景中承担服务员角色并…

Flutter中的三棵树

Widget Tree&#xff1a; 页面配置信息。 Element Tree&#xff1a; Widget tree的实例化对象&#xff0c;创建出renderObject&#xff0c;并关联到element.renderobject属性上&#xff0c;最后完成RenderObject Tree的创建。 RenderObject Tree&#xff1a;完成布局和图层绘制…

自测-1 打印沙漏

文章预览&#xff1a; 题目算法代码 题目 算法 以前做过这个&#xff0c;那次是c语言写的&#xff0c;一点一点处理一层一层完成&#xff0c;这次我换了一种语言用了另一种思想使用递归去写&#xff0c;还是我们要先求出应该有多少层这个很容易&#xff0c;中间输出部分我们算…

STM32标准库——(14)I2C通信协议、MPU6050简介

1.I2C通信 I2C 通讯协议(Inter&#xff0d;Integrated Circuit)是由Phiilps公司开发的&#xff0c;由于它引脚少&#xff0c;硬件实现简单&#xff0c;可扩展性强&#xff0c; 不需要USART、CAN等通讯协议的外部收发设备&#xff0c;现在被广泛地使用在系统内多个集成电路(IC)间…

睡眠问题大揭秘:中医如何轻松解决?

睡眠问题是现代社会普遍存在的难题&#xff0c;它的影响不仅仅局限于个体的疲惫和神经紧张&#xff0c;更深远的是对整体健康的长期威胁。根据世界卫生组织的数据&#xff0c;全球约有三分之一的人口遭受着不同程度的睡眠问题&#xff0c;其中包括失眠、入睡困难、多梦、易醒等…

flutter旋转动画,Android彻底组件化方案实践方法

Android基础 & 常用 针对Android基础&常用知识&#xff0c;我认为对于初级开发者来说&#xff0c;按照优先级最主要的知识点主要包括&#xff1a;四大组件、布局使用、多线程 & 动画&#xff1b;具体介绍如下&#xff1a; 2. Android进阶 针对Android进阶知识&am…

幸运星数(爷再也不想用pow了)

解法&#xff1a; 暴力 #include <iostream> #include <vector> using namespace std; #define endl \nint main() {ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);int n;long long sum 0, a;cin >> n;for (int i 1; i < n; i) {a 1;for (in…

NVMe开发——PCIe复位

简介 PCIe中有4种复位机制&#xff0c;早期的3种被称为传统复位(Conventional Reset)。传统复位中的前2种又称为基本复位(Fundamental Resets)&#xff0c;分别为冷复位(Cold Reset)&#xff0c;暖复位(Warm Reset)。第3种复位为热复位(Hot Reset)。第4种复位被称为功能级复位…

队列的结构概念和实现

文章目录 一、队列的结构和概念二、队列的实现三、队列的实现函数四、队列的思维导图 一、队列的结构和概念 什么是队列&#xff1f; 队列就是只允许在一端进行插入数据操作&#xff0c;在另一端进行删除数据操作的特殊线性表&#xff0c;队列具有先进先出 如上图所示&#x…

面经 | Java创建线程的三种方式

利用JUC包创建线程的三种方式&#xff1a; 通过继承Thread类创建线程类实现Runnable接口创建线程类通过Callable和Future接口创建线程 继承Thread类创建线程 class Thread1 extends Thread {Overridepublic void run() {System.out.println("启动线程1");} }实现R…

Fastjson2 <== 2.0.26反序列漏洞

根据Y4TACKER师傅在2023-03-20发布了一篇关于Fastjson原生反序列化的文章&#xff0c;文章中引入注目的是利用条件限制条件&#xff0c;不常常关注漏洞预警或者内容的几乎都是未发觉Fastjson2 到Fastjson2 2.0.26版本都有问题&#xff0c;其实如果单独去使用一些关键词去搜索&a…

若依前后端分离版本-自动生成代码

听说若依挺好用的&#xff0c;所以来学习一下。 1.下载项目&#xff0c;配置redis,配置mysql,安装npm&#xff08;版本一定要低于16&#xff09; 2.执行sql脚本数据库相关信息 3.启动后端ruoyi-admin的ruoyiApplication 4启动前端 选择terminal 进入ruoyi-ui&#xff0c;执…

并查集基础,死去的回忆突然攻击我

并查集普及【模板】并查集 - 洛谷 #include<iostream> #include<cstring> #include<queue> #include<vector> #include<algorithm> using namespace std; #define int long long typedef pair<int,int> PII; #define xx first #define y…

Mathtype安装时word启动显示“文件未找到:MathPage.WLL”

背景 由于老板布置的临时工作&#xff0c;需要安装Mathtype&#xff0c;但尝试了3个不同的版本后&#xff08;每次都卸载干净了&#xff09;&#xff0c;均未能成功安装&#xff0c;出现的报错3个版本各不相同&#xff1a; ①解压安装过程中失败&#xff08;这个版本不再尝试…

“智慧代码阁”千聊知识店铺成立了

前两天我在千聊上注册了知识店铺“智慧代码阁” 欢迎大家来购买更加精品的代码 点击这里进入知识店铺 非常感谢大家&#xff01;&#xff01;&#xff01; 欢迎来到“智慧代码阁”——您的专属知识宝库&#xff0c;专注于为代码爱好者和专业人士提供前沿、实用、系统的编程知…

C++数据结构与算法——二叉搜索树的属性

C第二阶段——数据结构和算法&#xff0c;之前学过一点点数据结构&#xff0c;当时是基于Python来学习的&#xff0c;现在基于C查漏补缺&#xff0c;尤其是树的部分。这一部分计划一个月&#xff0c;主要利用代码随想录来学习&#xff0c;刷题使用力扣网站&#xff0c;不定时更…

Linux 系统安装/卸载 Nginx教程

优质博文&#xff1a;IT-BLOG-CN 一、安装Nginx 【1】首先通过Nginx官网确定需要安装的版本&#xff0c;如果Linux联网则直接在Linux服务上使用wget命令将Nginx安装包下载到/usr/local/目录下&#xff1a; [rootxxx local]# wget -c http://nginx.org/download/nginx-1.22.1.…

中小企业“数智未来”行动|ZStack Cloud 荣获“推荐方案”奖

2月29日&#xff0c;以“数智未来 共创数字时代新篇章”为主题的中小企业“数智未来”行动在京成功举办&#xff0c;本次活动由中央广播电视总台央视频和中国中小企业协会作为联合观察单位&#xff0c;带来了一系列帮助中小企业成就业务新价值和数智化升级的优秀产品和方案&…

为什么推荐大家学习双拼输入法?

在现代信息技术迅速发展的今天&#xff0c;计算机已经成为人们日常工作生活中不可分割的一部分。而对于大多数人来说&#xff0c;输入法是与计算机打交道最频繁的交互方式之一。在中文输入法中&#xff0c;五笔输入法一直是非常受欢迎的一种输入方式。然而&#xff0c;双拼输入…