Repository navigation
Batch cluster score sums and their cross-product - #878
Conversation
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #878 +/- ##
==========================================
- Coverage 99.69% 99.69% -0.01%
==========================================
Files 202 202
Lines 38871 38866 -5
Branches 2501 2501
==========================================
- Hits 38752 38747 -5
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:
|
|
Thanks. LGTM. |
bashtage
left a comment
There was a problem hiding this comment.
Thanks, nice speed-up. I compared the covariance of every public path that uses cov_cluster with main (PooledOLS, PanelOLS, RandomEffects, IV2SLS, IVGMM and SUR, one- and two-way, string cluster ids): the largest relative difference is 2.3e-13. The two-way example in the description takes 23 ms instead of 486 ms here. LGTM.
…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.
The clustered covariance is the sum of the outer products of the cluster score totals, divided by the number of observations. After sorting the labels once, this change uses NumPy's segmented addition (
np.add.reduceat) to sum contiguous runs and a single matrix multiplication for all the outer products. The same group boundaries also supply the existing check for fewer than two groups. The regression test adds a singleton cluster to the independent summation check.I timed the complete public
PooledOLS(y, x).fit(cov_type="clustered", ...)call including model construction and the first evaluation of its lazy.covproperty. I also measured the first.covaccess separately. Median milliseconds from five alternating baseline/candidate blocks after warmup, Python 3.13.16, NumPy 2.5.5, pandas 3.1.1, arm64 macOS, one BLAS thread.I checked both versions against statsmodels OLS and its one- and two-way clustered sandwich estimators with corrections off. The largest absolute coefficient and covariance differences were 1.43e-14 and 6.08e-17. Floating-point summation order changes: both implementations can lose precision when within-cluster scores nearly cancel at the limit of float64.
Local full installed-package suite: 23,686 passed, 2,476 skipped, 47 xfailed, one failure. The same test fails at the same line on a separately built wheel from unmodified parent d7980a7:
test_constant_dependent_variable_panel[RandomEffects-0.1]divides 0 by 0 in the random-effects variance decomposition, before evaluating any clustered covariance.Minimal large fixture (time the entire body from model construction through first covariance access):