How do Fama-MacBeth regressions work?

Run one cross-sectional regression per month on a long pandas panel with groupby().apply(), then average the monthly slopes and compute their Fama-MacBeth t-statistic.

After this lesson, you can test whether a stock characteristic goes with higher returns by running one cross-sectional regression per month and averaging the slopes.

How do I run one regression per month?

A Fama-MacBeth regression first regresses the returns of the stocks in each month on their signal, which gives one slope per month. On five months of three made-up stocks, January’s slope is 2.0, February’s −1.0 and March’s 3.0.

\[ R_{i,t} = a_t + b_t\,S_{i,t-1} + e_{i,t} \]

Here \(R_{i,t}\) is stock \(i\)’s return in month \(t\) in percent, \(S_{i,t-1}\) its signal known before the month, and \(a_t\) and \(b_t\) the intercept and slope of month \(t\).

The example is a long panel, as in the lesson on long and wide panels, with one row per stock and month. cross_section fits the regression on one month’s rows, as in the lesson on OLS, and returns the intercept and slope. groupby("month") runs it on each month, and the result has one row per month.

The following box builds the panel and runs the monthly regressions.

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

panel = pd.DataFrame({
    "month": np.repeat(pd.period_range("2025-01", periods=5, freq="M"), 3),
    "ticker": np.tile(["AAA", "BBB", "CCC"], 5),
    "signal": [-1, 0, 1, 1, -1, 0, 0, 1, -1, -1, 1, 0, 1, 0, -1],
    "stock_return": [-1, 2, 3, 1, 3, 2, 0, 4, -2, 0, 1, 2, 5, 0, 1],
})

def cross_section(month_rows):
    return smf.ols("stock_return ~ signal", data=month_rows).fit().params

monthly = panel.groupby("month")[["signal", "stock_return"]].apply(cross_section)
print(monthly.round(3))
         Intercept  signal
month                     
2025-01      1.333     2.0
2025-02      2.000    -1.0
2025-03      0.667     3.0
2025-04      1.000     0.5
2025-05      2.000     2.0

The slope changes from month to month, so one month says little about the signal.

How do I average the slopes and test them?

The second step averages the monthly slopes and divides the average by its standard error, which comes from the spread of the slopes across months. The average slope is 1.3 percentage points per unit of signal, with a standard error of 0.7 and a t-statistic of 1.857.

\[ \bar b = \frac{1}{T}\sum_{t=1}^{T}\hat b_t, \qquad t = \frac{\bar b}{s_b / \sqrt{T}} \]

Here \(T\) is the number of months, \(\hat b_t\) the slope of month \(t\) and \(s_b\) the sample standard deviation of the slopes. fama_macbeth returns the average and its t-statistic for one column of monthly coefficients.

The following box tests the average slope.

def fama_macbeth(coefficients):
    standard_error = coefficients.std() / np.sqrt(len(coefficients))
    return pd.Series({"average": coefficients.mean(), "t": coefficients.mean() / standard_error}).round(3)

print(fama_macbeth(monthly["signal"]))
average    1.300
t          1.857
dtype: float64

The standard error assumes that the monthly slopes are uncorrelated. When they are not, a HAC standard error of their mean, as in the lesson on HAC errors, allows for it. Five months show the method, and research uses decades.

Your turn

Test the average monthly intercept. You should see an average of 1.4 and a t-statistic of 5.25.

fama_macbeth takes the intercept column in the same way.

print(fama_macbeth(monthly["Intercept"]))
average    1.40
t          5.25
dtype: float64