我们能有把握认为这只股票会均值回归吗?
在上一章中,您看到 MSFT 的周度股票收益率的一阶自相关为 -0.16。这个自相关看起来不小,但它在统计上显著吗?也就是说,若真实自相关确实为 0,我们观察到如此大的负自相关的概率是否小于 5%?另外,在其他滞后期是否存在显著异于 0 的自相关?
即使所有滞后期的真实自相关都为 0,在有限样本的收益率中,估计得到的自相关也不可能恰好为 0。事实上,样本自相关的标准差是 $\small 1/\sqrt{N}\(,其中 \)\small N\( 为观测值数量。因此,例如当 \)\small N=100\( 时,ACF 的标准差为 0.1;又由于正态分布中 95% 的概率质量位于均值上下 \)\small \pm 1.96\( 个标准差之间,所以 95% 置信区间为 \)\small \pm 1.96/\sqrt{N}$。该近似仅在真实自相关在所有滞后期都为 0 时成立。
您将计算 ACF 的实际与近似置信区间,并与上一章中的一阶自相关 -0.16 进行比较。Microsoft 的周度收益率已预加载在名为 returns 的 DataFrame 中。
本练习是课程的一部分
Python 中的时间序列分析
练习说明
- 重新计算
returnsDataFrame 中'Adj Close'序列的周度收益率自相关。 - 使用
len()函数获取returnsDataFrame 的观测数量。 - 近似计算估计自相关的 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()