<filename>datacamp-master/22-introduction-to-time-series-analysis-in-python/01-correlation-and-autocorrelation/06-a-popular-strategy-using-autocorrelation.py
'''
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.

INSTRUCTIONS
100XP
INSTRUCTIONS
100XP
Use the .resample() method with rule='W' and how='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.
'''
# Convert the daily data to weekly data
MSFT = MSFT.resample(rule='w',how='last')

# Compute the percentage change of prices
returns = MSFT.pct_change()

# Compute and print the autocorrelation of returns
autocorrelation = returns['Adj Close'].autocorr()
print("The autocorrelation of weekly returns is %4.2f" %(autocorrelation))
<filename>datacamp-master/22-introduction-to-time-series-analysis-in-python/01-correlation-and-autocorrelation/06-a-popular-strategy-using-autocorrelation.py
'''
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.

INSTRUCTIONS
100XP
INSTRUCTIONS
100XP
Use the .resample() method with rule='W' and how='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.
'''
# Convert the daily data to weekly data
MSFT = MSFT.resample(rule='w',how='last')

# Compute the percentage change of prices
returns = MSFT.pct_change()

# Compute and print the autocorrelation of returns
autocorrelation = returns['Adj Close'].autocorr()
print("The autocorrelation of weekly returns is %4.2f" %(autocorrelation))
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.  INSTRUCTIONS 100XP INSTRUCTIONS 100XP Use the .resample() method with rule='W' and how='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. # Convert the daily data to weekly data # Compute the percentage change of prices # Compute and print the autocorrelation of returns