Skip to content

BUG: Test the rank of the IV inputs after scaling the columns - #850

Open
inchang-ing wants to merge 1 commit into
bashtage:mainfrom
inchang-ing:fix/iv-rank-scale-invariant
Open

inchang-ing wants to merge 1 commit into
bashtage:mainfrom
inchang-ing:fix/iv-rank-scale-invariant

Conversation

@inchang-ing

Copy link
Copy Markdown
Contributor

Addresses case 3 of #823.

The rank checks in _validate_inputs call matrix_rank on the raw regressor and instrument matrices. matrix_rank thresholds the singular values relative to the largest one, so columns on very different scales look rank deficient even when they are not:

z = rng.standard_normal((500, 2))
x = z @ [1.0, 1.0] + rng.standard_normal(500)
y = 1 + 0.5 * x + rng.standard_normal(500)

IV2SLS(y, np.ones((500, 1)), x, z * [1.0, 1e13]).fit()
# ValueError: instruments [exog instruments]  do not have full column rank

Change

Both rank checks now scale the columns to unit length first, so the result does not depend on the units of the variables. A column that is exactly 0 cannot be scaled and is still detected as linearly dependent.

This is only the third case of #823; the LIML kappa cases (1 and 2) are untouched, since they need the relative-residual test and the joint change to the scaling of kappa.

Tests

  • Added test_instr_rank_check_is_scale_invariant in tests/iv/test_model.py; it fits the model with an instrument scaled by 1e13 and compares the estimates with the unscaled ones.
  • linearmodels/tests/iv/: 9522 passed, 2400 skipped.
  • ruff check and numpydoc lint pass on the changed files.

@codecov

codecov Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.68%. Comparing base (89c3487) to head (cb1d80c).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #850   +/-   ##
=======================================
  Coverage   99.68%   99.68%           
=======================================
  Files         199      199           
  Lines       37851    37861   +10     
  Branches     2433     2433           
=======================================
+ Hits        37730    37742   +12     
+ Misses         53       51    -2     
  Partials       68       68           
Flag Coverage Δ
adder 99.67% <100.00%> (+<0.01%) ⬆️
subtractor 99.67% <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.

@inchang-ing

Copy link
Copy Markdown
Contributor Author

Gentle ping on this one — CI is green (15/15) and it is a small, self-contained bug fix. Would appreciate a review when you get a chance. Thanks!

@bashtage

Copy link
Copy Markdown
Owner

I'm not a fan of this. the regression is in the scale of the original columns, and if they are poorly scaled the numerics of the regression may be problematic. A rank check in the same scale as the regression should help protect the user from these numeric problems. What do you think?

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.

2 participants