== Listing 9.1: embedding-conditioned digits and the guidance sweep ==
digits: 1437 train / 360 test; PCA to 16 coordinates keeps 0.848 of the variance
prompt embedding: 8 binary attributes -> 12 dimensions through a frozen random matrix and tanh; smallest distance between two classes 0.817 (digits 0 and 6), largest cosine similarity 0.918
network: input = 16 latent + 13 time features + 12 embedding + 1 null flag, three hidden layers of 128, 40592 parameters
evaluation classifier (logistic regression on 64 pixels): test accuracy 0.956
700 samples per run, 4 runs, 40 DDIM steps; mean and 95% t-interval over runs
  w     class accuracy   precision   recall   within-class spread   alignment score
  0.0   0.928 +- 0.010   0.949 +- 0.011  0.965 +- 0.017  3.887 +- 0.060   0.875 +- 0.009
  0.5   0.983 +- 0.004   0.950 +- 0.008  0.876 +- 0.046  3.731 +- 0.080   0.914 +- 0.007
  1.0   0.985 +- 0.006   0.913 +- 0.017  0.811 +- 0.052  3.835 +- 0.102   0.922 +- 0.006
  2.0   0.976 +- 0.009   0.707 +- 0.025  0.744 +- 0.029  4.501 +- 0.152   0.909 +- 0.007
  4.0   0.899 +- 0.016   0.210 +- 0.011  0.880 +- 0.037  7.262 +- 0.224   0.839 +- 0.010
real train images (700 of 1437): accuracy 0.983, precision 0.967, recall 0.981, spread 3.836, alignment 0.909

== Listing 9.2: ambiguous and unseen prompts ==
(a) exact mixture, classes 0 and 1 share an embedding: max |optimal eps given the embedding - posterior-weighted class eps| over 3 times, 200 points = 1.2e-14
(b) digits 4 and 9 given the same embedding; training images: 135 fours, 151 nines; 600 samples with that embedding
    w = 0: classified as 4: 243, as 9: 283, other: 74; share of 4 among the two 0.462 (training share 0.472)
    w = 2: classified as 4: 334, as 9: 263, other: 3; share of 4 among the two 0.559 (training share 0.472)
(c) training without any image of the held-out digit, then prompting with its embedding (300 samples):
    held-out 7, w = 0: classified as 7: 0.000; most common outputs: 6 (0.99), 2 (0.00), 4 (0.00)
    held-out 7, w = 2: classified as 7: 0.000; most common outputs: 6 (0.98), 2 (0.01), 4 (0.01)

== Listing 9.3: short-time Fourier transform ==
frame length 64, Hann window; max |istft(stft(x)) - x| for a random signal of 777 samples
  hop  8: frames 107, error 8.9e-16, window energy in the interior: min 3.000, max 3.000
  hop 16: frames  54, error 8.9e-16, window energy in the interior: min 1.500, max 1.500
  hop 32: frames  28, error 8.9e-16, window energy in the interior: min 0.500, max 1.000
  hop 64: frames  15, error 1.8e+00, window energy in the interior: min 0.000, max 1.000
  rectangular window, hop 64: error 6.7e-16
||Ax||^2 = 1116.116894 and sum_n x[n]^2 den[n] = 1116.116894
inconsistent spectrogram Y: objective at the overlap-add inverse 77.382; increase under 5 random perturbations: min 0.1211

== Listing 9.4: Griffin-Lim ==
one rising chirp, random start; relative spectral error sqrt(D(x_k)) / ||S||:
  iteration   0: 0.9901
  iteration   1: 0.4047
  iteration  10: 0.2197
  iteration  50: 0.0578
  iteration 100: 0.0394
  iteration 400: 0.0203
nonincreasing along all 400 iterations: True
correlation of the recovered waveform with the original clip: 0.957
20 clips (5 per class), 200 iterations: relative error median 0.0302 (max 0.1731); |correlation| with the original: median 0.461, minimum 0.042

