用python处理时间序列数据,检验平稳性跟纯随机性

用python处理时间序列数据,检验平稳性跟纯随机性

from statsmodels.tsa.stattools import adfuller as adf
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
import pandas as pd
import numpy as np
!pip install statsmodels
Requirement already satisfied: statsmodels in c:\programdata\anaconda3\lib\site-packages (0.11.0)
Requirement already satisfied: numpy>=1.14 in c:\programdata\anaconda3\lib\site-packages (from statsmodels) (1.18.1)
Requirement already satisfied: scipy>=1.0 in c:\programdata\anaconda3\lib\site-packages (from statsmodels) (1.4.1)
Requirement already satisfied: pandas>=0.21 in c:\programdata\anaconda3\lib\site-packages (from statsmodels) (1.0.1)
Requirement already satisfied: patsy>=0.5 in c:\programdata\anaconda3\lib\site-packages (from statsmodels) (0.5.1)
Requirement already satisfied: pytz>=2017.2 in c:\programdata\anaconda3\lib\site-packages (from pandas>=0.21->statsmodels) (2019.3)
Requirement already satisfied: python-dateutil>=2.6.1 in c:\programdata\anaconda3\lib\site-packages (from pandas>=0.21->statsmodels) (2.8.1)
Requirement already satisfied: six in c:\programdata\anaconda3\lib\site-packages (from patsy>=0.5->statsmodels) (1.14.0)
data=pd.read_excel('./data.xls',encoding='utf-8')
data
timewc(误差随机项)xt1xt2xt3
011.741.0000001.0001.000
12-0.700.8000000.8000.800
23-1.282.6500001.4401.430
340.437.4400005.0944.060
450.2413.80400010.2045.645
..................
95961.55739.0866856908.698146.490
96970.07748.3220117056.498147.345
9798-0.73756.3832077205.072148.140
98990.66765.1299247356.450150.725
99100-0.44773.1879547508.420151.515

100 rows × 5 columns

对X1做平稳性检验

xt1=data.xt1
dftest=adf(xt1)
pd.Series(dftest[0:4],index=['Test Statistic','p-value','#Lags Used','Number of Observations Used'])
#p值高达0.9几
Test Statistic                  0.678947
p-value                         0.989408
#Lags Used                      1.000000
Number of Observations Used    98.000000
dtype: float64
xt1.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x238faff6208>

在这里插入图片描述

[output_6_1.png)]

#一阶差分
xt1_1 = xt1.diff(1)
xt1_1.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x238fa6a5048>

在这里插入图片描述

[(output_7_1.png)]

#一阶差分的单位根检验
dftest_1 = adf(xt1_1.dropna())
pd.Series(dftest_1[0:4],index=['Test Statistic','p-value','#Lags Used','Number of Observations Used'])
#一阶差分后p值小于0.05,拒绝原假设(即不存在单位根,认为其已经平稳)
Test Statistic                -6.056515e+00
p-value                        1.241430e-07
#Lags Used                     0.000000e+00
Number of Observations Used    9.800000e+01
dtype: float64
dftest_1[1]-0.05
-0.049999875856979056
#画一阶差分之后的自相关图跟偏自相关图
plot_acf(xt1_1.dropna())

在这里插入图片描述

[(output_10_0.png)]

plot_pacf(xt1_1.dropna())

在这里插入图片描述

[(output_11_0.png)]

X2做平稳性检验

data.xt2.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x238fe204d88>

在这里插入图片描述

(output_13_1.png)]

#做二阶差分
x2_2=data.Xt2一阶差分.diff(1)
x2_2.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x238fc1d7a48>

在这里插入图片描述

[output_14_1.png)]

#对二阶差分后的Xt2做单位根检验
adf(x2_2.dropna())
#故拒绝原假设,该序列平稳
(-7.1910449486700525,
 2.5032477359463947e-10,
 4,
 93,
 {'1%': -3.502704609582561,
  '5%': -2.8931578098779522,
  '10%': -2.583636712914788},
 260.6359245108364)
#画二阶差分后的Xt2的自相关图跟偏自相关图
plot_acf(x2_2.dropna())

在这里插入图片描述

(output_16_0.png)]

plot_pacf(x2_2.dropna())

在这里插入图片描述

(output_17_1.png)]

对X3做平稳性检验

data.xt3.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x238fd5efc08>

在这里插入图片描述

(output_19_1.png)]

#做一阶差分
data.xt3.diff(1).plot()
<matplotlib.axes._subplots.AxesSubplot at 0x238ff5dac88>

在这里插入图片描述

(output_20_1.png)]

#对一阶差分后的序列做单位根检验
adf(data.xt3.diff(1).dropna())
(-10.661639595719135,
 4.391819453885797e-19,
 1,
 97,
 {'1%': -3.4996365338407074,
  '5%': -2.8918307730370025,
  '10%': -2.5829283377617176},
 257.9188344687909)
adf(data.xt3.diff(1).dropna())[1]-0.05
#故拒绝原假设,该序列平稳
-0.05
#对一阶差分后的Xt3画出自相关图跟偏自相关图
plot_acf(data.xt3.diff(1).dropna())

在这里插入图片描述

(output_23_1.png)]

plot_pacf(data.xt3.diff(1).dropna())

在这里插入图片描述

(output_24_1.png)]


已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页
实付 29.90元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值