== Listing 5.1: Frechet distance between Gaussians ==
closed form d^2 = 7.2229
tr sqrt via eig(S1 S2) = 2.354309, via symmetric form = 2.354309
optimal map            mean cost 7.2185   covariance of Y matches S2: True
random rotation of it  mean cost 10.6279   covariance of Y matches S2: True
independent            mean cost 11.9181   covariance of Y matches S2: True
rank-deficient case (d=64, n=40, rank 39):
  eigh-based 44.2514   scipy sqrtm 44.2514   imaginary part dropped 4.68e-08
  d=300, n=30: eigh-based 1229.1897   scipy sqrtm 1229.1897   imaginary part dropped 1.18e-06
ill-conditioned diagonal pair: exact 2.609469, eigh-based 2.609469, sqrtm 2.609469

== Listing 5.2: FID is biased upward at finite n ==
d=16, eigenvalues of Sigma: 4.0 * 0.8^i, trace 19.437
     n  mean FID   (s.e.)  predicted  ratio
    20    7.8672   0.0750     7.6691   1.03
    40    3.9342   0.0364     3.8345   1.03
    80    1.9477   0.0202     1.9173   1.02
   160    0.9831   0.0102     0.9586   1.03
   320    0.4797   0.0048     0.4793   1.00
   640    0.2414   0.0024     0.2397   1.01
  1280    0.1245   0.0022     0.1198   1.04
  2560    0.0622   0.0013     0.0599   1.04
mean term at n=100: simulated 0.3923 (s.e. 0.0030), exact 2 tr(Sigma)/n = 0.3887
true FID of model A = 0.490, of model B = 1.000 (A is better)
  A at n=50    mean FID 3.585
  B at n=2000  mean FID 1.086

== Listing 5.3: feature extractor dependence on digits ==
train 800, reference (held-out) 500, generated 500 per generator; classifier test accuracy 0.958
generator              pixels        pca16  random relu   mlp hidden
gaussian               83.052       55.717        0.283        1.227
kde h=1.5              59.121       18.544        0.165        0.370
pixel shuffle         463.067      210.265        1.224        5.167
diag mixture          152.009       82.211        0.396        1.023
knn interpolation       44.077       19.380        0.141        0.291
pca10 gaussian        249.951      125.373        0.759        1.565
rank of each generator (1 = lowest FID):
gaussian                    3            3            3            4
kde h=1.5                   2            1            2            2
pixel shuffle               6            6            6            6
diag mixture                4            4            4            3
knn interpolation            1            2            1            1
pca10 gaussian              5            5            5            5
for scale: real test half vs other half (250 vs 250), pixel FID 74.623
same generators, classifier retrained with 8 seeds (hidden-layer FID):
  gaussian       mean 1.319  min 1.227  max 1.376
  kde h=1.5      mean 0.410  min 0.370  max 0.459
  diag mixture   mean 1.083  min 1.023  max 1.192
  gaussian beats diag mixture in 0 of 8 seeds

== Listing 5.4: FID prefers the worse model ==
real: two blobs at (+-3, 0), sd 0.5; model A: Gaussian with the real mean and covariance;
model B: the same two blobs with weights 0.55 / 0.45
                         model A   model B  real vs real
FID                        0.001     0.084         0.002
precision                  0.594     0.998         0.999
recall                     0.999     0.997         0.996
density                    0.458     1.009         1.012
coverage                   0.686     0.972         0.981
MMD^2 gaussian             0.065     0.008        -0.001
sliced W2^2                0.996     0.382         0.019
share within 3.5 sd of a mode     0.526     0.997         1.000
population FID of B from the moments: 0.0902; of A: 0 by construction
30 repetitions (n=2000): FID of A 0.007 (sd 0.006), FID of B 0.116 (sd 0.076); FID is lower for A than for B in 30 of 30

== Listing 5.5: precision, recall, density and coverage ==
real: 4 blobs, sd 0.5, n=1000; k=5
generated                precision  recall  density  coverage
same law                     0.987   0.994    1.000     0.981
mode collapse (1 of 4)       0.997   0.262    1.003     0.262
mode dropping (2 of 4)       0.995   0.517    1.035     0.514
10% far-away outliers        0.888   0.994    0.905     0.958
too wide (sd 1.2)            0.679   0.999    0.504     0.768
too narrow (sd 0.15)         0.999   0.466    1.018     0.425
same law, n=300 each, 300 repetitions:
  mean precision  0.9828  (s.e. 0.0005)
  mean recall     0.9836  (s.e. 0.0005)
  mean density    0.9982  (s.e. 0.0021)
  mean coverage   0.9681  (s.e. 0.0009)
