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

# Fama-MacBeth Regressions

You are reading **Tidy Finance with Python**. You can find the equivalent chapter for the sibling **Tidy Finance with R** here.

In this chapter, we present a simple implementation of Fama and MacBeth (1973), a regression approach commonly called Fama-MacBeth regressions. Fama-MacBeth regressions are widely used in empirical asset pricing studies. We use individual stocks as test assets to estimate the risk premium associated with the three factors included in Fama and French (1993).

Researchers use the two-stage regression approach to estimate risk premiums in various markets, but predominately in the stock market. Essentially, the two-step Fama-MacBeth regressions exploit a linear relationship between expected returns and exposure to (priced) risk factors. The basic idea of the regression approach is to project asset returns on factor exposures or characteristics that resemble exposure to a risk factor in the cross-section in each time period. Then, in the second step, the estimates are aggregated across time to test if a risk factor is priced. In principle, Fama-MacBeth regressions can be used in the same way as portfolio sorts introduced in previous chapters.

The Fama-MacBeth procedure is a simple two-step approach: The first step uses the exposures (characteristics) as explanatory variables in \(T\) cross-sectional regressions. For example, if \(r_{i,t+1}\) denote the excess returns of asset \(i\) in month \(t+1\), then the famous Fama-French three-factor model implies the following return generating process (see also Campbell et al. 1998): \[\begin{aligned}r_{i,t+1} = \alpha_i + \lambda^{M}_t \beta^M_{i,t} + \lambda^{SMB}_t \beta^{SMB}_{i,t} + \lambda^{HML}_t \beta^{HML}_{i,t} + \epsilon_{i,t}.\end{aligned} \tag{1}\] Here, we are interested in the compensation \(\lambda^{f}_t\) for the exposure to each risk factor \(\beta^{f}_{i,t}\) at each time point, i.e., the risk premium. Note the terminology: \(\beta^{f}_{i,t}\) is an asset-specific characteristic, e.g., a factor exposure or an accounting variable. *If* there is a linear relationship between expected returns and the characteristic in a given month, we expect the regression coefficient to reflect the relationship, i.e., \(\lambda_t^{f}\neq0\).

In the second step, the time-series average \(\frac{1}{T}\sum_{t=1}^T \hat\lambda^{f}_t\) of the estimates \(\hat\lambda^{f}_t\) can then be interpreted as the risk premium for the specific risk factor \(f\). We follow Zaffaroni and Zhou (2022) and consider the standard cross-sectional regression to predict future returns. If the characteristics are replaced with time \(t+1\) variables, then the regression approach captures risk attributes rather than risk premiums.

Before we move to the implementation, we want to highlight that the characteristics, e.g., \(\hat\beta^{f}_{i}\), are often estimated in a separate step before applying the actual Fama-MacBeth methodology. You can think of this as a *step 0*. You might thus worry that the errors of \(\hat\beta^{f}_{i}\) impact the risk premiums’ standard errors. Measurement error in \(\hat\beta^{f}_{i}\) indeed affects the risk premium estimates, i.e., they lead to biased estimates. The literature provides adjustments for this bias (see, e.g., Shanken 1992; Kim 1995; Chen, Lee, and Lee 2015, among others) but also shows that the bias goes to zero as \(T \to \infty\). We refer to Gagliardini, Ossola, and Scaillet (2016) for an in-depth discussion also covering the case of time-varying betas. Moreover, if you plan to use Fama-MacBeth regressions with individual stocks, Hou, Xue, and Zhang (2020) advocates using weighted-least squares to estimate the coefficients such that they are not biased toward small firms. Without this adjustment, the high number of small firms would drive the coefficient estimates.

The current chapter relies on this set of Python packages.

## Data Preparation

We illustrate Fama and MacBeth (1973) with the monthly CRSP sample and use three characteristics to explain the cross-section of returns: Market capitalization, the book-to-market ratio, and the CAPM beta (i.e., the covariance of the excess stock returns with the market excess returns). We collect the data from our SQLite database introduced in Accessing and Managing Financial Data and WRDS, CRSP, and Compustat.

```
= sqlite3.connect(database="data/tidy_finance_python.sqlite")
tidy_finance
= pd.read_sql_query(
crsp_monthly ="SELECT permno, gvkey, month, ret_excess, mktcap FROM crsp_monthly",
sql=tidy_finance,
con={"month"}
parse_dates
)
= pd.read_sql_query(
compustat ="SELECT datadate, gvkey, be FROM compustat",
sql=tidy_finance,
con={"datadate"}
parse_dates
)
= pd.read_sql_query(
beta ="SELECT month, permno, beta_monthly FROM beta",
sql=tidy_finance,
con={"month"}
parse_dates )
```

We use the Compustat and CRSP data to compute the book-to-market ratio and the (log) market capitalization. Furthermore, we also use the CAPM betas based on monthly returns we computed in the previous chapters.

