Get startedGet started for free

Are We Confident This Stock is Mean Reverting?

In the last chapter, you saw that the autocorrelation of MSFT's weekly stock returns was -0.16. That autocorrelation seems large, but is it statistically significant? In other words, can you say that there is less than a 5% chance that we would observe such a large negative autocorrelation if the true autocorrelation were really zero? And are there any autocorrelations at other lags that are significantly different from zero?

Even if the true autocorrelations were zero at all lags, in a finite sample of returns you won't see the estimate of the autocorrelations exactly zero. In fact, the standard deviation of the sample autocorrelation is \(\small 1/\sqrt{N}\) where \(\small N\) is the number of observations, so if \(\small N=100\), for example, the standard deviation of the ACF is 0.1, and since 95% of a normal curve is between +1.96 and -1.96 standard deviations from the mean, the 95% confidence interval is \(\small \pm 1.96/\sqrt{N}\). This approximation only holds when the true autocorrelations are all zero.

You will compute the actual and approximate confidence interval for the ACF, and compare it to the lag-one autocorrelation of -0.16 from the last chapter. The weekly returns of Microsoft is pre-loaded in a DataFrame called returns.

This exercise is part of the course

Time Series Analysis in Python

View Course

Exercise instructions

  • Recompute the autocorrelation of weekly returns in the Series 'Adj Close' in the returns DataFrame.
  • Find the number of observations in the returns DataFrame using the len() function.
  • Approximate the 95% confidence interval of the estimated autocorrelation. The math function sqrt() has been imported and can be used.
  • Plot the autocorrelation function of returns using plot_acf that was imported from statsmodels. Set alpha=0.05 for the confidence intervals (that's the default) and lags=20.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# 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()
Edit and Run Code