== Listing 5.1: coverage of intervals for test accuracy ==
n=50, nominal 0.95, repetitions=4000
     p             Hoeffding                  Wald                Wilson       Clopper-Pearson
  0.50        0.993 (w 0.384)        0.936 (w 0.274)        0.934 (w 0.265)        0.962 (w 0.287)
  0.80        1.000 (w 0.366)        0.942 (w 0.218)        0.955 (w 0.215)        0.963 (w 0.233)
  0.90        1.000 (w 0.292)        0.881 (w 0.162)        0.968 (w 0.167)        0.970 (w 0.180)
  0.95        1.000 (w 0.242)        0.923 (w 0.112)        0.958 (w 0.129)        0.988 (w 0.137)
  0.99        1.000 (w 0.202)        0.400 (w 0.034)        0.910 (w 0.085)        0.986 (w 0.087)
n=500, nominal 0.95, repetitions=4000
     p             Hoeffding                  Wald                Wilson       Clopper-Pearson
  0.50        0.994 (w 0.121)        0.947 (w 0.088)        0.944 (w 0.087)        0.957 (w 0.089)
  0.80        1.000 (w 0.121)        0.948 (w 0.070)        0.948 (w 0.070)        0.960 (w 0.072)
  0.90        1.000 (w 0.121)        0.943 (w 0.053)        0.958 (w 0.053)        0.965 (w 0.054)
  0.95        1.000 (w 0.110)        0.930 (w 0.038)        0.946 (w 0.038)        0.959 (w 0.040)
  0.99        1.000 (w 0.071)        0.866 (w 0.017)        0.963 (w 0.019)        0.977 (w 0.020)

== Listing 5.2: how many test examples to tell two models apart ==
one-sided level 0.05, target power 0.8, repetitions=2000
  gap  disagree  n_Hoeff  n_McNemar  power Hoeff@n_H  power McN@n_H  power McN@n_M  power Hoeff@n_M
 0.02      0.10    44984       1544            1.000          1.000          0.782            0.000
 0.05      0.20     7198        493            1.000          1.000          0.760            0.001
 0.05      0.10     7198        246            1.000          1.000          0.749            0.000
 0.10      0.30     1800        184            1.000          1.000          0.759            0.017

== Listing 5.3: bootstrap interval coverage, and where it fails ==
nominal 0.95, B=400, repetitions=1500
(a) mean of an exponential(1) sample (true mean 1)
  n= 10  percentile 0.850   basic 0.830
  n= 30  percentile 0.907   basic 0.892
  n=100  percentile 0.932   basic 0.931
(b) accuracy with 10 test items, true accuracy 0.9
  percentile coverage 0.662; interval of zero width in 0.335 of runs
(c) maximum of a Uniform(0, 1) sample (true value 1)
  n= 20  percentile 0.000   basic 0.864   P(bootstrap max = sample max) = 0.642
  n=200  percentile 0.000   basic 0.873   P(bootstrap max = sample max) = 0.633

== Listing 5.4: paired comparison, type-I error and power ==
n=200, two-sided level 0.05, repetitions=1000, bootstrap B=300, sign flips=499
scenario                         McNemar exact      McNemar chi2      sign-flip MC  paired bootstrap   unpaired z-test
null  (p10=p01=0.08)                     0.029             0.051             0.029             0.052             0.023
alt   (p10=0.12, p01=0.04)               0.764             0.826             0.767             0.821             0.711
example n=12, b=6, c=2: McNemar exact p = 0.2891, exact sign-flip p = 0.2891
exact type-I error of the exact test, n=12, 16 percent discordant: 0.0002
exact type-I error of the exact test, n=40, 16 percent discordant: 0.0141

== Listing 5.5: multiple comparisons, FWER and FDR ==
m=100, true effects m1=0, shift=0.0, level 0.05, repetitions=5000, BH value of m0*q/m = 0.050
  procedure       FWER     FDR   power
  uncorrected    0.995   0.995   0.000
  Bonferroni     0.048   0.048   0.000
  Holm           0.048   0.048   0.000
  BH             0.050   0.050   0.000
m=100, true effects m1=10, shift=3.0, level 0.05, repetitions=5000, BH value of m0*q/m = 0.045
  procedure       FWER     FDR   power
  uncorrected    0.990   0.316   0.912
  Bonferroni     0.043   0.011   0.384
  Holm           0.044   0.011   0.388
  BH             0.273   0.044   0.612
m=100, true effects m1=50, shift=3.0, level 0.05, repetitions=5000, BH value of m0*q/m = 0.025
  procedure       FWER     FDR   power
  uncorrected    0.928   0.051   0.913
  Bonferroni     0.025   0.001   0.385
  Holm           0.034   0.002   0.410
  BH             0.657   0.024   0.836

== Listing 5.6: winner's curse on a leaderboard ==
each model scored on n=2000 test items, errors independent across models, repetitions=2000
true accuracies ~ Normal(0.80, 0.0^2)
     K  test acc of winner  true acc of winner  optimism Hoeffding bound  naive CI cov  Bonf. CI cov
     1              0.7999              0.8000   -0.0001          0.0000         0.952         0.952
     5              0.8104              0.8000    0.0104          0.0201         0.903         0.979
    20              0.8164              0.8000    0.0164          0.0274         0.655         0.984
   100              0.8221              0.8000    0.0221          0.0339         0.104         0.984
  1000              0.8284              0.8000    0.0284          0.0416         0.000         0.987
