Repository navigation
BUG: Do not materialize weighted data when all weights are 1 - #862
Open
DresdenGman wants to merge 2 commits into
Open
DresdenGman wants to merge 2 commits into
DresdenGman wants to merge 2 commits into
Conversation
The IV models always stored copies of the dependent variable, the regressors and the first-stage regressors scaled by the square root of the weights, even when all of the weights are 1, which is the case when weights is None or the weights are constant. Scaling by 1 does not change the data, so the weighted data is now the data in this case. This removes three full-size copies, which could require tens of GiB for the large data sets in GH 607, where constructing the weighted regressors raised a MemoryError. Estimation results are unchanged: multiplication by a square root weight of exactly 1 is exact, and the new tests verify that results with no weights or constant weights are identical, that non-constant weights still materialize the scaled data, and that float32 inputs with float32 weights agree with the float64 estimates.
Add tests requested in review of the GH 607 fix: float32 data with all-one float32 weights and integer data with constant integer weights are fitted and agree with the float64 estimates; the unmaterialized path agrees with a reference computed from explicitly materialized weighted data; a single non-one weight materializes; and a NaN weight drops its observation, as before.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #862 +/- ##
========================================
Coverage 99.68% 99.68%
========================================
Files 199 199
Lines 37875 37998 +123
Branches 2436 2437 +1
========================================
+ Hits 37756 37879 +123
Misses 51 51
Partials 68 68
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.
Closes #607
Problem
In
IV2SLS(and the other IV estimators sharing the same base class), the weighted arrays_wy,_wx, and_wzwere always constructed by multiplying the input data by the square root of the weights. That multiplication promotes the arrays tofloat64and allocates full-size copies, even when every weight is1and the weighted data is identical to the input — including the default unweighted case, where the weights are populated with1.0internally. For large low-dtype inputs, this causes the memory blow-up reported in #607: full-sizefloat64copies are materialized unconditionally before estimation begins.Change
linearmodels/iv/model.py: when all weights are1,_wy,_wx, and_wznow alias the original arrays instead of allocating multipliedfloat64copies, preserving input dtypes and avoiding the extra full-size allocations. (Weights are normalized to have mean1.0before these arrays are built, so any constant weight vector reaches this check as all ones.) Any non-constant weight vector takes the existing materializing path unchanged.Multiplying by a weight of exactly
1is exact, so forfloat64and integer inputs the estimation results are unchanged. One deliberate consequence:float32inputs now stay infloat32through the weighting step instead of being silently promoted tofloat64, so afloat32fit agrees with thefloat64fit of the same data withinfloat32precision rather than exactly (the tests below use rtol1e-6for parameters and1e-5for the covariance).Scope note: #607 also mentioned
PanelOLS.PanelOLSdoes not unconditionally materialize weighted copies — it already branches onnp.any(weights != 1.0)to take a fast path for unweighted data (the same idiom used here) — so this PR is scoped to the IV models, where the unconditional copy occurred.Tests
9 new test functions in
linearmodels/tests/iv/test_model.py:float32fit with all-onesfloat32weights matches thefloat64fit of the same data within the tolerances above; an integer-input fit with constant integer weights matches thefloat64results exactly.float64arrays usingnumpy.linalg.pinvagrees with the new path to1e-8(measured deviation~1e-13).NaNweights follow the existing missing-value contract (the observation is dropped, verified against a manual row deletion).Verification
IV test suite (same command before and after): baseline
9521 passed / 2400 skipped / 0 failed→ with this change9530 passed / 2400 skipped / 0 failed. The delta is exactly the 9 new test functions; the affected subset (test_model,test_weight_config,test_data,test_gmm) is140 passed.Panel suite re-run:
3680 passed(PanelOLScode untouched).An A/B comparison of 20 result items between unweighted and all-ones-weighted runs of the new code is bit-identical; equivalence to the previous materialized path is covered by the independent-reference test above.
Peak memory on the reproduction case:
21.2 MiB→10.7 MiB.Known limitation (performance only): weights that are constant only up to floating-point rounding (within 1 ulp of a constant after normalization) still take the materializing path. Results are correct; the shortcut is simply not taken.