Beta is measure of risk/volatility of a stock. The fundamental assumption is that returns of a stock are linearly co-related to returns of an index, which means they can be defined by a linear equation like `y = mx + c`

.

Mathematically β can be defined as slope of line defining relation between stock and index ie-

`return_stock = β x return_index + α + e`

β can be calculated by regression with below formula

`β = Cov(return_stock, return_index)/Var(return_market)`

You can read more about it here-

There are readymade filters online based on beta like topstockreasearch.com, but if you want to know how to calculate it by yourself, read on-

As mentioned earlier, it is measure of volatility.

Value of beta | Interpretation |
---|---|

> 1 | Stock is more volatile than the index but moves in same direction, ie. if stock has a beta of 1.2, when the NIFTY moves by +1%, the stock is likely to move by +1.2%, but similarly when index falls by -1% stock will likely fall by -1.2% |

< 1 | Stock is less volatile than the index and moves in same direction slower than index, ie. if stock has a beta of 0.5, when the NIFTY moves by +1% stock is likely to move by +0.5%, but similarly when index falls by -0.5% stock will likely fall by -0.5% |

< 0 | Negative beta means stock moves in opposite direction of the index. if stock has a beta of -1.2, when the nifty moves by +1%, the stock will likely fall -1.2% and vice-verca |

Hope this gives you a good idea. Please do read more about it on investopedia Beta is also used in Modern Portfolio Theory and for calculating Equity risk premium in CAPM

Now that we have understood the finance concepts, let us now look at the code

Hope you have installed `jugaad-data`

and `pandas`

using pip. If not, just run `pip install jugaad-data pandas`

to install it.

We will download last six months data.

```
from datetime import date
import pandas as pd
from jugaad_data.nse import index_df, stock_df
```

```
idx = index_df("NIFTY 50", date(2020,1,1), date(2020,6,1) )
stk = stock_df("RELIANCE", date(2020,1,1), date(2020,6,1))
print(idx.head())
print(stk.head())
```

```
Index Name INDEX_NAME HistoricalDate OPEN HIGH LOW CLOSE
0 Nifty 50 NIFTY 50 2020-06-01 9726.85 9931.60 9706.95 9826.15
1 Nifty 50 NIFTY 50 2020-05-29 9422.20 9598.85 9376.90 9580.30
2 Nifty 50 NIFTY 50 2020-05-28 9364.95 9511.25 9336.50 9490.10
3 Nifty 50 NIFTY 50 2020-05-27 9082.20 9334.00 9004.25 9314.95
4 Nifty 50 NIFTY 50 2020-05-26 9099.75 9161.65 8996.65 9029.05
DATE SERIES OPEN HIGH LOW ... 52W L VOLUME VALUE NO OF TRADES SYMBOL
0 2020-06-01 EQ 1480.00 1538.35 1475.95 ... 875.65 18434065 2.789048e+10 354436 RELIANCE
1 2020-05-29 EQ 1468.00 1472.00 1452.65 ... 875.65 18471770 2.702029e+10 300018 RELIANCE
2 2020-05-28 EQ 1455.00 1479.75 1449.00 ... 875.65 18519252 2.717698e+10 405603 RELIANCE
3 2020-05-27 EQ 1431.00 1454.00 1412.00 ... 875.65 16460764 2.354223e+10 348477 RELIANCE
4 2020-05-26 EQ 1448.15 1449.70 1416.30 ... 875.65 15330793 2.190317e+10 341795 RELIANCE
[5 rows x 15 columns]
```

You can read more on `jugaad-data`

capabilities in the documentation

Basic clean-up

- In NIFTY’s dataframe replace column name ‘HistoricalDate’ with ‘DATE’ so that we can merge the two data frames
- Create new merged dataframe with new column names ie. NIFTY and STK

```
idx['DATE'] = idx['HistoricalDate']
```

```
df = idx[["DATE", 'CLOSE']]
df.columns = ['DATE', "NIFTY"]
```

```
df = pd.merge(df, stk[['DATE', 'CLOSE']], how='inner', on='DATE')
df.columns = ['DATE', 'NIFTY', 'STK']
```

Now you can calculate beta on daily, weekly or monthly returns depending on your use case, we are going to calculate beta on daily returns here, you can shift by 5 if you want a weekly return.

```
df['STK_PREV'] = df['STK'].shift(1)
df['NIFTY_PREV'] = df['NIFTY'].shift(1)
df['NIFTY_CHG'] = (df['NIFTY']-df['NIFTY_PREV'])/df['NIFTY_PREV']
df['STK_CHG'] = (df['STK']-df['STK_PREV'])/df['STK_PREV']
df.dropna(inplace=True)
print(df.head())
```

```
DATE NIFTY STK STK_PREV NIFTY_PREV NIFTY_CHG STK_CHG
2 2020-05-28 9490.10 1472.25 1464.40 9580.30 -0.009415 0.005361
3 2020-05-27 9314.95 1445.55 1472.25 9490.10 -0.018456 -0.018136
4 2020-05-26 9029.05 1424.05 1445.55 9314.95 -0.030693 -0.014873
5 2020-05-22 9039.25 1431.55 1424.05 9029.05 0.001130 0.005267
6 2020-05-21 9106.25 1441.25 1431.55 9039.25 0.007412 0.006776
```

Numpy provides you readymade functions to calculate Covariance and Variance of a series.

A note on np.cov function, it returns a covariance matrix, eg

np.cov(a,b) returns-

```
[
[cov(a,a), cov(a,b)],
[cov(b,a), cov(b,b)]
]
```

So either select `[1,0] or [0,1]`

```
import numpy as np
cov = np.cov(df['STK_CHG'], df['NIFTY_CHG'])
var = np.var(df['NIFTY_CHG'])
```

```
print(cov)
```

```
[[0.00156533 0.00096934]
[0.00096934 0.0008748 ]]
```

```
print(cov[1,0]/var)
```

```
1.119145517341673
```

In this article we learnt

- What is beta and how to interprete it
- How to download data from NSE using
`jugaad-data`

- How to calculate beta using pandas and numpy

You can download the ipython notebook here

Hope you learnt something interesting