開始使用免費開始

我們有把握這檔股票會均值回歸嗎?

在上一章中,你看到 MSFT 每週股票報酬率的自相關為 -0.16。這個自相關看起來不小,但在統計上顯著嗎?換句話說,如果真實的自相關其實是 0,你能否說明觀察到這麼大的負自相關的機率小於 5%?另外,在其他延遲期是否也有與 0 顯著不同的自相關?

即使在所有延遲期的真實自相關皆為 0,在有限樣本的報酬中,你也不會看到估計的自相關恰好等於 0。事實上,樣本自相關的標準差是 $\small 1/\sqrt{N}\(,其中 \)\small N\( 是觀測值個數。因此,例如當 \)\small N=100\( 時,ACF 的標準差是 0.1,而由於常態分配有 95% 的機率落在平均數的 ±1.96 個標準差之間,所以 95% 信賴區間為 \)\small \pm 1.96/\sqrt{N}$。此近似僅在真實自相關「全部」為 0 時成立。

你將計算 ACF 的實際與近似信賴區間,並與上一章中延遲一期的自相關 -0.16 做比較。Microsoft 的每週報酬已預先載入至名為 returns 的 DataFrame。

本練習屬於課程

Python 中的時間序列分析

檢視課程

練習說明

  • 重新計算 returns DataFrame 中 Series 'Adj Close' 的每週報酬自相關。
  • 使用 len() 函式找出 returns DataFrame 的觀測值個數。
  • 近似估計自相關的 95% 信賴區間。可使用已匯入的數學函式 sqrt()
  • 使用從 statsmodels 匯入的 plot_acf 繪製 returns 的自相關函數。將信賴區間參數設為 alpha=0.05(這是預設值),並設定 lags=20

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Import the plot_acf module from statsmodels and sqrt from math
from statsmodels.graphics.tsaplots import plot_acf
from math import sqrt

# Compute and print the autocorrelation of MSFT weekly returns
autocorrelation = returns['Adj Close'].___
print("The autocorrelation of weekly MSFT returns is %4.2f" %(autocorrelation))

# Find the number of observations by taking the length of the returns DataFrame
nobs = ___

# Compute the approximate confidence interval
conf = 1.96/___
print("The approximate confidence interval is +/- %4.2f" %(conf))

# Plot the autocorrelation function with 95% confidence intervals and 20 lags using plot_acf
plot_acf(___, alpha=0.05, ___)
plt.show()
編輯並執行程式碼