PIXELBANKv9.1.0
Menu

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=αˉt−1 βt1−αˉt x0+αt (1−αˉt−1)1−αˉt xt\tilde{\mu}_t = \frac{\sqrt{\bar{\alpha}_{t-1}}\,\beta_t}{1 - \bar{\alpha}_t}\, x_0 + \frac{\sqrt{\alpha_t}\,(1 - \bar{\alpha}_{t-1})}{1 - \bar{\alpha}_t}\, x_t

β~t=1−αˉt−11−αˉt βt\tilde{\beta}_t = \frac{1 - \bar{\alpha}_{t-1}}{1 - \bar{\alpha}_t}\, \beta_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:

Input:
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}
Reasoning:

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.
🔒

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.

solution.py

Test Results

0/0
Run code to see test results.