== Listing 1.1: propriety of scoring rules, numerically ==
true probability q = 0.3; reports p on a grid of 99 points
rule              argmin p  E score at p=q  E score at p=0.5
log score             0.30          0.6109            0.6931
Brier score           0.30          0.2100            0.2500
absolute error        0.01          0.4200            0.5000
entropy H(q) = 0.6109;  max |E log score - H(q) - KL(q||p)| = 8.88e-16
q(1-q) = 0.2100;  max |E Brier - q(1-q) - (p-q)^2| = 1.67e-16

== Listing 1.2: Brier decomposition into reliability, resolution, uncertainty ==
n = 50000; the world has known probabilities; forecasts are rounded to one decimal
forecaster               Brier     REL     RES     UNC  REL-RES+UNC
true probability        0.1744  0.0001  0.0673  0.2416       0.1744
overconfident (1.6 g)   0.1811  0.0068  0.0672  0.2416       0.1811
same forecasters, unrounded, grouped into 10 equal-width bins
true probability        0.1736  0.0000  0.0672  0.2416       0.1744   remainder -0.0008
overconfident (1.6 g)   0.1800  0.0061  0.0667  0.2416       0.1810   remainder -0.0010

== Listing 1.3: reliability table of an overconfident forecaster ==
n = 50000; forecast = sigmoid(1.6 g), truth = sigmoid(g)
      bin  count  mean forecast  observed freq  mean true q
[0.0,0.1)   8855          0.042          0.118        0.115
[0.1,0.2)   5140          0.147          0.251        0.249
[0.2,0.3)   4016          0.249          0.337        0.333
[0.3,0.4)   3566          0.349          0.397        0.404
[0.4,0.5)   3348          0.450          0.468        0.469
[0.5,0.6)   3388          0.550          0.522        0.532
[0.6,0.7)   3583          0.650          0.599        0.596
[0.7,0.8)   4002          0.751          0.668        0.667
[0.8,0.9)   5126          0.853          0.747        0.751
[0.9,1.0)   8976          0.957          0.877        0.884
binned ECE (10 bins) = 0.0732

== Listing 1.4: binned ECE of perfectly calibrated forecasts ==
forecasts p ~ Beta(2,2), labels y ~ Bernoulli(p); mean ECE over 200 repetitions
each cell: simulated / first-order approximation sqrt(2/(pi n)) sum_b sqrt(w_b v_b)
      n          5 bins         10 bins         20 bins         50 bins        100 bins
    100 0.0761 / 0.0751 0.1000 / 0.1041 0.1427 / 0.1462 0.2163 / 0.2305 0.2781 / 0.3258
   1000 0.0225 / 0.0237 0.0323 / 0.0329 0.0463 / 0.0462 0.0722 / 0.0729 0.1012 / 0.1030
  10000 0.0075 / 0.0075 0.0103 / 0.0104 0.0147 / 0.0146 0.0229 / 0.0230 0.0324 / 0.0326

miscalibrated forecaster: y ~ Bernoulli(0.9 p + 0.05); population ECE = 0.0188
n =    100: mean estimated ECE (10 bins) = 0.1054
n =   1000: mean estimated ECE (10 bins) = 0.0383
n =  10000: mean estimated ECE (10 bins) = 0.0210

== Listing 1.5: temperature scaling on a three-class synthetic problem ==
model logits = 2.5 x true logits; calibration n = 2000, test n = 20000
fitted T = 2.581   (the value that undoes the distortion is 2.5)
smallest second difference of NLL on a grid in beta = 1/T: 8.80e-07
smallest second difference of NLL on a grid in T:          -2.24e-06
            test NLL  top-label ECE  KL(true||model)  accuracy
raw           0.8166         0.1392           0.2248    0.7460
scaled        0.5974         0.0093           0.0002    0.7460
truth         0.5971         0.0076           0.0000    0.7460

== Listing 1.6: the PAV algorithm on ten sorted outcomes ==
outcomes sorted by score: [0, 1, 0, 0, 1, 1, 0, 1, 1, 1]
PAV fit:                  [0.0, 0.333, 0.333, 0.333, 0.667, 0.667, 0.667, 1.0, 1.0, 1.0]
max |PAV - scikit-learn| = 0.0e+00
squared error of the fit = 1.333 (identity fit y_i itself would give 0, but it is not monotone)

