Isotonic Regression Calibration via Pool-Adjacent-Violators (PAV)

Quick Overview

Implement isotonic regression calibration with the pool-adjacent-violators algorithm in NumPy, including weighted tie merging and clipped linear interpolation for new scores. It tests monotone calibration, weighted least squares under an order constraint, and matching scikit-learn's behavior.

Isotonic Regression Calibration via Pool-Adjacent-Violators (PAV)

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

Implement `fit_isotonic(scores, y_true, sample_weight=None)`, which fits a non-decreasing calibration map with pool-adjacent-violators (PAV), minimizing weighted squared error, and returns `(x_knots, y_knots)`. Then implement `predict_isotonic(x_knots, y_knots, new_scores)`, which maps new scores to calibrated probabilities. Requirements: - Merge tied scores first. A tied group's label is the weighted mean of its labels and its weight is the summed weight. `sample_weight=None` means every weight is 1. - `x_knots` is the sorted unique scores (float) and `y_knots` is the fitted non-decreasing value at each unique score (same length). - Prediction linearly interpolates between knots and clips to the end values outside the knot range. A single knot predicts that constant everywhere. - Misaligned `scores`/`y_true`/`sample_weight` shapes or empty input raise `ValueError`. - The fitted value at every unique score and every prediction must match `sklearn.isotonic.IsotonicRegression(out_of_bounds='clip')`. Example: ```python x, y = fit_isotonic([0.1, 0.2, 0.3, 0.4, 0.5], [0, 1, 0, 1, 1]) # x = [0.1, 0.2, 0.3, 0.4, 0.5] # y = [0.0, 0.5, 0.5, 1.0, 1.0] # the 1, 0 violation is pooled to 0.5 predict_isotonic(x, y, [0.0, 0.15, 0.25, 0.9]) # -> [0.0, 0.25, 0.5, 1.0] fit_isotonic([0.5, 0.2, 0.8, 0.2, 0.5], [0, 1, 1, 0, 0]) # -> x = [0.2, 0.5, 0.8], y = [0.25, 0.25, 1.0] ``` ```hint Ties first Collapse tied scores before any pooling. `np.unique(..., return_inverse=True)` together with `np.bincount` gives each unique score's total weight and weighted label sum without a Python loop. ``` ```hint Pooling Keep a stack of blocks, each with a weighted mean and a total weight. A merge can create a new violation with the block below it, so keep checking the top two blocks after every merge. ``` ### Constraints & Assumptions - Scores are finite floats, labels are in `{0, 1}`, weights are positive. - Up to a few thousand samples. Only `numpy` may be used in the solution. - Return `np.ndarray` of floats, with predictions shaped like `new_scores`. ### What a Strong Answer Covers - Tie merging with weighted mean labels and summed weights before pooling, and why it is needed to match sklearn. - A PAV pass that merges violating blocks into their weighted mean and keeps merging until the stack is non-decreasing. - Prediction by linear interpolation with clipping at both ends, including the single-knot case. - Input validation for misaligned shapes and empty input. - The time complexity of the sort and of the pooling pass. ### Follow-up Questions - What is the time complexity of your PAV, and can it be made linear? - Isotonic vs Platt scaling: when would you prefer each? - How would you evaluate whether calibration actually improved?

Overview: Implement isotonic regression calibration with the pool-adjacent-violators algorithm in NumPy, including weighted tie merging and clipped linear interpolation for new scores. It tests monotone calibration, weighted least squares under an order constraint, and matching scikit-learn's behavior.

General logo
General
Nov 24, 2026
mediumMachine Learning EngineerOtherMachine Learning
0
0

Implement fit_isotonic(scores, y_true, sample_weight=None), which fits a non-decreasing calibration map with pool-adjacent-violators (PAV), minimizing weighted squared error, and returns (x_knots, y_knots). Then implement predict_isotonic(x_knots, y_knots, new_scores), which maps new scores to calibrated probabilities.

Requirements:

  • Merge tied scores first. A tied group's label is the weighted mean of its labels and its weight is the summed weight. sample_weight=None means every weight is 1.
  • x_knots is the sorted unique scores (float) and y_knots is the fitted non-decreasing value at each unique score (same length).
  • Prediction linearly interpolates between knots and clips to the end values outside the knot range. A single knot predicts that constant everywhere.
  • Misaligned scores / y_true / sample_weight shapes or empty input raise ValueError .
  • The fitted value at every unique score and every prediction must match sklearn.isotonic.IsotonicRegression(out_of_bounds='clip') .

Example:

x, y = fit_isotonic([0.1, 0.2, 0.3, 0.4, 0.5], [0, 1, 0, 1, 1])
# x = [0.1, 0.2, 0.3, 0.4, 0.5]
# y = [0.0, 0.5, 0.5, 1.0, 1.0]   # the 1, 0 violation is pooled to 0.5

predict_isotonic(x, y, [0.0, 0.15, 0.25, 0.9])
# -> [0.0, 0.25, 0.5, 1.0]

fit_isotonic([0.5, 0.2, 0.8, 0.2, 0.5], [0, 1, 1, 0, 0])
# -> x = [0.2, 0.5, 0.8], y = [0.25, 0.25, 1.0]

Constraints & Assumptions

  • Scores are finite floats, labels are in {0, 1} , weights are positive.
  • Up to a few thousand samples. Only numpy may be used in the solution.
  • Return np.ndarray of floats, with predictions shaped like new_scores .

What a Strong Answer Covers Guidance

  • Tie merging with weighted mean labels and summed weights before pooling, and why it is needed to match sklearn.
  • A PAV pass that merges violating blocks into their weighted mean and keeps merging until the stack is non-decreasing.
  • Prediction by linear interpolation with clipping at both ends, including the single-knot case.
  • Input validation for misaligned shapes and empty input.
  • The time complexity of the sort and of the pooling pass.

Follow-up Questions Guidance

  • What is the time complexity of your PAV, and can it be made linear?
  • Isotonic vs Platt scaling: when would you prefer each?
  • How would you evaluate whether calibration actually improved?