Skip to content

BUG: Do not depend on the labels in two-way clustered IV covariances - #864

Merged
bashtage merged 1 commit into
bashtage:mainfrom
echuq:iv-two-way-cluster-labels
Nov 6, 2026
Merged

bashtage merged 1 commit into
bashtage:mainfrom
echuq:iv-two-way-cluster-labels

Conversation

@echuq

@echuq echuq commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

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:

  • Float labels such as 0.0, 0.1, 0.2, ... are truncated by astype(int64). Distinct groups of the intersection are merged, and the covariance of IV2SLS, IVLIML and AbsorbingLS is silently wrong. In the new test, the standard errors are 0.0270/0.0457 instead of 0.0305/0.0496.
  • String labels raise UFuncTypeError (from min), and a DataFrame with a string column raises TypeError. 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:

  • For float, string and mixed-DataFrame labels, the covariance equals the covariance with integer labels and an independent pandas groupby implementation of (X'X)⁻¹(S_entity + S_time − S_entity,time)(X'X)⁻¹.
  • AbsorbingLS gives the same covariance with float labels as with integer labels.
  • All 4 tests fail on main (2be3f6c) and pass with the fix.

pytest linearmodels/tests/iv linearmodels/tests/system gives 12398 passed and 2400 skipped. ruff check, ruff format --check and the numpydoc lint are clean.

I used Claude (Anthropic) to find this, write the fix and the tests, and draft this description.

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

codecov Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.68%. Comparing base (2be3f6c) to head (09c19dd).

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           
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.

@bashtage

bashtage commented Nov 6, 2026

Copy link
Copy Markdown
Owner

Thanks

@bashtage
bashtage merged commit 1b4d429 into bashtage:main Nov 6, 2026
15 checks passed
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
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