Calibrated Soft Voting with Platt and Isotonic

Quick Overview

Implement per-member Platt scaling and isotonic calibration for an ensemble of binary classifiers, then soft-vote the members and compare test log loss and Brier score with and without calibration. Tests Newton optimization of a convex loss, the pool-adjacent-violators algorithm, and leak-free calibration practice.

Calibrated Soft Voting with Platt and Isotonic

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Other

You have positive-class probabilities from an ensemble of binary classifiers, stored as arrays shaped `(members, n)`: one on a calibration set (`cal_probs`, with labels `y_cal`) and one on a test set (`test_probs`, with labels `y_test`). Fit a separate calibrator for each member on the calibration set, apply it to that member's test probabilities, soft-vote the members, and compare the test log loss and Brier score with and without calibration. Implement every function below. ```python def fit_platt(scores, y): ... # returns (a, b) def apply_platt(scores, a, b): ... def fit_isotonic(scores, y): ... # returns (x_knots, y_knots) def apply_isotonic(scores, x_knots, y_knots): ... def calibrate_members(cal_probs, y_cal, test_probs, method="platt"): ... def soft_vote(probs, weights=None): ... def binary_log_loss(y, p): ... def brier_score(y, p): ... def compare_calibration(cal_probs, y_cal, test_probs, y_test, method="platt", weights=None): ... ``` Requirements: - `fit_platt` works on `z = logit(clip(s, 1e-12, 1 - 1e-12))` and fits `(a, b)` by minimizing cross-entropy against Platt's smoothed targets: `(n_pos + 1) / (n_pos + 2)` for positives and `1 / (n_neg + 2)` for negatives. `apply_platt` returns `sigmoid(a * z + b)`. - `fit_isotonic` runs pool-adjacent-violators (PAV) over the unique sorted scores, with tied scores pooled by their mean label. It returns those unique scores as `x_knots` and the non-decreasing fitted values as `y_knots`. `apply_isotonic` interpolates linearly between knots and clamps outside the knot range. - `calibrate_members` returns a `(members, n_test)` array. A `method` other than `"platt"` or `"isotonic"` raises `ValueError`. - `soft_vote` normalizes the weights to sum to 1 (equal weights by default). Any negative weight or a non-positive sum raises `ValueError`. - `binary_log_loss` clips `p` to `[1e-15, 1 - 1e-15]`. `compare_calibration` returns a dict with the keys `uncalibrated_log_loss`, `uncalibrated_brier`, `calibrated_log_loss` and `calibrated_brier` (floats). Example: ```python xk, yk = fit_isotonic([0.1, 0.2, 0.3, 0.4], [0, 1, 0, 1]) # xk = [0.1, 0.2, 0.3, 0.4], yk = [0.0, 0.5, 0.5, 1.0] apply_isotonic([0.05, 0.15, 0.25, 0.9], xk, yk) # [0.0, 0.25, 0.5, 1.0] soft_vote([[0.2, 0.9, 0.5], [0.6, 0.3, 0.5], [0.4, 0.6, 0.8]], [2, 1, 1]) # [0.35, 0.675, 0.575] brier_score([1, 0], [0.8, 0.4]) # 0.1 ``` ```hint Fitting Platt The Platt objective is convex in (a, b). With a two-column design matrix of the logit and a constant, a few Newton steps reach the minimum, but guard each step so the loss never goes up. ``` ```hint Merging blocks Collapse tied scores first and remember how many rows each one holds. When two adjacent blocks are out of order and must merge, their counts decide the merged value. ``` ```hint A fair comparison Use the same voting weights for the raw and calibrated ensembles, and think about what happens to log loss when a calibrator outputs exactly 0 or 1. ``` ### Constraints & Assumptions - Labels are 0/1, and all members share the same `y_cal` and `y_test`. - `numpy` and `scipy` are available. - Training the base classifiers is out of scope. The calibration set is held out from the data the members were trained on. ### What a Strong Answer Covers - A correct Platt fit: the cross-entropy objective with smoothed targets, the clipped logit transform, and an optimizer that converges reliably. - A PAV implementation that pools ties by mean label, merges blocks by count-weighted mean, and returns knots matching the example, plus clamped linear interpolation. - Input validation that raises `ValueError` exactly where the spec requires it. - A comparison that changes only the calibration step, with log loss clipping so extreme calibrated outputs do not produce infinite loss. - Time and space complexity, and edge cases such as a one-class calibration set or a member with constant scores. ### Follow-up Questions - When would you prefer Platt over isotonic calibration? - Why calibrate each member before voting, when you could calibrate the averaged output instead? - How would you avoid leakage when you have no separate calibration set?

