== Listing 6.1: the synthetic cohort ==
n=6000  features=['age', 'sbp', 'creatinine', 'lactate', 'comorbidity', 'hba1c']  note embedding dim=16
group  share  true 12m event rate  mean true eta  cens before 12m
  0    0.550        0.152              -0.004         0.328
  1    0.299        0.242               0.701         0.341
  2    0.151        0.271               0.747         0.347
all: true 12m event rate 0.197, median event time 37.2 months
fraction with an observed event: 0.297  (censored: 0.703)
crude share with an event observed by 12 months: 0.161  (truth 0.197)
sd of measurement noise (observed minus latent) by group: 0.35, 0.50, 1.01

== Listing 6.2: Cox partial likelihood and its gradient ==
distinct times among 300 patients: 63 (ties present)
loss, cumulative-sum version vs O(n^2) definition: 1.7677405277 vs 1.7677405277
max |analytic gradient - central finite difference| = 1.60e-10
gradient norm at the fitted optimum: 2.91e-06
covariate     true beta (latent z)   fitted beta (noisy x)
age               0.45                  0.372
sbp               0.20                  0.203
creatinine        0.35                  0.307
lactate           0.40                  0.328
comorbidity       0.30                  0.217
hba1c             0.15                  0.151

== Listing 6.3: discrete-time hazards as logistic models ==
person-period rows (monthly, up to 24): 57285 from 4000 patients
mean true S(12|x): 0.8028   share alive at 12 months (latent T): 0.8033
cox    mean predicted S(12|x) 0.8137   RMSE vs true S(12|x) 0.1455
logit  mean predicted S(12|x) 0.8173   RMSE vs true S(12|x) 0.1455
gbm    mean predicted S(12|x) 0.8089   RMSE vs true S(12|x) 0.1465

== Listing 6.4: concordance under censoring ==
events observed: original censoring 0.297, heavy censoring 0.130, none 1.000
risk score          C (original)  C (heavy cens.)  C (no censoring)
true eta (oracle)     0.7668       0.7789         0.7409
Cox linear predictor  0.6909       0.7091         0.6561
discrete logit        0.6910       0.7095         0.6562
discrete GBM          0.6852       0.7020         0.6523

== Listing 6.5: IPCW Brier score and the bias of naive scoring ==
Cox model, 40 test cohorts of 3000 patients, horizon 12 months
estimator                       mean     bias vs oracle   sd across cohorts
oracle (uses latent T)        0.1413   +0.0000          0.0045
IPCW (Graf)                   0.1414   +0.0001          0.0050
censored as survivors         0.1243   -0.0171          0.0041
complete cases only           0.1584   +0.0171          0.0055

Model comparison on one test cohort (n=6000):  null model S(12) = 0.8210
model            oracle Brier   IPCW Brier
null (marginal KM)  0.1583         0.1566
Cox                 0.1405         0.1386
discrete logit      0.1405         0.1386
discrete GBM        0.1406         0.1388

== Listing 6.6: calibration of 12-month risk ==
calibration cohort: 3000 patients, 914 censored before 12 months
Ch.1 ECE on complete cases only vs IPCW-weighted ECE vs oracle ECE (Cox, test cohort)
  complete-case ECE 0.0440   IPCW ECE 0.0165   oracle ECE 0.0126
  mean forecast 0.1863; observed 12m rate: complete cases 0.2312, IPCW 0.1941, oracle 0.1967
model / recalibration                       oracle ECE   oracle Brier   REL (Brier decomp.)
Cox, raw                                    0.0126       0.1405         0.00074
GBM, raw                                    0.0196       0.1406         0.00122
GBM + Platt on complete cases (Ch.1)        0.0344       0.1418         0.00212
GBM + IPCW-weighted Platt                   0.0156       0.1404         0.00090
GBM + IPCW-weighted isotonic                0.0263       0.1411         0.00096
Cox + IPCW-weighted Platt                   0.0128       0.1404         0.00053

== Listing 6.7: conformal intervals for a biomarker ==
ridge on tabular + note embedding; alpha=0.1, n_cal=600, 200 random splits; R2 of the point model: 0.531
method     group   mean coverage   mean width   share of splits below 0.88
pooled     0       0.957           2.394        0.000
pooled     1       0.912           2.394        0.010
pooled     2       0.678           2.394        1.000
mondrian   0       0.903           1.980        0.075
mondrian   1       0.905           2.361        0.160
mondrian   2       0.906           3.995        0.185

== Listing 6.8: conformal sets for the 12-month event ==
alpha=0.1, n_cal=2000, 100 splits; event rate 0.199, share of non-events 0.801
method               cov    cov | event   cov by group (0,1,2)     singletons  empty
marginal             0.899  0.504       0.918 0.890 0.847           0.753       0.000
mondrian by group    0.900  0.504       0.899 0.901 0.898           0.755       0.000
label-conditional    0.900  0.899       0.927 0.879 0.846           0.408       0.000

