How do I estimate a stock’s market beta with a regression?

Regress a stock’s monthly excess returns on the market’s with statsmodels, read alpha and beta with their standard errors, and check the estimates against the covariance formula.

After this lesson, you can estimate a stock’s alpha and beta with a regression and read their standard errors.

How do I estimate alpha and beta?

A regression of the stock’s excess return on the market’s excess return gives alpha as the intercept and beta as the slope. For a made-up stock with a true beta of 1.1, ten years of monthly returns give a beta of 1.0846 with a standard error of 0.0756.

\[ r_t = \alpha + \beta\, m_t + \varepsilon_t \]

Here \(r_t\) is the stock’s excess return in month \(t\), its return minus the risk-free rate, \(m_t\) the market’s excess return and \(\varepsilon_t\) the part the market does not explain. Ordinary least squares (OLS) chooses \(\alpha\) and \(\beta\) to make the sum of the squared \(\varepsilon_t\) as small as possible.

The example draws the market’s excess returns from a normal distribution with a mean of 0.7% and a standard deviation of 4.5% a month, and sets the stock’s excess return to \(0.002 + 1.1\,m_t\) plus a normal error with a standard deviation of 4%. pd.period_range gives the 120 months from January 2015 to December 2024.

smf.ols("stock_return ~ market_return", data=data) puts the stock’s return on the left and the market’s on the right and adds an intercept. .fit() estimates the model. .params holds the estimates, .bse their standard errors, .tvalues each estimate divided by its standard error and .pvalues the two-sided p-values of a test against zero. .rsquared is the share of the stock’s variation that the market explains.

The following box simulates the returns and fits the regression.

import numpy as np
import pandas as pd
import statsmodels.formula.api as smf

months = pd.period_range("2015-01", "2024-12", freq="M")
rng = np.random.default_rng(23)
market_return = rng.normal(0.007, 0.045, len(months))
stock_return = 0.002 + 1.1 * market_return + rng.normal(0, 0.04, len(months))
data = pd.DataFrame({"stock_return": stock_return, "market_return": market_return}, index=months)

model = smf.ols("stock_return ~ market_return", data=data).fit()
print(pd.DataFrame({"estimate": model.params, "standard_error": model.bse, "t": model.tvalues, "p": model.pvalues}).round(4))
print(round(model.rsquared, 2))
               estimate  standard_error        t       p
Intercept        0.0036          0.0037   0.9729  0.3326
market_return    1.0846          0.0756  14.3447  0.0000
0.64

The true beta of 1.1 lies 0.2 standard errors from the estimate. Alpha’s p-value of 0.3326 means the sample cannot tell its estimate of 0.0036 apart from zero, although the true alpha is 0.002. The market explains 64% of the stock’s variation.

How do I check the estimates with the covariance formula?

With one predictor, the OLS slope is the covariance of the two returns divided by the variance of the market’s return, and the intercept puts the line through the two means. Both formulas give the same 0.003566 and 1.084589 as the regression.

\[ \hat\beta = \frac{\operatorname{Cov}(m, r)}{\operatorname{Var}(m)}, \qquad \hat\alpha = \bar r - \hat\beta\, \bar m \]

Here the bars are sample means. .cov() on one column with another gives their covariance, and .var() the variance, as in the lesson on rolling betas.

The following box computes alpha and beta from the formulas.

beta = data["stock_return"].cov(data["market_return"]) / data["market_return"].var()
alpha = data["stock_return"].mean() - beta * data["market_return"].mean()
print(round(alpha, 6), round(beta, 6))
0.003566 1.084589

Your turn

Estimate beta on the months from January 2020 only and print it with its standard error. You should see a beta of 1.097 with a standard error of 0.106, larger than the 0.0756 of all ten years, because half the months give a less precise estimate.

data.loc["2020-01":] keeps the months from January 2020 to the end.

late = smf.ols("stock_return ~ market_return", data=data.loc["2020-01":]).fit()
print(round(late.params["market_return"], 3), round(late.bse["market_return"], 3))
1.097 0.106