Repository navigation
BUG: Do not depend on the labels in two-way clustered IV covariances - #864
Merged
Merged
Conversation
The intersection of the two cluster variables was encoded as c0 * (max(c1) + 1) + c1 and truncated to int64. Labels that are not evenly spaced integers, for example 0.0, 0.1, 0.2, merged groups of the intersection, so that the covariance of IV2SLS, IVLIML and AbsorbingLS was silently wrong, and string labels raised. Use cluster_union, which the panel and system models use, so that two observations are in the same group of the intersection only if they share both labels. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01W74MiDikqd2RitwCNYw17E
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #864 +/- ##
=======================================
Coverage 99.68% 99.68%
=======================================
Files 200 201 +1
Lines 38076 38115 +39
Branches 2442 2442
=======================================
+ Hits 37955 37994 +39
Misses 51 51
Partials 70 70
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:
|
Owner
|
Thanks |
This was referenced Dec 3, 2026
bashtage
added a commit
to echuq/linearmodels
that referenced
this pull request
Dec 3, 2026
…ir contributors bashtage#864 fixed the two-way clustered covariance of the IV models for float and string cluster labels, which 7.1 got wrong or rejected, and bashtage#878 made the clustered covariances faster, but neither had a 7.2 note. The note of the negative bandwidth fix cites bashtage#887 as well as bashtage#886, and the contributors of the pull requests merged since the list was started are thanked: echuq, AryanRG, soodoku, WeiLiuu and leninworld.
soodoku
added a commit
to finite-sample/linearmodels
that referenced
this pull request
Dec 4, 2026
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.
The IV clustered covariance built the intersection of two cluster variables as
(c0 * (max(c1) + 1) + c1).astype(int64), after shifting each column to start at 1. Labels that are not evenly spaced integers break this encoding:0.0, 0.1, 0.2, ...are truncated byastype(int64). Distinct groups of the intersection are merged, and the covariance ofIV2SLS,IVLIMLandAbsorbingLSis silently wrong. In the new test, the standard errors are 0.0270/0.0457 instead of 0.0305/0.0496.UFuncTypeError(frommin), and a DataFrame with a string column raisesTypeError. One-way clustering accepts both.The fix uses
linearmodels.shared.covariance.cluster_union, which the panel and system covariances already use. With it, two observations are in the same group of the intersection only if they share both labels, whatever the labels are. Results do not change for integer labels.Tests
New
linearmodels/tests/iv/test_two_way_cluster_labels.py:groupbyimplementation of (X'X)⁻¹(S_entity + S_time − S_entity,time)(X'X)⁻¹.AbsorbingLSgives the same covariance with float labels as with integer labels.main(2be3f6c) and pass with the fix.pytest linearmodels/tests/iv linearmodels/tests/systemgives 12398 passed and 2400 skipped.ruff check,ruff format --checkand the numpydoc lint are clean.I used Claude (Anthropic) to find this, write the fix and the tests, and draft this description.