Repository navigation
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
Open
DresdenGman wants to merge 2 commits into
DresdenGman wants to merge 2 commits into
Conversation
…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 Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was wrong
Two cases of issue #823, both in
IVModel._estimate_kappa:[1.002657 1.002214 1.004278 1.004001 1.000129 1.001426 1.005058 1.00002], seed-dependent noise instead of an error.method="pinv"and 2 or more endogenous regressors, rescaling one endogenous regressor by 1e6 moved kappa from1.03241615825324to1.0325121627636185(relative change 9.3e-5; the issue reports up to 2.6e-4). Theqrpath was already invariant.What this does
W = [y, endog]is flagged if||M_Z w|| / ||M_1 w|| <= sqrt(eps)(~1.49e-8), whereM_Zprojects out[exog, instruments]andM_1projects outexogonly. 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 raisesLinAlgError. The existing error flow then applies unchanged:IVLIML.fitraisesValueError("Unable to estimate kappa...");IV2SLS/IVJIVEfit with_liml_kappaNaN and invalid Anderson-Rubin/Basmann tests; a user-givenkappastill fits.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. Theqrpath is unchanged. Kappa now agrees with the reference generalized eigenvalue (scipy.linalg.eighon independently computed projections) to ~1e-15 at every tested scale (y, either endogenous regressor, or both, by 1e6 and 1e-6).[exog, instruments], is not changed: opt-inmethod="qr"already covers it (issue ENH: Add an opt-in QR solver to IV2SLS and IVLIML #870), andtest_unscaled_rank_check_depends_on_the_unitspins the defaultpinvbehaviour.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 forIVLIML,IV2SLSandIVJIVE(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_beforestill 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_havebuilt its endogenous regressor asz.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 given0.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 themethod="pinv"sentence now points at it.Verification
linearmodels/tests/iv/suite: 9924 passed, 2400 skipped, 0 failed — identical to the baseline run on the unpatched tree. Zero new failures.Closes #823 (cases 1 and 2; case 3 is covered by
method="qr", issue #870).