Overview: Implement per-member Platt scaling and isotonic calibration for an ensemble of binary classifiers, then soft-vote the members and compare test log loss and Brier score with and without calibration. Tests Newton optimization of a convex loss, the pool-adjacent-violators algorithm, and leak-free calibration practice.

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

You have positive-class probabilities from an ensemble of binary classifiers, stored as arrays shaped (members, n): one on a calibration set (cal_probs, with labels y_cal) and one on a test set (test_probs, with labels y_test). Fit a separate calibrator for each member on the calibration set, apply it to that member's test probabilities, soft-vote the members, and compare the test log loss and Brier score with and without calibration.

Implement every function below.

def fit_platt(scores, y): ...                      # returns (a, b)
def apply_platt(scores, a, b): ...
def fit_isotonic(scores, y): ...                   # returns (x_knots, y_knots)
def apply_isotonic(scores, x_knots, y_knots): ...
def calibrate_members(cal_probs, y_cal, test_probs, method="platt"): ...
def soft_vote(probs, weights=None): ...
def binary_log_loss(y, p): ...
def brier_score(y, p): ...
def compare_calibration(cal_probs, y_cal, test_probs, y_test, method="platt", weights=None): ...

Requirements:

  • fit_platt works on z = logit(clip(s, 1e-12, 1 - 1e-12)) and fits (a, b) by minimizing cross-entropy against Platt's smoothed targets: (n_pos + 1) / (n_pos + 2) for positives and 1 / (n_neg + 2) for negatives. apply_platt returns sigmoid(a * z + b) .
  • fit_isotonic runs pool-adjacent-violators (PAV) over the unique sorted scores, with tied scores pooled by their mean label. It returns those unique scores as x_knots and the non-decreasing fitted values as y_knots . apply_isotonic interpolates linearly between knots and clamps outside the knot range.
  • calibrate_members returns a (members, n_test) array. A method other than "platt" or "isotonic" raises ValueError .
  • soft_vote normalizes the weights to sum to 1 (equal weights by default). Any negative weight or a non-positive sum raises ValueError .
  • binary_log_loss clips p to [1e-15, 1 - 1e-15] . compare_calibration returns a dict with the keys uncalibrated_log_loss , uncalibrated_brier , calibrated_log_loss and calibrated_brier (floats).

Example:

xk, yk = fit_isotonic([0.1, 0.2, 0.3, 0.4], [0, 1, 0, 1])
# xk = [0.1, 0.2, 0.3, 0.4], yk = [0.0, 0.5, 0.5, 1.0]
apply_isotonic([0.05, 0.15, 0.25, 0.9], xk, yk)  # [0.0, 0.25, 0.5, 1.0]

soft_vote([[0.2, 0.9, 0.5], [0.6, 0.3, 0.5], [0.4, 0.6, 0.8]], [2, 1, 1])
# [0.35, 0.675, 0.575]
brier_score([1, 0], [0.8, 0.4])  # 0.1

Constraints & Assumptions

  • Labels are 0/1, and all members share the same y_cal and y_test .
  • numpy and scipy are available.
  • Training the base classifiers is out of scope. The calibration set is held out from the data the members were trained on.

What a Strong Answer Covers Guidance

  • A correct Platt fit: the cross-entropy objective with smoothed targets, the clipped logit transform, and an optimizer that converges reliably.
  • A PAV implementation that pools ties by mean label, merges blocks by count-weighted mean, and returns knots matching the example, plus clamped linear interpolation.
  • Input validation that raises ValueError exactly where the spec requires it.
  • A comparison that changes only the calibration step, with log loss clipping so extreme calibrated outputs do not produce infinite loss.
  • Time and space complexity, and edge cases such as a one-class calibration set or a member with constant scores.

Follow-up Questions Guidance

  • When would you prefer Platt over isotonic calibration?
  • Why calibrate each member before voting, when you could calibrate the averaged output instead?
  • How would you avoid leakage when you have no separate calibration set?