Skip to content

LIML kappa: detect a regressor spanned by the instruments, and make kappa independent of the units (issue #823) - #881

Open
DresdenGman wants to merge 2 commits into
bashtage:mainfrom
DresdenGman:fix-823-liml-kappa
Open

DresdenGman wants to merge 2 commits into
bashtage:mainfrom
DresdenGman:fix-823-liml-kappa

Conversation

@DresdenGman

Copy link
Copy Markdown
Contributor

What was wrong

Two cases of issue #823, both in IVModel._estimate_kappa:

  1. A regressor spanned by the instruments. If an endogenous regressor (or the dependent variable) is exactly a linear combination of the exogenous regressors and instruments, its residuals on them are only rounding error, and so is kappa: on current main the kappas over 8 seeds were [1.002657 1.002214 1.004278 1.004001 1.000129 1.001426 1.005058 1.00002], seed-dependent noise instead of an error.
  2. Kappa depends on the units. With method="pinv" and 2 or more endogenous regressors, rescaling one endogenous regressor by 1e6 moved kappa from 1.03241615825324 to 1.0325121627636185 (relative change 9.3e-5; the issue reports up to 2.6e-4). The qr path was already invariant.

What this does

  • Detection. After the residuals are computed, a column of W = [y, endog] is flagged if ||M_Z w|| / ||M_1 w|| <= sqrt(eps) (~1.49e-8), where M_Z projects out [exog, instruments] and M_1 projects out exog only. The threshold is calibrated, not guessed: exact-span ratios are 3.4e-16–9.2e-16 (n = 60 and 500, columns scaled 1e6/1e-6), nearly-spanned ratios are 4.6e-9 (delta = 1e-8) and 4.6e-11 (delta = 1e-10), and the smallest genuine ratio over normal designs, small-coefficient-of-variation variables without a constant, and ill-conditioned instruments (cond ~5e8) is 0.49. Comparing against ||M_1 w|| rather than ||w|| is what keeps a small-coefficient-of-variation variable in a model without a constant from being flagged. A flagged column raises LinAlgError. The existing error flow then applies unchanged: IVLIML.fit raises ValueError("Unable to estimate kappa..."); IV2SLS/IVJIVE fit with _liml_kappa NaN and invalid Anderson-Rubin/Basmann tests; a user-given kappa still fits.
  • Scaling. With method="pinv", kappa is now computed from residuals scaled to have columns of unit length. Scaling the columns of the residuals is a congruence transformation of both cross products, so the generalized eigenvalues are unchanged mathematically and much better conditioned numerically. The qr path is unchanged. Kappa now agrees with the reference generalized eigenvalue (scipy.linalg.eigh on independently computed projections) to ~1e-15 at every tested scale (y, either endogenous regressor, or both, by 1e6 and 1e-6).
  • Out of scope (deliberate). Case 3 of the issue, the scale-dependent rank check of [exog, instruments], is not changed: opt-in method="qr" already covers it (issue ENH: Add an opt-in QR solver to IV2SLS and IVLIML #870), and test_unscaled_rank_check_depends_on_the_units pins the default pinv behaviour.

Tests

  • test_liml_kappa_singular.py: the two tests that pinned the old behaviour are replaced. The span test now asserts the raise / NaN / invalid-test behaviour for IVLIML, IV2SLS and IVJIVE (8 seeds) plus the given-kappa fit; the scales test now asserts kappa equals the reference generalized eigenvalue at every scale (rtol 1e-10). test_kappa_is_computed_as_before still passes unchanged — on designs without rescaling the scaled computation agrees with the previous one to rtol 1e-12.
  • test_absorbing_validation.py: test_cluster_message_names_only_what_the_models_have built its endogenous regressor as z.sum(axis=1) + x["x0"], an exact linear combination of the instruments and an exogenous regressor — case 1, by accident. Its purpose is the cluster-nobs error message, so the local regressor is given 0.01 * N(0,1) noise (local to that test only).
  • doc/source/changes/7.0.rst: new "Changes that alter results" bullet for IV: LIML kappa with an endogenous regressor spanned by the instruments, kappa's unit dependence, scale-dependent instrument rank check #823, and the method="pinv" sentence now points at it.

Verification

  • Full linearmodels/tests/iv/ suite: 9924 passed, 2400 skipped, 0 failed — identical to the baseline run on the unpatched tree. Zero new failures.
  • New tests verified red on the unpatched tree: 13 of the 18 new-test cases fail without the fix (the 5 that pass are scales where the old computation happened to be accurate).

Closes #823 (cases 1 and 2; case 3 is covered by method="qr", issue #870).

…ependent of the units

An endogenous regressor that is exactly a linear combination of the
exogenous regressors and instruments has residuals that are only rounding
error, and so is kappa. Detect it with ||M_Z w|| / ||M_1 w|| <= sqrt(eps)
and raise LinAlgError instead of returning the rounding error as kappa.
With method="pinv", compute kappa from residuals scaled to have columns
of unit length, so that it does not depend on the units of the variables.
Closes bashtage#823 (cases 1 and 2; case 3 is covered by method="qr", bashtage#870).
@codecov

codecov Bot commented Nov 24, 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 (c489245).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #881      +/-   ##
==========================================
- Coverage   99.69%   99.69%   -0.01%     
==========================================
  Files         202      202              
  Lines       38871    38897      +26     
  Branches     2501     2502       +1     
==========================================
+ Hits        38752    38777      +25     
  Misses         51       51              
- Partials       68       69       +1     
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.

IV: LIML kappa with an endogenous regressor spanned by the instruments, kappa's unit dependence, scale-dependent instrument rank check

1 participant