How do I fit OLS with sm.OLS and predict new values?

Fit a regression with sm.OLS and a constant from sm.add_constant, check it against the formula interface, predict a stock’s return for new market moves, and add the constant to a one-row input with has_constant.

After this lesson, you can fit a regression with sm.OLS and predict a stock’s return for new market moves.

How do I fit the regression with sm.OLS?

sm.OLS(y, X) fits the return y on the columns of X, and sm.add_constant adds the column of ones that carries the intercept. With it, sm.OLS gives the same alpha of 0.0058 and beta of 1.1379 as the formula interface from the lesson on OLS.

The example has 120 months of a made-up stock’s returns, equal to \(0.002 + 1.2\) times the market’s return plus a normal error with a standard deviation of 4%. sm.add_constant(data["market_return"]) returns a table with a column const of ones before the market column. sm.OLS takes the return to predict first and the predictors second, and .fit() estimates the model.

The following box fits the regression both ways and prints the two sets of estimates.

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

rng = np.random.default_rng(5)
data = pd.DataFrame({"market_return": rng.normal(0.008, 0.045, 120)})
data["stock_return"] = 0.002 + 1.2 * data["market_return"] + rng.normal(0, 0.04, 120)

model = sm.OLS(data["stock_return"], sm.add_constant(data["market_return"])).fit()
formula_model = smf.ols("stock_return ~ market_return", data=data).fit()
print(pd.DataFrame({"sm_OLS": model.params.to_numpy(), "formula": formula_model.params.to_numpy()}, index=["alpha", "beta"]).round(4))
       sm_OLS  formula
alpha  0.0058   0.0058
beta   1.1379   1.1379

How do I predict returns for new market moves?

.predict() applies the fitted line to new rows, which need the same columns in the same order as the fit. For market returns of −5%, 0% and 5%, the predicted returns are −5.11%, 0.58% and 6.27%.

sm.add_constant(new) adds the column of ones before the market column, as in the fit.

The following box predicts the stock’s return for three market moves.

new = pd.DataFrame({"market_return": [-0.05, 0.0, 0.05]})
print(model.predict(sm.add_constant(new)).round(4))
0   -0.0511
1    0.0058
2    0.0627
dtype: float64

What goes wrong with one new row?

On a one-row table, sm.add_constant returns the market column alone, because that column is already constant, and model.predict() then raises a ValueError, because the model expects two columns. has_constant="add" adds the column of ones anyway.

The following box prints the columns of both versions and the prediction with the forced constant.

one_row = pd.DataFrame({"market_return": [0.05]})
print(sm.add_constant(one_row).columns.tolist())
print(sm.add_constant(one_row, has_constant="add").columns.tolist())
print(model.predict(sm.add_constant(one_row, has_constant="add")).round(4))
['market_return']
['const', 'market_return']
0    0.0627
dtype: float64

Your turn

Predict the stock’s return for a market fall of 10%. You should see −0.108.

The one-row table needs has_constant="add".

crash = pd.DataFrame({"market_return": [-0.10]})
print(model.predict(sm.add_constant(crash, has_constant="add")).round(4))
0   -0.108
dtype: float64