【时间序列分析】平稳性检验及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_1\epsilon_{t-1}+\epsilon_t yt=μ+θ1ϵt1+ϵt
ARMA(1,1) y t = c + Φ 1 y t − 1 + θ 1 ϵ t − 1 + ϵ t y_t=c+\Phi_1 y_{t-1}+\theta_1\epsilon_{t-1}+\epsilon_t yt=c+Φ1yt1+θ1ϵt1+ϵ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()
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值