reference with 5% scattered outliers (50 of 1000):
  clean 4-blob generator       precision 0.998  density 1.037  coverage 0.957
  uniform noise on the square  precision 0.954  density 0.671  coverage 0.313
effect of k on the same-law precision (n=300 each, 100 repetitions):
  k= 1: precision 0.707
  k= 3: precision 0.961
  k= 5: precision 0.983
  k=10: precision 0.994
  k=20: precision 0.999

== Listing 5.6: MMD, kernels and power ==
same law (n=m=20), 3000 repetitions, Gaussian kernel with bandwidth 1:
  unbiased MMD^2: mean +0.00009 (s.e. 0.00063), share negative 0.582
  biased MMD^2:   mean +0.06681; predicted bias (1/n + 1/m)(E k(X,X) - E k(X,X')) = 0.06667  (E k(X,X') = 1/3 for this kernel and N(0,I_2))
alternative: first coordinate bimodal (+-0.97, sd 0.243), second N(0,1): mean 0 and identity covariance, as for the null
median-heuristic bandwidth for N(0,I_2): 1.706
rejection rate at 5% (n=m=100, 200 datasets, 49 permutations):
kernel            same law  alternative
linear               0.055        0.055
cubic poly           0.045        0.060
quartic poly         0.035        0.045
gauss 0.1*med        0.065        1.000
gauss median         0.060        0.070
gauss 10*med         0.070        0.030
FID between the two samples (n=2000 each): 0.0074

== Listing 5.7: likelihood versus sample quality ==
d=100; truth N(0,I). Held-out mean log-likelihood in nats: truth -141.80
  model A -142.49   model B -142.97   (bound for A: truth minus ln(1/eps) = -142.49)
  KL(truth || A) <= ln(1/eps) = 0.693 for any garbage component; KL(truth || B) = d (ln 0.9 + 1/(2 * 0.81) - 1/2) = 1.192
sample quality, judged with the known truth:
  A: 50% garbage   share inside the 99% region 0.495  precision 0.335  recall 0.995
  B: sd 0.9        share inside the 99% region 1.000  precision 0.970  recall 0.243
  truth            share inside the 99% region 0.991  precision 0.693  recall 0.712
2-D ring, 200 training points; KDE generator with bandwidth h (samples = training point + noise):
     h  train loglik  test loglik  precision  recall    memo p
  0.01          2.08      -115.69      1.000   0.560  1.0e-300
  0.03         -0.04       -13.12      0.998   0.703  2.0e-234
  0.10         -1.86        -3.02      0.998   0.962   7.8e-40
  0.25         -2.55        -2.69      0.981   0.996   1.0e+00
  0.50         -2.94        -2.97      0.858   0.999   1.0e+00
  1.00         -3.47        -3.48      0.632   0.999   1.0e+00

== Listing 5.8: memorization test on digits ==
train 800, held-out 500, generated 497. Distances are Euclidean in pixel space.
median nearest-train distance of held-out digits: 17.62
generator             median d    AUC   p-value  below 1%     FID  precision
fresh real digits        17.46  0.487   2.5e-01     0.026   35.18      0.968
copy + noise h=0.0        0.00  0.000  9.9e-188     1.000   43.47      0.972
copy + noise h=0.5        3.95  0.000  9.1e-165     1.000   41.58      0.952
copy + noise h=1.0        8.03  0.001  2.0e-164     1.000   56.57      0.918
copy + noise h=2.0       16.00  0.354   7.7e-16     0.000  128.36      0.660
copy + noise h=3.0       23.67  0.884   1.0e+00     0.000  260.71      0.155
gaussian fit             26.55  0.950   1.0e+00     0.000   82.79      0.698
level check on real digits: the test rejected at 5% in 12 of 300 random partitions (0.040)

