# 変分近似の概要


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

## EM アルゴリズム

### 概要

EM
アルゴリズムの目的は、潜在変数を持つモデルについて最尤解を見出すことである。

全ての観測データの集合を **X** 、全ての潜在変数の集合を **Z**
、全てのモデルパラメータの集合を **θ** で表す。

最尤解 $\hat{\boldsymbol\theta}$ は次式で表される。

$$\hat{\boldsymbol\theta}=\arg\max\_{\boldsymbol\theta}p(\mathbf X\mid\boldsymbol\theta)=\arg\max\_{\boldsymbol\theta}\sum\_{\mathbf Z}p(\mathbf X,\mathbf Z\mid\boldsymbol\theta)$$

対数尤度関数は以下のように書ける。

ln *p*(**X** ∣ **θ**) = ln {∑<sub>**Z**</sub>*p*(**X**, **Z** ∣ **θ**)}

このように、潜在変数に関する総和が対数の中にあり、同時分布
*p*(**X**, **Z** ∣ **θ**) が指数型分布族に属する場合でも、周辺分布
*p*(**X** ∣ **θ**) は指数型分布族にならない。

{**X**, **Z**} という組を**完全データ集合**と呼び、実際の観測データ
**X** は**不完全**と呼ぶことにする。完全データ集合に関する対数尤度関数
ln *p*(**X**, **Z** ∣ **θ**) の最大化は簡単にできると仮定する。

実際には、完全データ集合 {**X**, **Z**} は与えられず、不完全データ **X**
だけが与えられる。潜在変数 **Z** についての知識は事後確率分布
*p*(**Z** ∣ **X**, **θ**) によるものだけである。

<table>
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<thead>
<tr>
<th><span class="math inline"> </span></th>
<th>数式</th>
</tr>
</thead>
<tbody>
<tr>
<td>目的 （指数型分布族ではない）</td>
<td><span
class="math inline">arg max<sub><strong>θ</strong></sub><em>p</em>(<strong>X</strong> ∣ <strong>θ</strong>)</span></td>
</tr>
<tr>
<td>簡単　（指数型分布族である）</td>
<td><span
class="math inline">arg max<sub><strong>θ</strong></sub><em>p</em>(<strong>X</strong>, <strong>Z</strong> ∣ <strong>θ</strong>)</span></td>
</tr>
<tr>
<td>既知</td>
<td><span
class="math inline"><em>p</em>(<strong>Z</strong> ∣ <strong>X</strong>, <strong>θ</strong>), <em>p</em>(<strong>X</strong>, <strong>Z</strong> ∣ <strong>θ</strong>)</span></td>
</tr>
</tbody>
</table>

EMアルゴリズムでは、次の２ステップを踏む。

**Eステップ**

$$
\begin{split}
\mathcal Q(\boldsymbol\theta,\boldsymbol\theta^{\text{old}}) &=\mathbb E\_{\mathbf Z\mid\mathbf X,\boldsymbol\theta^{\text{old}}}\left\[\ln p(\mathbf X,\mathbf Z\mid\boldsymbol\theta)\right\] \\
&= \sum\_{\mathbf Z} p(\mathbf Z\mid\mathbf X,\boldsymbol\theta^{\text{old}})\ln p(\mathbf X,\mathbf Z\mid\boldsymbol\theta)
\end{split}
$$

**Mステップ**

**θ**<sup>new</sup> = arg max<sub>**θ**</sub>𝒬(**θ**, **θ**<sup>old</sup>)

### *p*(**θ**) が既知の場合

パラメータの事前分布 *p*(**θ**) が既知の場合、MAP 解を計算できる。

<table>
<colgroup>
<col style="width: 50%" />
<col style="width: 50%" />
</colgroup>
<thead>
<tr>
<th><span class="math inline"> </span></th>
<th>数式</th>
</tr>
</thead>
<tbody>
<tr>
<td>目的 （指数型分布族ではない）</td>
<td><span
class="math inline">arg max<sub><strong>θ</strong></sub><em>p</em>(<strong>θ</strong> ∣ <strong>X</strong>)</span></td>
</tr>
<tr>
<td>簡単　（指数型分布族である）</td>
<td><span
class="math inline">arg max<sub><strong>θ</strong></sub><em>p</em>(<strong>X</strong>, <strong>Z</strong> ∣ <strong>θ</strong>)</span></td>
</tr>
<tr>
<td>既知</td>
<td><span
class="math inline"><em>p</em>(<strong>Z</strong> ∣ <strong>X</strong>, <strong>θ</strong>), <em>p</em>(<strong>X</strong>, <strong>Z</strong> ∣ <strong>θ</strong>), <em>p</em>(<strong>θ</strong>)</span></td>
</tr>
</tbody>
</table>

