Speed

Reliability Table Compares Fractions

Assemble disjoint forecast buckets into a reliability table that plots each bin's empirical mean prediction against its observed positive rate.

The problem

Inspecting one bucket at a time leaves the full probability range fragmented. A reliability table places disjoint buckets side by side across [0, 1], recording three numbers per bin: row count n, empirical mean_predicted, and empirical observed_fraction. Plotting (mean_predicted, observed_fraction) for each bin against the 45-degree identity line turns that table into a reliability diagram.

Partition [0, 1] into three equal-width bins [0, 1/3), [1/3, 2/3), and [2/3, 1] over ten rows [p, y]: (0.06, 0), (0.10, 0), (0.14, 1), (0.18, 0), (0.36, 0), (0.44, 1), (0.72, 1), (0.80, 0), (0.88, 1), and (0.96, 1). Where does each bin sit relative to the calibration diagonal when shift = 0?

Work through the numbers

Bin zero [0, 1/3) collects the first four rows (0.06, 0.10, 0.14, 0.18). Their forecast sum is 0.48, giving mean_predicted = 0.48 / 4 = 0.12, while one positive outcome gives observed_fraction = 1 / 4 = 0.25. Because 0.12 lies below 0.25, the point (0.12, 0.25) sits 0.13 above the diagonal (underpredicting risk).

Bin one [1/3, 2/3) holds two rows (0.36 and 0.44, n = 2). Their forecasts average (0.36 + 0.44) / 2 = 0.40, and one positive gives observed_fraction = 1 / 2 = 0.50, placing (0.40, 0.50) 0.10 above the diagonal. Notice what happens if a plotter lazily marks the bin's geometric center 0.50 on the horizontal axis instead of 0.40: the point moves to (0.50, 0.50) right on the diagonal, erasing a 0.10 underprediction gap.

Bin two [2/3, 1] holds the top four rows (0.72, 0.80, 0.88, 0.96, n = 4). Their forecasts sum to 3.36 (mean_predicted = 0.84) against three positives (observed_fraction = 3 / 4 = 0.75), placing (0.84, 0.75) 0.09 below the diagonal (overpredicting risk).

The live model

The square on the left plots the three (mean_predicted, observed_fraction) points connected in blue, with green vertical segments where observed rates exceed forecasts and red segments where forecasts overshoot. Push shift up to 0.10 (forecasts clamped to [0.01, 0.99]): bin one slides right to (0.50, 0.50) on the diagonal while bin two moves further right to (0.9225, 0.75). Pull shift down to -0.10: boundary rows cross bin edges, changing bin counts from (4, 2, 4) to (5, 2, 3).

Reliability curve across three equal-width bins plotting actual bin mean forecasts (0.12, 0.40, 0.84) against observed positive fractions (0.25, 0.50, 0.75).
let shift = scene.parameter("shift", 0.0, -0.1, 0.1, 0.05);
let p = (input.select(1, 0) + shift).clamp(0.01, 0.99);
let y = input.select(1, 1);
let c1 = 1.0 / 3.0;
let c2 = 2.0 / 3.0;
let m0 = p.lt(c1).to(DType.F64);
let m1 = p.ge(c1).to(DType.F64) * p.lt(c2).to(DType.F64);
let m2 = p.ge(c2).to(DType.F64);
let n0 = m0.sum();
let n1 = m1.sum();
let n2 = m2.sum();
let mp0 = (p * m0).sum().div(n0);
let mp1 = (p * m1).sum().div(n1);
let mp2 = (p * m2).sum().div(n2);
let ob0 = (y * m0).sum().div(n0);
let ob1 = (y * m1).sum().div(n1);
let ob2 = (y * m2).sum().div(n2);
let r0 = torch.stack(torch.list().add(n0).add(mp0).add(ob0), 0);
let r1 = torch.stack(torch.list().add(n1).add(mp1).add(ob1), 0);
let r2 = torch.stack(torch.list().add(n2).add(mp2).add(ob2), 0);
let answer = torch.stack(torch.list().add(r0).add(r1).add(r2), 0);
out.set("mid_bin_mean", mp1.item()).set("high_bin_mean", mp2.item());
{
  "mid_bin_mean": 0.4,
  "high_bin_mean": 0.84
}
Watch the parameter change

Your turn

Each bin's horizontal coordinate in a reliability table must reflect the actual forecasts that landed inside that bin. The starting code reports fixed geometric bin midpoints (1/6, 0.5, 5/6), disguising the middle bin's 0.40 forecast mean as 0.50 on the calibration diagonal. Return the 3 x 3 table of [count, mean_predicted, observed_fraction]. Check grades unseen tables at shift = 0.1 and shift = -0.1 where clamping matters.

Starter substitutes fixed bin midpoints (0.167, 0.5, 0.833) for empirical bin means, placing the middle bin directly on the diagonal.
let shift = scene.parameter("shift", 0.0, -0.1, 0.1, 0.05);
let p = (input.select(1, 0) + shift).clamp(0.01, 0.99);
let y = input.select(1, 1);
let c1 = 1.0 / 3.0;
let c2 = 2.0 / 3.0;
let m0 = p.lt(c1).to(DType.F64);
let m1 = p.ge(c1).to(DType.F64) * p.lt(c2).to(DType.F64);
let m2 = p.ge(c2).to(DType.F64);
let n0 = m0.sum();
let n1 = m1.sum();
let n2 = m2.sum();
let mp0 = torch.scalar(1.0 / 6.0).to(DType.F64);
let mp1 = torch.scalar(0.5).to(DType.F64);
let mp2 = torch.scalar(5.0 / 6.0).to(DType.F64);
let ob0 = (y * m0).sum().div(n0);
let ob1 = (y * m1).sum().div(n1);
let ob2 = (y * m2).sum().div(n2);
let r0 = torch.stack(torch.list().add(n0).add(mp0).add(ob0), 0);
let r1 = torch.stack(torch.list().add(n1).add(mp1).add(ob1), 0);
let r2 = torch.stack(torch.list().add(n2).add(mp2).add(ob2), 0);
let answer = torch.stack(torch.list().add(r0).add(r1).add(r2), 0);
out.set("mid_bin_mean", mp1.item()).set("high_bin_mean", mp2.item());
{
  "mid_bin_mean": 0.5,
  "high_bin_mean": 0.8333333134651184
}

A reliability table plots the empirical forecast average inside each bin rather than fixed geometric midpoints. The starting model reports 0.50 for the middle bin instead of 0.40. Return the 3x3 table of [count, mean_pred, obs_frac].

With empirical bin forecast averages in place, the middle marker shifts left from (0.50, 0.50) to (0.40, 0.50), exposing its -0.10 gap.

Read the result carefully

Equal-width bins do not force forecasts to spread evenly inside each interval. Whenever scores cluster near one edge of a bin—such as 0.36 and 0.44 sitting in the lower half of [1/3, 2/3)—the empirical bin mean (0.40) diverges from the geometric midpoint (0.50). Always anchor horizontal coordinates at mean_predicted and keep row counts n visible alongside every plotted point so sparse bins do not masquerade as dense ones. If a bin holds zero rows, it has no empirical mean or observed fraction—report it as empty, never as 0.

Recap

  • Record [count, mean_predicted, observed_fraction] for every disjoint bin across [0, 1].
  • Plot mean_predicted on the horizontal axis rather than geometric bin centers 1/6, 1/2, 5/6.
  • Read points above the diagonal as underprediction (mean_pred below obs_frac) and points below as overprediction.