== Listing 1.1: the VP forward process ==
schedule: beta(t) = 0.1 + 19.9 t on [0, 1]; data law 0.4 N(-2, 0.3^2) + 0.6 N(1.5, 0.5^2)
data mean 0.1000, data variance 3.1260; Euler-Maruyama with 1000 steps, 40000 particles
  t     m(t)    sigma(t)   mean(sim)  mean(theory)   var(sim)  var(theory)
  0.05  0.9852    0.1716      0.0950       0.0985      3.0677      3.0634
  0.20  0.8114    0.5845      0.0815       0.0811      2.3960      2.3997
  0.50  0.2812    0.9597      0.0223       0.0281      1.1663      1.1681
  1.00  0.0066    1.0000      0.0048       0.0007      1.0249      1.0001
terminal signal weight m(1) = 0.00657, noise weight sigma(1) = 0.999978
discrete schedule with T=100: beta_1 = 0.00199, beta_100 = 0.1805, prod alpha_i = 4.319e-05 = abar(1) = 4.319e-05

== Listing 1.2: exact scores and Tweedie's formula ==
t = 0.05: max |score - finite difference of log p_t| over 200 points = 4.63e-08
t = 0.30: max |score - finite difference of log p_t| over 200 points = 1.02e-09
t = 0.80: max |score - finite difference of log p_t| over 200 points = 7.89e-10
Tweedie at t = 0.3 (m = 0.6296, sigma = 0.7770), 400000 importance samples:
  x_t = [1.5 2. ]: E[x0|x_t] by importance sampling = [1.78  2.223], (x + sigma^2 score)/m = [1.78  2.222]
  x_t = [-0.5  0.2]: E[x0|x_t] by importance sampling = [-1.728  0.691], (x + sigma^2 score)/m = [-1.728  0.691]
  x_t = [ 2.5 -1. ]: E[x0|x_t] by importance sampling = [ 2.596 -1.026], (x + sigma^2 score)/m = [ 2.597 -1.025]

== Listing 1.3: equivalence of the objectives ==
  t     SNR      L_x0/L_eps   sigma^2/m^2   L_score/L_eps   1/sigma^2   (model B)
  0.05   32.975      0.0303       0.0303        33.9753      33.9753
  0.30    0.657      1.5231       1.5231         1.6565       1.6565
  0.80    0.002    630.4382     630.4382         1.0016       1.0016
DSM and ESM at t = 0.3 for three score models (200000 samples, same samples for every row):
  model                    DSM       ESM     DSM - ESM
  exact score              1.8651    0.0000     1.8651
  0.7 x exact              1.9954    0.1301     1.8653
  exact + 0.2 tanh(x)      1.9135    0.0488     1.8647
  E|cond score|^2 - E|score|^2 (theory: the constant) = 1.8657

== Listing 1.4: the DDPM variational bound ==
KL weights lambda_i (T=100): i=2: 1.0017, i=10: 0.1173, i=50: 0.0573, i=100: 0.1101
max |KL - lambda_i (eps - eps_hat)^2| over 4 steps x 5000 draws = 2.54e-13
standard normal data, T = 50, reverse variance beta_i (true): -ELBO = 1.4160, NLL = 1.4189
standard normal data, T = 50, reverse variance beta~_i (DDPM): -ELBO = 2.3775, NLL = 1.4189
data entropy (negative log-likelihood of the true density) = 1.2064 nats
  T     L_T     variance beta~_i: sum L_(i-1)   L_0    -ELBO   gap  | variance beta_i: -ELBO   gap
    10  0.0001                  1.9373   0.1125   2.0499  0.8435  |                1.5179  0.3115
    50  0.0001                  3.1680   -1.1591   2.0090  0.8026  |                1.2866  0.0802
   200  0.0001                  4.0566   -2.1831   1.8736  0.6672  |                1.2213  0.0149
  1000  0.0001                  4.7853   -3.1450   1.6405  0.4340  |                1.2033  -0.0031

