WRITE · TRACE · UNDERSTAND

Pool adjacent violations in weighted isotonic calibration

Combine equal finite scores before fitting.

Read lesson: Pool adjacent violations in weighted isotonic calibration →

Your experiment

Combine equal finite scores before fitting. Each tied score starts one block with total positive weight and total weight. Visit ascending scores and merge adjacent blocks while their positive-weight ratios decrease. Return [minimum_score, maximum_score, fitted_probability] per remaining block. Require aligned nonempty scores, binary integer labels and strictly positive finite weights. Keep equal neighboring fitted probabilities as separate blocks unless a decrease triggers a merge.

Python runs in your browser. Your draft stays on this device.

What to look for

Fit on held-out calibration data. Define prediction behavior between blocks and outside the observed range before using the mapping in a serving artifact.

Watch your code work

Ready to experiment

Write a solution or load the worked answer, then choose Trace & check.

Step through arrays, variables and training curves produced by your own Python.

About this algorithm

How Pool adjacent violations in weighted isotonic calibration works

The idea

Isotonic calibration turns raw scores into probabilities under one assumption: a higher score should never mean a lower probability. It fits a step function that is as close as possible to the observed labels while never decreasing.

The pool adjacent violators algorithm builds it in one sweep. Each distinct score starts as a block holding its total weight and its positive weight. Blocks are visited in ascending score order, and whenever a block's positive rate is lower than that of the block to its left, the two are merged into one block with the pooled rate. A merge can expose a new violation further left, so merging repeats until the order holds. isotonic_fit returns [minimum_score, maximum_score, fitted_probability] for each surviving block.

How it works

  1. Validate aligned nonempty inputs, finite scores, integer 0 or 1 labels and strictly positive finite weights.
  2. Combine tied scores in tied, summing total weight and positive weight per distinct score.
  3. Visit the distinct scores in ascending order and append a block [score, score, weight, positive].
  4. While the previous block's ratio positive / weight is greater than the new block's, pop both and append their merge with summed weights.
  5. Leave neighbours with equal ratios as separate blocks, since only a decrease triggers a merge.
  6. Return [low, high, positive / weight] for every remaining block.

What it costs

Time
O(n log n)
Space
O(n)

Sorting the distinct scores dominates; the sweep is linear because each merge removes a block and at most n - 1 merges can happen.

Where it is used

  • scikit-learn's IsotonicRegression fits this model, and CalibratedClassifierCV(method='isotonic') uses it to calibrate classifiers.
  • Zadrozny and Elkan proposed isotonic regression for calibrating classifier scores in 2002.
  • R's isoreg function fits the same piecewise-constant, non-decreasing least squares regression.

Watch out for

  • Merging once and moving on: a pooled block can now be lower than the block before it, so the check must repeat.
  • Averaging the two block rates instead of pooling positive weight and total weight, which is wrong when the blocks differ in weight.
  • Treating tied scores as separate blocks, so one score could receive two different fitted probabilities.