# 10,000 simulated studies: 15 diseased and 15 healthy mice, log-normal marker with a 0.9-SD shift (true AUC 0.74).
# In each study the Youden cut-off is chosen on the data; its sensitivity + specificity is then measured (a) on the
# same data and (b) on a fresh study of the same size. Also the coverage of the DeLong 95% CI for the true AUC.
import numpy as np
from scipy import stats
rng = np.random.default_rng(12)
true_auc = stats.norm.cdf(0.9 / np.sqrt(2))
def draw(): return np.exp(1.54 + rng.normal(0, 0.6, 15)), np.exp(1.0 + rng.normal(0, 0.6, 15))
runs, ins, outs, cover = 10_000, [], [], 0
for _ in range(runs):
    pos, neg = draw()
    psi = (pos[:, None] > neg[None, :]).astype(float); auc = psi.mean()
    se = np.sqrt(psi.mean(1).var(ddof=1) / 15 + psi.mean(0).var(ddof=1) / 15)
    cover += abs(auc - true_auc) <= 1.96 * se
    cuts = np.sort(np.r_[pos, neg])
    j = [(pos >= c).mean() + (neg < c).mean() for c in cuts]
    c = cuts[int(np.argmax(j))]
    ins.append(max(j)); p2, n2 = draw(); outs.append((p2 >= c).mean() + (n2 < c).mean())
print(f"true AUC = {true_auc:.3f}; Youden cut-off sensitivity + specificity: same data {np.mean(ins):.2f}, new data {np.mean(outs):.2f}; DeLong 95% CI coverage {cover / runs:.1%}")