true accuracies ~ Normal(0.80, 0.02^2)
     K  test acc of winner  true acc of winner  optimism Hoeffding bound  naive CI cov  Bonf. CI cov
     1              0.8000              0.7998    0.0002          0.0000         0.955         0.955
     5              0.8251              0.8210    0.0041          0.0201         0.943         0.983
    20              0.8404              0.8342    0.0062          0.0274         0.904         0.992
   100              0.8544              0.8465    0.0079          0.0339         0.860         0.998
  1000              0.8696              0.8600    0.0096          0.0416         0.787         0.998

== Listing 5.7: classifier two-sample test for covariate shift ==
500 samples per domain, 5 features, logistic regression, 5-fold AUC, 200 permutations
  mean shift of x1     AUC   perm. p-value  oracle AUC
               0.0   0.471           0.846       0.500
               0.2   0.582           0.005       0.556
               0.4   0.618           0.005       0.611
               0.8   0.723           0.005       0.714
variance doubled in x1 (same mean): AUC 0.519, p-value 0.189  (linear detector is blind)

== Listing 5.8: black box shift estimation (BBSE) ==
source priors [0.333 0.333 0.333], target priors [0.6 0.3 0.1]
joint confusion matrix C[i,j] = P_s(pred=i, y=j):
[[0.243 0.039 0.042]
 [0.04  0.248 0.047]
 [0.046 0.046 0.249]]
condition number of C: 1.66
true weights q_t/q_s: [1.8 0.9 0.3]
BBSE weights:         [1.804 0.97  0.248]
naive prediction frequencies: [0.487 0.324 0.189]
BBSE target priors:           [0.597 0.321 0.082]
mean L1 error of the estimated target priors, 200 repetitions
  n (val = target)     naive      BBSE
               500    0.2327    0.1192
              2000    0.2268    0.0577
              8000    0.2253    0.0293
             32000    0.2261    0.0143
classes 2 and 3 almost identical: median cond(C) = 30.4, mean L1 error at n=8000: BBSE 0.2500, naive 0.2446

== Listing 5.9: importance weighting and effective sample size ==
source N(0,1), target N(mu,1); n_s=1000; estimate the squared-error risk of a fixed linear model
  mu true risk  naive bias  IW bias  IW rmse SNIW rmse est-w rmse  ESS/n exp(-mu^2)
 0.0     0.192      -0.001   -0.001    0.018     0.018      0.018  1.000      1.000
 0.5     0.269      -0.077   -0.001    0.061     0.059      0.061  0.780      0.779
 1.0     0.558      -0.365    0.007    0.266     0.241      0.245  0.380      0.368
 1.5     1.215      -1.023    0.033    0.891     0.730      0.832  0.139      0.105
 2.0     2.470      -2.278   -0.208    2.050     1.425      2.133  0.052      0.018

== Listing 5.10: n-gram overlap contamination detector ==
corpus: 1500 documents of 200 tokens, vocabulary 5000; items of 40 tokens
rule: flag an item if any n-gram in the corpus
  n           clean  clean+template        verbatim      10% edited      25% edited  (1-0.1)^n (1-0.25)^n
  1           1.000           1.000           1.000           1.000           1.000      0.900      0.750
  2           1.000           1.000           1.000           1.000           1.000      0.810      0.562
  3           0.993           1.000           1.000           1.000           1.000      0.729      0.422
  4           0.747           1.000           1.000           1.000           1.000      0.656      0.316
  6           0.040           1.000           1.000           1.000           0.920      0.531      0.178
  8           0.000           0.320           1.000           0.990           0.770      0.430      0.100
 13           0.000           0.000           1.000           0.800           0.210      0.254      0.024
rule: flag an item if at least half of the n-grams in the corpus
  n           clean  clean+template        verbatim      10% edited      25% edited  (1-0.1)^n (1-0.25)^n
  1           1.000           1.000           1.000           1.000           1.000      0.900      0.750
  2           0.967           1.000           1.000           1.000           1.000      0.810      0.562
  3           0.027           0.060           1.000           1.000           0.830      0.729      0.422
  4           0.000           0.000           1.000           0.890           0.200      0.656      0.316
  6           0.000           0.000           1.000           0.620           0.050      0.531      0.178
  8           0.000           0.000           1.000           0.420           0.000      0.430      0.100
 13           0.000           0.000           1.000           0.230           0.000      0.254      0.024
mean overlap fraction at n=8:
  clean: 0.000  clean+template: 0.010  verbatim: 1.000  10% edited: 0.459  25% edited: 0.120

== Listing 5.11: permutation test for memorized items ==
trigram model trained on the corpus; 99 random re-orderings per item; level 0.05
  clean            rejected at 0.05: 0.033   median p-value 0.490
  clean+template   rejected at 0.05: 0.840   median p-value 0.010
  verbatim         rejected at 0.05: 1.000   median p-value 0.010
  10% edited       rejected at 0.05: 1.000   median p-value 0.010
  25% edited       rejected at 0.05: 1.000   median p-value 0.010
Benjamini-Hochberg at q=0.05 over 500 items: 343 flagged, 43 false discoveries, 300 of 300 contaminated items found
same, after dropping the templated clean items: 304 flagged, 4 false discoveries