== Listing 9.5: spectrogram diffusion for synthetic audio ==
4 classes (low tone, high tone, rising chirp, falling chirp), 2 kHz, 512 samples; 250 train and 100 test clips per class
log(1+|STFT|) with frame 64, hop 16: 37 frames x 33 bins = 1221 numbers per clip; PCA to 24 coordinates keeps 0.965 of the variance
classifier on real test spectrograms: accuracy 1.000
200 generated spectrograms per run, 3 runs: classifier accuracy on decoded spectrograms
  w = 0: 0.980 +- 0.043
  w = 1: 1.000 +- 0.000
  w = 2: 1.000 +- 0.000
  w = 4: 1.000 +- 0.000
Griffin-Lim, 150 iterations, 40 clips (10 per class): final relative spectral error, generated magnitudes 0.093 (median), true magnitudes of real test clips 0.051
classifier accuracy on the resynthesized waveforms: generated 1.000, real 1.000
ridge frequency (Hz) of the strongest bin at 20% and 80% of the clip, mean over the class:
  low tone      real  205.3 ->  205.0   generated  196.9 ->  197.5
  high tone     real  651.2 ->  651.2   generated  645.6 ->  643.8
  rising chirp  real  263.8 ->  601.2   generated  273.1 ->  595.6
  falling chirp real  599.4 ->  275.0   generated  596.2 ->  270.6

== Listing 9.6: FID can prefer the worse model ==
feature extractor B: 32 hidden ReLU units of an MLP classifier (test accuracy 0.967); feature extractor A: the 16 PCA coordinates
1000 samples per run, 3 runs, reference = 360 test images; mean and 95% t-interval
  model      FID (A)           FID (B)           precision        recall           classifier confidence
  gaussian   0.339 +- 0.029   1.781 +- 0.143   0.512 +- 0.021  0.922 +- 0.021  0.604 +- 0.005
  w=0        0.322 +- 0.067   0.980 +- 0.044   0.943 +- 0.017  0.952 +- 0.022  0.858 +- 0.010
  w=1        0.937 +- 0.193   0.931 +- 0.069   0.899 +- 0.046  0.772 +- 0.068  0.950 +- 0.004
  w=2        2.766 +- 0.592   1.445 +- 0.143   0.687 +- 0.073  0.701 +- 0.032  0.945 +- 0.012
FID(A) ranks the Gaussian above w=1: True, above w=2: True; FID(B): False, False
class-mean images (one per class, repeated): FID(A) 4.426, classifier confidence 0.979, accuracy 1.000

== Listing 9.7: an alignment score and its limits ==
image encoder: ridge regression from 64 pixels to the 12-dimensional prompt embedding; score = cosine similarity
real test images: score with the right prompt 0.902, with a wrong prompt 0.369; zero-shot accuracy (best of the 10 prompts) 0.775
diffusion samples, w = 0: score 0.871
diffusion samples, w = 1: score 0.925
diffusion samples, w = 4: score 0.847
class-mean images (no diversity at all): score 0.974
images optimized for the score by projected gradient ascent (400 steps from a gray image): score min 0.9993, max 1.0000
  logistic classifier agrees with the intended digit on 10 of 10; the independent MLP on 7 of 10
  nearest training image: median distance 2.82 for the optimized images, 1.01 for real test images
  share of pixels within 0.01 of 0 or 1: 0.019 for optimized images, 0.585 for real test images

== Listing 9.8: watermark detection under the null ==
gamma = 0.25, n = 100 scored tokens: threshold, exact binomial tail, normal tail, Chernoff bound
  z >= 1.645: green count >= 33, exact 4.46e-02, normal 5.00e-02, Chernoff 2.01e-01, Hoeffding 2.78e-01
  z >= 2.326: green count >= 36, exact 9.41e-03, normal 1.00e-02, Chernoff 5.10e-02, Hoeffding 8.89e-02
  z >= 3.000: green count >= 38, exact 2.75e-03, normal 1.35e-03, Chernoff 1.64e-02, Hoeffding 3.40e-02
  z >= 4.000: green count >= 43, exact 6.42e-05, normal 3.17e-05, Chernoff 4.63e-04, Hoeffding 1.53e-03
