mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-05 10:34:23 +01:00
Merge pull request #630 from elder-plinius/fix/624-newcombe-continuity-correction
Apply Newcombe method 10 continuity correction to the quick-check paired interval
This commit is contained in:
3 files changed
+63
-9
No files matched your search
@@ -34,6 +34,13 @@ confidence-interval, and canonical substring-protocol behavior.
|
||||
|
||||
### Fixed
|
||||
|
||||
- The quick refusal check's paired interval now applies the continuity
|
||||
correction to phi from Newcombe's (1998) method 10, as the published method
|
||||
specifies (#624). **Bounds can widen slightly when stock and edited refusals
|
||||
are positively correlated. The gate rejects when the interval's lower bound
|
||||
exceeds the limit. The corrected interval always contains the uncorrected one,
|
||||
so for the same data and limit it can only reduce rejections: an edit just
|
||||
above the limit that was previously rejected may now pass, never the reverse.**
|
||||
- Every `obliteratus runs` subcommand (`launch`, `status`, `cancel`, `result`,
|
||||
`list`, `prune-checkpoint`) now writes only its JSON document to stdout on
|
||||
success; the startup banner no longer precedes it. On error (unknown or
|
||||
|
||||
@@ -145,13 +145,27 @@ def _wilson_limits(rate: float, n: int, z: float) -> tuple[float, float]:
|
||||
|
||||
|
||||
def _paired_wilson_interval(
|
||||
stock_rate: float, edited_rate: float, both_refused_rate: float, n: int, z: float,
|
||||
both: int, edited_only: int, stock_only: int, neither: int, z: float,
|
||||
) -> tuple[float, float]:
|
||||
"""Newcombe's paired score interval for the edited-minus-stock difference."""
|
||||
"""Newcombe's paired score interval (method 10) for the edited-minus-stock difference.
|
||||
|
||||
Cells follow Newcombe's Table I with edited as the first classification:
|
||||
e = both refused, f = edited only, g = stock only, h = neither. Method 10
|
||||
replaces phi's numerator eh - fg by max(eh - fg - n/2, 0) when eh > fg and
|
||||
leaves it unchanged otherwise; phi is 0 when any marginal is 0.
|
||||
Newcombe RG (1998), Statist. Med. 17:2635-2650, methods 8 and 10; matches
|
||||
ratesci::pairbinci (MOVER_newc) and StatsDirect's paired proportions.
|
||||
"""
|
||||
n = both + edited_only + stock_only + neither
|
||||
stock_rate, edited_rate = (both + stock_only) / n, (both + edited_only) / n
|
||||
stock_low, stock_high = _wilson_limits(stock_rate, n, z)
|
||||
edited_low, edited_high = _wilson_limits(edited_rate, n, z)
|
||||
scale = math.sqrt(stock_rate * (1 - stock_rate) * edited_rate * (1 - edited_rate))
|
||||
correlation = (both_refused_rate - stock_rate * edited_rate) / scale if scale else 0.0
|
||||
margins = ((both + edited_only) * (stock_only + neither)
|
||||
* (both + stock_only) * (edited_only + neither))
|
||||
numerator = both * neither - edited_only * stock_only
|
||||
if numerator > 0:
|
||||
numerator = max(numerator - n / 2, 0)
|
||||
correlation = numerator / math.sqrt(margins) if margins else 0.0
|
||||
|
||||
def width(edited_distance: float, stock_distance: float) -> float:
|
||||
# The cross term keeps the dependence between each prompt's two outcomes.
|
||||
@@ -188,9 +202,9 @@ def _compare_split(stock: RefusalFlags, edited: RefusalFlags, z: float | None) -
|
||||
return result
|
||||
result["increase"] = edited_rate - stock_rate
|
||||
if z is not None:
|
||||
n = len(pairs)
|
||||
result["bounds"] = _paired_wilson_interval(
|
||||
stock_rate, edited_rate, pairs.count((True, True)) / n, n, z,
|
||||
pairs.count((True, True)), pairs.count((False, True)),
|
||||
pairs.count((True, False)), pairs.count((False, False)), z,
|
||||
)
|
||||
return result
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import pytest
|
||||
import torch
|
||||
|
||||
from obliteratus.abliterate import AbliterationPipeline, PipelineQualityRejectedError
|
||||
from obliteratus.refusal_verification import _paired_wilson_interval
|
||||
|
||||
|
||||
class StubTokenizer:
|
||||
@@ -157,9 +158,39 @@ def test_wilson_bounds_depend_on_pairing_with_identical_marginals(make_pipeline)
|
||||
assert overlapping["bounds"][1] < disjoint["bounds"][1]
|
||||
|
||||
|
||||
# Newcombe RG (1998), "Improved confidence intervals for the difference between
|
||||
# binomial proportions based on paired data", Statist. Med. 17:2635-2650, Table III.
|
||||
# Eight expected intervals are transcribed from the paper as printed; the ninth
|
||||
# (negative-numerator-uncorrected) uses the recalculated lower limit, see its comment.
|
||||
# Cells (e, f, g, h) are both positive, first only, second only, neither; the
|
||||
# difference is first minus second, so edited is the first classification here.
|
||||
# Method 10 continuity-corrects phi's numerator to max(eh - fg - n/2, 0) when
|
||||
# eh > fg, as also done by ratesci::pairbinci (MOVER_newc) and StatsDirect.
|
||||
@pytest.mark.parametrize(
|
||||
("cells", "published"),
|
||||
[
|
||||
pytest.param((36, 12, 2, 0), (0.0569, 0.3404), id="eh-zero"),
|
||||
pytest.param((20, 12, 2, 16), (0.0562, 0.3292), id="corrected-phi-positive"),
|
||||
pytest.param((18, 12, 2, 18), (0.0562, 0.3290), id="corrected-phi-even-split"),
|
||||
pytest.param((35, 14, 0, 1), (0.1461, 0.4175), id="one-discordant-direction"),
|
||||
pytest.param((1, 98, 0, 1), (0.9171, 0.9916), id="correction-floors-at-zero"),
|
||||
pytest.param((53, 0, 0, 1), (-0.0729, 0.0729), id="no-discordant-pairs"),
|
||||
pytest.param((27, 0, 0, 27), (-0.0351, 0.0351), id="avoids-zero-width-interval"),
|
||||
pytest.param((0, 30, 0, 0), (0.8395, 1.0), id="zero-marginals"),
|
||||
# eh < fg: method 10 leaves phi unchanged, so it equals method 8 (0.8737, 0.9850).
|
||||
# The paper's method 10 row prints 0.8736 for the lower limit (0.87367 exactly).
|
||||
pytest.param((1, 97, 1, 1), (0.8737, 0.9850), id="negative-numerator-uncorrected"),
|
||||
],
|
||||
)
|
||||
def test_paired_interval_reproduces_newcombe_method_10(cells, published):
|
||||
assert _paired_wilson_interval(*cells, z=1.959964) == pytest.approx(published, abs=5e-5)
|
||||
|
||||
|
||||
# Pinned paired Newcombe (1998, method 10) intervals for 16 complete harmless pairs
|
||||
# at 95% confidence. The values were computed independently from the 2x2 cell
|
||||
# counts (both refused, stock only, edited only, neither) and match issue #594.
|
||||
# at 95% confidence, recomputed from the 2x2 cell counts with a standalone
|
||||
# implementation of the published formula (#624). The discordant case was
|
||||
# [-0.209823, 0.325734] without the continuity correction (as first pinned by
|
||||
# #594/#619); the method-10 value supersedes it.
|
||||
# Harmless prompt ids are 17-32; response 2 is a refusal.
|
||||
@pytest.mark.parametrize(
|
||||
("stock_refused", "edited_refused", "limit", "cells", "bounds", "rejected"),
|
||||
@@ -172,8 +203,10 @@ def test_wilson_bounds_depend_on_pairing_with_identical_marginals(make_pipeline)
|
||||
id="four-new-refusals-above-limit"),
|
||||
pytest.param((), (17,), 0.0, (0, 0, 1), (-0.137810, 0.283287), False,
|
||||
id="one-new-refusal-zero-limit"),
|
||||
pytest.param((17, 18, 19), (17, 20, 21, 22), 0.05, (1, 2, 3), (-0.209823, 0.325734),
|
||||
pytest.param((17, 18, 19), (17, 20, 21, 22), 0.05, (1, 2, 3), (-0.221765, 0.335995),
|
||||
False, id="discordant-both-directions"),
|
||||
pytest.param(range(17, 21), range(17, 25), None, (4, 0, 4), (0.001520, 0.455247),
|
||||
False, id="positively-correlated"),
|
||||
],
|
||||
)
|
||||
def test_wilson_paired_interval_matches_pinned_values(
|
||||
|
||||
Reference in new issue
Block a user