Skip to content

BUG: Require residual degrees of freedom in FamaMacBethCovariance - #883

Open
Cyber-Marty wants to merge 1 commit into
bashtage:mainfrom
Cyber-Marty:fix/famamacbeth-single-period-df
Open

Cyber-Marty wants to merge 1 commit into
bashtage:mainfrom
Cyber-Marty:fix/famamacbeth-single-period-df

Conversation

@Cyber-Marty

Copy link
Copy Markdown

Summary

Fixes FamaMacBeth.fit() dividing by zero and returning an all-NaN covariance matrix (with RuntimeWarning: invalid value encountered in divide) when only a single cross-sectional period is estimable (nperiods == 1) and debiased=True.

Closes #882

  • Compute deviations and the count of finite cross-sectional period estimates (nperiods) in FamaMacBethCovariance.__init__, raising ValueError with "no residual degrees of freedom" when nperiods - int(bool(debiased)) <= 0.
  • Reuses nperiods for the subsequent bandwidth >= nperiods check and updates the FamaMacBethCovariance Raises docstring.
  • Preserves fit(debiased=False) on single-period panels (returning finite zero covariance and InvalidTestStatistic for f_statistic_robust, consistent with test_saturated_model_that_is_not_debiased in linearmodels/tests/panel/test_no_residual_df.py).
  • Adds regression tests in linearmodels/tests/panel/test_no_residual_df.py covering unadjusted, robust, kernel (bandwidth=None and bandwidth=0), multi-period panels reduced to one estimable period, debiased=False, and FamaMacBethCovariance directly.

Verification

  • Environment: Python 3.13.15, NumPy 2.5.5, pandas 3.1.1, SciPy 1.18.1 (Windows 11, pure-Python tree without building optional Cython extension or running notebook example scripts).
  • Base (00528bdb) RED: pytest -W always linearmodels/tests/panel/test_no_residual_df.py -> 6 failed, 29 passed in 5.28s (all 6 new tests failed on the unpatched covariance check).
  • Fix GREEN:
    • pytest -n 2 -q -m "not slow" --ignore=linearmodels/tests/test_examples.py -> 23397 passed, 2473 skipped, 47 xfailed, 1 warning in 1846.99s (final revision after black formatting; slow tests and example notebook tests excluded, optional-dependency/Cython tests skipped).
    • Pre-format targeted check: pytest -W always linearmodels/tests/panel/test_no_residual_df.py linearmodels/tests/panel/test_fama_macbeth_bandwidth.py linearmodels/tests/panel/test_fama_macbeth.py linearmodels/tests/test_kernel_bandwidth_few_observations.py -> 812 passed, 45 warnings in 173.70s.
  • Style checks:
    • black --check linearmodels/panel/covariance.py linearmodels/tests/panel/test_no_residual_df.py -> 2 files would be left unchanged
    • ruff check linearmodels/panel/covariance.py linearmodels/tests/panel/test_no_residual_df.py -> All checks passed!

AI assistance disclosure: AI tools were used to locate the degrees-of-freedom mismatch in FamaMacBethCovariance.__init__, draft the patch and tests, and format notes; all reproductions, tests, and linters were executed and verified locally.

@codecov

codecov Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.69%. Comparing base (00528bd) to head (be957b5).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #883   +/-   ##
=======================================
  Coverage   99.69%   99.69%           
=======================================
  Files         202      202           
  Lines       38871    38921   +50     
  Branches     2501     2502    +1     
=======================================
+ Hits        38752    38802   +50     
  Misses         51       51           
  Partials       68       68           
Flag Coverage Δ
adder 99.68% <100.00%> (+<0.01%) ⬆️
subtractor 99.68% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bashtage bashtage left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. This gives Fama-MacBeth the same no-residual-df error that the other panel covariances got in 7.1, and it covers both behaviors asked for in #882.

  1. It now conflicts with main: #887 rewrote the same Raises paragraph. Only the docstring conflicts; this text keeps both changes:

    If there are no residual degrees of freedom, which occurs when at most
    one cross-sectional estimate is available and ``debiased`` is True, or
    if no finite cross-sectional estimates are available. It is also raised
    if the bandwidth is negative or is not below the number of time periods
    that are used, if the bandwidth is not provided and there are fewer time
    periods than the kernel needs to compute it, or if the bandwidth that is
    computed is not finite.
    
  2. 7.1 returned estimates with NaN standard errors here, so this is a backward-incompatible change. Please add a .. versionchanged:: 7.2 to FamaMacBethCovariance and a 7.2 note under "Backward-incompatible changes":

    - ``FamaMacBeth.fit`` raises a ``ValueError`` when only one cross-sectional regression
      can be estimated and ``debiased`` is True, since the covariance then has no residual
      degrees of freedom. 7.1 returned NaN standard errors with a ``RuntimeWarning``. Pass
      ``debiased=False`` to get the estimates (:issue:`882`, :issue:`883`).

Nit: assert str(res.summary) is always true; calling str(res.summary) on its own line checks the same thing.

@CodingSelim CodingSelim left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one small wording tweak for the 7.2 note: i'd say debiased=False gets you the coefficient estimates, and inference is unavailable with one estimable period. i tried the example in #882 on main and on this branch. the workaround gives zero standard errors and p-values of 0 for both coefficients, and conf_int() puts both ends exactly on the estimate. that predates this PR, so a line in the release note would be enough here.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FamaMacBeth.fit() divides by zero and returns NaN covariance on single-period panels when debiased=True

3 participants