Contrast Bucket Shows a Mismatch
Split forecasts into contrasting buckets so opposite overprediction and underprediction errors cannot cancel inside a single dataset average.
The problem
Checking whether a model's global mean forecast matches the dataset's overall base rate (mean(p) = mean(y)) only tests calibration in the aggregate. A scorer that pushes low probabilities too close to 0 and high probabilities too close to 1 produces negative bias on the bottom half and positive bias on the top half—two errors that cancel to zero when pooled together.
Consider eight rows [p, y]: (0.05, 0), (0.10, 1), (0.15, 0), (0.30, 1), (0.70, 0), (0.85, 1), (0.90, 0), and (0.95, 1). Across all eight rows, forecasts sum to 4.00 (mean = 0.50) and positive outcomes sum to 4 (rate = 0.50), so overall bias mean(p - y) equals 0.0. How badly does each half actually miss when split at cut = 0.5?
Work through the numbers
Split the eight rows into a low bucket [0, 0.5) and a contrast bucket [0.5, 1]. The four low-bucket forecasts (0.05, 0.10, 0.15, 0.30) sum to 0.60, giving mean_pred_low = 0.15, while two of those four rows finish with y = 1 (obs_low = 0.50). Their signed bucket gap is gap_low = 0.15 - 0.50 = -0.35: the low bucket underpredicts event frequency by 35 percentage points.
In the upper bucket (0.70, 0.85, 0.90, 0.95), forecasts sum to 3.40 (mean_pred_high = 0.85), yet again two of the four rows finish positive (obs_high = 0.50). Their signed bucket gap is gap_high = 0.85 - 0.50 = +0.35, overpredicting by 35 percentage points.
Adding signed bucket totals (4 * -0.35 + 4 * 0.35) / 8 recovers overall_bias = 0.0. Expected Calibration Error (ece) prevents cancellation by weighting each bucket's absolute gap by its share of rows:
On our two buckets, (4 * |-0.35| + 4 * |+0.35|) / 8 = 0.35.
The live model
Signed bars extend left in green for underprediction (gap_low = -0.35) and right in red for overprediction (gap_high = +0.35), while the third row confirms overall_bias = 0 and the bottom blue bar shows ece = 0.35. Shift cut to 0.25 (3 low rows vs 5 high rows) or 0.75 (5 low rows vs 3 high rows): overall_bias stays pinned at 0, while ece drops to 0.175 because row (0.30, 1) or (0.70, 0) joins the opposite group.

let cut = scene.parameter("cut", 0.5, 0.25, 0.75, 0.25);
let p = input.select(1, 0);
let y = input.select(1, 1);
let diff = p - y;
let n = float(p.numel());
let m_low = p.lt(cut).to(DType.F64);
let m_high = p.ge(cut).to(DType.F64);
let n_low = m_low.sum();
let n_high = m_high.sum();
let gap_low = (diff * m_low).sum().div(n_low);
let gap_high = (diff * m_high).sum().div(n_high);
let bias = diff.sum().div(n);
let ece = (n_low * gap_low.abs() + n_high * gap_high.abs()).div(n);
let items = torch.list().add(gap_low).add(gap_high).add(bias).add(ece);
let answer = torch.stack(items, 0);
out.set("overall_bias", bias.item()).set("ece", ece.item());
{
"overall_bias": 0,
"ece": 0.35
}Watch the parameter change
Your turn
Opposite calibration errors in separate buckets must add rather than cancel inside Expected Calibration Error. The starting model lets -0.35 and +0.35 cancel before measuring magnitude, reporting ece = 0 on a table where both halves miss by 35 percentage points. Return [gap_low, gap_high, bias, ece]. Check tests uneven splits (cut = 0.4) and boundary ties (p = 0.5).

let cut = scene.parameter("cut", 0.5, 0.25, 0.75, 0.25);
let p = input.select(1, 0);
let y = input.select(1, 1);
let diff = p - y;
let n = float(p.numel());
let m_low = p.lt(cut).to(DType.F64);
let m_high = p.ge(cut).to(DType.F64);
let n_low = m_low.sum();
let n_high = m_high.sum();
let gap_low = (diff * m_low).sum().div(n_low);
let gap_high = (diff * m_high).sum().div(n_high);
let bias = diff.sum().div(n);
let ece = (n_low * gap_low + n_high * gap_high).div(n).abs();
let items = torch.list().add(gap_low).add(gap_high).add(bias).add(ece);
let answer = torch.stack(items, 0);
out.set("overall_bias", bias.item()).set("ece", ece.item());
{
"overall_bias": 0,
"ece": 0
}Expected Calibration Error must accumulate the magnitude of each bucket's mismatch weighted by bucket share. The starting model lets -0.35 and +0.35 cancel to ece = 0. Return [gap_low, gap_high, bias, ece].
Once opposite bucket errors no longer cancel, the blue ece bar rises from 0 to 0.35.
Read the result carefully
Calibration is a conditional property—given forecast p, the positive rate should match p—whereas global bias mean(p - y) is marginal ("calibration-in-the-large"). Passing calibration-in-the-large is necessary for calibration, never sufficient. Notice also that ECE depends on the binning chosen: on these exact eight forecasts, our slider reads 0.35 at cut = 0.5 yet drops to 0.175 at cut = 0.25 and 0.75! In practice, evaluators partition [0, 1] into about ten bins rather than two so narrow overconfident regions cannot hide inside coarse buckets.
Recap
- Never rely on dataset-wide
mean(p) - mean(y)alone, because negative and positive bucket errors cancel. - Contrast low and high buckets to detect S-shaped underconfidence or inverse-S overconfidence.
- Weight
|gap_k|by bucket sizen_k / nwhen computing Expected Calibration Error (ece).