```
= (compustat
characteristics =lambda x: x["datadate"].dt.to_period("M").dt.to_timestamp())
.assign(month="left", on=["gvkey", "month"], )
.merge(crsp_monthly, how="left", on=["permno", "month"])
.merge(beta, how
.assign(=lambda x: x["be"]/x["mktcap"],
bm=lambda x: np.log(x["mktcap"]),
log_mktcap=lambda x: x["month"]+pd.DateOffset(months=6)
sorting_date
)"gvkey", "bm", "log_mktcap", "beta_monthly", "sorting_date"])
.get([={"beta_monthly": "beta"})
.rename(columns
)
= (crsp_monthly
data_fama_macbeth
.merge(characteristics, ="left",
how=["gvkey", "month"], right_on=["gvkey", "sorting_date"])
left_on"month", "permno"])
.sort_values(["permno")
.groupby(apply(lambda x: x.assign(
.=x["beta"].fillna(method="ffill"),
beta=x["bm"].fillna(method="ffill"),
bm=x["log_mktcap"].fillna(method="ffill")
log_mktcap
)
)=True)
.reset_index(drop
)
= (data_fama_macbeth
data_fama_macbeth_lagged =lambda x: x["month"]-pd.DateOffset(months=1))
.assign(month"permno", "month", "ret_excess"])
.get([={"ret_excess": "ret_excess_lead"})
.rename(columns
)
= (data_fama_macbeth
data_fama_macbeth ="left", on=["permno", "month"])
.merge(data_fama_macbeth_lagged, how"permno", "month", "ret_excess_lead", "beta", "log_mktcap", "bm"])
.get([
.dropna() )
```

## Cross-Sectional Regression

Next, we run the cross-sectional regressions with the characteristics as explanatory variables for each month. We regress the returns of the test assets at a particular time point on the characteristics of each asset. By doing so, we get an estimate of the risk premiums \(\hat\lambda^{f}_t\) for each point in time.

```
= (data_fama_macbeth
risk_premiums "month")
.groupby(apply(lambda x: smf.ols(
.="ret_excess_lead ~ beta + log_mktcap + bm",
formula=x
data
).fit()
.params
)
.reset_index() )
```

## Time-Series Aggregation

Now that we have the risk premiums’ estimates for each period, we can average across the time-series dimension to get the expected risk premium for each characteristic. Similarly, we manually create the \(t\)-test statistics for each regressor, which we can then compare to usual critical values of 1.96 or 2.576 for two-tailed significance tests at a five percent and a one percent significance level.

```
= (risk_premiums
price_of_risk ="month", var_name="factor", value_name="estimate")
.melt(id_vars"factor")["estimate"]
.groupby(apply(lambda x: pd.Series({
."risk_premium": 100*x.mean(),
"t_statistic": x.mean()/x.std()*np.sqrt(len(x))
})
)
.reset_index()="factor", columns="level_1", values="estimate")
.pivot(index
.reset_index() )
```

It is common to adjust for autocorrelation when reporting standard errors of risk premiums. As in Univariate Portfolio Sorts, the typical procedure for this is computing Newey and West (1987) standard errors.

```
= (risk_premiums
price_of_risk_newey_west ="month", var_name="factor", value_name="estimate")
.melt(id_vars"factor")
.groupby(apply(lambda x: (
."estimate"].mean()/
x["estimate ~ 1", x)
smf.ols(="HAC", cov_kwds={"maxlags": 6}).bse
.fit(cov_type
)
)
.reset_index()={"Intercept": "t_statistic_newey_west"})
.rename(columns
)
(price_of_risk="factor")
.merge(price_of_risk_newey_west, onround(3)
. )
```

factor | risk_premium | t_statistic | t_statistic_newey_west | |
---|---|---|---|---|

0 | Intercept | 1.244 | 4.809 | 4.168 |

1 | beta | 0.001 | 0.013 | 0.012 |

2 | bm | 0.161 | 3.054 | 2.860 |

3 | log_mktcap | -0.108 | -3.005 | -2.829 |

Finally, let us interpret the results. Stocks with higher book-to-market ratios earn higher expected future returns, which is in line with the value premium. The negative value for log market capitalization reflects the size premium for smaller stocks. Consistent with results from earlier chapters, we detect no relation between beta and future stock returns.

## Exercises

- Download a sample of test assets from Kenneth French’s homepage and reevaluate the risk premiums for industry portfolios instead of individual stocks.
- Use individual stocks with weighted-least squares based on a firm’s size as suggested by Hou, Xue, and Zhang (2020). Then, repeat the Fama-MacBeth regressions without the weighting-scheme adjustment but drop the smallest 20 percent of firms each month. Compare the results of the three approaches.

## References

*Macroeconomic Dynamics*2 (4): 559–62. https://doi.org/10.1017/S1365100598009092.

*The Quarterly Review of Economics and Finance*58: 213–27. https://doi.org/10.1016/j.qref.2014.12.002.

*Journal of Financial Economics*33 (1): 3–56. https://doi.org/10.1016/0304-405X(93)90023-5.

*Journal of Political Economy*81 (3): 607–36. https://doi.org/10.1086/260061.

*Econometrica*84 (3): 985–1046. https://doi.org/10.3982/ECTA11069.

*Review of Financial Studies*33 (5): 2019–2133. https://doi.org/10.1093/rfs/hhy131.

*The Journal of Finance*50 (5): 1605–34. https://doi.org/10.1111/j.1540-6261.1995.tb05190.x.

*Econometrica*55 (3): 703–8. http://www.jstor.org/stable/1913610.

*Review of Financial Studies*5 (1): 1–33. https://doi.org/10.1093/rfs/5.1.1.

*Working Paper*. http://dx.doi.org/10.2139/ssrn.4111428.