## Rank Preserving Calibration of Multiclass Probabilities
[](https://pypi.org/project/rank_preserving_calibration/)
[](https://pepy.tech/projects/rank_preserving_calibration)
[](https://www.python.org/downloads/)
[](https://opensource.org/licenses/MIT)
Survey statisticians and machine learning practitioners often need to adjust the predicted class probabilities from a classifier so that they match known population totals (column marginals). Simple post-hoc methods that apply separate logit shifts or raking to each class can scramble the ranking of individuals within a class when there are three or more classes. This package implements a rank-preserving calibration procedure that projects probabilities onto the intersection of two convex sets:
1. **Row-simplex**: each row sums to one and all entries are non-negative.
2. **Isotonic column marginals**: within each class, values are non-decreasing when instances are sorted by their original scores for that class, and the sum of each column equals a user-supplied target.
The algorithm uses Dykstra's alternating projection method in Euclidean geometry. When the specified column totals are feasible, the procedure returns a matrix that preserves cross-person discrimination within each class, matches the desired totals, and remains a valid probability distribution for each instance. If no such matrix exists, the algorithm converges to the closest point (in L2 sense) satisfying both sets of constraints.
### New: Nearly Isotonic Calibration
This package now supports **nearly isotonic** constraints that allow small violations of strict monotonicity when appropriate:
- **Epsilon-slack constraints**: Allow z[i+1] ≥ z[i] - ε instead of strict z[i+1] ≥ z[i]
- **Lambda-penalty approach**: Penalize isotonicity violations with a tunable parameter
These relaxed constraints can provide better balance between rank preservation and probability calibration when strict isotonic constraints are too restrictive.
An **ADMM optimization** implementation is also provided as an alternative solver that minimizes `||Q - P||²` subject to the same constraints.
## Installation
```bash
pip install rank_preserving_calibration
```
The only runtime dependency is `numpy`. Optional dependencies include `scipy` (for enhanced test case generation) and `matplotlib` (for examples).
## Usage
### Basic Usage
```python
import numpy as np
from rank_preserving_calibration import calibrate_dykstra
P = np.array([
[0.6, 0.3, 0.1],
[0.2, 0.5, 0.3],
[0.1, 0.2, 0.7],
])
# Target column sums, e.g. population class frequencies. Must sum to the
# number of rows (3 in this example) for perfect feasibility.
M = np.array([1.0, 1.0, 1.0])
result = calibrate_dykstra(P, M)
print("Adjusted probabilities:\n", result.Q)
print("Converged:", result.converged)
print("Iterations:", result.iterations)
print("Max row error:", result.max_row_error)
print("Max column error:", result.max_col_error)
print("Rank violations:", result.max_rank_violation)
```
### Nearly Isotonic Usage
```python
# Epsilon-slack: Allow small rank violations (recommended)
nearly_params = {"mode": "epsilon", "eps": 0.05}
result = calibrate_dykstra(P, M, nearly=nearly_params)
# Lambda-penalty: Soft isotonic constraint (experimental)
nearly_params = {"mode": "lambda", "lam": 1.0}
result = calibrate_admm(P, M, nearly=nearly_params)
```
The returned `CalibrationResult` contains the calibrated matrix `Q` with the same shape as `P`. Each row of `Q` sums to one, the column sums match `M`, and within each column the entries are sorted in non-decreasing order according to the order implied by the original `P`.
## Functions
### `calibrate_dykstra(P, M, **kwargs)`
Calibrate using Dykstra's alternating projections (recommended). Supports both strict and nearly isotonic constraints.
### `calibrate_admm(P, M, **kwargs)`
Calibrate using ADMM optimization with penalty parameter `rho`. Supports lambda-penalty nearly isotonic constraints.
### `create_test_case(case_type, N, J, **kwargs)` (in `examples.data_helpers`)
Generate synthetic test data for various scenarios used in examples and tests.
## Arguments
| Parameter | Type | Description |
| --- | --- | --- |
| `P` | `ndarray` of shape `[N, J]` | Base multiclass probabilities or non-negative scores. Rows will be projected to the simplex. |
| `M` | `ndarray` of shape `[J]` | Target column totals (e.g. population class frequencies). The sum of `M` should equal the number of rows `N` for exact feasibility. |
| `max_iters` | `int` | Maximum number of projection iterations (default `3000` for Dykstra, `1000` for ADMM). |
| `tol` | `float` | Relative convergence tolerance (default `1e-7` for Dykstra, `1e-6` for ADMM). |
| `verbose` | `bool` | If `True`, prints convergence diagnostics. |
| `nearly` | `dict` | Nearly isotonic parameters: `{"mode": "epsilon", "eps": 0.05}` or `{"mode": "lambda", "lam": 1.0}`. |
| `rho` | `float` | ADMM penalty parameter (default `1.0`, ADMM only). |
## Returns
### CalibrationResult
Both functions return a `CalibrationResult` object with the following attributes:
* `Q`: NumPy array of shape `[N, J]` containing the calibrated probabilities. Each row sums to one, each column approximately sums to the corresponding entry of `M`, and within each column the values are non-decreasing according to the ordering induced by `P`.
* `converged`: boolean indicating whether the solver met the tolerance criteria.
* `iterations`: number of iterations performed.
* `max_row_error`: maximum absolute deviation of row sums from 1.
* `max_col_error`: maximum absolute deviation of column sums from `M`.
* `max_rank_violation`: maximum violation of monotonicity (should be 0 up to numerical tolerance).
* `final_change`: final relative change between iterations.
### ADMMResult
The ADMM function returns an `ADMMResult` object with additional convergence history:
* All `CalibrationResult` attributes plus:
* `objective_values`: objective function values over iterations.
* `primal_residuals`: primal residual norms over iterations.
* `dual_residuals`: dual residual norms over iterations.
## Algorithm Notes
* **Dykstra's Method**: Uses alternating projections with memory terms to ensure convergence to the intersection of constraint sets. Rows are projected onto the simplex via the algorithm of Duchi et al., and columns are projected via the pool-adjacent-violators algorithm followed by an additive shift to match column totals. This is the recommended method for most applications.
* **Nearly Isotonic Extensions**:
- **Epsilon-slack (Dykstra)**: Projects onto the convex set {z : z[i+1] ≥ z[i] - ε} using coordinate transformation. Maintains theoretical convergence guarantees.
- **Lambda-penalty (ADMM)**: Uses proximal operator to minimize ||Q - P||² + λ∑max(0, z[i] - z[i+1]). More experimental but provides soft constraints.
* **ADMM**: Solves the constrained optimization problem using the Alternating Direction Method of Multipliers. May converge faster for some problems but requires tuning the penalty parameter `rho`. The algorithm minimizes the sum of squared differences `0.5 * ||Q - P||²_F` subject to the calibration constraints.
## Examples
See `examples/` directory for comprehensive examples including:
- `example.ipynb`: Basic usage and visualization
- `focused_nearly_isotonic_example.py`: When to use nearly isotonic calibration
- Real-world classifier calibration scenarios
- Survey reweighting applications
- Algorithm comparison and performance analysis
### When to Use Nearly Isotonic Calibration
**Use Nearly Isotonic When:**
- Model predictions have good discrimination but need marginal calibration
- Some predictions are already well-calibrated
- Small rank violations are acceptable in your domain
- You want to preserve model confidence where possible
**Use Strict Isotonic When:**
- Rank order is critical (regulatory, safety applications)
- Model predictions have clear monotonic relationship
- Conservative approach is preferred
## Testing
```bash
python -m pytest tests/ -v
```
## Legacy Compatibility
For backward compatibility, the following aliases are available:
- `calibrate_rank_preserving` → `calibrate_dykstra`
- `admm_rank_preserving_simplex_marginals` → `calibrate_dykstra`
## License
This software is released under the terms of the MIT license.
## Author
Gaurav Sood `<gsood07@gmail.com>`
Raw data
{
"_id": null,
"home_page": null,
"name": "rank-preserving-calibration",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": "calibration, machine learning, multiclass, isotonic, dykstra, admm",
"author": null,
"author_email": "Gaurav Sood <contact@gsood.com>",
"download_url": "https://files.pythonhosted.org/packages/5d/e0/29ddf3c1f8366cba8868afb5772d42c3e4789d54e16fc2e1bc802490a599/rank_preserving_calibration-0.4.0.tar.gz",
"platform": null,
"description": "## Rank Preserving Calibration of Multiclass Probabilities\n\n[](https://pypi.org/project/rank_preserving_calibration/)\n[](https://pepy.tech/projects/rank_preserving_calibration)\n[](https://www.python.org/downloads/)\n[](https://opensource.org/licenses/MIT)\n\nSurvey statisticians and machine learning practitioners often need to adjust the predicted class probabilities from a classifier so that they match known population totals (column marginals). Simple post-hoc methods that apply separate logit shifts or raking to each class can scramble the ranking of individuals within a class when there are three or more classes. This package implements a rank-preserving calibration procedure that projects probabilities onto the intersection of two convex sets:\n\n1. **Row-simplex**: each row sums to one and all entries are non-negative.\n2. **Isotonic column marginals**: within each class, values are non-decreasing when instances are sorted by their original scores for that class, and the sum of each column equals a user-supplied target.\n\nThe algorithm uses Dykstra's alternating projection method in Euclidean geometry. When the specified column totals are feasible, the procedure returns a matrix that preserves cross-person discrimination within each class, matches the desired totals, and remains a valid probability distribution for each instance. If no such matrix exists, the algorithm converges to the closest point (in L2 sense) satisfying both sets of constraints.\n\n### New: Nearly Isotonic Calibration\n\nThis package now supports **nearly isotonic** constraints that allow small violations of strict monotonicity when appropriate:\n\n- **Epsilon-slack constraints**: Allow z[i+1] \u2265 z[i] - \u03b5 instead of strict z[i+1] \u2265 z[i]\n- **Lambda-penalty approach**: Penalize isotonicity violations with a tunable parameter\n\nThese relaxed constraints can provide better balance between rank preservation and probability calibration when strict isotonic constraints are too restrictive.\n\nAn **ADMM optimization** implementation is also provided as an alternative solver that minimizes `||Q - P||\u00b2` subject to the same constraints.\n\n## Installation\n\n```bash\npip install rank_preserving_calibration\n```\n\nThe only runtime dependency is `numpy`. Optional dependencies include `scipy` (for enhanced test case generation) and `matplotlib` (for examples).\n\n## Usage\n\n### Basic Usage\n\n```python\nimport numpy as np\nfrom rank_preserving_calibration import calibrate_dykstra\n\nP = np.array([\n [0.6, 0.3, 0.1],\n [0.2, 0.5, 0.3],\n [0.1, 0.2, 0.7],\n])\n\n# Target column sums, e.g. population class frequencies. Must sum to the\n# number of rows (3 in this example) for perfect feasibility.\nM = np.array([1.0, 1.0, 1.0])\n\nresult = calibrate_dykstra(P, M)\n\nprint(\"Adjusted probabilities:\\n\", result.Q)\nprint(\"Converged:\", result.converged)\nprint(\"Iterations:\", result.iterations)\nprint(\"Max row error:\", result.max_row_error)\nprint(\"Max column error:\", result.max_col_error)\nprint(\"Rank violations:\", result.max_rank_violation)\n```\n\n### Nearly Isotonic Usage\n\n```python\n# Epsilon-slack: Allow small rank violations (recommended)\nnearly_params = {\"mode\": \"epsilon\", \"eps\": 0.05}\nresult = calibrate_dykstra(P, M, nearly=nearly_params)\n\n# Lambda-penalty: Soft isotonic constraint (experimental)\nnearly_params = {\"mode\": \"lambda\", \"lam\": 1.0}\nresult = calibrate_admm(P, M, nearly=nearly_params)\n```\n\nThe returned `CalibrationResult` contains the calibrated matrix `Q` with the same shape as `P`. Each row of `Q` sums to one, the column sums match `M`, and within each column the entries are sorted in non-decreasing order according to the order implied by the original `P`.\n\n## Functions\n\n### `calibrate_dykstra(P, M, **kwargs)`\n\nCalibrate using Dykstra's alternating projections (recommended). Supports both strict and nearly isotonic constraints.\n\n### `calibrate_admm(P, M, **kwargs)` \n\nCalibrate using ADMM optimization with penalty parameter `rho`. Supports lambda-penalty nearly isotonic constraints.\n\n### `create_test_case(case_type, N, J, **kwargs)` (in `examples.data_helpers`)\n\nGenerate synthetic test data for various scenarios used in examples and tests.\n\n## Arguments\n\n| Parameter | Type | Description |\n| --- | --- | --- |\n| `P` | `ndarray` of shape `[N, J]` | Base multiclass probabilities or non-negative scores. Rows will be projected to the simplex. |\n| `M` | `ndarray` of shape `[J]` | Target column totals (e.g. population class frequencies). The sum of `M` should equal the number of rows `N` for exact feasibility. |\n| `max_iters` | `int` | Maximum number of projection iterations (default `3000` for Dykstra, `1000` for ADMM). |\n| `tol` | `float` | Relative convergence tolerance (default `1e-7` for Dykstra, `1e-6` for ADMM). |\n| `verbose` | `bool` | If `True`, prints convergence diagnostics. |\n| `nearly` | `dict` | Nearly isotonic parameters: `{\"mode\": \"epsilon\", \"eps\": 0.05}` or `{\"mode\": \"lambda\", \"lam\": 1.0}`. |\n| `rho` | `float` | ADMM penalty parameter (default `1.0`, ADMM only). |\n\n## Returns\n\n### CalibrationResult\n\nBoth functions return a `CalibrationResult` object with the following attributes:\n\n* `Q`: NumPy array of shape `[N, J]` containing the calibrated probabilities. Each row sums to one, each column approximately sums to the corresponding entry of `M`, and within each column the values are non-decreasing according to the ordering induced by `P`.\n* `converged`: boolean indicating whether the solver met the tolerance criteria.\n* `iterations`: number of iterations performed.\n* `max_row_error`: maximum absolute deviation of row sums from 1.\n* `max_col_error`: maximum absolute deviation of column sums from `M`.\n* `max_rank_violation`: maximum violation of monotonicity (should be 0 up to numerical tolerance).\n* `final_change`: final relative change between iterations.\n\n### ADMMResult\n\nThe ADMM function returns an `ADMMResult` object with additional convergence history:\n\n* All `CalibrationResult` attributes plus:\n* `objective_values`: objective function values over iterations.\n* `primal_residuals`: primal residual norms over iterations.\n* `dual_residuals`: dual residual norms over iterations.\n\n## Algorithm Notes\n\n* **Dykstra's Method**: Uses alternating projections with memory terms to ensure convergence to the intersection of constraint sets. Rows are projected onto the simplex via the algorithm of Duchi et al., and columns are projected via the pool-adjacent-violators algorithm followed by an additive shift to match column totals. This is the recommended method for most applications.\n\n* **Nearly Isotonic Extensions**: \n - **Epsilon-slack (Dykstra)**: Projects onto the convex set {z : z[i+1] \u2265 z[i] - \u03b5} using coordinate transformation. Maintains theoretical convergence guarantees.\n - **Lambda-penalty (ADMM)**: Uses proximal operator to minimize ||Q - P||\u00b2 + \u03bb\u2211max(0, z[i] - z[i+1]). More experimental but provides soft constraints.\n\n* **ADMM**: Solves the constrained optimization problem using the Alternating Direction Method of Multipliers. May converge faster for some problems but requires tuning the penalty parameter `rho`. The algorithm minimizes the sum of squared differences `0.5 * ||Q - P||\u00b2_F` subject to the calibration constraints.\n\n## Examples\n\nSee `examples/` directory for comprehensive examples including:\n- `example.ipynb`: Basic usage and visualization\n- `focused_nearly_isotonic_example.py`: When to use nearly isotonic calibration\n- Real-world classifier calibration scenarios \n- Survey reweighting applications\n- Algorithm comparison and performance analysis\n\n### When to Use Nearly Isotonic Calibration\n\n**Use Nearly Isotonic When:**\n- Model predictions have good discrimination but need marginal calibration\n- Some predictions are already well-calibrated \n- Small rank violations are acceptable in your domain\n- You want to preserve model confidence where possible\n\n**Use Strict Isotonic When:**\n- Rank order is critical (regulatory, safety applications)\n- Model predictions have clear monotonic relationship\n- Conservative approach is preferred\n\n## Testing\n\n```bash\npython -m pytest tests/ -v\n```\n\n## Legacy Compatibility\n\nFor backward compatibility, the following aliases are available:\n- `calibrate_rank_preserving` \u2192 `calibrate_dykstra`\n- `admm_rank_preserving_simplex_marginals` \u2192 `calibrate_dykstra`\n\n## License\n\nThis software is released under the terms of the MIT license.\n\n## Author\n\nGaurav Sood `<gsood07@gmail.com>`\n",
"bugtrack_url": null,
"license": null,
"summary": "Rank-preserving calibration of multiclass probabilities via Dykstra's projections and ADMM.",
"version": "0.4.0",
"project_urls": {
"Documentation": "https://github.com/finite_sample/rank_preserving_calibration#readme",
"Homepage": "https://github.com/finite_sample/rank_preserving_calibration",
"Issues": "https://github.com/finite_sample/rank_preserving_calibration/issues",
"Source": "https://github.com/finite_sample/rank_preserving_calibration"
},
"split_keywords": [
"calibration",
" machine learning",
" multiclass",
" isotonic",
" dykstra",
" admm"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "3f67997c625cd9b84100cc57df79480130edb8bbff2ba8b1710e01fac04c2799",
"md5": "44c9fe4e2c1f1db550b4e46fd77447a7",
"sha256": "b1a9e17bb4bda0e4fb880d2bac9f70a659e42e005099a9517d9d58fafdce448d"
},
"downloads": -1,
"filename": "rank_preserving_calibration-0.4.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "44c9fe4e2c1f1db550b4e46fd77447a7",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 14815,
"upload_time": "2025-08-25T02:57:19",
"upload_time_iso_8601": "2025-08-25T02:57:19.159615Z",
"url": "https://files.pythonhosted.org/packages/3f/67/997c625cd9b84100cc57df79480130edb8bbff2ba8b1710e01fac04c2799/rank_preserving_calibration-0.4.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "5de029ddf3c1f8366cba8868afb5772d42c3e4789d54e16fc2e1bc802490a599",
"md5": "5cb3d23a3cbe5b44166fb1bebebec5f5",
"sha256": "ca6a97d8dc3390d54ebeffc93b2cee8bc635d5a41ee8bdf280f1e0f889d70360"
},
"downloads": -1,
"filename": "rank_preserving_calibration-0.4.0.tar.gz",
"has_sig": false,
"md5_digest": "5cb3d23a3cbe5b44166fb1bebebec5f5",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 23155,
"upload_time": "2025-08-25T02:57:20",
"upload_time_iso_8601": "2025-08-25T02:57:20.440123Z",
"url": "https://files.pythonhosted.org/packages/5d/e0/29ddf3c1f8366cba8868afb5772d42c3e4789d54e16fc2e1bc802490a599/rank_preserving_calibration-0.4.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-08-25 02:57:20",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "finite_sample",
"github_project": "rank_preserving_calibration#readme",
"github_not_found": true,
"lcname": "rank-preserving-calibration"
}