bigram model on 256 tokens, mean row entropy 4.64 nats (uniform would be 5.55); unwatermarked texts of 100 tokens, 12 keys x 1000 texts
  z > 2 (nominal 0.0228): mean over keys 0.0291; smallest and largest key 0.021, 0.053
  exact binomial p < 0.0228: mean over keys 0.0192; mean z 0.023, range of per-key mean z -0.113 to 0.194
peaked model (row scales 1 to 3), 200 tokens, 4 keys x 1000 texts: sd of z with distinct pairs only 0.975, counting every pair 1.016; share of z > 2: 0.0175 vs 0.0165

== Listing 9.9: detection power as a function of length ==
threshold z > 4 (normal-tail false-positive rate 3.2e-05); 3000 texts of 200 tokens per delta
  delta  mean green prob   Jensen bound   NLL under base model
   0.0   0.246            0.250          4.631
   0.5   0.346            0.355          4.633
   1.0   0.461            0.475          4.646
   2.0   0.692            0.711          4.688
empirical power (share of texts with z > 4) vs the large-n formula, by length of the scored prefix:
  delta   n=20            n=40            n=80            n=160
   0.5    0.006 / 0.003    0.012 / 0.008    0.030 / 0.030    0.139 / 0.131
   1.0    0.070 / 0.053    0.251 / 0.207    0.608 / 0.623    0.967 / 0.971
   2.0    0.759 / 0.707    0.987 / 0.990    1.000 / 1.000    1.000 / 1.000
  (each cell: empirical / predicted)
  delta = 1: length for power 0.50 predicted 67.3; empirical power at length 68 (n_eff 67.7): 0.496
  delta = 1: length for power 0.90 predicted 124.8; empirical power at length 125 (n_eff 124.0): 0.892
  delta = 1: length for power 0.99 predicted 184.8; empirical power at length 185 (n_eff 182.8): 0.987
  lowering the threshold to z > 2 (false-positive rate 0.0228) cuts the length for power 0.90 from 124.8 to 50.0

== Listing 9.10: robustness to editing ==
delta = 1, 3000 texts of 200 tokens, unedited: mean green probability 0.461, mean z 6.78
  eps   attack         green fraction (empirical / predicted)   share z > 4 (empirical / predicted)   length for power 0.9 (formula)
  0.1   substitution   0.420 / 0.421                       0.907 / 0.916                           189.9
  0.1   deletion       0.439 / 0.440                       0.944 / 0.950
  0.2   substitution   0.385 / 0.385                       0.625 / 0.638                           302.6
  0.2   deletion       0.418 / 0.419                       0.778 / 0.792
  0.3   substitution   0.354 / 0.353                       0.298 / 0.282                           512.3
  0.3   deletion       0.398 / 0.398                       0.501 / 0.510
  0.5   substitution   0.305 / 0.303                       0.021 / 0.016                          1932.0
  0.5   deletion       0.358 / 0.356                       0.090 / 0.077
unedited length for power 0.9: 124.8; predicted length at eps = 0.3 over unedited: 4.10, against (1-eps)^-4 = 4.16
substitution with uniformly random tokens, eps = 0.3: green fraction 0.354, share z > 4 0.285
text regenerated from the unwatermarked model: share z > 4 0.0000, mean z -0.13

== Listing 9.11: membership audit of a diffusion model ==
300 members (training set), 300 calibration non-members, 600 evaluation non-members; 16 PCA coordinates; three hidden layers of 128; batch 64
score = mean squared epsilon error over 12 fixed (t, noise) pairs (loss test), or distance to the nearest of 1000 generated samples (NN test); small = member
  steps   epochs   mean loss (member / non)   loss: AUC [95% CI]       TPR@1%  TPR@5%     NN: AUC     TPR@1%
    250       53   4.365 / 4.435            0.532 [0.490, 0.574]   0.010   0.043     0.542     0.007
    500      107   3.928 / 4.057            0.553 [0.513, 0.593]   0.010   0.060     0.551     0.030
   1000      213   3.582 / 3.773            0.574 [0.529, 0.613]   0.013   0.067     0.583     0.010
   2000      427   3.259 / 3.595            0.625 [0.588, 0.663]   0.013   0.077     0.611     0.003
   4000      853   2.954 / 3.564            0.710 [0.674, 0.743]   0.023   0.100     0.658     0.027
   8000     1707   2.715 / 3.714            0.801 [0.771, 0.828]   0.073   0.190     0.702     0.080
