DDPM Posterior Mean and Variance from x0
Problem Statement
The reverse step samples from the tractable posterior q(x_{t-1} | x_t, x_0). Compute its mean and variance from x_t, the (predicted) x0, and the schedule.
Background
Given beta_t, alpha_bar_t, and alpha_bar_{t-1} (with alpha_t = 1 - beta_t), the posterior is Gaussian with
μ~​t​=1−αˉt​αˉt−1​​βt​​x0​+1−αˉt​αt​​(1−αˉt−1​)​xt​
β~​t​=1−αˉt​1−αˉt−1​​βt​
Your Task
Implement:
def posterior(x_t, x0, beta_t, alpha_bar_t, alpha_bar_prev):
Return a dict with "mean" (a list, rounded to 4 decimals) and "variance" (a float, rounded to 6 decimals).
Input Format
- x_t, x0: lists of length D.
- beta_t, alpha_bar_t, alpha_bar_prev (float).
Output Format
- A dict with a list and a float.
Sample
print(posterior([1.0, 1.0], [0.5, 0.5], 0.2, 0.5, 0.64))
Output:
{'mean': [0.804, 0.804], 'variance': 0.144}
Example:
print(posterior([1.0, 1.0], [0.5, 0.5], 0.2, 0.5, 0.64))
{'mean': [0.804, 0.804], 'variance': 0.144}coef_x0 = sqrt(0.64)0.2/(1-0.5) = 0.80.2/0.5 = 0.32; coef_xt = sqrt(0.8)(1-0.64)/0.5 = 0.89440.72 = 0.644. mean = 0.320.5 + 0.6441 = 0.804. variance = (1-0.64)/(1-0.5)*0.2 = 0.144.
Constraints:
len(x_t) == len(x0); schedule values in(0, 1).alpha_t = 1 - beta_t; use the two coefficients above.- Mean rounded to 4 decimals; variance to 6 decimals.
1. Background Knowledge
In Denoising Diffusion Probabilistic Models (DDPMs), the forward process gradually adds Gaussian noise to data, while the reverse process learns to denoise. A key insight is that the posterior distribution q(xt−1​∣xt​,x0​) is analytically tractable and Gaussian. This means we can compute its mean and variance in closed form without sampling.
The posterior mean μ~​t​ is a weighted combination of the current noisy sample xt​ and the predicted clean sample x0​. The weights depend on the noise schedule parameters: βt​ (the noise added at step t), αˉt​ (the cumulative product of αi​=1−βi​ up to step t), and αˉt−1​ (the same cumulative product up to step t−1). Intuitively, as t increases, αˉt​ decreases, meaning more noise has been added, so the weight on x0​ (the "target") increases relative to xt​.
The posterior variance β~​t​ is a scalar (isotropic) that scales the identity covariance matrix. It is always smaller than βt​ because knowing x0​ reduces uncertainty about xt−1​. This tractable posterior is what allows DDPMs to train efficiently: the model predicts x0​ (or equivalently ϵ), and the posterior provides the target distribution for the reverse step.
2. Algorithm Approach
This is a direct formula evaluation problem. No iterative or optimization algorithm is needed. The approach is:
- Parse the input parameters: xt​, x0​ (vectors), and βt​, αˉt​, αˉt−1​ (scalars).
- Compute the two scalar coefficients for the mean:
- Coefficient for x0​: 1−αˉt​αˉt−1​​βt​​
- Coefficient for xt​: 1−αˉt​αt​​(1−αˉt−1​)​ where αt​=1−βt​.
- Compute the mean vector as the element-wise weighted sum of x0​ and xt​ using these coefficients.
- Compute the variance scalar: β~​t​=1−αˉt​1−αˉt−1​​βt​.
- Round the mean elements to 4 decimal places and the variance to 6 decimal places.
- Return a dictionary with keys "mean" and "variance".
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.