From 1b2af41ce6a024690a2531018d7ab9b1035f6e25 Mon Sep 17 00:00:00 2001 From: NaiveCoding <71413722+ReviveCoding@users.noreply.github.com> Date: Wed, 14 Oct 2026 10:20:42 -0400 Subject: [PATCH 1/6] Consolidate Platt log-odds clipping into a shared helper --- src/aix_page/calibration/calibrators.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/aix_page/calibration/calibrators.py b/src/aix_page/calibration/calibrators.py index 01510b9..dab0e76 100644 --- a/src/aix_page/calibration/calibrators.py +++ b/src/aix_page/calibration/calibrators.py @@ -17,19 +17,27 @@ def pseudo_rows( return x[keep], y[keep], w[keep] +def _logit(probabilities: np.ndarray) -> np.ndarray: + """Apply the historical probability clipping contract before taking log odds.""" + return np.log( + np.clip(probabilities, 1e-8, 1 - 1e-8) + / np.clip(1 - probabilities, 1e-8, 1) + ) + + class Platt: def __init__(self) -> None: self.model = LogisticRegression(C=1e6) def fit(self, click: np.ndarray, impression: np.ndarray, p: np.ndarray) -> Platt: x, y, w = pseudo_rows( - click, impression, np.log(np.clip(p, 1e-8, 1 - 1e-8) / np.clip(1 - p, 1e-8, 1)) + click, impression, _logit(p) ) self.model.fit(x, y, sample_weight=w) return self def predict(self, p: np.ndarray) -> np.ndarray: - logit = np.log(np.clip(p, 1e-8, 1 - 1e-8) / np.clip(1 - p, 1e-8, 1)) + logit = _logit(p) return np.asarray(self.model.predict_proba(logit[:, None])[:, 1], dtype=np.float64) From d7fef33c0257ece2025aa78ad7461c53a105214f Mon Sep 17 00:00:00 2001 From: NaiveCoding <71413722+ReviveCoding@users.noreply.github.com> Date: Wed, 14 Oct 2026 10:20:45 -0400 Subject: [PATCH 2/6] Test unchanged Platt clipping and calibrated pseudo-row weights --- tests/unit/test_calibration_polish.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 tests/unit/test_calibration_polish.py diff --git a/tests/unit/test_calibration_polish.py b/tests/unit/test_calibration_polish.py new file mode 100644 index 0000000..60ac2fa --- /dev/null +++ b/tests/unit/test_calibration_polish.py @@ -0,0 +1,25 @@ +"""Confirm Platt log-odds clipping remains numerically identical.""" + +import numpy as np + +from aix_page.calibration.calibrators import _logit, pseudo_rows + + +def test_logit_reuses_exact_historical_clipping_formula(): + probabilities = np.array([0.0, 1e-9, 0.1, 0.5, 0.9, 1.0]) + expected = np.log( + np.clip(probabilities, 1e-8, 1 - 1e-8) + / np.clip(1 - probabilities, 1e-8, 1) + ) + np.testing.assert_array_equal(_logit(probabilities), expected) + + +def test_pseudo_rows_keep_positive_and_negative_weights(): + x, labels, weights = pseudo_rows( + np.array([2.0, 0.0]), + np.array([10.0, 5.0]), + np.array([0.2, 0.8]), + ) + assert x.shape == (3, 1) + assert labels.tolist() == [1.0, 0.0, 0.0] + assert weights.tolist() == [2.0, 8.0, 5.0] From 9c33ad6da3110d3cf46620841a5f52ffa7e80f80 Mon Sep 17 00:00:00 2001 From: NaiveCoding <71413722+ReviveCoding@users.noreply.github.com> Date: Thu, 15 Oct 2026 20:54:30 -0400 Subject: [PATCH 3/6] Apply exact Ruff formatting fixes from CI diff --- src/aix_page/calibration/calibrators.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/aix_page/calibration/calibrators.py b/src/aix_page/calibration/calibrators.py index dab0e76..12a633d 100644 --- a/src/aix_page/calibration/calibrators.py +++ b/src/aix_page/calibration/calibrators.py @@ -19,10 +19,7 @@ def pseudo_rows( def _logit(probabilities: np.ndarray) -> np.ndarray: """Apply the historical probability clipping contract before taking log odds.""" - return np.log( - np.clip(probabilities, 1e-8, 1 - 1e-8) - / np.clip(1 - probabilities, 1e-8, 1) - ) + return np.log(np.clip(probabilities, 1e-8, 1 - 1e-8) / np.clip(1 - probabilities, 1e-8, 1)) class Platt: @@ -30,9 +27,7 @@ def __init__(self) -> None: self.model = LogisticRegression(C=1e6) def fit(self, click: np.ndarray, impression: np.ndarray, p: np.ndarray) -> Platt: - x, y, w = pseudo_rows( - click, impression, _logit(p) - ) + x, y, w = pseudo_rows(click, impression, _logit(p)) self.model.fit(x, y, sample_weight=w) return self From dcdd0215840d192e6f333f5c20e9436130e08ba4 Mon Sep 17 00:00:00 2001 From: NaiveCoding <71413722+ReviveCoding@users.noreply.github.com> Date: Thu, 15 Oct 2026 20:54:32 -0400 Subject: [PATCH 4/6] Apply exact Ruff formatting fixes from CI diff --- tests/unit/test_calibration_polish.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/unit/test_calibration_polish.py b/tests/unit/test_calibration_polish.py index 60ac2fa..ec43bdf 100644 --- a/tests/unit/test_calibration_polish.py +++ b/tests/unit/test_calibration_polish.py @@ -7,10 +7,7 @@ def test_logit_reuses_exact_historical_clipping_formula(): probabilities = np.array([0.0, 1e-9, 0.1, 0.5, 0.9, 1.0]) - expected = np.log( - np.clip(probabilities, 1e-8, 1 - 1e-8) - / np.clip(1 - probabilities, 1e-8, 1) - ) + expected = np.log(np.clip(probabilities, 1e-8, 1 - 1e-8) / np.clip(1 - probabilities, 1e-8, 1)) np.testing.assert_array_equal(_logit(probabilities), expected) From c3816e713e3e21f511bb73a9ab1b2a62eccd8da6 Mon Sep 17 00:00:00 2001 From: NaiveCoding <71413722+ReviveCoding@users.noreply.github.com> Date: Thu, 15 Oct 2026 20:56:05 -0400 Subject: [PATCH 5/6] Preserve logit ndarray type under strict mypy --- src/aix_page/calibration/calibrators.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aix_page/calibration/calibrators.py b/src/aix_page/calibration/calibrators.py index 12a633d..2d66709 100644 --- a/src/aix_page/calibration/calibrators.py +++ b/src/aix_page/calibration/calibrators.py @@ -19,7 +19,7 @@ def pseudo_rows( def _logit(probabilities: np.ndarray) -> np.ndarray: """Apply the historical probability clipping contract before taking log odds.""" - return np.log(np.clip(probabilities, 1e-8, 1 - 1e-8) / np.clip(1 - probabilities, 1e-8, 1)) + return np.asarray(np.log(np.clip(probabilities, 1e-8, 1 - 1e-8) / np.clip(1 - probabilities, 1e-8, 1))) class Platt: From 4d05dccdeeeaf40665df65baea7f4a489a63b7d5 Mon Sep 17 00:00:00 2001 From: NaiveCoding <71413722+ReviveCoding@users.noreply.github.com> Date: Thu, 15 Oct 2026 20:56:31 -0400 Subject: [PATCH 6/6] Align strict-typed logit with Ruff line width --- src/aix_page/calibration/calibrators.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/aix_page/calibration/calibrators.py b/src/aix_page/calibration/calibrators.py index 2d66709..15004c7 100644 --- a/src/aix_page/calibration/calibrators.py +++ b/src/aix_page/calibration/calibrators.py @@ -19,7 +19,9 @@ def pseudo_rows( def _logit(probabilities: np.ndarray) -> np.ndarray: """Apply the historical probability clipping contract before taking log odds.""" - return np.asarray(np.log(np.clip(probabilities, 1e-8, 1 - 1e-8) / np.clip(1 - probabilities, 1e-8, 1))) + return np.asarray( + np.log(np.clip(probabilities, 1e-8, 1 - 1e-8) / np.clip(1 - probabilities, 1e-8, 1)) + ) class Platt: