Calculating Stock Beta

The Beta of an asset is a measure of the sensitivity of its returns relative to a market benchmark (usually a market index). How sensitive/insensitive is the returns of an asset to the overall market returns (usually a market index like S&P 500 index). What happens when the market jumps, do the returns of the asset jump accordingly?

CAPM attempts to prices securities by examining the relationship that exists between expected returns and risk. The model implies that investors always combine two types of assets or securities; a risky free asset and a risky asset in the form of a market portfolio of various assets. CAPM further posits that investors expect to be rewarded for holding these risky assets according to the risk inherited for holding on to such assets. After all, such kind of risk cannot be diversified (market related usually referred to as systematic risk) and as a result, investors need to be compensated for taking on such “undiversifiable” risks. It is intuitive when you think about. Let’s look this example:

An investor can buy risk free asset like treasury bills of a stable central bank. If such an investor opts to buy some investment package from company ABC instead of this risk free assets, then such an investor ought to be compensated for this decision. According to CAPM, company ABC does this by offering the returns of the treasury bill plus an incentive usually referred to as market premium/excess market returns (Excess Market Return = Market Return - Risk Free Rate) for the given level of risk (Beta) the investors take.

CAPM = Risk Free Rate + beta × Excess Market Return

To calculate the beta of a security, the covariance between the return of the security and the return of market must be known, as well as the variance of the market returns.

Covariance measures how two stocks move together. A positive covariance means the stocks tend to move together when their prices go up or down. A negative covariance means the stocks move opposite of each other.


With \(x\) is the independent variable, \(y\) is the dependent variable, \(n\) is the number of data points in the sample, \(\bar{x}\) is the mean of the independent variable x, \(\bar{y}\) is the mean of the dependent variable \(y\).

Variance, on the other hand, refers to how far a stock moves relative to its mean. For example, variance is used in measuring the volatility of an individual stock’s price over time. Covariance is used to measure the correlation in price moves of two different stocks. The variance (\(\sigma^2\)), is defined as the sum of the squared distances of each term in the distribution from the mean (\(\mu\)), divided by the number of terms in the distribution (\(N\)):

$$\sigma^2=\frac{\sum_{i=1}^N (X -\mu)^2}{N}$$
$$\beta = \frac{Covariance(r_{AAPL},r_{SP500})}{Variance(r_{SP500})}$$

Interpretation of a Beta result

A stock with a beta of:

  • 0 indicates no correlation with the chosen benchmark (e.g. NASDAQ index )
  • 1 indicates a stock has the same volatility as the market
  • >1 indicates a stock that’s more volatile than its benchmark
  • <1 is less volatile than the benchmark
  • 1.5 is 50% more volatile than the benchmark

Calculating Beta Using Linear Regression

# Import the necessary modules

%matplotlib inline

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import math
import statsmodels.api as sm
import numpy as np

aapl_df = pd.read_csv('AAPL.csv', index_col='Date')

sp500_df = pd.read_csv('^GSPC.csv', index_col='Date')

# Joining the closing prices of the two datasets 

daily_prices = pd.concat([aapl_df['Close'], sp500_df['Close']], axis=1)
daily_prices.columns = ['AAPL', 'SP500']

# Calculate daily returns

daily_returns = daily_prices.pct_change(1)
clean_daily_returns = daily_returns.dropna(axis=0)  # drop first missing row

Good! Now that we have a clean set of monthly returns on Facebook and S&P 500. Let’s go ahead and make the Ordinary Least Square (OLS) Regression with Statsmodel.

# Split dependent and independent variable

X = clean_daily_returns['SP500']
y = clean_daily_returns['AAPL']

# Add a constant to the independent value

X1 = sm.add_constant(X)

# Make regression model 

model = sm.OLS(y, X1)

# Fit model and print results

results =

print("Beta: %f"%results.params[1])
Beta: 1.119065

Calculating Beta Using Formula

beta = np.cov(y,X)[0][1]/np.var(X)
print("Beta: %f"% beta)
Beta: 1.123541
Written on August 15, 2021