时间序列分析与无监督学习方法详解
1. 自相关检验与ARIMA模型构建
1.1 自相关检验
在时间序列分析中,为了使时间序列平稳,我们发现对其取对数后至少需要进行一阶差分。接下来,我们绘制一阶对数序列的自相关函数(ACF)和偏自相关函数(PACF)图。以下是实现代码:
import matplotlib.pyplot as plt
import statsmodels.api as sm
import numpy as np
fig, (ax1, ax2) = plt.subplots(1, 2, figsize = (10,3))
# ACF chart
fig = sm.graphics.tsa.plot_acf(ts_log_diff.values.squeeze(), lags=20, ax=ax1)
# draw 95% confidence interval line
ax1.axhline(y=-1.96/np.sqrt(len(ts_log_diff)),linestyle='--',color='gray')
ax1.axhline(y=1.96/np.sqrt(len(ts_log_diff)),linestyle='--',color='gray')
ax1.set_xlabel('Lags')
# PACF chart
fig = sm.graphics.tsa.plot_pacf(ts_log_diff, lags=20, ax=ax2)
# draw 95% confidence interval line
ax2.axhline(y=-1.96/np.sqrt(len(ts_log_