== Listing 0.1: the triage running example ==
costs: c_FP = 1, c_FN = 20; Bayes threshold t* = 0.0476
n = 200000; prevalence P(Y=1) = 0.1168; mean of true q = 0.1167
true q percentiles 10/50/90/99: 0.008 0.042 0.336 0.881
share of cases with q > t*: 0.4663
test n = 100000: AUC of true q = 0.8681; AUC of logistic model (trained on 5000) = 0.8360
mean squared error to true q: logistic model = 0.00647

== Listing 0.2: the Bayes threshold t* = c_FP / (c_FP + c_FN) ==
general rule argmin_a E[L(a,Y)|x] agrees with the rule q > t* on 1.0000 of cases
policy                      expected cost  realized cost  escalated
never escalate                     2.3322         2.3400     0.0000
always escalate                    0.8834         0.8830     1.0000
threshold 0.5                      1.5337         1.5482     0.0572
threshold t* = 0.0476              0.5778         0.5787     0.4687
grid search over thresholds in [0.005, 0.5): best threshold = 0.0475
logistic model at t*: expected cost under true q = 0.6621, realized = 0.6624

== Listing 0.3: mean, median and quantile as Bayes point forecasts ==
loss                argmin on grid  sample statistic
squared                      1.650             1.649  (mean)
absolute                     1.005             1.005  (median)
pinball tau=0.9              3.640             3.638  (0.9 quantile)
newsvendor with c_u = 9, c_o = 1: critical fractile = 0.90
expected cost of stocking the mean = 6.295, the median = 8.196, the 0.9 quantile = 4.720

== Listing 0.4: EVPI and EVSI for one patient ==
prior P(urgent) = 0.10; expected loss: do nothing = 2.000, escalate = 0.900
prior Bayes risk = 0.900; EVPI = 0.900
test (sens, spec)    P(+)  P(urg|+)  P(urg|-)    EVSI  <= EVPI
(0.90, 0.80)         0.270    0.3333    0.0137   0.520     True
(0.99, 0.50)         0.549    0.1803    0.0022   0.430     True
(0.50, 0.80)         0.230    0.2174    0.0649   0.000     True
(0.60, 0.95)         0.105    0.5714    0.0447   0.055     True
Monte Carlo (n = 400000) for the (0.90, 0.80) test: mean cost with test = 0.383; saving vs 0.900 = 0.517

== Listing 0.5: accuracy and AUC versus expected cost ==
prevalence = 0.10, c_FP = 1, c_FN = 20; thresholds tuned on validation, results on test
model                            AUC  accuracy    cost    FNR    FPR
A (blind to a subtype)        0.8306    0.9107  0.6945  0.319  0.064
B (smooth scorer)             0.8213    0.5605  0.6437  0.108  0.476
always negative               0.5000    0.9001  1.9973  1.000  0.000
cost of each model, with its own tuned threshold, as c_FN varies (c_FP = 1)
  c_FN   cost A   cost B  cheaper
     1   0.0428   0.0932        A
     2   0.0809   0.1700        A
     5   0.1896   0.3326        A
    10   0.3635   0.4895        A
    20   0.6945   0.6437        B
    50   0.9068   0.7939        B

== Listing 0.6: net benefit and decision curves ==
test n = 100000, prevalence = 0.1163
      t  treat none  treat all  logistic   true q
 0.0200      0.0000     0.0982    0.0996   0.1013
 0.0476      0.0000     0.0721    0.0839   0.0881
 0.1000      0.0000     0.0181    0.0647   0.0721
 0.2000      0.0000    -0.1047    0.0447   0.0535
 0.3000      0.0000    -0.2625    0.0326   0.0406
 0.5000      0.0000    -0.7675    0.0168   0.0235
identity cost(treat none) - cost(rule) = c_FN * NB(t): max abs difference over 4 cost ratios = 6.94e-18

== Listing 0.7: cost curves ==
prevalence = 0.1153; probability cost at c_FP = 1, c_FN = 20 is PC = 0.7228
normalized cost of threshold t* at that PC = 0.2072; cost / (pi c_FN + (1-pi) c_FP) = 0.2072
   PC  always neg  always pos  envelope  best threshold
 0.05       0.050       0.950    0.0470          0.6093
 0.10       0.100       0.900    0.0868          0.4874
 0.20       0.200       0.800    0.1511          0.3705
 0.30       0.300       0.700    0.1966          0.2644
 0.50       0.500       0.500    0.2385          0.1397
 0.70       0.700       0.300    0.2120          0.0642
 0.90       0.900       0.100    0.0980          0.0052

== Listing 0.8: calibration and the Bayes threshold ==
n = 200000; threshold t* = 0.0476 applied to each forecast; exact expected cost under the true q
forecast                       AUC  cost at t*  cost, tuned thr.  tuned thr.
true q                      0.8727      0.5767            0.6076      0.0761
under-confident 0.5 g       0.8727      0.8733            0.6076      0.2229
over-confident 2.0 g        0.8727      0.8593            0.6076      0.0067
shifted by +1.5             0.8727      0.7554            0.6076      0.2695
shifted by -1.5             0.8727      0.8599            0.6076      0.0180
g denotes the true log-odds logit(q); the tuned threshold uses only the first 4000 labels.

== Listing 0.9: abstention with two thresholds ==
c_FP = 1, c_FN = 20; no-deferral cost with true q = 0.5782
  c_D    t_lo    t_hi  deferred  cost (true q)  cost (logistic)
 0.10  0.0050  0.9000    0.9530         0.0982           0.1073
 0.30  0.0150  0.7000    0.7536         0.2691           0.2981
 0.50  0.0250  0.5000    0.5875         0.4026           0.4512
 0.80  0.0400  0.2000    0.3453         0.5447           0.6210
 0.95  0.0475  0.0500    0.0131         0.5782           0.6619
 1.00  0.0500  0.0000    0.0000         0.5782           0.6619
defer region is nonempty iff c_D < c_FP c_FN / (c_FP + c_FN) = 0.9524
Chow check (c_FP = c_FN = 1, c_D = 0.2): thresholds (0.20, 0.80); agreement with reject-if-max-posterior-below-0.8 on 200000 draws: 1.0000
