Get startedGet started for free

A Popular Strategy Using Autocorrelation

One puzzling anomaly with stocks is that investors tend to overreact to news. Following large jumps, either up or down, stock prices tend to reverse. This is described as mean reversion in stock prices: prices tend to bounce back, or revert, towards previous levels after large moves, which are observed over time horizons of about a week. A more mathematical way to describe mean reversion is to say that stock returns are negatively autocorrelated.

This simple idea is actually the basis for a popular hedge fund strategy. If you're curious to learn more about this hedge fund strategy (although it's not necessary reading for anything else later in the course), see here.

You'll look at the autocorrelation of weekly returns of MSFT stock from 2012 to 2017. You'll start with a DataFrame MSFT of daily prices. You should use the .resample() method to get weekly prices and then compute returns from prices. Use the pandas method .autocorr() to get the autocorrelation and show that the autocorrelation is negative. Note that the .autocorr() method only works on Series, not DataFrames (even DataFrames with one column), so you will have to select the column in the DataFrame.

This exercise is part of the course

Time Series Analysis in Python

View Course

Exercise instructions

  • Use the .resample() method with rule='W' followed by the function .last() to convert daily data to weekly data.
  • Create a new DataFrame, returns, of percent changes in weekly prices using the .pct_change() method.
  • Compute the autocorrelation using the .autocorr() method on the series of closing stock prices, which is the column 'Adj Close' in the DataFrame returns.

Hands-on interactive exercise

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

# Convert the daily data to weekly data
MSFT = MSFT.resample(___).___

# Compute the percentage change of prices
returns = MSFT.___

# Compute and print the autocorrelation of returns
autocorrelation = returns[___].___
print("The autocorrelation of weekly returns is %4.2f" %(autocorrelation))
Edit and Run Code