Fix D6: match Clojure two-proportion test formula (+1 pseudocount)#2520
Open
jucor wants to merge 1 commit into
Open
Fix D6: match Clojure two-proportion test formula (+1 pseudocount)#2520jucor wants to merge 1 commit into
jucor wants to merge 1 commit into
Conversation
This was referenced Mar 30, 2026
f593bae to
cd39374
Compare
be320d5 to
c30ab91
Compare
ballPointPenguin
approved these changes
Apr 26, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates Delphi’s representativeness z-score computation to match the Clojure stats/two-prop-test behavior by switching two_prop_test (and its vectorized variant) from proportion inputs to raw count inputs and applying a +1 pseudocount to all four parameters.
Changes:
- Changed
two_prop_test/two_prop_test_vectorizedsignatures to accept raw counts(succ_in, succ_out, pop_in, pop_out)and implemented the +1 pseudocount pooled z-test formula. - Updated scalar and vectorized call sites to pass raw counts (
na/nd/nsand “other” counts) instead of proportions. - Updated and expanded unit + discrepancy tests and documentation to reflect the new signature and intended Clojure parity.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
delphi/polismath/pca_kmeans_rep/repness.py |
Implements the new count-based two-proportion test (scalar + vectorized) and updates callers. |
delphi/tests/test_repness_unit.py |
Updates unit tests for the new two_prop_test / vectorized signature and expectations. |
delphi/tests/test_old_format_repness.py |
Updates old-format API tests for the new two_prop_test signature. |
delphi/tests/test_discrepancy_fixes.py |
Rewrites D6 tests to assert parity with a Clojure-reference implementation and adds blob-injection coverage. |
delphi/docs/PLAN_DISCREPANCY_FIXES.md |
Marks D6 as done in the discrepancy plan. |
delphi/docs/CLJ-PARITY-FIXES-JOURNAL.md |
Adds a journal entry documenting the D6 parity work and test updates. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+121
to
122
| if pop_in == 0 or pop_out == 0: | ||
| return 0.0 |
Comment on lines
+546
to
+547
| # Handle edge cases: pop_in=0 or pop_out=0 → 0, pi_hat=1 → 0 | ||
| z = z.where((pop_in > 0) & (pop_out > 0), 0.0) |
Comment on lines
+73
to
+75
| # Edge cases: pop=0 → 0 | ||
| assert two_prop_test(5, 5, 0, 100) == 0.0 | ||
| assert two_prop_test(5, 5, 100, 0) == 0.0 |
Comment on lines
+67
to
71
| # Edge cases: pop=0 → 0 | ||
| assert two_prop_test(5, 5, 0, 100) == 0.0 | ||
| assert two_prop_test(5, 5, 100, 0) == 0.0 | ||
|
|
||
|
|
Comment on lines
+835
to
+841
| """Edge cases: n=0 should return 0, pi_hat=1 should return 0.""" | ||
| # n=0 cases | ||
| check.equal(two_prop_test(5, 5, 0, 10), 0.0) | ||
| check.equal(two_prop_test(5, 5, 10, 0), 0.0) | ||
| # Both zero | ||
| check.equal(two_prop_test(0, 0, 0, 0), 0.0) | ||
|
|
## Summary The Python `two_prop_test` used a standard two-proportion z-test with no pseudocounts, while Clojure's `stats/two-prop-test` (stats.clj:18-33) adds +1 to all four inputs (`succ-in`, `succ-out`, `pop-in`, `pop-out`) via `(map inc ...)` before computing the pooled z-test. This Laplace smoothing regularizes z-scores for small group sizes, which are common in Polis conversations. ## Changes - **Signature change**: `two_prop_test(p1, n1, p2, n2)` (proportions) → `two_prop_test(succ_in, succ_out, pop_in, pop_out)` (raw counts) - **Formula**: Standard pooled z-test on pseudocount-adjusted values: `pi1 = (succ_in+1)/(pop_in+1)`, `pi_hat = (s1+s2)/(p1+p2)` - **Callers updated**: Both scalar (`add_comparative_stats`) and vectorized (`compute_group_comment_stats_df`) now pass raw counts matching Clojure's `(stats/two-prop-test (:na in-stats) (sum :na rest-stats) (:ns in-stats) (sum :ns rest-stats))` (repness.clj:97-100) ## Affected output fields - `rat` (agree representativeness test z-score) - `rdt` (disagree representativeness test z-score) - `agree_metric`, `disagree_metric` (downstream of rat/rdt) ## Test plan - [x] Targeted D6 tests pass (formula, edge cases, regularization effect) - [x] Full test suite passes (excluding DynamoDB/MinIO tests) - [x] Private dataset tests pass (--include-local) - [x] Golden snapshots re-recorded for all 7 datasets 🤖 Generated with [Claude Code](https://claude.com/claude-code) ## Squashed commits - RED: add D6 blob injection test (two_prop_test vs Clojure repness-test) - Fix D6: match Clojure two-proportion test formula (+1 pseudocount) - Plan: add D6 PR number and stack position to cross-reference commit-id:23c03d70
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
The Python
two_prop_testused a standard two-proportion z-test with no pseudocounts,while Clojure's
stats/two-prop-test(stats.clj:18-33) adds +1 to all four inputs(
succ-in,succ-out,pop-in,pop-out) via(map inc ...)before computingthe pooled z-test. This Laplace smoothing regularizes z-scores for small group sizes,
which are common in Polis conversations.
Changes
two_prop_test(p1, n1, p2, n2)(proportions) →two_prop_test(succ_in, succ_out, pop_in, pop_out)(raw counts)pi1 = (succ_in+1)/(pop_in+1),pi_hat = (s1+s2)/(p1+p2)add_comparative_stats) and vectorized(
compute_group_comment_stats_df) now pass raw counts matching Clojure's(stats/two-prop-test (:na in-stats) (sum :na rest-stats) (:ns in-stats) (sum :ns rest-stats))(repness.clj:97-100)
Affected output fields
rat(agree representativeness test z-score)rdt(disagree representativeness test z-score)agree_metric,disagree_metric(downstream of rat/rdt)Test plan
🤖 Generated with Claude Code
Squashed commits
commit-id:23c03d70
Stack: