Vectorize participant info computation (3-15x speedup)#2516
Open
jucor wants to merge 1 commit into
Open
Conversation
This was referenced Mar 30, 2026
750e32f to
07c707d
Compare
510205d to
b1ebec4
Compare
e046e08 to
b982198
Compare
b1ebec4 to
8637399
Compare
ballPointPenguin
approved these changes
Apr 26, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Vectorizes _compute_participant_info_optimized in Conversation by replacing the per-participant Python loop with bulk NumPy operations: matrix-wide np.sum for vote counts and a per-group P @ g matmul for Pearson correlation, yielding 3–15x speedup. Removes the now-unused legacy participant_stats() function from repness.py and updates all callers/imports. Adds a comprehensive unit test file and an A/B benchmark script.
Changes:
- Replace per-participant loop with vectorized vote counting and matmul-based Pearson correlation in
_compute_participant_info_optimized - Delete dead
participant_stats()fromrepness.py; update imports inconversation.py,pca_kmeans_rep/__init__.py,notebooks/run_analysis.py, and 4 test files to call the optimized method instead - Add
tests/test_participant_info.py(31 tests covering vote counts, group correlations, edge cases, real-data equivalence, golden snapshot regression) andscripts/benchmark_participant_info.py
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| delphi/polismath/conversation/conversation.py | Vectorize participant info using np.sum and P @ g Pearson formula |
| delphi/polismath/pca_kmeans_rep/repness.py | Delete dead participant_stats(); clarify PSEUDO_COUNT (MAP) comment |
| delphi/polismath/pca_kmeans_rep/init.py | Drop participant_stats from public exports |
| delphi/notebooks/run_analysis.py | Remove unused participant_stats import |
| delphi/tests/test_participant_info.py | New comprehensive unit + golden + real-data equivalence tests |
| delphi/tests/test_repness_unit.py | Switch test_participant_stats to call optimized method; centralize PSEUDO_COUNT import |
| delphi/tests/test_old_format_repness.py | Same switch and import centralization |
| delphi/tests/test_repness_smoke.py | Replace participant_stats call with _compute_participant_info_optimized |
| delphi/tests/test_legacy_repness_comparison.py | Drop unused participant_stats import |
| delphi/tests/simplified_repness_test.py | Import PSEUDO_COUNT from repness instead of hardcoding |
| delphi/scripts/benchmark_participant_info.py | New A/B benchmark of old loop vs vectorized impl |
| delphi/docs/PLAN_DISCREPANCY_FIXES.md | Add stack cross-reference table and GitHub PR numbers |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
## Summary - Replaces the O(N×G×C) per-participant Python loop in `_compute_participant_info_optimized` with bulk NumPy operations: matrix-wide vote counting (`np.sum` over axis) and per-group Pearson correlation via `P @ g` matrix multiply - Adds 31 unit tests covering vote counts, group correlations, edge cases (small groups, zero-std, NaN handling, missing members), and golden snapshot regression - Correlations now return Python `float` instead of `numpy.float64` - Includes a benchmark script (`scripts/benchmark_participant_info.py`) that runs old vs new on the same data ## Benchmark results Measured on real datasets (5 runs, median), old per-participant loop vs new vectorized: | Dataset | Size | Old | New | Speedup | |---------|------|-----|-----|---------| | vw | 69p × 125c × 4g | 0.011s | 0.001s | **14.6x** | | biodiversity | 536p × 314c × 2g | 0.047s | 0.006s | **8.1x** | | _(larger private datasets)_ | | | | **3–6x** | Speedup is higher on smaller datasets (loop overhead dominates) and lower on very large ones (matrix materialization dominates). Overall **3–15x** depending on size. ## Test plan - [x] 31 unit tests pass (pre-vectorization baseline established first, then re-run post) - [x] Golden snapshot regression passes for biodiversity + vw - [x] Full regression test suite passes (40/40) - [x] Benchmark run on all datasets including private (results above) - [x] Max correlation diff across all datasets: < 2e-15 🤖 Generated with [Claude Code](https://claude.com/claude-code) ## Squashed commits - Address Copilot review: clarify MAP estimate, centralize PSEUDO_COUNT imports - Update plan: add stack cross-reference and GitHub PR numbers - Vectorize _compute_participant_info_optimized for ~100x speedup - Add benchmark script for participant info vectorization - Address Copilot review: drop unused cache, validate --runs, tighten type assert - Remove old participant_stats() in favor of vectorized replacement commit-id:ea747196
Delphi Coverage Report
|
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.
Summary
_compute_participant_info_optimizedwith bulk NumPy operations: matrix-wide vote counting (np.sumover axis) and per-group Pearson correlation viaP @ gmatrix multiplyfloatinstead ofnumpy.float64scripts/benchmark_participant_info.py) that runs old vs new on the same dataBenchmark results
Measured on real datasets (5 runs, median), old per-participant loop vs new vectorized:
Speedup is higher on smaller datasets (loop overhead dominates) and lower on very large ones (matrix materialization dominates). Overall 3–15x depending on size.
Test plan
🤖 Generated with Claude Code
Squashed commits
commit-id:ea747196
Stack: