Repository navigation
Description
Is your feature request related to a problem? Please describe
Statsmodels lacks an implementation of the Fama-MacBeth (1973) two-stage regression, a fundamental method in empirical asset pricing for estimating factor risk premiums.
The main issues without this feature:
-
No HAC standard errors: Financial time-series data exhibits autocorrelation in risk premium estimates, violating i.i.d. assumptions. Users need Newey-West HAC estimators with multiple kernel options (Bartlett, Parzen, Quadratic-Spectral) and automatic bandwidth selection.
-
No rank validation: When time periods have insufficient cross-sectional observations, singular matrix errors occur or unreliable estimates result. Users need automatic detection and exclusion of rank-deficient periods.
-
Missing diagnostics: Users need period-by-period R² statistics, validity tracking, and GRS tests to identify problematic time periods and assess model fit.
This becomes critical in production financial applications (risk management, portfolio construction, regulatory compliance) where robust inference and comprehensive diagnostics are mandatory.
Describe the solution you'd like
A FamaMacBeth model class following statsmodels conventions with comprehensive features:
Basic Usage:
import statsmodels.api as sm
from statsmodels.regression.fama_macbeth import FamaMacBeth
# Model specification
model = FamaMacBeth(returns, factors, time_index, entity_index)
# Fit with HAC standard errors
results = model.fit(cov_type='kernel', kernel='bartlett', bandwidth=6)
print(results.summary()) # Standard regression table
print(results.diagnostics()) # Model fit diagnosticsKey Features:
-
Multiple Covariance Estimators:
cov_type='robust': Standard Fama-MacBeth (default)cov_type='kernel': HAC estimators with Bartlett/Parzen/Quadratic-Spectral kernels- Automatic bandwidth selection via Newey-West rule(a thumbrule):
m = floor(4(T/100)^(2/9))
-
Automatic Rank Validation:
- Pre-estimation QR decomposition to detect rank-deficient periods
- Informative warnings when periods lack sufficient observations
- Automatic exclusion of invalid periods with tracking
- Optional
check_rank=Falsefor performance
-
Enhanced Results Object:
results.params # Risk premium estimates results.bse # Standard errors results.tvalues # t-statistics results.pvalues # p-values results.conf_int() # Confidence intervals results.summary() # Standard regression table results.diagnostics() # Model fit statistics results.period_params # Period-by-period estimates results.avg_r2 # Average R² across periods results.grs_statistic # GRS test for pricing errors
-
Additional Features:
- Weighted regression for heteroskedastic errors
- Support for both wide and long panel data formats
- Missing data handling with automatic alignment
- Annualization for different data frequencies (daily/monthly/annual)
Mathematical Framework:
Stage 1: For each asset i, estimate factor loadings via time-series OLS:
Stage 2: For each period t, estimate risk premiums via cross-sectional OLS:
Final: Average risk premiums with HAC standard errors:
where
Describe alternatives you have considered
-
Using linearmodels library: Already has Fama-MacBeth, but anyways, not bad to have the feature here as well.
-
Manual implementation: Error-prone and requires deep expertise, especially for HAC standard errors, and no standardization across projects.
-
Basic implementation without HAC: Most production applications require HAC standard errors when autocorrelation is present. A basic implementation wouldn't serve real-world needs.
Additional context
Academic References:
- Fama & MacBeth (1973): "Risk, return, and equilibrium: Empirical tests" - Original methodology
- Cochrane (2001): "Asset Pricing" - Modern treatment
- Newey & West (1987): HAC covariance matrix theory
- Jagannathan et al. (2010): Modern best practices
Use Cases:
- Academic: Testing asset pricing models (CAPM, Fama-French), estimating risk premiums
- Industry: Portfolio construction, risk management, performance attribution, regulatory compliance
- Education: Teaching empirical asset pricing, demonstrating two-stage procedures
Note
I have a primary implementation and unit tests as well, and I would like to open a pull request on approval or further discussion.
(Edit: Subjective comments have been removed, and a heartfelt apology for any demeanour caused)
Activity
bashtage commentedon Jan 6, 2026
FWIW, as the author and maintainer of linearmodels, I would say it is better maintined than statsmodels. It has fairly regular releases and keeps up to date with changes in pandas and other dependencies.
gaixen commentedon Jan 6, 2026
Subjective comments have been removed, and a heartfelt apology is offered for any demeaning behavior caused.