Master Differencing and ARIMA Modeling: From Theory to Python Implementation
This article explains the concept of differencing for extracting deterministic information, outlines when to use first, second, or seasonal differencing, introduces the ARIMA(p,d,q) model and its special cases, and provides a complete Python example with code and visualizations.
差分运算
差分方法是一种非常简便、有效的确定性信息提取方法。Cramer 分解定理在理论上保证了适当阶数的差分一定可以充分提取确定性信息。差分运算实际上是使用自回归的方式提取确定性信息。
在实践操作中,我们会根据序列不同的特点选择合适的差分方式,常见情况有以下三种:
(1)序列蕴含着显著的线性趋势,一阶差分就可以实现平稳。
(2)序列蕴含着曲线趋势,通常二阶或三阶差分就可以提取曲线趋势的影响。
(3)对于蕴含着固定周期的序列进行步长为周期长度的差分运算,通常可以较好地提取周期信息。
从理论上来说,足够多次的差分运算可以充分地提取原序列中的非平稳确定性信息,但是,过度的差分会造成有用信息的浪费。因此,在实际运用中差分运算阶数应当适当,避免过度差分。
ARIMA 模型
差分运算具有强大的确定信息提取能力,对差分运算后得到的平稳序列可用 ARMA 模型进行拟合。具有如下结构的模型称为 ARIMA(p, d, q) 模型。
特别地,当 d=0 时,ARIMA(p, d, q) 模型实际上就是 ARMA(p, q) 模型;当 p=0 时,ARIMA(p, d, q) 模型实际上就是 IMA(d, q) 模型;当 q=0 时,ARIMA(p, d, q) 模型实际上就是 ARI(p, d) 模型。ARIMA 模型的建模过程与 ARMA 模型类似。
例子
使用文件 austa.csv (共 31 条数据)建立适当的 ARIMA 模型。首先对原始数据进行一次差分,并绘制差分数据的自相关图;随后通过尝试不同的 (p, d, q) 参数组合,依据 AIC、BIC 等指标确定模型,并使用 Python 完成求解和绘图。
代码
<code>import pandas as pd
from statsmodels.graphics.tsaplots import plot_acf
import pylab as plt
from statsmodels.tsa.arima_model import ARIMA
plt.rc('axes',unicode_minus=False)
plt.rc('font',size=16); plt.rc('font',family='SimHei')
df = pd.read_csv('data/austa.csv')
plt.subplot(121); plt.plot(df.value.diff())
plt.title('一次差分')
ax2 = plt.subplot(122)
plot_acf(df.value.diff().dropna(), ax=ax2, title='自相关')
md = ARIMA(df.value, order=(2,1,0))
mdf = md.fit(disp=0)
print(mdf.summary())
residuals = pd.DataFrame(mdf.resid)
fig, ax = plt.subplots(1,2)
residuals.plot(title="残差", ax=ax[0])
residuals.plot(kind='kde', title='密度', ax=ax[1])
plt.legend(''); plt.ylabel('')
mdf.plot_predict() # 原始数据与预测值对比图
plt.show()
</code>Model Perspective
Insights, knowledge, and enjoyment from a mathematical modeling researcher and educator. Hosted by Haihua Wang, a modeling instructor and author of "Clever Use of Chat for Mathematical Modeling", "Modeling: The Mathematics of Thinking", "Mathematical Modeling Practice: A Hands‑On Guide to Competitions", and co‑author of "Mathematical Modeling: Teaching Design and Cases".
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.