如何用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.pswp.cn/news/712168.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/712168.shtml
英文地址,請注明出處:http://en.pswp.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;對使…