Speed

Score Buckets Hold Rows

Partition predicted probabilities into disjoint score buckets, count rows on both sides of a cut, and preserve boundary ties.

The problem

A weather or risk model emits a predicted probability p between zero and one for each row alongside an observed outcome y equal to 0 or 1. A single outcome cannot verify whether a forecast of 0.28 was honest, because one trial finishes as either 0 or 1 rather than 0.28. Calibration begins by collecting nearby forecasts into score buckets so that a whole group of rows can be inspected together.

Grouping happens strictly on the forecast column p, never on the outcome column y. Sorting by y first would ask what scores positive events received; calibration asks the opposite question: among rows given a similar forecast, how many rows arrived and what happened next? Consider six scored rows: (0.12, 0), (0.28, 1), (0.45, 0), (0.50, 0), (0.74, 1), and (0.88, 1).

Work through the numbers

Choose a split point cut = 0.5 to form one half-open and one closed bucket that cover the unit interval without overlap. Place every forecast strictly below 0.5 into the low bucket [0, 0.5), and place every forecast at or above 0.5 into the high bucket [0.5, 1]. Stating where equality lands keeps every row accounted for once and only once.

Scan the six forecasts in order: 0.12, 0.28, and 0.45 are strictly below 0.5, so low_count equals 3. The fourth row (index three) has forecast 0.50, which meets the inclusive upper condition, joining 0.74 and 0.88 to give high_count equal to 3. Adding the two bucket sizes gives 3 + 3 = 6 assigned rows, matching the six input rows.

Two boundary mistakes corrupt calibration tables before any fraction is computed. Using strict inequalities on both sides drops 0.50 completely, leaving 3 + 2 = 5 assigned rows. Using inclusive inequalities on both sides counts 0.50 twice, inflating the total to 4 + 3 = 7.

The live model

The top strip plots the six forecast probabilities from 0.0 to 1.0 with a red vertical marker at cut. Blue circles belong to the low bucket and green circles belong to the high bucket; the bars beneath report [3, 3, 6] at the default cut of 0.5. Drag cut down to 0.2 and only 0.12 remains in the low bucket (1 and 5); move cut up to 0.8 and five forecasts sit below cut while 0.88 stands alone above (5 and 1).

Six forecast probabilities along a zero-to-one axis split at cut 0.5 into 3 low-bucket rows and 3 high-bucket rows, accounting for all 6 rows.
let cut = scene.parameter("cut", 0.5, 0.2, 0.8, 0.1);
let p = input.select(1, 0);
let low_mask = p.lt(cut).to(DType.F64);
let high_mask = p.ge(cut).to(DType.F64);
let n_low = low_mask.sum();
let n_high = high_mask.sum();
let total = n_low + n_high;
let answer = torch.stack(torch.list().add(n_low).add(n_high).add(total), 0);
out.set("low_count", n_low.item()).set("high_count", n_high.item());
out.set("assigned", total.item());
{
  "low_count": 3,
  "high_count": 3,
  "assigned": 6
}
Watch the parameter change

Your turn

A row sitting right on cut belongs in the high bucket so that every input row is counted once. The starting program drops boundary ties completely, stranding the 0.50 forecast in neither bucket and reporting [3, 2, 5] on six rows. Return [n_low, n_high, total]. Check tests unseen forecast tables at cut = 0.4 and cut = 0.7 with boundary ties.

Starter partition at cut 0.5 with 3 low-bucket rows, 2 high-bucket rows, and 1 unassigned boundary row at 0.5.
let cut = scene.parameter("cut", 0.5, 0.2, 0.8, 0.1);
let p = input.select(1, 0);
let low_mask = p.lt(cut).to(DType.F64);
let high_mask = p.gt(cut).to(DType.F64);
let n_low = low_mask.sum();
let n_high = high_mask.sum();
let total = n_low + n_high;
let answer = torch.stack(torch.list().add(n_low).add(n_high).add(total), 0);
out.set("low_count", n_low.item()).set("high_count", n_high.item());
out.set("assigned", total.item());
{
  "low_count": 3,
  "high_count": 2,
  "assigned": 5
}

A forecast sitting right on cut belongs in the high bucket so every row is assigned once. The starting model drops boundary rows and reports only 5 assigned rows out of 6. Return [n_low, n_high, total].

Once boundary ties enter the high bucket, the 0.50 row turns green on the default table and the returned vector changes from [3, 2, 5] to [3, 3, 6].

Read the result carefully

Bucket counts show how many forecasts fall on each side of the cut before outcomes enter the calculation. An empty or one-row bucket warns that any observed fraction inside that bin will rest on almost no data; at cut = 0.8, the high bucket holds 1 row (0.88). Checking that bucket sizes sum to the dataset length 6 catches lost ties, NaN scores, and overlapping intervals immediately.

Recap

  • Form calibration buckets from the predicted probability p alone, keeping observed labels y untouched during bin assignment.
  • Pair < cut with >= cut so forecasts sitting directly on a bin edge enter one bucket rather than zero or two.
  • Verify that bucket counts sum to the input row count and inspect small bins before trusting per-bucket rates.