$$
\begin{split}
\boldsymbol\theta^{\text{new}} &=\arg\max\_{\boldsymbol\theta}\left(\mathcal Q(\boldsymbol\theta,\boldsymbol\theta^{\text{old}})+\ln p(\boldsymbol\theta)\right) \\
&= \arg\max\_{\boldsymbol\theta}\mathbb E\_{\mathbf Z\mid\mathbf X,\boldsymbol\theta^{\text{old}}}\left\[\ln p(\mathbf X,\mathbf Z,\boldsymbol\theta)\right\] \\
&= \arg\max\_{\boldsymbol\theta}\mathbb E\_{\mathbf Z\mid\mathbf X,\boldsymbol\theta^{\text{old}}}\left\[\ln p(\boldsymbol\theta\mid\mathbf X,\mathbf Z)\right\]
\end{split}
$$

## 一般のEMアルゴリズム

潜在変数についての分布 *q*(**Z**) を導入する。 *q*(**Z**)
の設定の仕方に関わらず、次の分解が成り立つ。

ln *p*(**X** ∣ **θ**) = ℒ(*q*, **θ**) + KL(*q*∥*p*)

ただし、

$$\mathcal L(q,\boldsymbol\theta)=\sum\_{\mathbf Z}q(\mathbf Z)\ln\left\\ \frac{p(\mathbf X,\mathbf Z\mid\boldsymbol\theta)}{q(\mathbf Z)} \right\\$$
$$\mathrm{KL}(q\\p)=-\sum\_{\mathbf Z}q(\mathbf Z)\ln\left\\ \frac{p(\mathbf Z\mid\mathbf X,\boldsymbol\theta)}{q(\mathbf Z)} \right\\$$

$$
\begin{split}
\because\ln p(\mathbf X\mid\boldsymbol\theta) &=\mathcal L(q,\boldsymbol\theta)+\mathrm{KL}(q\\p) \\
&= \sum\_{\mathbf Z}q(\mathbf Z)\left(\ln\left\\ \frac{p(\mathbf X,\mathbf Z\mid\boldsymbol\theta)}{q(\mathbf Z)} \right\\-\ln\left\\ \frac{p(\mathbf Z\mid\mathbf X,\boldsymbol\theta)}{q(\mathbf Z)} \right\\\right) \\
&= \sum\_{\mathbf Z}q(\mathbf Z)\left(\ln\left\\ \frac{p(\mathbf Z\mid\mathbf X,\boldsymbol\theta)p(\mathbf X\mid\boldsymbol\theta)}{q(\mathbf Z)} \right\\-\ln\left\\ \frac{p(\mathbf Z\mid\mathbf X,\boldsymbol\theta)}{q(\mathbf Z)} \right\\\right) \\
&= \sum\_{\mathbf Z}q(\mathbf Z)\ln p(\mathbf X\mid\boldsymbol\theta)
\end{split}
$$

ℒ(*q*, **θ**) は分布 *q*(**Z**) の汎関数であり、かつパラメータ **θ**
の関数である。

KL(*q*∥*p*) ≥ 0 、等号は *q*(**Z**) = *p*(**Z** ∣ **X**, **θ**)
のときのみ成立する。よって (1) より、
ℒ(*q*, **θ**) ≤ ln *p*(**X** ∣ **θ**)
が成り立つ。つまり、 ℒ(*q*, **θ**) は *q* と **θ** によらず常に
ln *p*(**X** ∣ **θ**) の下界をなす。

## 局所的変分推論法

次の積分を計算したいとする。

*I* = ∫*σ*(*a*)*p*(*a*)*d**a*

積分をそのまま求めるのは不可能なため、変分下界 *σ*(*a*) ≥ *f*(*a*, *ξ*)
の形で用いる。ここで、 *ξ* は変分パラメータである。

*I* ≥ ∫*f*(*a*, *ξ*)*p*(*a*)*d**a* = ℒ(*ξ*)

ℒ(*ξ*) は単に *ξ* をパラメータとする関数である。 ℒ(*ξ*) を最大化する値
*ξ*<sup>\*</sup> を求める。

ただし、最適化したこの下界は一般に正確ではない。なぜなら
*σ*(*a*) ≥ *f*(*a*, *ξ*) は厳密に最適化できるが、選んだ *ξ* は *a*
の値に依存しており、下界が正しいのはある *a*
の値についてだけだからである。

ℒ(*ξ*) は全ての *a* の値について積分して得られるから、 *ξ*<sup>\*</sup>
の値は分布 *p*(*a*) による重みで平均化してしまった値を表している。