== Listing 1.5: samplers against ground truth (exact score) ==
target: 8 Gaussians on a circle of radius 3.0, standard deviation 0.2 each; time grid from t = 1 to t = 0.001
n = 3000 samples per run, 5 runs; sliced Wasserstein-2 to a 20000-point real sample (95% t-interval)
noise floor (20 fresh real samples of size 3000): 0.119 +- 0.014
sampler                          5 steps        10 steps        20 steps        50 steps      
reverse SDE (Euler-Maruyama)    0.613 +- 0.008  0.251 +- 0.012  0.108 +- 0.034  0.105 +- 0.032
ancestral (DDIM eta=1)          0.232 +- 0.004  0.148 +- 0.035  0.140 +- 0.028  0.096 +- 0.020
DDIM (eta=0)                    0.208 +- 0.006  0.113 +- 0.013  0.108 +- 0.015  0.103 +- 0.015
PF-ODE Heun                     0.156 +- 0.007  0.101 +- 0.014  0.099 +- 0.015  0.101 +- 0.015
RMS distance to a 200-step Heun solution on the same noise (ODE samplers, first run):
DDIM (eta=0)                        0.387          0.143          0.066          0.028    
PF-ODE Euler                        0.415          0.141          0.052          0.019    
PF-ODE Heun                         0.268          0.078          0.023          0.004    
(Heun uses 2 network evaluations per step; the other samplers use 1.)

== Listing 1.6: DDIM as an ODE solver ==
two eta=0 runs from the same noise: max |difference| = 0.0e+00
two eta=1 runs from the same noise: RMS distance between outputs = 4.241
DDIM step vs Euler step of dy/dtau = eps (y = x/m, tau = sigma/m): max |difference| = 1.8e-15
point-mass data,  1 DDIM steps: max |x - a| = 2.1e-14
point-mass data,  2 DDIM steps: max |x - a| = 1.0e-15
point-mass data, 10 DDIM steps: max |x - a| = 4.4e-16
Gaussian data N(0, 4): RMS error of the sampler against the exact flow map (t from 1 to 0.001)
  N=  5  DDIM 5.24e-01 (ratio    - )   Euler 1.93e-01 (ratio    - )   Heun 8.32e-02 (ratio    - )
  N= 10  DDIM 2.79e-01 (ratio  1.88)   Euler 9.26e-02 (ratio  2.08)   Heun 1.94e-02 (ratio  4.29)
  N= 20  DDIM 1.44e-01 (ratio  1.93)   Euler 4.54e-02 (ratio  2.04)   Heun 4.52e-03 (ratio  4.29)
  N= 40  DDIM 7.35e-02 (ratio  1.96)   Euler 2.25e-02 (ratio  2.02)   Heun 1.08e-03 (ratio  4.19)
  N= 80  DDIM 3.71e-02 (ratio  1.98)   Euler 1.12e-02 (ratio  2.01)   Heun 2.63e-04 (ratio  4.10)
  N=160  DDIM 1.86e-02 (ratio  1.99)   Euler 5.60e-03 (ratio  2.00)   Heun 6.48e-05 (ratio  4.05)
DDIM as an encoder on the ring (exact score):
  N= 20: latent mean [ 0.006 -0.002], latent std [0.848 0.861], SW(latent, N(0,I)) = 0.162, RMS reconstruction error = 0.1350
  N=100: latent mean [ 0.007 -0.003], latent std [0.98  0.998], SW(latent, N(0,I)) = 0.056, RMS reconstruction error = 0.0307

== Listing 1.7: flow matching ==
relative difference between grad L_CFM and grad L_FM (same x_tau; random Theta):
  n =   1000: ||grad_CFM - grad_FM|| / ||grad_FM|| = 0.0284
  n =  10000: ||grad_CFM - grad_FM|| / ||grad_FM|| = 0.0120
  n =  40000: ||grad_CFM - grad_FM|| / ||grad_FM|| = 0.0037
sampling with the exact marginal velocity, 2000 samples per run, 3 runs, SW to a 20000-point real sample:
noise floor (20 fresh real samples): 0.131 +- 0.015
integrator     1 steps          2 steps          5 steps         10 steps         20 steps       
Euler         2.131 +- 0.000  0.530 +- 0.010  0.162 +- 0.075  0.140 +- 0.098  0.134 +- 0.108
Heun          1.664 +- 0.009  0.214 +- 0.019  0.129 +- 0.111  0.130 +- 0.112  0.131 +- 0.113
check u = (E[x1|x] - x)/(1 - tau) at tau = 0.4: max |difference| = 3.3e-15

== Listing 1.8: classifier-free guidance, exact scores ==
max |guided score - grad log[p_t(x) p_t(c|x)^(1+w)]| (t=0.3, w=2.0, 100 points) = 7.7e-10
three classes, each N(mean, 0.8^2 I), means on a circle of radius 1.5
class 0: class mean (0.00, 1.50); per-coordinate std of the class is 0.80
guided probability-flow samples (200 DDIM steps, 2000 samples):
  w     P(c|x)  Bayes-acc  mean y  trace cov  SW to p(x|c)  SW to tilt
  0.0   0.868   0.908      1.495   1.252     0.027      0.025
  1.0   0.992   1.000      2.152   0.766     0.488      0.376
  2.0   0.999   1.000      2.584   0.650     0.785      0.609
  4.0   1.000   1.000      3.132   0.532     1.163      0.958
