Source code for rank_preserving_calibration._types

"""Public result types and exception for rank-preserving marginal calibration."""

from __future__ import annotations

from dataclasses import dataclass
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    import numpy as np


[docs] class CalibrationError(Exception): """Raised when inputs are invalid or a calibration solve fails."""
[docs] @dataclass(frozen=True, slots=True) class ReversalExample: """One within-class pair whose ordering IPF reverses. Attributes: class_idx: Zero-based class column index. higher_idx: Row index that started with the higher probability. lower_idx: Row index that started with the lower probability. p_higher: Initial probability of ``higher_idx`` for ``class_idx``. p_lower: Initial probability of ``lower_idx`` for ``class_idx``. q_higher: Calibrated probability of ``higher_idx`` after IPF. q_lower: Calibrated probability of ``lower_idx`` after IPF. gap: Magnitude of the reversal, ``q_lower - q_higher > 0``. """ class_idx: int higher_idx: int lower_idx: int p_higher: float p_lower: float q_higher: float q_lower: float gap: float
[docs] @dataclass(slots=True) class CalibrationResult: """Output of :func:`~rank_preserving_calibration.calibrate`. Attributes: probs: Calibrated probability matrix of shape ``(N, J)``. converged: True when simplex and marginal tolerances are met. kl_divergence: Mean weighted KL divergence per unit from ``reference``. max_row_error: Maximum absolute row-sum error ``|sum_j q_ij - 1|``. max_marginal_error: Maximum absolute class-share error ``|sum_i (w_i / W) q_ij - target_shares_j|``. max_rank_violation: Largest weak-order violation across all columns. l1_rank_violation: Total L1 weak-order hinge violation across columns. rank_preservation_pct: Percentage of strictly ordered anchor pairs whose weak ordering survives calibration. iterations: Solver iteration count. """ probs: np.ndarray converged: bool kl_divergence: float max_row_error: float max_marginal_error: float max_rank_violation: float l1_rank_violation: float rank_preservation_pct: float iterations: int
[docs] @dataclass(slots=True) class DiagnosisResult: """Output of :func:`~rank_preserving_calibration.diagnose`. Attributes: ipf_probs: Unconstrained IPF probability matrix of shape ``(N, J)``. multipliers: Positive IPF column multipliers normalized to unit geometric mean across positive-target classes. l1_shift: L1 distance between empirical and target class shares in ``[0, 2]``. max_rank_violation: Largest within-class reversal induced by IPF. l1_rank_violation: Total L1 weak-order hinge violation under IPF. rank_preservation_pct: Percentage of strictly ordered pairs kept by IPF. n_reversed_pairs: Count of strictly ordered pairs reversed by IPF. worst_reversals: Up to ``top_k`` largest reversed pairs. """ ipf_probs: np.ndarray multipliers: np.ndarray l1_shift: float max_rank_violation: float l1_rank_violation: float rank_preservation_pct: float n_reversed_pairs: int worst_reversals: list[ReversalExample]
[docs] @dataclass(slots=True) class ParetoResult: """Output of :func:`~rank_preserving_calibration.pareto`. Attributes: lambdas: Sorted rank-penalty values swept (may include ``0.0`` and ``inf``). probs: Calibrated probability matrix for each ``lambda``. kl_divergences: Mean weighted KL divergence for each solution. l1_rank_violations: Total L1 weak-order violation for each solution. max_rank_violations: Maximum weak-order violation for each solution. rank_preservation_pcts: Pairwise rank preservation percentage for each solution. """ lambdas: list[float] probs: list[np.ndarray] kl_divergences: list[float] l1_rank_violations: list[float] max_rank_violations: list[float] rank_preservation_pcts: list[float]
[docs] @dataclass(slots=True) class EvaluationResult: """Output of :func:`~rank_preserving_calibration.evaluate`. Attributes: max_row_error: Maximum absolute row-sum error. max_marginal_error: Maximum absolute class-share error against ``target_shares``. kl_divergence: Mean weighted KL divergence from ``reference``. max_rank_violation: Largest weak-order violation against ``anchor``. l1_rank_violation: Total L1 weak-order violation against ``anchor``. rank_preservation_pct: Percentage of strictly ordered anchor pairs kept. brier: Weighted multiclass Brier score if labels ``y`` are given, else ``None``. nll: Weighted multiclass negative log-likelihood if ``y`` is given, else ``None``. """ max_row_error: float max_marginal_error: float kl_divergence: float max_rank_violation: float l1_rank_violation: float rank_preservation_pct: float brier: float | None nll: float | None