Skip to content

BUG: Raise a ValueError if LinearFactorModelGMM has too few observations - #807

Merged
bashtage merged 1 commit into
mainfrom
lm71-fix-system-asset-pricing-small
Oct 13, 2026
Merged

bashtage merged 1 commit into
mainfrom
lm71-fix-system-asset-pricing-small

Conversation

@bashtage

Copy link
Copy Markdown
Owner

LinearFactorModelGMM has nportfolio * (nfactor + 1) + nfactor moment conditions. Their covariance weights the moment conditions, and it is singular unless there are more observations than moment conditions. The estimator then raised "LinAlgError: Singular matrix", or returned a J-statistic that was NaN or arbitrary, such as -4.6e35 with 5 portfolios, 2 factors and 6 observations. It also raised a bare ZeroDivisionError with as many observations as factors plus one.

The model now raises a ValueError when it is created if the number of observations, after the observations with missing values are dropped, does not exceed the number of moment conditions. The message gives both numbers and the way the number of moment conditions is computed. With the default centering of the moment conditions the covariance is singular for as many observations as moment conditions, and it is not for one more, which is the boundary that the tests check, with and without an estimated risk-free rate. Fewer observations were never enough, and more observations than this are not changed.

The docstring of the model gives the number of moment conditions and the requirement.

LinearFactorModelGMM has nportfolio * (nfactor + 1) + nfactor moment conditions. Their
covariance weights the moment conditions, and it is singular unless there are more
observations than moment conditions. The estimator then raised "LinAlgError: Singular
matrix", or returned a J-statistic that was NaN or arbitrary, such as -4.6e35 with 5
portfolios, 2 factors and 6 observations. It also raised a bare ZeroDivisionError with
as many observations as factors plus one.

The model now raises a ValueError when it is created if the number of observations,
after the observations with missing values are dropped, does not exceed the number of
moment conditions. The message gives both numbers and the way the number of moment
conditions is computed. With the default centering of the moment conditions the
covariance is singular for as many observations as moment conditions, and it is not
for one more, which is the boundary that the tests check, with and without an
estimated risk-free rate. Fewer observations were never enough, and more observations
than this are not changed.

The docstring of the model gives the number of moment conditions and the requirement.
@codecov

codecov Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.66%. Comparing base (2366ab2) to head (d205193).
⚠️ Report is 75 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #807      +/-   ##
==========================================
+ Coverage   99.62%   99.66%   +0.03%     
==========================================
  Files         143      161      +18     
  Lines       28244    30653    +2409     
  Branches     1985     2099     +114     
==========================================
+ Hits        28138    30550    +2412     
+ Misses         41       40       -1     
+ Partials       65       63       -2     
Flag Coverage Δ
adder 99.65% <100.00%> (+0.04%) ⬆️
subtractor 99.65% <100.00%> (+0.04%) ⬆️

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 merged commit e037bba into main Oct 13, 2026
24 checks passed
@bashtage
bashtage deleted the lm71-fix-system-asset-pricing-small branch October 13, 2026 07:29
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.

1 participant