# 高斯分佈

## 概率密度函數

$$x \sim \mathcal{N}(\mu, \sigma^2)$$，則：

$f(x ; \mu, \sigma)=\frac{1}{\sigma \sqrt{2 \pi}} \exp \left(-\frac{(x-\mu)^2}{2 \sigma^2}\right)$

## 兩個高斯的KL散度

$D_{\mathrm{KL}}\left(\mathcal{N(\mu_1, \sigma_1^2) \mid\mid N(\mu_2, \sigma_2^2)}\right) = \ln \frac{\sigma_2}{\sigma_1} + \frac{\sigma_1^2 + (\mu_1 - \mu_2)^2}{2\sigma_2^2} - \frac{1}{2}$

## 性質1

$ax + b \sim \mathcal{N}(a\mu + b, (a\sigma)^2)$

$\mathbf{x} = \epsilon * \sigma + \mu, \epsilon \sim \mathcal{N}(0, \mathbf{I})$

## 性質2

\begin{aligned} & U=x+y \sim N\left(\mu_x+\mu_y, \sigma_x^2+\sigma_y^2\right) \\ & V=x-y \sim N\left(\mu_x-\mu_y, \sigma_x^2+\sigma_y^2\right) \end{aligned}

# 推導一

$$\text{Diffusion Forward process}$$中，任意時刻$$t$$的狀態$$\mathbf{x}_t$$如何基於$$\mathbf{x}_0$$表示？

$q\left(\mathbf{x}_t \mid \mathbf{x}_{t-1}\right) = \mathcal{N}\left(\sqrt{1-\beta_t} \mathbf{x}_{t-1}, \beta_t \mathbf{I}\right)\tag{1}$

$$\beta_{t}$$進行變換，定義：

\begin{aligned} \alpha_t & =1-\beta_t \\ \bar{\alpha}_t & =\prod_{i=1}^t \alpha_i \end{aligned}

$$(1)$$式展開如下：

\begin{aligned} q\left(\mathbf{x}_t \mid \mathbf{x}_{t-1}\right) & =\mathcal{N}\left(\sqrt{1-\beta_t} \mathbf{x}_{t-1}, \beta_t \mathbf{I}\right) \\ \mathbf{x}_t & =\sqrt{1-\beta_t} \mathbf{x}_{t-1}+\sqrt{\beta_t} \epsilon, \quad \epsilon \sim \mathcal{N}(0, \mathbf{I}) \\ & =\sqrt{\alpha_t} \mathbf{x}_{t-1}+\sqrt{1-\alpha_t} \epsilon \end{aligned} \tag{2}

\begin{aligned} & \sqrt{\alpha_t}\mathbf{x}_{t-1} + \sqrt{1 - \alpha_t} \epsilon \\ =& \sqrt{\alpha_t}\left(\sqrt{\alpha_{t-1}}\mathbf{x}_{t-2} + \sqrt{1 - \alpha_{t-1}} \bar{\epsilon} \right) + \sqrt{1 - \alpha_t} \epsilon \\ =& \sqrt{\alpha_t \alpha_{t-1}} \mathbf{x}_{t-2} + \sqrt{\alpha_t \left(1 - \alpha_{t-1}\right)} \bar{\epsilon} + \sqrt{1 - \alpha_t} \epsilon \end{aligned} \tag{3}

\begin{aligned} \epsilon \sim \mathcal{N}(0, \mathbf{I}) \quad &\Rightarrow \quad \sqrt{1 - \alpha_t} \epsilon \sim \mathcal{N}(0, \left(1-\alpha_t\right)\mathbf{I}) \ \\ \bar{\epsilon} \sim \mathcal{N}(0, \mathbf{I}) \quad &\Rightarrow \quad \sqrt{\alpha_t \left(1 - \alpha_{t-1}\right)} \epsilon \sim \mathcal{N}(0, \alpha_t\left(1-\alpha_{t-1}\right)\mathbf{I}) \end{aligned} \tag{a}