final model, per-example calibration with 3 reference models trained on other data (score = loss / mean reference loss, or the z-score against the references):
  raw loss                   AUC 0.801   TPR@1% 0.073   TPR@5% 0.190
  loss minus reference mean  AUC 0.856   TPR@1% 0.110   TPR@5% 0.303
  loss / reference mean      AUC 0.882   TPR@1% 0.140   TPR@5% 0.437
  z against references       AUC 0.853   TPR@1% 0.077   TPR@5% 0.263
conformal p-values from the 300 calibration non-members (score = loss / reference mean):
  alpha = 0.01: rejected among evaluation non-members 0.017, among members 0.197
  alpha = 0.05: rejected among evaluation non-members 0.058, among members 0.477
  alpha = 0.10: rejected among evaluation non-members 0.105, among members 0.647
naive rule 'loss below the median training loss (2.65)': flags 0.500 of members and 0.145 of non-members

== Listing 9.12: what AUC hides ==
constructed audit, 400000 members and 400000 non-members; a share f of members is memorized (score far below every non-member), the rest look like non-members
  f       AUC (formula 1/2 + f/2)    TPR at FPR 0.1% (formula f + (1-f) 0.001)    TPR at FPR 1% (formula)
  0.00    0.4998 (0.5000)           0.0010 (0.0010)                       0.0100 (0.0100)
  0.01    0.5051 (0.5050)           0.0107 (0.0110)                       0.0196 (0.0199)
  0.05    0.5250 (0.5250)           0.0506 (0.0510)                       0.0592 (0.0595)
  0.20    0.6004 (0.6000)           0.2008 (0.2008)                       0.2079 (0.2080)
differential privacy: any (eps, 0)-DP training run forces TPR <= e^eps FPR and, for the area under the ROC curve, AUC <= e^eps / (1 + e^eps); at FPR = 1%:
  eps = 0.5: TPR <= 0.0165; AUC <= e^eps / (1 + e^eps) = 0.6225
  eps = 1.0: TPR <= 0.0272; AUC <= e^eps / (1 + e^eps) = 0.7311
  eps = 2.0: TPR <= 0.0739; AUC <= e^eps / (1 + e^eps) = 0.8808
  eps = 4.0: TPR <= 0.5460; AUC <= e^eps / (1 + e^eps) = 0.9820
nearest-neighbour copying test on 1000 samples of the final model (generated vs held-out distances to the 300 training points): median distance 1.942 vs 1.846, AUC 0.546, p = 1.0e+00, share below the held-out 1st percentile 0.011
a copier (training points plus noise of standard deviation 0.3): median distance 1.165, AUC 0.095, p = 1.1e-162, share below the 1st percentile 0.080

== Listing 9.13: provenance manifests and detection base rates ==
manifest binds SHA-256 fbd79bd7c0824a5b... to the claims; (signature valid, content matches) = (True, True)
flip one bit of one pixel: (True, False)
requantize to 16 levels (max pixel change 15 of 255): (True, False)
edit the claims and keep the old signature: (False, True); verify with a different key: (False, True)
the same kind of change to watermarked text (10% of tokens substituted): share of texts still byte-identical 0.000 (so every hash would fail), share still detected at z > 4 0.907
positive predictive value of a z > 4 flag, using the exact binomial false-positive rate at n = 200 and the unedited power measured in Listing 9.10
  false-positive rate 6.15e-05, detection rate 0.994
  hypothetical share of watermarked texts among all texts 5e-01: P(watermarked | flagged) = 0.9999
  hypothetical share of watermarked texts among all texts 1e-02: P(watermarked | flagged) = 0.9939
  hypothetical share of watermarked texts among all texts 1e-04: P(watermarked | flagged) = 0.6176
  hypothetical share of watermarked texts among all texts 1e-06: P(watermarked | flagged) = 0.0159