== Listing 1.7: calibration-set size and the choice of calibrator ==
error = 1000 x mean (p_hat - q)^2 on 20000 test points, mean over 100 calibration sets

setting A: score = 2.5 g + 1 (sigmoid-shaped distortion)
 n_cal      raw  temperature    Platt  isotonic  iso levels
    50    24.62         7.17     6.48     17.27         5.8
   200    24.62         4.93     1.15      5.84        10.6
  1000    24.62         4.56     0.32      1.96        21.4
  5000    24.62         4.46     0.06      0.64        39.1

setting B: score = 1.2 g + 0.15 g^3 (non-sigmoid distortion)
 n_cal      raw  temperature    Platt  isotonic  iso levels
    50     3.80         4.03     7.82     18.30         5.7
   200     3.80         2.33     3.74      6.49        10.7
  1000     3.80         1.72     1.90      1.99        20.6
  5000     3.80         1.65     1.68      0.65        39.2

== Listing 1.8: breast-cancer data, three classifiers, two calibrators ==
569 cases: 200 train, 150 calibration, 219 test; mean over 30 random splits
log loss clips probabilities to [1e-6, 1-1e-6]; ECE uses 10 equal-width bins
model        calibrator  accuracy  log loss   Brier     ECE
logistic     raw            0.971     0.088  0.0235  0.0289
logistic     Platt          0.970     0.117  0.0244  0.0287
logistic     isotonic       0.968     0.251  0.0281  0.0251
naive Bayes  raw            0.936     0.454  0.0588  0.0608
naive Bayes  Platt          0.937     0.161  0.0444  0.0421
naive Bayes  isotonic       0.934     0.290  0.0466  0.0403
forest       raw            0.952     0.149  0.0378  0.0458
forest       Platt          0.951     0.153  0.0380  0.0437
forest       isotonic       0.949     0.252  0.0393  0.0322

== Listing 1.9: temperature scaling on the digits data ==
1797 cases: 500 train, 400 calibration, 897 test; mean over 10 random splits
model                     T  accuracy     NLL  top-label ECE
logistic     raw          -     0.947   0.357         0.0403
logistic     scaled    3.10     0.947   0.177         0.0152
naive Bayes  raw          -     0.833   2.993         0.1580
naive Bayes  scaled    6.35     0.833   0.651         0.0404
forest       raw          -     0.954   0.456         0.2700
forest       scaled    0.30     0.954   0.154         0.0155

== Listing 1.10: calibration versus accuracy versus sharpness ==
n = 100000; sharpness = variance of the forecasts; RES, REL from 10 equal-width bins
forecaster                  acc   Brier     REL     RES     ECE   sharp
climatology (base rate)   0.502  0.2500  0.0000  0.0000  0.0000  0.0000
true probability          0.735  0.1762  0.0000  0.0729  0.0025  0.0733
two-level, calibrated     0.735  0.1948  0.0000  0.0552  0.0012  0.0548
overconfident 1.6 g       0.735  0.1820  0.0055  0.0724  0.0699  0.1152
hard 0.01 / 0.99          0.735  0.2599  0.0651  0.0552  0.2551  0.2401
uncertainty term of the data: 0.2500

== Listing 1.11: calibration and cost-sensitive decisions ==
true log-odds g ~ N(-1, 2^2); model score = g - 1.5; calibration set of 2000 labelled cases
Platt fit on the calibration set: a = 0.991, b = 1.523
c_FP = 1, c_FN = 5, t* = 0.1667; threshold minimizing empirical cost of the raw model: 0.04
expected cost per 1000 cases, computed exactly from the true probabilities
oracle (true q, t*)             422.9   flagged 0.620
raw model at t*                 663.2   flagged 0.328
raw model, tuned threshold      423.3   flagged 0.633
Platt-calibrated at t*          423.1   flagged 0.630
isotonic-calibrated at t*       427.7   flagged 0.668

across cost ratios (cost per 1000 cases): raw at t* / Platt at t* / oracle
 c_FN      t*      raw    Platt   oracle
    1  0.5000    266.7    204.2    204.2
    2  0.3333    426.1    296.9    296.8
    5  0.1667    663.2    423.1    422.9
   10  0.0909    802.4    504.3    504.1
   20  0.0476    869.4    564.7    564.5
