報稅季的季節性調整
許多時間序列具有強烈的季節性行為。移除時間序列中的季節性成分稱為季節性調整。舉例來說,多數政府公布的經濟資料都會先做季節性調整。
你先前看過,對隨機漫步取一次差分後,會得到平穩的白噪音過程。做季節性調整時,並非取一次差分,而是取與週期性相對應落後期數的差分。
請再次查看已預先載入至 HRB DataFrame 的 H&R Block 逐季盈餘的 ACF,可以清楚看到季節性成分。由於每逢報稅季每 4 季會有一個盈餘尖峰,因而在落後 4、8、12、16、… 的自我相關都很高。請取第 4 階差分(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()