报税季的季节性调整
许多时间序列表现出强烈的季节性。去除时间序列中的季节成分的过程称为季节性调整。例如,大多数政府公布的经济数据都是经过季节性调整的。
您之前看到,对随机游走取一阶差分会得到平稳的白噪声过程。进行季节性调整时,不是取一阶差分,而是取与周期相对应滞后的差分。
再看一下预先加载在 DataFrame HRB 中的 H&R Block 按季度的收益的自相关函数(ACF),可以清楚地看到季节性成分。由于每逢报税季每 4 个季度出现一次收益高峰,因此在滞后 4、8、12、16、… 处的自相关较高。请通过取四阶差分进行季节性调整(4 表示该序列的周期)。然后计算变换后序列的自相关函数。
本练习是课程的一部分
Python 中的时间序列分析
练习说明
- 使用
.diff()方法对季度收益取滞后 4 的差分,创建一个新的季节性调整后收益的 DataFrame。 - 查看季节性调整后 DataFrame 的前 10 行,注意前 4 行为
NaN。 - 使用
.dropna()方法删除包含NaN的行。 - 绘制季节性调整后 DataFrame 的自相关函数。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Import the plot_acf module from statsmodels
from statsmodels.graphics.tsaplots import plot_acf
# Seasonally adjust quarterly earnings
HRBsa = ___
# Print the first 10 rows of the seasonally adjusted series
print(HRBsa.___)
# Drop the NaN data in the first four rows
HRBsa = ___
# Plot the autocorrelation function of the seasonally adjusted series
plot_acf(HRBsa)
plt.show()