== Listing 5.9: pass@k ==
one problem: n=20, k=5, p=0.1; true pass@k = 1-(1-p)^k = 0.409510
  exact mean of unbiased estimator 0.409510; exact mean of plug-in 1-(1-c/n)^k 0.377874
  exact variance of the unbiased estimator 0.050735; formula 0.050735
  variance by (n, k, p):
    n= 10 k= 1 p=0.30: sd 0.1449   (true pass@k 0.3000)
    n= 10 k= 5 p=0.30: sd 0.2092   (true pass@k 0.8319)
    n= 10 k=10 p=0.30: sd 0.1657   (true pass@k 0.9718)
    n=100 k=10 p=0.30: sd 0.0202   (true pass@k 0.9718)
    n=100 k=10 p=0.02: sd 0.1172   (true pass@k 0.1829)
benchmark of m=100 problems, n=10 samples each, p_i ~ Beta(0.4, 1.2); this draw has mean p = 0.234
(a) the problems are fixed, only the samples are redrawn (2000 repetitions):
  k    true  unbiased  plug-in  sd(unb.) sd formula
  1  0.2340    0.2337   0.2337    0.0109     0.0109
  5  0.5256    0.5253   0.4851    0.0210     0.0213
 10  0.6388    0.6375   0.5679    0.0298     0.0296
(b) problems drawn afresh each time; target is the population value (400 repetitions):
  k population  cover normal  cover boot  mean width
  1     0.2500         0.958       0.965      0.1127
  5     0.5217         0.935       0.927      0.1697
 10     0.6258         0.935       0.920      0.1893
fixed budget of 1000 samples, k=1: sd of the pass@1 estimate over repeated draws of problems and samples
  m=  25 problems x n=40 samples: sd 0.0521  (theory 0.0548)
  m= 100 problems x n=10 samples: sd 0.0292  (theory 0.0289)
  m= 500 problems x n= 2 samples: sd 0.0166  (theory 0.0161)
  m=1000 problems x n= 1 samples: sd 0.0134  (theory 0.0137)
two models on the same 100 problems (model B is slightly stronger by construction):
  pass@1  A 0.235 B 0.271  paired diff +0.036 95% CI [+0.006, +0.066]   unpaired s.e. 0.0395 vs paired 0.0151
  pass@10 A 0.660 B 0.700  paired diff +0.040 95% CI [-0.048, +0.128]   unpaired s.e. 0.0662 vs paired 0.0448
truth for the difference: pass@1 +0.036, pass@10 +0.058
n=5 samples with k=10 requested: ValueError (need k <= n)

== Listing 5.10: scorecard for digit generators ==
features: hidden layer of a small classifier (32-d); 500 reference digits, 497 generated per draw; 20 repeated draws (new reference subset and new generated set each time)
generator                       FID     precision        recall   (mean with s.d. over draws)
real (held-out pool)     0.32 (0.07)   0.965 (0.010)   0.965 (0.008)
gaussian                 1.25 (0.10)   0.750 (0.035)   0.818 (0.023)
kde h=1.5                0.44 (0.11)   0.964 (0.008)   0.951 (0.014)
pixel shuffle            4.89 (0.17)   0.609 (0.054)   0.516 (0.032)
diag mixture             0.98 (0.05)   0.936 (0.014)   0.505 (0.022)
knn interpolation        0.37 (0.11)   0.994 (0.003)   0.853 (0.016)
pca10 gaussian           1.61 (0.09)   0.821 (0.024)   0.723 (0.025)
copy of training set     0.37 (0.08)   0.975 (0.008)   0.946 (0.013)
single-draw tests (reference = the 500 held-out test digits, generated = 497 samples):
generator               MMD^2 gauss   MMD p     memo p  memo AUC
real (fresh split)           0.0006    0.29    2.5e-01     0.487
gaussian                     0.0070    0.01    1.0e+00     0.947
kde h=1.5                    0.0009    0.18   1.2e-157     0.011
pixel shuffle                0.0197    0.01    1.0e+00     0.997
diag mixture                 0.0130    0.01    1.0e+00     0.877
knn interpolation           -0.0005    0.57   7.6e-161     0.006
pca10 gaussian               0.0064    0.01    1.0e+00     0.860
copy of training set         0.0010    0.14   9.9e-188     0.000
MMD bandwidth (median heuristic on features): 6.80; 99 permutations, 300 points per side