== Listing 6.9: decision curve analysis ==
pt     cost ratio c_fn/c_fp   NB model (IPCW)  NB model (oracle)  NB treat-all (IPCW)  NB raw GBM (oracle)
0.05    19.00                 0.1527           0.1552             +0.1517              0.1542
0.10     9.00                 0.1120           0.1142             +0.1045              0.1094
0.15     5.67                 0.0803           0.0820             +0.0519              0.0816
0.20     4.00                 0.0621           0.0630             -0.0074              0.0646
0.30     2.33                 0.0376           0.0358             -0.1513              0.0355
0.40     1.50                 0.0185           0.0178             -0.3432              0.0180
IPCW net benefit gain over treat-all at pt=0.10: 0.0074
flagged share at pt=0.05, 0.10, 0.20: 0.955, 0.772, 0.403

== Listing 6.10: fairness audit by subgroup ==
(a) group-blind recalibrated Cox score; rule: flag if risk >= 0.25
group  prevalence  mean score  ECE_w   flagged   TPR    FPR    PPV   E[S|Y=1]  E[S|Y=0]
  0      0.153      0.181     0.033   0.217    0.475  0.171  0.335   0.270     0.165
  1      0.236      0.230     0.026   0.350    0.633  0.262  0.427   0.327     0.199
  2      0.262      0.225     0.077   0.318    0.457  0.269  0.377   0.294     0.200
    FPR implied by prevalence, PPV, FNR (Theorem 6.5): 0.171, 0.262, 0.269
(b) after Platt recalibration within each group; same rule
group  prevalence  mean score  ECE_w   flagged   TPR    FPR    PPV   E[S|Y=1]  E[S|Y=0]
  0      0.153      0.151     0.025   0.169    0.402  0.127  0.365   0.253     0.133
  1      0.236      0.268     0.039   0.457    0.729  0.373  0.377   0.373     0.235
  2      0.262      0.248     0.028   0.403    0.580  0.340  0.377   0.290     0.233
    FPR implied by prevalence, PPV, FNR: 0.127, 0.373, 0.340
(c) group-calibrated scores, group-specific thresholds equalizing FPR at 0.225: 0.190, 0.314, 0.296
group  TPR    FPR    PPV
  0    0.534  0.225  0.301
  1    0.609  0.226  0.455
  2    0.417  0.227  0.395

== Listing 6.11: multimodal fusion ==
late-fusion combiner weights on (tabular lp, note lp): 0.824, 1.011
20 test cohorts of 3000 patients (mean over cohorts)
model           Harrell C   IPCW Brier   oracle Brier
tabular         0.6919      0.1388       0.1399
notes only      0.6289      0.1502       0.1512
early fusion    0.7214      0.1327       0.1337
late fusion     0.7173      0.1338       0.1347
late minus tabular: C +0.0254 (sd 0.0051, positive in 20/20), IPCW Brier -0.0050 (sd 0.0018, negative in 20/20)
Harrell C by subgroup on the main test cohort (tabular vs late fusion)
  group 0: 0.6965 -> 0.7166
  group 1: 0.6944 -> 0.7133
  group 2: 0.6434 -> 0.6813

== Listing 6.12: leakage from future codes ==
coefficient on log(1 + code count): 1.857  (largest of all coefficients: 0.237)
model                         setting              Harrell C   IPCW Brier   mean risk   true mean risk
with codes                    retrospective test   0.8572      0.1047       0.1621      0.1972
with codes                    deployment time      0.6090      0.1678       0.0724      0.1972
without codes                 deployment time      0.6909      0.1386       0.1863      0.1972

== Listing 6.13: shift to a second hospital ==
quantity                               hospital A   hospital B
Harrell C                              0.6909       0.6957
mean predicted 12m risk                0.1863       0.3131
true 12m event rate                    0.1967       0.3872
observed / expected (IPCW)             1.0491       1.2344
ECE (oracle labels)                    0.0126       0.0741
IPCW Brier                             0.1386       0.2008
after Platt recalibration on 500 local patients: ECE 0.0247, mean risk 0.3796, C unchanged (monotone map)
net benefit at hospital B (oracle labels), raw vs locally recalibrated score:
  pt=0.30: raw 0.1726   recalibrated 0.1763   treat-all +0.1245
  pt=0.40: raw 0.1255   recalibrated 0.1297   treat-all -0.0214
  pt=0.50: raw 0.0860   recalibrated 0.0942   treat-all -0.2257
biomarker, Mondrian interval calibrated at A, 90% target: coverage at B 0.863
same, recalibrated on the 500 local patients only: coverage at B 0.916

== Listing 6.14: immortal time bias ==
true treatment effect: none (log hazard ratio 0). 50 simulated cohorts of 3000
share ever treated: 0.691
naive 'ever treated' Cox:  mean log HR -1.478  (HR 0.228),  sd 0.055
landmark at 6 months:       mean log HR +0.008  (HR 1.008),  sd 0.063
