python简单实现一个ARIMA模型(模型构建、绘图、预测结果、评估)

废话少说,先上代码:

# ACF2
plot_acf(diff1)

# PACF
plot_pacf(diff1)

# MODEL
model = sm.tsa.ARIMA(dta, order=(4, 1, 2))
results = model.fit(disp=0)
print('The detail of the model:')
print(results.summary())

# predict
predict_sunspots = results.predict(start=1, end=367)
predict_sunspots_2 = results.forecast(100)
print('The result of the prediction, next 100 :')
print(predict_sunspots_2[0])

# draw predictions
fig, ax = plt.subplots(figsize=(8, 4))
ax = diff1.plot(ax=ax)
predict_sunspots.plot(ax=ax)


# Model evaluation
residuals = pd.DataFrame(results.resid)
fig, ax = plt.subplots(1, 2)
residuals.plot(title="Residuals", ax=ax[0])
residuals.plot(kind='kde', title='Density', ax=ax[1])
print(residuals)

s = pd.DataFrame(results.resid, columns=['value'])
u = s['value'].mean()  # calculate the average
print(u)
std = s['value'].std()  # calculate the standard deviation
print(std)

# show results
plt.show()

解释:

模型构建

首先对模型进行ACF和PACF检验。根据图像确定p,q

model = sm.tsa.ARIMA(dta, order=(4, 1, 2)) 这是模型的构造函数。dta为时间序列,三个参数分别是p,d,q

d是差分的次数。p,q需要通过ACF,PACF图像来确定

预测

这里有两种方法,用predict可以设置预测的区间范围,通过start,end参数设置。这里有个无敌需要注意的地方:你构建ARIMA模型所用的这个序列,必须是等间隔的,而且必须是str,datetime或int类型。

什么是等间隔呢?比如说:你的数据点是2021-02-09,2021-02-10,2021-02-13。这就是不等间隔的时间序列。这样是没办法通过start和end来设置参数的

那么,你有一个不等间隔的时间序列,该怎么解决预测的问题呢?

1.使用插值法,填充缺失的值,使其转化为等间隔

2.不用start和end参数

forecast(100)的含义就是预测之后的100条数据

模型评估

画一个标准差分布图,看它符合正态分布的程度

  • 3
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值