Skip to content

Feature Request: Fama-Macbeth for two-stage regression with HAC standard errors #9723

Open

Description

@gaixen

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:

  1. 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.

  2. 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.

  3. 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 diagnostics

Key Features:

  1. 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))
  2. 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=False for performance
  3. 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
  4. 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:

$$r_{i,t} = α_i + β_i' F_t + ε_{i,t}$$

Stage 2: For each period t, estimate risk premiums via cross-sectional OLS:

$$r_{i,t} = λ_{0,t} + λ_t' β̂_i + η_{i,t}$$

Final: Average risk premiums with HAC standard errors:

$$λ̂ = (1/T) Σ_t λ_t Var(λ̂) = (1/T)[Γ_0 + Σ_{j=1}^m w_j(Γ_j + Γ_j')]$$

where

$$w_j = K(j/m)$$

Describe alternatives you have considered

  1. Using linearmodels library: Already has Fama-MacBeth, but anyways, not bad to have the feature here as well.

  2. Manual implementation: Error-prone and requires deep expertise, especially for HAC standard errors, and no standardization across projects.

  3. 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

  1. bashtage commented on Jan 6, 2026

    @bashtage
    Member

    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.

  2. gaixen commented on Jan 6, 2026

    @gaixen
    Author

    Subjective comments have been removed, and a heartfelt apology is offered for any demeaning behavior caused.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions