Compute the forward-process posterior q(x_{t-1} | x_t, x_0)
Why it matters DDPM samplers and many diffusion training objectives are written in terms of this posterior.
If you know both the clean sample x_0 and the noisy x_t, the previous step x_(t-1) is Gaussian with a closed-form mean and variance. DDPM training targets and samplers that predict x_0 use it.
Write q_posterior(x0, xt, betas, t). t is 1-based and betas[0] is beta_1. With alpha_s = 1 - beta_s, alpha_bar_t = product of alpha up to t, and alpha_bar_0 = 1:
- mean = c0 * x0 + ct * xt with c0 = sqrt(alpha_bar_(t-1)) * beta_t / (1 - alpha_bar_t) and ct = sqrt(alpha_t) * (1 - alpha_bar_(t-1)) / (1 - alpha_bar_t)
- var = beta_t * (1 - alpha_bar_(t-1)) / (1 - alpha_bar_t)
Return (mean as an array, var as a float).
def q_posterior(x0, xt, betas, t)Example
q_posterior([1.0], [0.0], [0.1, 0.2], 2) gives (array([0.6776]), 0.0714).
Understand the idea
The forward diffusion process adds noise step by step. Going backwards is hard in general, but if we also know the clean sample x_0, the step back from x_t to x_(t-1) is a simple Gaussian. Its mean is a weighted average of x_0 and x_t: early in the chain (small t) it leans on x_0, later it leans on x_t.
Example: betas 0.1 and 0.2, t = 2. alpha_bar_1 = 0.9 and alpha_bar_2 = 0.72. Then c0 = sqrt(0.9) * 0.2 / 0.28 = 0.678, ct = sqrt(0.8) * 0.1 / 0.28 = 0.319 and the variance is 0.2 * 0.1 / 0.28 = 0.071. With x_0 = 1 and x_t = 0 the mean is 0.678.
Rules: mean = sqrt(ab_(t-1)) beta_t / (1 - ab_t) * x_0 + sqrt(alpha_t) (1 - ab_(t-1)) / (1 - ab_t) * x_t; var = beta_t (1 - ab_(t-1)) / (1 - ab_t).
A model that predicts x_0 plugs its prediction into this formula to sample.