'''进行ADF检验
adf_test的返回值
Test statistic:代表检验统计量
p-value:代表p值检验的概率
Lags used:使用的滞后k,autolag=AIC时会自动选择滞后
Number of Observations Used:样本数量
Critical Value(5%) : 显著性水平为5%的临界值。
(1)假设是存在单位根,即不平稳;
(2)显著性水平,1%:严格拒绝原假设;5%:拒绝原假设,10%类推。
(3)看P值和显著性水平a的大小,p值越小,小于显著性水平的话,就拒绝原假设,认为序列是平稳的;大于的话,不能拒绝,认为是不平稳的
(4)看检验统计量和临界值,检验统计量小于临界值的话,就拒绝原假设,认为序列是平稳的;大于的话,不能拒绝,认为是不平稳的
'''
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
# 滚动统计
def rolling_statistics(timeseries):
# Determing rolling statistics
rolmean = pd.rolling_mean(timeseries, window=12)
rolstd = pd.rolling_std(timeseries, window=12)
# Plot rolling statistics:
orig = plt.plot(timeseries, color='blue', label='Original')
mean = plt.plot(rolmean, color='red', label='Rolling Mean')
std = plt.plot(rolstd, color='black', label='Rolling Std')
plt.legend(loc='best')
plt.title('Rolling Mean & Standard Deviation')
plt.show(block=False)
##ADF检验
from statsmodels.tsa.stattools import adfuller
def adf_test(timeseries):
rolling_statistics(timeseries) # 绘图
print('Results of Augment Dickey-Fuller Test:')
dftest = adfuller(timeseries, autolag='AIC')
dfoutput = pd.Series(dftest[0:4], index=['Test Statistic', 'p-value', '#Lags Used', 'Number of Observations Used'])
for key, value in dftest[4].items():
dfoutput['Critical Value (%s)' % key] = value # 增加后面的显著性水平的临界值
print(dfoutput)
num = 2700
inputfile = '../data/value_data.xlsx' # 数据路径
# outputfile = '../data/cjwnew.xls' # 输出数据路径
data = pd.read_excel(inputfile) # 读入数据
data_raw = np.array(data)
data_cjw = data_raw[:num, 3]
# 拟合曲线图
plt.figure(figsize=(24, 8))
plt.title('真实值与预测值对比', color='k')
plt.xlabel('时间')
plt.ylabel('参数值')
plt.plot(data_cjw, c ='b', label='参数值')
plt.legend()
plt.show()
plt.close()
# adf_test(data_cjw) # 从结果中可以看到p值为0.1097>0.1,不能拒绝H0,认为该序列不是平稳序列
评论0