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.

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

1 participant