Mathematical Theory

This page summarizes the optimization problem and theoretical results developed in Rank-Preserving Marginal Calibration for Multiclass Probabilities.

Weighted Marginal-Matching Polytope

Let \(P \in (0, 1)^{N \times J}\) be a matrix of predicted class probabilities whose rows lie on the probability simplex \(\Delta^{J-1} = \{q \in \mathbb{R}_+^J : \sum_{j=1}^J q_j = 1\}\), let \(w \in \mathbb{R}_{++}^N\) be positive row weights (with \(W = \sum_i w_i\); unweighted tables set \(w = \mathbf{1}_N\)), and let \(\pi \in \Delta^{J-1}\) be known target class shares (target_shares). Define the weighted transportation polytope

\[\mathcal{C}_w(\pi) = \left\{ Q \in \mathbb{R}_+^{N \times J} : Q\mathbf{1}_J = \mathbf{1}_N,\; \sum_{i=1}^N \frac{w_i}{W} Q_{ij} = \pi_j \;\; \forall j \right\}.\]

Because every row sums to one, the problem is completely invariant to scaling \(w\) by a positive constant: only relative weights \(w_i / W\) and target shares \(\pi_j\) enter the constraints.

Why IPF Reverses Ranks for \(J \ge 3\)

Minimizing weighted KL divergence over \(\mathcal{C}_w(\pi)\) without rank constraints yields Iterative Proportional Fitting (IPF / Sinkhorn scaling):

\[Q^{\mathrm{IPF}}_{ij} = \frac{b_j P_{ij}}{\sum_{k=1}^J b_k P_{ik}},\]

where \(b_1, \dots, b_J > 0\) are chosen so \(\sum_i (w_i / W) Q^{\mathrm{IPF}}_{ij} = \pi_j\).

  • Binary preservation (\(J = 2\)): \(Q^{\mathrm{IPF}}_{i1}\) is strictly increasing in \(P_{i1}\), so no within-class rankings ever flip.

  • Pairwise odds-ratio invariance (\(J \ge 2\)): For any pair of classes \((j, k)\), the denominator cancels, so \(Q^{\mathrm{IPF}}_{ij} / Q^{\mathrm{IPF}}_{ik}\) preserves the exact ordering of \(P_{ij} / P_{ik}\).

  • Exact reversal condition (\(J \ge 3\)): A pair with \(P_{i_1 j} > P_{i_2 j}\) inverts to \(Q^{\mathrm{IPF}}_{i_1 j} < Q^{\mathrm{IPF}}_{i_2 j}\) if and only if \(\frac{\sum_k b_k P_{i_1 k}}{\sum_k b_k P_{i_2 k}} > \frac{P_{i_1 j}}{P_{i_2 j}}\), which enters solely through third-class denominator shifts.

  • Pure-label-shift impossibility: When \(P\) is the exact calibrated source Bayes posterior and only class priors shift (\(P_{\mathrm{tgt}}(X \mid Y) = P_{\mathrm{src}}(X \mid Y)\)), \(Q^{\mathrm{IPF}}\) is the exact target Bayes posterior. Thus exact Bayes optimality under pure label shift, exact marginal matching, and strict within-class rank preservation are mutually incompatible for \(J \ge 3\).

Weak-Order Block-Boundary Constraints

Within class \(j\), \(Q\) preserves the weak order of an anchor matrix \(A \in \mathbb{R}^{N \times J}\) (default \(A = P\)) if

\[A_{i_1 j} < A_{i_2 j} \;\Longrightarrow\; Q_{i_1 j} \le Q_{i_2 j}.\]

Tied anchor scores (\(A_{i_1 j} = A_{i_2 j}\)) impose no ordering constraint between \(Q_{i_1 j}\) and \(Q_{i_2 j}\). Partitioning column \(j\) into strictly ordered tie blocks \(B_{j,1} \prec \cdots \prec B_{j,K_j}\), exact weak-order preservation is represented with \(O(N)\) linear inequalities using monotone boundary variables \(u_{j,1} \le \cdots \le u_{j,K_j-1}\):

\[Q_{ij} \le u_{j,k} \quad (\forall i \in B_{j,k},\; k < K_j), \qquad u_{j,k-1} \le Q_{ij} \quad (\forall i \in B_{j,k},\; k > 1).\]

This representation (ties="weak", default) is invariant to row permutations and yields strictly lower divergence than stable row-index tie sorting (ties="stable") whenever any arbitrary intra-tie constraint would be active.

Soft \(\lambda\)-Pareto Frontier and KL vs. Euclidean Geometry

Normalizing row weights to mean one (\(\tilde{w}_i = N w_i / W\)) so the penalty scale is invariant to raw counts vs. shares, calibrate(..., rank_penalty=lam) solves the convex exponential-cone program

\[\min_{Q \in \mathcal{C}_w(\pi),\; u,\; v \ge 0} \; \sum_{i=1}^N \tilde{w}_i \sum_{j=1}^J Q_{ij} \log\!\frac{Q_{ij}}{R_{ij}} + \lambda \sum_r v_r,\]

where \(v_r \ge 0\) are hinge slacks relaxing the weak-order inequalities.

  • Multiplicative KL updates: At any interior optimum, \(Q^\star_{ij} = R_{ij} \exp(-\nu_i - \mu_j - \gamma_{ij})\), preserving strict positivity on the open simplex and recovering weighted IPF at \(\lambda = 0\).

  • Additive Euclidean updates (loss="euclidean"): Minimizing squared distance yields \(Q^\star_{ij} = \max(0, R_{ij} - \nu_i - \mu_j - \gamma_{ij})\), which clips small probabilities to zero and distorts log-odds.

  • Pareto monotonicity: As \(\lambda\) increases from \(0\) (IPF) to \(\infty\) (strict weak-order KL), total L1 rank violation \(V_A(Q^\star(\lambda))\) is non-increasing and KL divergence is non-decreasing.

  • Anchor-reference decoupling: Separating the KL reference \(R\) (e.g., temperature-scaled probabilities from apply_temperature) from the ranking anchor \(A = P\) reduces overconfidence without losing original within-class rankings.