栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【时间序列分析】平稳性检验及ARIMA模型

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【时间序列分析】平稳性检验及ARIMA模型

Augmented Dicky-Fuller test

用于检验序列平稳性
H0:时间序列可以表示成单位根过程,即非平稳
H1:时间序列是平稳的

from statsmodels.tsa.stattools import adfuller
import numpy as np
import matplotlib.pyplot as plt
x=[np.random.randn()]
for i in range(100):
    x.append(0.5*x[-1]+np.random.rand())

#plt.plot(x)
#plt.show()
result=adfuller(x)
print('ADF Statistic: %f' % result[0])
print('p-value: %f' % result[1])
print('Critical Values:')
for key, value in result[4].items():
    print('t%s: %.3f' % (key, value))
ADF Statistic: -6.601666
p-value: 0.000000
Critical Values:
	1%: -3.498
	5%: -2.891
	10%: -2.582   

可以发现ADF检验得到的p值几乎等于0,说明拒绝原假设,原时间序列是平稳的。

绘制acf和pacf

acf(Autocorrelation Function)是指一个时间序列的自相关函数。pacf(Partial Autocorrelation Function)是指偏自相关函数。

from statsmodels.tsa.stattools import adfuller
import numpy as np
import matplotlib.pyplot as plt
from statsmodels.graphics.tsaplots import plot_acf,plot_pacf
x=[np.random.randn()]
for i in range(100):
    x.append(0.5*x[-1]+np.random.rand())
plt.subplot(211)
plot_acf(x,lags=3,ax=plt.gca())

plt.subplot(212)
plot_pacf(x,lags=3,ax=plt.gca())
plt.show()
ARIMA(Autoregressive Integrated Moving Average Model)

AR:Autoregression 自回归模型
I:Integrated
MA:Moving Average 移动平均
ARIMA(p,d,q)
例子:
AR(1) $y_t=c+Phi_1 y_{t-1}+epsilon_t $
MA(1) y t = μ + θ 1 ϵ t − 1 + ϵ t y_t=mu+theta_1epsilon_{t-1}+epsilon_t yt​=μ+θ1​ϵt−1​+ϵt​
ARMA(1,1) y t = c + Φ 1 y t − 1 + θ 1 ϵ t − 1 + ϵ t y_t=c+Phi_1 y_{t-1}+theta_1epsilon_{t-1}+epsilon_t yt​=c+Φ1​yt−1​+θ1​ϵt−1​+ϵt​

from statsmodels.tsa.stattools import adfuller
from statsmodels.tsa.arima_model import ARMA,ARMAResults,ARIMA,ARIMAResults
from statsmodels.graphics.tsaplots import plot_acf,plot_pacf
from pmdarima import auto_arima
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(1)
x=[np.random.randn(),np.random.randn()]
for i in range(100):
    x.append(0.8*x[-1]-0.5*x[-2]+np.random.rand())
plt.plot(x)
plt.show()
plt.subplot(211)
plot_acf(x,lags=5,ax=plt.gca())

plt.subplot(212)
plot_pacf(x,lags=5,ax=plt.gca())
plt.show()

print(auto_arima(x).summary())

model = ARMA(x,order=(2,0))
results = model.fit()
print(results.summary())

model=ARIMA(x,order=(2,0,0))
results=model.fit()
print(results.summary())

模型预测

from statsmodels.tsa.stattools import adfuller
from statsmodels.tsa.arima_model import ARMA,ARMAResults,ARIMA,ARIMAResults
from statsmodels.graphics.tsaplots import plot_acf,plot_pacf
from pmdarima import auto_arima
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(2)
x=[2,3]
for i in range(20):
    x.append(0.5*x[-1]-0.8*x[-2])

plt.plot(x)
plt.show()
train_x=x[:15]
test_x=x[15:]


model=ARIMA(train_x,order=(2,0,0))
results=model.fit()
print(results.summary())


start=len(train_x)
end=len(train_x)+len(test_x)-1
predictions=results.predict(start=start,end=end)
print(len(predictions))
print(len(test_x))
print(predictions)
print(test_x)
plt.plot(range(len(predictions)),predictions,label="predict")
plt.plot(range(len(test_x)),test_x,label="real")
plt.legend()
plt.show()
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/286407.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号