target tilted density p(x|c) p(c|x)^w (importance resampling from 200000 class samples):
  w     P(c|x)  mean y  trace cov
  0.0   0.862   1.489   1.285
  1.0   0.929   1.667   1.071
  2.0   0.949   1.731   0.987
  4.0   0.965   1.809   0.939

== Listing 1.9: gradient check of the MLP and the training batches ==
  eps-prediction, conditional    input dim 18: max relative error of backprop vs finite differences = 3.6e-07
  x0-prediction, unconditional   input dim 15: max relative error of backprop vs finite differences = 7.5e-07
  flow matching, conditional     input dim 18: max relative error of backprop vs finite differences = 9.3e-09
parameter counts: 2-D networks 20354, digits network 40208
time embedding: shape (3, 13); row for t = 0.5 starts [ 0.5  1.   0.  -0. ]

== Listing 1.10: trained models on the ring ==
training (all three models): 8000 data points, 3000 Adam steps, batch 256, learning rate 2e-3 decaying linearly to 2e-4, EMA decay 0.995, three hidden layers of 96 units
  trained 'eps' model: mean training loss over the last 500 steps = 0.566
  trained 'x0' model: mean training loss over the last 500 steps = 5.042
  trained 'fm' model: mean training loss over the last 500 steps = 5.513
sliced Wasserstein-2 to a 20000-point real sample (1500 samples, 3 runs, 95% t-interval); steps = network calls for Euler/DDIM, 2 per step for Heun
method                   5 steps        10 steps        20 steps        50 steps      
exact score, DDIM       0.221 +- 0.008  0.162 +- 0.018  0.161 +- 0.024  0.157 +- 0.026
eps-net, DDIM           0.587 +- 0.103  0.300 +- 0.139  0.207 +- 0.136  0.169 +- 0.101
eps-net, ancestral      0.287 +- 0.015  0.155 +- 0.090  0.126 +- 0.110  0.131 +- 0.065
x0-net, DDIM            0.299 +- 0.009  0.235 +- 0.016  0.214 +- 0.024  0.201 +- 0.035
flow-net, Euler         0.215 +- 0.035  0.190 +- 0.087  0.195 +- 0.098  0.190 +- 0.094
flow-net, Heun          0.175 +- 0.074  0.180 +- 0.106  0.194 +- 0.102  0.191 +- 0.095
baseline: real vs real            0.174 +- 0.019
baseline: Gaussian fit to data    0.686 +- 0.039
MMD^2 (unbiased, median-heuristic Gaussian kernel) and kNN classifier two-sample accuracy at 50 steps, 1000 samples vs 1000 real (0.5 = indistinguishable):
  exact score, DDIM      MMD^2 = -0.0003 +- 0.0006    C2ST = 0.502 +- 0.019
  eps-net, DDIM          MMD^2 = 0.0002 +- 0.0012    C2ST = 0.572 +- 0.049
  eps-net, ancestral     MMD^2 = -0.0001 +- 0.0011    C2ST = 0.540 +- 0.016
  x0-net, DDIM           MMD^2 = 0.0003 +- 0.0010    C2ST = 0.726 +- 0.042
  flow-net, Euler        MMD^2 = 0.0005 +- 0.0011    C2ST = 0.551 +- 0.030
  flow-net, Heun         MMD^2 = 0.0005 +- 0.0011    C2ST = 0.532 +- 0.029
  Gaussian fit                  MMD^2 = 0.0166 +- 0.0065    C2ST = 0.907 +- 0.014
  real vs real                  MMD^2 = 0.0002 +- 0.0029    C2ST = 0.517 +- 0.015