\begin{aligned} \sqrt{1 - \alpha_t} \epsilon + \sqrt{\alpha_t \left(1 - \alpha_{t-1}\right)} \bar{\epsilon} &\sim \mathcal{N}(0, \left(1-\alpha_t\right)\mathbf{I} +\alpha_t\left(1-\alpha_{t-1}\right)\mathbf{I}) \\ \Rightarrow U & \sim \mathcal{N}(0, \left(1-\alpha_t\alpha_{t-1}\right)\mathbf{I}) \end{aligned} \tag{b}

\begin{aligned} U &\sim \mathcal{N}(0, \left(1-\alpha_t\alpha_{t-1}\right)\mathbf{I}) \Rightarrow U = \sqrt{1 - \alpha_t\alpha_{t-1}} \epsilon \end{aligned} \tag{c}

$$(c)$$代入$$(3)$$，可得：

\begin{aligned} & \sqrt{\alpha_t}\mathbf{x}_{t-1} + \sqrt{1 - \alpha_t} \epsilon \\ =& \sqrt{\alpha_t}\left(\sqrt{\alpha_{t-1}}\mathbf{x}_{t-2} + \sqrt{1 - \alpha_{t-1}} \bar{\epsilon} \right) + \sqrt{1 - \alpha_t} \epsilon \\ =& \sqrt{\alpha_t \alpha_{t-1}} \mathbf{x}_{t-2} + \sqrt{\alpha_t \left(1 - \alpha_{t-1}\right)} \bar{\epsilon} + \sqrt{1 - \alpha_t} \epsilon \\ =& \sqrt{\alpha_t \alpha_{t-1}}\mathbf{x}_{t-2} + \sqrt{1 - \alpha_t \alpha_{t-1}} \epsilon \end{aligned}

\begin{aligned} q\left(\mathbf{x}_t \mid \mathbf{x}_{t-1}\right) & =\mathcal{N}\left(\sqrt{1-\beta_t} \mathbf{x}_{t-1}, \beta_t \mathbf{I}\right) \\ \mathbf{x}_t & =\sqrt{1-\beta_t} \mathbf{x}_{t-1}+\sqrt{\beta_t} \epsilon, \quad \epsilon \sim \mathcal{N}(0, \mathbf{I}) \\ & =\sqrt{\alpha_t} \mathbf{x}_{t-1}+\sqrt{1-\alpha_t} \epsilon \\ & =\sqrt{\alpha_t \alpha_{t-1}} \mathbf{x}_{t-2}+\sqrt{1-\alpha_t \alpha_{t-1}} \epsilon \\ & =\ldots \\ & =\sqrt{\bar{\alpha}_t} \mathbf{x}_0+\sqrt{1-\bar{\alpha}_t} \epsilon \end{aligned}

# 推導二

$$diffusion$$中，定義$$q$$服從高斯分佈，故對$$q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t, \mathbf{x}_0\right)$$定義如下：

\begin{aligned} q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t, \mathbf{x}_0\right) & =\mathcal{N}\left(\mathbf{x}_{t-1} ; \tilde{\boldsymbol{\mu}}_t\left(\mathbf{x}_t, \mathbf{x}_0\right), \tilde{\beta}_t \mathbf{I}\right) \end{aligned}

\begin{aligned} \tilde{\boldsymbol{\mu}}_t\left(\mathbf{x}_t, \mathbf{x}_0\right) &:= \frac{\sqrt{\bar{\alpha}_{t-1}} \beta_t}{1-\bar{\alpha}_t} \mathbf{x}_0+\frac{\sqrt{\alpha_t}\left(1-\bar{\alpha}_{t-1}\right)}{1-\bar{\alpha}_t} \mathbf{x}_t, \\ \tilde{\beta}_t &:= \frac{1-\bar{\alpha}_{t-1}}{1-\bar{\alpha}_t} \beta_t \end{aligned}

