# Auto-correlation in Squared Returns

Hi All! In our previous tutorial, we had covered Stylized fact 3: Is auto-correlation absent in returns?. In this tutorial, we’ll continue exploring stylized fact and will go through Stylized fact 4: Decreasing auto-correlation trend in squared returns or absolute returns and will see if there is decreasing auto-correlation trend in squared/absolute returns using Python. If you want to learn what are stylized facts, please go here. If you’re new to Financial Analytics, I suggest you start from here.

### Stylized fact 4: Decreasing auto-correlation in squared returns or absolute returns

In [1]:
```# Importing libraries
import pandas as pd
import yfinance as yf
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import scipy.stats as scs
import statsmodels.api as sm
import statsmodels.tsa.api as smt
```
In [2]:
```# Downloading MSFT data from yfinance from 1st January 2010 to 31st March 2020
start = '2010-01-01',
end = '2020-03-31',
progress = False)
```
In [3]:
```# Checking what's in there the dataframe by loading first 5 rows
```
Out[3]:
Open High Low Close Adj Close Volume
Date
2009-12-31 30.980000 30.990000 30.480000 30.480000 23.925440 31929700
2010-01-04 30.620001 31.100000 30.590000 30.950001 24.294369 38409100
2010-01-05 30.850000 31.100000 30.639999 30.959999 24.302216 49749600
2010-01-06 30.879999 31.080000 30.520000 30.770000 24.153070 58182400
2010-01-07 30.629999 30.700001 30.190001 30.450001 23.901886 50559700
In [4]:
```# Checking what's in there the dataframe by loading last 5 rows
msftStockData.tail()
```
Out[4]:
Open High Low Close Adj Close Volume
Date
2020-03-24 143.750000 149.600006 141.270004 148.339996 148.339996 82516700
2020-03-25 148.910004 154.330002 144.440002 146.919998 146.919998 75638200
2020-03-26 148.399994 156.660004 148.369995 156.110001 156.110001 64568100
2020-03-27 151.750000 154.889999 149.199997 149.699997 149.699997 57042300
2020-03-30 152.440002 160.600006 150.009995 160.229996 160.229996 63420300
In [5]:
```# Calculating log returns and obtaining column to contain it
```
In [6]:
```# Checking what's in there the dataframe by loading first 5 rows
```
Out[6]:
Open High Low Close Adj Close Volume Log Returns
Date
2009-12-31 30.980000 30.990000 30.480000 30.480000 23.925440 31929700 NaN
2010-01-04 30.620001 31.100000 30.590000 30.950001 24.294369 38409100 0.015302
2010-01-05 30.850000 31.100000 30.639999 30.959999 24.302216 49749600 0.000323
2010-01-06 30.879999 31.080000 30.520000 30.770000 24.153070 58182400 -0.006156
2010-01-07 30.629999 30.700001 30.190001 30.450001 23.901886 50559700 -0.010454
In [7]:
```# Using back fill method to replace NaN values
msftStockData['Log Returns'] = msftStockData['Log Returns'].fillna(method = 'bfill')
```
Out[7]:
Open High Low Close Adj Close Volume Log Returns
Date
2009-12-31 30.980000 30.990000 30.480000 30.480000 23.925440 31929700 0.015302
2010-01-04 30.620001 31.100000 30.590000 30.950001 24.294369 38409100 0.015302
2010-01-05 30.850000 31.100000 30.639999 30.959999 24.302216 49749600 0.000323
2010-01-06 30.879999 31.080000 30.520000 30.770000 24.153070 58182400 -0.006156
2010-01-07 30.629999 30.700001 30.190001 30.450001 23.901886 50559700 -0.010454
In [9]:
```# Creating the acf plots for squared and absolute returns
fig, ax = plt.subplots(2, 1, figsize=(14, 10))

# Using statsmodels library to obtain acf plot for squared returns
smt.graphics.plot_acf(msftStockData['Log Returns'] ** 2, lags=35, alpha=0.05, ax = ax[0])

# Setting title, y-axis labels and x-axis labels of first subplot
ax[0].set(title='Autocorrelation plots of sqaured log returns', ylabel='Squared Log Returns', xlabel='Lag')

# Obtaining acf plot of absolute returns using abs function of numpy library
smt.graphics.plot_acf(np.abs(msftStockData['Log Returns']), lags=35, alpha=0.05, ax = ax[1])

# Setting title, y-axis labels and x-axis labels of second subplot
ax[1].set(title='Autocorrelation plots of absolute log returns', ylabel='Absolute Log Returns', xlabel='Lag')
```
Out[9]:
```[Text(0, 0.5, 'Absolute Log Returns'),
Text(0.5, 0, 'Lag'),
Text(0.5, 1.0, 'Autocorrelation plots of absolute log returns')]```

As can be seen from above,

• In the first sub-plot, the auto-correlation for lags up to 12 crosses the blue shaded area (confidence interval), after that it’s inside that area and is decreasing also.
• In the second sub-plot, the auto-correlation for lags up to 18 crosses the said area and after that lies within the area (except for lag 25 and 30.

Thus, we have established and verified fourth stylized fact here, i.e., there is decreasing auto-correlation for squared and absolute log returns.

With this, we conclude our tutorial. In the next tutorial, we will learn about Stylized fact 5: Leverage effect. Don’t forget to subscribe to our YouTube channel where we explain all this by means of videos. Stay tuned!