== Listing 1.11: digits, guidance sweep ==
digits: 1437 train / 360 test images; PCA to 16 coordinates keeps 0.848 of the variance; coordinate scale 0.498
evaluation classifier (logistic regression on 64 pixels): test accuracy 0.956; on PCA-16 reconstructions of the test images 0.950
training: 4000 Adam steps, batch 256, learning rate 2e-3 decaying linearly to 2e-4, EMA decay 0.995, three hidden layers of 128 units, 10% label dropout, float32
network: mean training loss over the last 500 steps 2.962
720 samples per run, 4 runs, 50 DDIM steps; mean and 95% t-interval over runs
  w     class accuracy   SW to test set    within-class spread   mean norm   C2ST vs train
  0.0   0.925 +- 0.014    0.105 +- 0.011      4.007 +- 0.097         4.021      0.517 +- 0.012
  0.5   0.988 +- 0.008    0.158 +- 0.003      3.778 +- 0.123         4.381      0.589 +- 0.026
  1.0   0.992 +- 0.005    0.241 +- 0.005      3.807 +- 0.149         4.701      0.686 +- 0.014
  2.0   0.989 +- 0.003    0.429 +- 0.014      4.281 +- 0.196         5.341      0.818 +- 0.014
  4.0   0.974 +- 0.014    0.955 +- 0.027      6.299 +- 0.266         7.109      0.920 +- 0.010
baselines (the same metrics):
  real train images (720 of 1437) vs test: SW = 0.105, within-class spread = 3.870, mean norm = 3.988, C2ST of two disjoint train halves = 0.512
  class-conditional Gaussian fit: accuracy 0.975, SW = 0.116, within-class spread = 3.863, C2ST vs train = 0.594

== Listing 1.12: digits, samplers, novelty and samples ==
guidance w = 1, 720 samples per run, 3 runs: class accuracy / SW to test / C2ST vs train
  DDIM 10 steps        0.993 +- 0.002    0.264 +- 0.030    0.702 +- 0.013
  DDIM 20 steps        0.995 +- 0.004    0.250 +- 0.033    0.694 +- 0.022
  DDIM 50 steps        0.995 +- 0.004    0.247 +- 0.034    0.683 +- 0.010
  DDIM 100 steps       0.994 +- 0.005    0.246 +- 0.034    0.691 +- 0.019
  ancestral 50 steps   1.000 +- 0.000    0.159 +- 0.010    0.640 +- 0.029
nearest-training-image distance (PCA space): test images median 1.420, 5th percentile 0.896
  generated, w = 0: median 1.693, share below the test 5th percentile 0.006
  generated, w = 1: median 1.513, share below the test 5th percentile 0.022
  generated, w = 2: median 1.768, share below the test 5th percentile 0.006
real test images, classes 0-4:
    --@@%%..               --@@==         ==%%%%             **@@@@%%..         ..@@        
    @@****%%           --**@@@@++       ==@@++%%==         --%%----@@++         ****        
  ..@@..  @@::       **@@@@%%@@++       --@@..++++               ..@@--         @@..%%--    
  ::##    %%::       ..==  ::@@++             ##--               ++@@         ++%%  @@==--  
  ::**    @@..             --@@++             %%                 ++@@         %%%%++@@@@**  
  ::##    @@..             --@@==           **--                 ::@@--       **@@@@%%++..  
    %%++**@@..             ==@@++         ..@@--::::         ....%%##           ::@@++      
    ::##%%--               ++@@++         --@@@@%%@@--       @@%%==             ..@@--      
generated, w = 0, classes 0-4:
    ==%%==               ::@@==           **@@**             ++##%%%%..           ++%%      
  ..@@%%@@**             ##@@--         ==@@==##             ++--++@@--         ..%%++      
  --@@::--**..       ..**@@%%           ==..  %%               ::**@@..         ==##::      
  --%%    ==--       --@@@@**           ..::**##..::           --%%++         ::%%++==::..  
  --##    ====       --@@**%%==           ==@@--  ..             %%##         ==@@%%@@@@--  
  ::##    **--       --%%##@@##           ==@@    ::       ::::  **@@..       ::**++##@@--  
    %%++**%%..         ::++@@++           %%**  ==--       ..++  **%%..           ==%%++..  
    ==%%**::             ::@@==           ##****++..         **%%##++..           ++%%..    
generated, w = 2, classes 0-4:
    ==%%++               ::@@++         ..@@@@..             %%%%@@@@..           ++%%      
  ..@@%%@@##             **@@##         **@@**%%           ..**  ::@@--           @@==      
  --@@  ::%%..       ..==@@@@##         ==----@@                 ==@@..         ==**    --  
  ==%%    ++--       ::%%@@@@++             %%%%               ==%%==         --@@==  ++++  
  ==**    ++==       ..++##@@::           ..@@++             ....@@%%         ++@@@@@@@@--  
  ::##..  ##--       ..--##@@==           ==@@             ..::  ##@@..       ::==++@@@@..  
    @@****@@..         ..++@@==         ::@@@@++%%++       ..++  **@@..           --@@++    
    ==%%##::             ::%%++::       ..@@%%##%%==         @@@@%%++..           **@@      