$q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t, \mathbf{x}_0\right) =q\left(\mathbf{x}_t \mid \mathbf{x}_{t-1}, \mathbf{x}_0\right) \frac{q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_0\right)}{q\left(\mathbf{x}_t \mid \mathbf{x}_0\right)}\tag{1}$

$q\left(\mathbf{x}_t \mid \mathbf{x}_{t-1}, \mathbf{x}_0\right) = q\left(\mathbf{x}_t \mid \mathbf{x}_{t-1}\right)$

$$(1)$$式寫作$$(2)$$式：

\begin{aligned} q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t, \mathbf{x}_0\right) &=q\left(\mathbf{x}_t \mid \mathbf{x}_{t-1}, \mathbf{x}_0\right) \frac{q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_0\right)}{q\left(\mathbf{x}_t \mid \mathbf{x}_0\right)} \\ &=q\left(\mathbf{x}_t \mid \mathbf{x}_{t-1}\right) \frac{q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_0\right)}{q\left(\mathbf{x}_t \mid \mathbf{x}_0\right)} \end{aligned} \tag{2}

\begin{aligned} q\left(\mathbf{x}_t \mid \mathbf{x}_{0}\right) &= \mathcal{N}\left(\sqrt{\bar{\alpha}_t} \mathbf{x}_{0}, \sqrt{1 - \bar{\alpha}_t} \mathbf{I}\right) \\ q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_{0}\right) &= \mathcal{N}\left(\sqrt{\bar{\alpha}_{t-1}} \mathbf{x}_{0}, \sqrt{1 - \bar{\alpha}_{t-1}} \mathbf{I}\right) \end{aligned}

\begin{aligned} q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t, \mathbf{x}_0\right) & = q\left(\mathbf{x}_t \mid \mathbf{x}_{t-1}\right) \frac{q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_0\right)}{q\left(\mathbf{x}_t \mid \mathbf{x}_0\right)} \\ & \propto \exp \left(-\frac{1}{2}\left(\frac{\left(\mathbf{x}_t-\sqrt{\alpha_t} \mathbf{x}_{t-1}\right)^2}{\beta_t}+\frac{\left(\mathbf{x}_{t-1}-\sqrt{\bar{\alpha}_{t-1}} \mathbf{x}_0\right)^2}{1-\bar{\alpha}_{t-1}}-\frac{\left(\mathbf{x}_t-\sqrt{\bar{\alpha}_t} \mathbf{x}_0\right)^2}{1-\bar{\alpha}_t}\right)\right) \end{aligned}\tag{3}

\begin{aligned} &q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_t, \mathbf{x}_0\right) = q\left(\mathbf{x}_t \mid \mathbf{x}_{t-1}\right) \frac{q\left(\mathbf{x}_{t-1} \mid \mathbf{x}_0\right)}{q\left(\mathbf{x}_t \mid \mathbf{x}_0\right)} \\ & \propto \exp \left(-\frac{1}{2}\left(\frac{\left(\mathbf{x}_t-\sqrt{\alpha_t} \mathbf{x}_{t-1}\right)^2}{\beta_t}+\frac{\left(\mathbf{x}_{t-1}-\sqrt{\bar{\alpha}_{t-1}} \mathbf{x}_0\right)^2}{1-\bar{\alpha}_{t-1}}-\frac{\left(\mathbf{x}_t-\sqrt{\bar{\alpha}_t} \mathbf{x}_0\right)^2}{1-\bar{\alpha}_t}\right)\right) \\ &=\exp \left(-\frac{1}{2}\left(\frac{\mathbf{x}_t^2-2 \sqrt{\alpha_t} \mathbf{x}_t \mathbf{x}_{t-1}+\alpha_t \mathbf{x}_{t-1}^2}{\beta_t}+\frac{\mathbf{x}_{t-1}^2-2 \sqrt{\bar{\alpha}_{t-1}} \mathbf{x}_0 \mathbf{x}_{t-1}+\bar{\alpha}_{t-1} \mathbf{x}_0^2}{1-\bar{\alpha}_{t-1}}-\frac{\left(\mathbf{x}_t-\sqrt{\bar{\alpha}_t} \mathbf{x}_0\right)^2}{1-\bar{\alpha}_t}\right)\right) \\ &=\exp \left(-\frac{1}{2}\left(\left(\frac{\alpha_t}{\beta_t}+\frac{1}{1-\bar{\alpha}_{t-1}}\right) \mathbf{x}_{t-1}^2-\left(\frac{2 \sqrt{\alpha_t}}{\beta_t} \mathbf{x}_t+\frac{2 \sqrt{\bar{\alpha}_{t-1}}}{1-\bar{\alpha}_{t-1}} \mathbf{x}_0\right) \mathbf{x}_{t-1}+C\left(\mathbf{x}_t, \mathbf{x}_0\right)\right)\right) \end{aligned} \tag{4}

