# Leverage Effect using Python

Hi All! In our previous tutorial, we had covered Stylized fact 4: Decreasing auto-correlation trend in squared/absolute returns. In this tutorial, we’ll continue exploring stylized fact and will go through Stylized fact 5: understanding leverage effect 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 5: Understanding Leverage Effect using Python

### What is Leverage effect?

### Leverage Effect using Python

```
# 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
```

```
# Downloading MSFT data from yfinance from 1st January 2010 to 31st March 2020
msftStockData = yf.download( 'MSFT',
start = '2010-01-01',
end = '2020-03-31',
progress = False)
```

```
# Checking what's in there the dataframe by loading first 5 rows
msftStockData.head()
```

```
# Checking what's in there the dataframe by loading last 5 rows
msftStockData.tail()
```

```
# Calculating log returns and obtaining column to contain it
msftStockData['Log Returns'] = np.log(msftStockData['Adj Close']/msftStockData['Adj Close'].shift(1))
```

```
# Checking what's in there the dataframe by loading first 5 rows
msftStockData.head()
```

```
# Using back fill method to replace NaN values
msftStockData['Log Returns'] = msftStockData['Log Returns'].fillna(method = 'bfill')
msftStockData.head()
```

```
# Taking monthly window - average of 21 days per month is taken in the trading world,
# This is the time span for which particular stock exchange is open
# We have on average 21 trading days in a month
msftStockData['21_day_mstd'] = msftStockData[['Log Returns']].rolling(window=21).std()
# Taking yearly window - average of 252 days per year is taken in the trading world,
# This is the time span for which particular stock exchange is open
# We have on average 252 trading days in an year
msftStockData['252_day_mstd'] = msftStockData[['Log Returns']].rolling(window=252).std()
```

```
# Plotting MSFT Adj Close, daily log returns, 21_day_mstd and 252_day_mstd
# Distributing in 3 subplots
# sharex controls sharing of properties among x-axis.
# That is, all three subplots will use same x-axis
fig, ax = plt.subplots(3, 1, figsize=(20, 15), sharex=True)
# Subplot 1 - Daily Adj Close trend
msftStockData['Adj Close'].plot(ax=ax[0])
# Setting title and ylabel
ax[0].set(title='MSFT Adj Close', ylabel='Adjusted Close Price')
# Subplot 2 - Daily log returns
msftStockData['Log Returns'].plot(ax=ax[1])
# Setting title and ylabel
ax[1].set(title='MSFT Log Returns', ylabel='Log returns')
# Subplot 3 - 21 days and 252 days windows to calculate rolling standard deviations
msftStockData['252_day_mstd'].plot(ax=ax[2], color='g', label='Rolling yearly standard deviation/volatility')
msftStockData['21_day_mstd'].plot(ax=ax[2], color='b', label='Rolling monthly standard deviation/volatility')
ax[2].set(ylabel='Rolling standard deivation/volatility', xlabel='Date')
ax[2].legend()
```

From the graph, we can see that when log returns decrease, the volatility shoots up. Hence, our stylized fact 5: Leverage effect gets verified here.

So guys, with this I conclude this tutorial. If you like our blog posts, please follow our blog to stay updated on what’s going on latest in the world of data science, machine learning and business intelligence. Also, please check out our YouTube channel here. Stay tuned!