# Leverage Effect using VIX and Python

Hi All! Today, in the part 13 of Financial Analytics series, we will be learning about an alternative method of identifying Leverage Effect using VIX and Python. If you’re new to this series, we suggest that you start from here.

### What is VIX?

- VIX is also called as CBOE Volatility Index.
- It was created by Chicago Board Options Exchange, hence the name CBOE Volatility Index.
- It is a metric that gives us an indication as to how the volatility of the market is expected to be in the coming 30 days.
- Basically, it is a 30-day forward-looking volatility expectation of the market.
- It is a good indicator of markets’ risk and how the sentiments of investors are currently.
- It is a bi-product of price inputs of S&P 500 index options.
- People also refer to it as “Fear Gauge” or “Fear Index”.

### Leverage Effect using VIX and Python

Let’s implement this using Python. The stock ticker I’ve chosen in this case-study is MSFT (Microsoft Corporation). Let’s fetch VIX and MSFT Adjusted Price data using yfinance library of Python.

```
# Importing libraries
import pandas as pd
import yfinance as yf
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
```

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

```
# Checking first five rows of the StockData
StockData.head()
```

```
# Choosing only Adj Close
StockData = StockData['Adj Close']
StockData.head()
```

```
# Renaming the columns
StockData = StockData.rename(columns = {'MSFT': 'Adj Close', '^VIX': 'VIX'})
StockData.head()
```

```
# Calculating log returns and volatility returns
## Log returns calculation
StockData['Log Returns'] = np.log(StockData['Adj Close']/StockData['Adj Close'].shift(1))
## Volatility returns calculation
StockData['Volatility Returns'] = np.log(StockData['VIX']/StockData['VIX'].shift(1))
# Dropping null values
StockData = StockData.dropna()
StockData.head()
```

```
# Obtaining correlation co-efficient between Log Returns and Volatility Returns
corrCoeff = StockData['Log Returns'].corr(StockData['Volatility Returns'])
corrCoeff
```

We can see that negative correlation lies between Log Returns and Volatility Returns series. That is, if one increases, the other decreases.

```
# Plotting Log Returns and Volatility Returns on a scatter plot
# Fitting a regression line on the plot
fig, ax = plt.subplots()
fig.set_size_inches(20, 15)
sns.regplot(x='Log Returns', y='Volatility Returns', data=StockData,
line_kws={'color': 'Gray'}, ax = ax)
ax.set(title=f'MSFT Log Returns vs. VIX ($\\rho$ = {corrCoeff:.2f})',
ylabel='VIX log returns',
xlabel='MSFT log returns')
```

### Result

We can see that in the above figure, we have negative slope between MSFT log returns and VIX log returns. Thus, as MSFT log returns increase, the Volatility log returns decrease. Thus, Leverage Effect is identified here.

So guys, with this, I conclude this tutorial. Please don’t forget to check out and subscribe to our YouTube channel.

Stay tuned!