\begin{aligned} \tilde{\boldsymbol{\mu}}_t &= \frac{1}{\frac{\alpha_t}{\beta_t}+\frac{1}{1-\bar{\alpha}_{t-1}}} * \left(\frac{\sqrt{\alpha_t}}{\beta_t} \mathbf{x}_t+\frac{\sqrt{\bar{\alpha}_{t-1}}}{1-\bar{\alpha}_{t-1}} \mathbf{x}_0\right) \\ &= \frac{\left(1 - \bar{\alpha}_{t-1}\right) \beta_{t}}{\alpha_t\left(1 - \bar{\alpha}_{t-1}\right) + \beta_t} * \left(\frac{\sqrt{\alpha_t}}{\beta_t} \mathbf{x}_t+\frac{\sqrt{\bar{\alpha}_{t-1}}}{1-\bar{\alpha}_{t-1}} \mathbf{x}_0\right) \\ & = \frac{\left(1 - \bar{\alpha}_{t-1}\right)\sqrt{\alpha_t}}{\alpha_t\left(1 - \bar{\alpha}_{t-1}\right) + \beta_t} \mathbf{x}_t + \frac{\sqrt{\bar{\alpha}_{t-1}} \beta_{t}}{\alpha_t\left(1 - \bar{\alpha}_{t-1}\right) + \beta_t} \mathbf{x}_0 \\ \end{aligned}\tag{5}

$$\alpha_t = 1 - \beta_t$$，故：

\begin{aligned} \alpha_t\left(1 - \bar{\alpha}_{t-1}\right) + \beta_t &= \alpha_t - \alpha_t \bar{\alpha}_{t-1} + \beta_t \\ &= 1 - \beta_t - \alpha_t \bar{\alpha}_{t-1} + \beta_t \\ &= 1 - \alpha_t \bar{\alpha}_{t-1} \\ &= 1 - \bar{\alpha}_{t} \end{aligned}\tag{6}

$$(6)$$式代入$$(5)$$，有：

$\tilde{\boldsymbol{\mu}}_t\left(\mathbf{x}_t, \mathbf{x}_0\right) :=\frac{\sqrt{\bar{\alpha}_{t-1}} \beta_t}{1-\bar{\alpha}_t} \mathbf{x}_0+\frac{\sqrt{\alpha_t}\left(1-\bar{\alpha}_{t-1}\right)}{1-\bar{\alpha}_t} \mathbf{x}_t$

\begin{aligned} \tilde{\beta}_t &= \frac{1}{\frac{\alpha_t}{\beta_t}+\frac{1}{1-\bar{\alpha}_{t-1}}} \\ &= \frac{\left(1 - \bar{\alpha}_{t-1}\right) \beta_{t}}{\alpha_t\left(1 - \bar{\alpha}_{t-1}\right) + \beta_t} \\ &= \frac{1-\bar{\alpha}_{t-1}}{1-\bar{\alpha}_t} \beta_t \end{aligned}