GMM-From generation process perspective
Introduce a latent variable
我们认为在分类任务中,每个点本身就隐含了属于哪个类的信息,只是我们没有观察到。EM 算法的 E-step 就相当于使用后验概率来求这个信息。我们使用 z \mathbf{z} z 来表示这个信息,z k = 1 z_k=1 z k = 1 表示某个点 x \mathbf{x} x 属于 cluster k k k 。
z = ( z 1 , … , z K ) z k ∈ { 0 , 1 } ∑ k z k = 1 \mathbf{z}=(z_{1},\ldots,z_{K})\quad z_{k}\in\{0,1\}\quad\sum_{k}z_{k}=1
z = ( z 1 , … , z K ) z k ∈ { 0 , 1 } k ∑ z k = 1
这个变量称为隐变量 (latent variable)。
在概率模型中,我们记样本点属于某个 cluster 的概率为 p ( z k = 1 ) = π k p(z_k=1)=\pi_k p ( z k = 1 ) = π k ,或者写为
p ( z ) = ∏ k = 1 K π k z k p(\mathbf{z})=\prod_{k=1}^{K} \pi_k^{z_k}
p ( z ) = k = 1 ∏ K π k z k
假如一个样本已经能够确定属于某个类,我们假设其满足高斯分布:
p ( x ∣ z k = 1 ) = N ( x ∣ μ k , Σ k ) p(\mathbf{x}|z_k=1)=\mathcal{N}(\mathbf{x}|\boldsymbol{\mu}_k,\boldsymbol{\Sigma}_k)
p ( x ∣ z k = 1 ) = N ( x ∣ μ k , Σ k )
但是如果我们并没有该样本点属于某个类的信息,则其概率分布为高斯分布的加权
p ( x ) = ∑ z p ( z ) p ( x ∣ z ) = ∑ k = 1 K π k N ( x ∣ μ k , Σ k ) p(\mathbf{x})=\sum_{\mathbf{z}}p(\mathbf{z})p(\mathbf{x}|\mathbf{z})=\sum_{k=1}^{K}\pi_{k}\mathcal{N}(\mathbf{x}|\boldsymbol{\mu}_{k},\boldsymbol{\Sigma}_{k})
p ( x ) = z ∑ p ( z ) p ( x ∣ z ) = k = 1 ∑ K π k N ( x ∣ μ k , Σ k )
EM algorithm for maximum likelihood
Details of the EM Algorithm for GMM
初始化均值 μ k \mathbf{\mu}_k μ k ,协方差矩阵 Σ k \mathbf{\Sigma}_k Σ k 以及混合系数(mixing coefficients) π k \pi_k π k ,并且计算 log likelihood 的初始化值
E step 。
γ ( z n k ) = π k N ( x n ∣ μ k , Σ k ) ∑ j = 1 K π j N ( x n ∣ μ j , Σ j ) \gamma(z_{nk})=\frac{\pi_{k}{\mathcal N}(\mathbf{x}_{n}|\boldsymbol{\mu}_{k},\boldsymbol{\Sigma}_{k})}{\sum_{j=1}^{K}\pi_{j}{\mathcal N}(\mathbf{x}_{n}|\boldsymbol{\mu}_{j},\boldsymbol{\Sigma}_{j})}
γ ( z nk ) = ∑ j = 1 K π j N ( x n ∣ μ j , Σ j ) π k N ( x n ∣ μ k , Σ k )
M step 。
μ k n e w = 1 N k ∑ n = 1 N γ ( z n k ) x n Σ k n e w = 1 N k ∑ n = 1 N γ ( z n k ) ( x n − μ k n e w ) ( x n − μ k n e w ) T π k n e w = N k N \begin{array}{rcl}
\mathbf{\mu}_k^\mathrm{new}&=&\frac1{N_k}\sum_{n=1}^N\gamma(z_{nk})\mathbf{x}_n\\\\
\mathbf{\Sigma}_k^\mathrm{new}&=&\frac1{N_k}\sum_{n=1}^N\gamma(z_{nk})\left(\mathbf{x}_n-\mathbf{\mu}_k^\mathrm{new}\right)\left(\mathbf{x}_n-\mathbf{\mu}_k^\mathrm{new}\right)^\mathrm{T}\\\\
\pi_k^\mathrm{new}&=&\frac{N_k}N
\end{array}
μ k new Σ k new π k new = = = N k 1 ∑ n = 1 N γ ( z nk ) x n N k 1 ∑ n = 1 N γ ( z nk ) ( x n − μ k new ) ( x n − μ k new ) T N N k
其中
N k = ∑ n = 1 N γ ( z n k ) N_k = \sum_{n=1}^{N} \gamma(z_{nk})
N k = n = 1 ∑ N γ ( z nk )
计算 log likelihood 以检查是否收敛
ln p ( X ∣ μ , Σ , π ) = ∑ n = 1 N ln { ∑ k = 1 K π k N ( x n ∣ μ k , Σ k ) } \ln p(\mathbf{X}|\boldsymbol{\mu},\boldsymbol{\Sigma},\boldsymbol{\pi})=\sum_{n=1}^N\ln\left\{\sum_{k=1}^K\pi_k\mathcal{N}(\mathbf{x}_n|\boldsymbol{\mu}_k,\boldsymbol{\Sigma}_k)\right\}
ln p ( X ∣ μ , Σ , π ) = n = 1 ∑ N ln { k = 1 ∑ K π k N ( x n ∣ μ k , Σ k ) }
The general EM algorithm
给定联合概率分布 p ( X , Z ∣ θ ) p(\mathbf{X},\mathbf{Z}|\boldsymbol{\theta}) p ( X , Z ∣ θ ) ,其中 X \mathbf{X} X 为所有观测到的样本数据,Z \mathbf{Z} Z 为隐变量,θ \boldsymbol{\theta} θ 为所有参数。算法的目标是调整参数 θ \boldsymbol{\theta} θ 最大化似然函数 p ( X ∣ θ ) p(\mathbf{X}|\boldsymbol{\theta}) p ( X ∣ θ )
选择初始化参数 θ o l d \boldsymbol{\theta}^{old} θ o l d
E step 。计算后验概率 p ( Z ∣ X , θ o l d ) p(\mathbf{Z}|\mathbf{X},\boldsymbol{\theta}^{old}) p ( Z ∣ X , θ o l d )
p ( Z ∣ X , θ o l d ) = p ( Z ) p ( X ∣ Z , θ o l d ) p ( X ∣ θ ) p(\mathbf{Z}|\mathbf{X}, \boldsymbol{\theta}^{old}) = \frac{p(\mathbf{Z})p(\mathbf{X}|\mathbf{Z},\boldsymbol{\theta}^{old})}{p(\mathbf{X}|\boldsymbol{\theta})}
p ( Z ∣ X , θ o l d ) = p ( X ∣ θ ) p ( Z ) p ( X ∣ Z , θ o l d )
M step 。更新参数
θ n e w = arg max θ Q ( θ , θ o l d ) \boldsymbol{\theta^\mathrm{new}}=\underset{\theta}{\operatorname*{\arg\max}}\mathcal{Q}(\boldsymbol{\theta},\boldsymbol{\theta^\mathrm{old}})
θ new = θ arg max Q ( θ , θ old )
其中
Q ( θ , θ o l d ) = ∑ Z p ( Z ∣ X , θ o l d ) ln p ( X , Z ∣ θ ) . \mathcal{Q}(\boldsymbol{\theta},\boldsymbol{\theta^\mathrm{old}})=\sum_\mathbf{Z}p(\mathbf{Z}|\mathbf{X},\boldsymbol{\theta^\mathrm{old}})\ln p(\mathbf{X},\mathbf{Z}|\boldsymbol{\theta}).
Q ( θ , θ old ) = Z ∑ p ( Z ∣ X , θ old ) ln p ( X , Z ∣ θ ) .
检查收敛性,更新参数
θ o l d ← θ n e w \boldsymbol{\theta}^\mathrm{old}\leftarrow\boldsymbol{\theta}^\mathrm{new}
θ old ← θ new
Derive an EM algorithm for BMM
这里将 EM 算法用于伯努利分布上。
首先写出伯努利分布:B e r n ( x ∣ μ j ) = μ j x ( 1 − μ j ) 1 − x Bern(x|\mu_j) = \mu_j^{x} (1-\mu_j)^{1-x} B er n ( x ∣ μ j ) = μ j x ( 1 − μ j ) 1 − x 。
BMM:p ( x ∣ θ ) = ∑ j = 1 K α j B ( x ∣ μ j ) p(x|\theta)=\sum_{j=1}^{K}\alpha_j B(x|\mu_j) p ( x ∣ θ ) = ∑ j = 1 K α j B ( x ∣ μ j ) 。
记 θ \theta θ 为参数的集合,即 θ = { α j , μ j ∣ j = 1 , 2 , … , K , ∑ j = 1 K α j = 1 } \theta=\left\{ \alpha_j,\mu_j|j=1,2, \ldots ,K , \sum_{j=1}^{K}\alpha_j=1\right\} θ = { α j , μ j ∣ j = 1 , 2 , … , K , ∑ j = 1 K α j = 1 }
E-step:
P ( Z ∣ X , θ ) = ∏ t = 1 N P ( z t ∣ x t , θ ) P ( z t = j ∣ x t , θ ) ≡ P ( j ∣ x t , θ ) = α j B ( x t ∣ μ j ) P ( x t ∣ θ ) ∣ θ = θ o l d \begin{aligned}
&P(\mathbf{Z}|\mathbf{X}, \theta) = \prod_{t=1}^{N} P(z_t|x_t, \theta) \\
&P(z_t=j|x_t, \theta) \equiv P(j|x_t, \theta) = \frac{\alpha_j B(x_t|\mu_j)}{P(x_t|\theta)} \bigg|_{\theta=\theta^{old}}
\end{aligned}
P ( Z ∣ X , θ ) = t = 1 ∏ N P ( z t ∣ x t , θ ) P ( z t = j ∣ x t , θ ) ≡ P ( j ∣ x t , θ ) = P ( x t ∣ θ ) α j B ( x t ∣ μ j ) θ = θ o l d
M-step:
Q ( θ , θ o l d ) = ∑ Z P ( Z ∣ X , θ o l d ) ln P ( X , Z ∣ θ ) = ∑ Z P ( Z ∣ X , θ o l d ) ln ∏ t = 1 N P ( x t , z t ∣ θ ) = ∑ t = 1 N ∑ Z P ( Z ∣ X , θ o l d ) ln P ( x t , z t ∣ θ ) = ∑ t = 1 N ∑ z t P ( z t ∣ x t , θ o l d ) ln P ( x t , z t ∣ θ ) ( use P ( Z ∣ X , θ ) = ∏ t = 1 N P ( z t ∣ x t , θ ) and ∑ Z = ∑ z 1 ∑ z 2 ⋯ ∑ z K ) = ∑ t = 1 N ∑ j = 1 K P ( j ∣ x t , θ o l d ) ln [ α j B ( x t ∣ μ j ) ] = ∑ t = 1 N ∑ j = 1 K P ( j ∣ x t , θ o l d ) [ ln α j + ln B ( x t ∣ μ j ) ] = ∑ t = 1 N ∑ j = 1 K P ( j ∣ x t , θ o l d ) [ ln α j + x t ln μ j + ( 1 − x t ) ln ( 1 − μ j ) ] \begin{aligned}
Q(\theta,\theta^{old}) & = \sum_{\mathbf{Z}}P(\mathbf{Z}|\mathbf{X},\theta^{old})\ln P(\mathbf{X},\mathbf{Z}|\theta) \\
& = \sum_{\mathbf{Z}}P(\mathbf{Z}|\mathbf{X},\theta^{old})\ln \prod_{t=1}^{N} P(x_t,z_t|\theta) \\
& = \sum_{t=1}^{N}\sum_{\mathbf{Z}}P(\mathbf{Z}|\mathbf{X},\theta^{old})\ln P(x_t,z_t|\theta) \\
& = \sum_{t=1}^{N} \sum_{z_t} P(z_t|x_t, \theta^{old}) \ln P(x_t,z_t|\theta) \quad (\text{use~} P(\mathbf{Z}|\mathbf{X}, \theta) = \prod_{t=1}^{N} P(z_t|x_t, \theta) \text{~and~} \sum_{\mathbf{Z}}=\sum_{z_1}\sum_{z_2}\cdots \sum_{z_{K}}) \\
& = \sum_{t=1}^{N} \sum_{j=1}^{K} P(j|x_t,\theta^{old}) \ln [\alpha_j B(x_t|\mu_j)] \\
& = \sum_{t=1}^{N} \sum_{j=1}^{K} P(j|x_t,\theta^{old}) [\ln \alpha_j+ \ln B(x_t|\mu_j)] \\
& = \sum_{t=1}^{N} \sum_{j=1}^{K} P(j|x_t,\theta^{old}) [\ln \alpha_j+ x_t \ln \mu_j + (1-x_t)\ln (1-\mu_j)] \\
\end{aligned}
Q ( θ , θ o l d ) = Z ∑ P ( Z ∣ X , θ o l d ) ln P ( X , Z ∣ θ ) = Z ∑ P ( Z ∣ X , θ o l d ) ln t = 1 ∏ N P ( x t , z t ∣ θ ) = t = 1 ∑ N Z ∑ P ( Z ∣ X , θ o l d ) ln P ( x t , z t ∣ θ ) = t = 1 ∑ N z t ∑ P ( z t ∣ x t , θ o l d ) ln P ( x t , z t ∣ θ ) ( use P ( Z ∣ X , θ ) = t = 1 ∏ N P ( z t ∣ x t , θ ) and Z ∑ = z 1 ∑ z 2 ∑ ⋯ z K ∑ ) = t = 1 ∑ N j = 1 ∑ K P ( j ∣ x t , θ o l d ) ln [ α j B ( x t ∣ μ j )] = t = 1 ∑ N j = 1 ∑ K P ( j ∣ x t , θ o l d ) [ ln α j + ln B ( x t ∣ μ j )] = t = 1 ∑ N j = 1 ∑ K P ( j ∣ x t , θ o l d ) [ ln α j + x t ln μ j + ( 1 − x t ) ln ( 1 − μ j )]
接下来通过最大化 Q Q Q 来求 μ \mu μ 和 α \alpha α 。
∂ Q ∂ μ j = ∑ t = 1 N P ( j ∣ x t , θ o l d ) ( x t μ j − 1 − x t 1 − μ j ) = ∑ t = 1 N P ( j ∣ x t , θ o l d ) x t − μ j μ j ( 1 − μ j ) = 0 ⇒ μ j = ∑ t = 1 N x t P ( j ∣ x t , θ o l d ) ∑ t = 1 N P ( j ∣ x t , θ o l d ) \begin{aligned}
\frac{\partial Q}{\partial \mu_j} &= \sum_{t=1}^{N} P(j|x_t,\theta^{old}) (\frac{x_t}{\mu_j}-\frac{1-x_t}{1-\mu_j}) = \sum_{t=1}^{N} P(j|x_t,\theta^{old}) \frac{x_t-\mu_j}{\mu_j(1-\mu_j)} = 0 \\
\Rightarrow \mu_j &= \frac{\sum_{t=1}^{N}x_t P(j|x_t,\theta^{old})}{\sum_{t=1}^{N} P(j|x_t,\theta^{old})}
\end{aligned}
∂ μ j ∂ Q ⇒ μ j = t = 1 ∑ N P ( j ∣ x t , θ o l d ) ( μ j x t − 1 − μ j 1 − x t ) = t = 1 ∑ N P ( j ∣ x t , θ o l d ) μ j ( 1 − μ j ) x t − μ j = 0 = ∑ t = 1 N P ( j ∣ x t , θ o l d ) ∑ t = 1 N x t P ( j ∣ x t , θ o l d )
对于 α \alpha α ,由于 α \alpha α 还有一个约束,即 ∑ j = 1 K α j = 1 \sum_{j=1}^{K}\alpha_j=1 ∑ j = 1 K α j = 1 ,因此使用拉格朗日乘子法,记 Q ′ = Q + λ ( ∑ j = 1 K α j − 1 ) Q' = Q + \lambda (\sum_{j=1}^{K} \alpha_j - 1) Q ′ = Q + λ ( ∑ j = 1 K α j − 1 )
{ ∂ Q ′ ∂ α j = ∑ t = 1 N P ( j ∣ x t , θ o l d ) α j − 1 + λ = 0 ∑ j = 1 K α j = 1 ⇒ { λ = − ∑ t = 1 N ∑ j = 1 K P ( j ∣ x t , θ o l d ) = − N α j = − 1 N ∑ t = 1 N P ( j ∣ x t , θ o l d ) \begin{aligned}
& \begin{cases}
\displaystyle \frac{\partial Q'}{\partial \alpha_j} = \sum_{t=1}^{N} P(j|x_t,\theta^{old}) \alpha_j^{-1} + \lambda =0 \\
\displaystyle \sum_{j=1}^{K} \alpha_j = 1
\end{cases} \\
\Rightarrow
& \begin{cases}
\displaystyle \lambda = -\sum_{t=1}^{N} \sum_{j=1}^{K} P(j|x_t,\theta^{old}) = -N \\
\displaystyle \alpha_j = - \frac{1}{N} \sum_{t=1}^{N} P(j|x_t,\theta^{old})
\end{cases}
\end{aligned}
⇒ ⎩ ⎨ ⎧ ∂ α j ∂ Q ′ = t = 1 ∑ N P ( j ∣ x t , θ o l d ) α j − 1 + λ = 0 j = 1 ∑ K α j = 1 ⎩ ⎨ ⎧ λ = − t = 1 ∑ N j = 1 ∑ K P ( j ∣ x t , θ o l d ) = − N α j = − N 1 t = 1 ∑ N P ( j ∣ x t , θ o l d )
Why EM never decreases the (log)-likelihood?
Jensen’s Inequality
一个凸函数的性质,这里应用在 log \log log 函数上
对于 α i ≥ 0 , ∑ α i = 1 \alpha_i\ge 0, \sum \alpha_i=1 α i ≥ 0 , ∑ α i = 1 ,并且 x i > 0 x_i>0 x i > 0 ,有
log ( ∑ i α i x i ) ≥ ∑ i α i log ( x i ) \log \left( \sum_{i}\alpha_i x_i \right) \ge \sum_{i}\alpha_i \log(x_i)
log ( i ∑ α i x i ) ≥ i ∑ α i log ( x i )
KL divergence
K L ( p ∣ ∣ q ) = ∑ i q i log q i p i or ∫ p ( x ) log p ( x ) q ( x ) d x ≥ 0 KL(p||q) = \sum_{i}q_i \log \frac{q_i}{p_{i}} \quad \text{or} \quad \int p(x)\log \frac{p(x)}{q(x)}\mathrm{d}x \ge 0
K L ( p ∣∣ q ) = i ∑ q i log p i q i or ∫ p ( x ) log q ( x ) p ( x ) d x ≥ 0
通常用于度量两个分布之间的距离。
证明 1 (Jenson’s Inequality) :
K L ( q ∣ ∣ p ) = ∑ i q i log q i p i = − ∑ i q i log p i q i KL(q||p) = \sum_{i}q_i \log \frac{q_i}{p_{i}} = - \sum_{i} q_i \log \frac{p_{i}}{q_i}
K L ( q ∣∣ p ) = i ∑ q i log p i q i = − i ∑ q i log q i p i
利用 Jenson’s Inequality,
∑ i q i log p i q i ≤ log ( ∑ i q i ⋅ p i q i ) = 0 ⇒ K L ( p ∣ ∣ q ) ≥ 0 \sum_{i} q_i \log \frac{p_{i}}{q_i} \le \log(\sum_{i}q_i \cdot \frac{p_{i}}{q_i}) = 0
\Rightarrow KL(p||q) \ge 0
i ∑ q i log q i p i ≤ log ( i ∑ q i ⋅ q i p i ) = 0 ⇒ K L ( p ∣∣ q ) ≥ 0
证明 2 (Lagrange multiplier) :
E = d e f KL [ q ∥ p ] + λ ( 1 − ∑ i q i ) = ∑ i q i log q i p i + λ ( 1 − ∑ i q i ) \begin{aligned}E&\stackrel{\mathrm{def}}{=}\textbf{KL}[q\|p]+\lambda\big(1-\sum_iq_i\big)=\sum_iq_i\log\frac{q_i}{p_i}+\lambda\big(1-\sum_iq_i\big)\end{aligned}
E = def KL [ q ∥ p ] + λ ( 1 − i ∑ q i ) = i ∑ q i log p i q i + λ ( 1 − i ∑ q i )
∂ E ∂ q i = log q i − log p i + 1 − λ = 0 ⇒ q i = p i exp ( λ − 1 ) ∂ E ∂ λ = 1 − ∑ i q i = 0 ⇒ ∑ i q i = 1 } ⇒ q i = p i \left.
\begin{array}{rcl}
\displaystyle \frac{\partial E}{\partial q_i}&=&\log q_i-\log p_i+1-\lambda=0\Rightarrow\color{red}{q_i=p_i\exp(\lambda-1)}\\ \\
\displaystyle \frac{\partial E}{\partial\lambda}&=&1-\sum_iq_i=0\Rightarrow\color{red}{\sum_iq_i=1}
\end{array}
\right\}
\Rightarrow q_i=p_i
∂ q i ∂ E ∂ λ ∂ E = = log q i − log p i + 1 − λ = 0 ⇒ q i = p i e x p ( λ − 1 ) 1 − ∑ i q i = 0 ⇒ ∑ i q i = 1 ⎭ ⎬ ⎫ ⇒ q i = p i
EM as maximizing a variational lower bound
根据 Jensen’s Inequality,E [ log ( x ) ] ≤ log ( E [ x ] ) E[\log(x)]\le \log (E[x]) E [ log ( x )] ≤ log ( E [ x ])
log ( P ( x ∣ θ ) ) = log ( ∑ y P ( x , y ∣ θ ) ) = log ( ∑ y q ( y ) P ( x , y ∣ θ ) q ( y ) ) ≥ E q [ log ( P ( x , y ∣ θ ) q ( y ) ) ] = E q [ log ( P ( y ∣ x , θ ) P ( x ∣ θ ) q ( y ) ) ] = E q [ log ( P ( x ∣ θ ) ) ] − E q [ log ( q ( y ) P ( y ∣ x , θ ) ) ] = E q [ log ( P ( x ∣ θ ) ) ] − K L ( q ( y ) ∥ P ( y ∣ x , θ ) ) = log ( P ( x ∣ θ ) ) − K L ( q ( y ) ∥ P ( y ∣ x , θ ) ) \begin{aligned}
\log(P(\mathbf{x}|\theta)) & = \log(\sum_{\mathbf{y}}P(\mathbf{x},\mathbf{y}|\theta)) \\
& =\log(\sum_{\mathbf{y}}q(\mathbf{y})\frac{P(\mathbf{x},\mathbf{y}|\theta)}{q(\mathbf{y})}) \\
&\geq E_{q} [\log(\frac{P(\mathbf{x},\mathbf{y}|\theta)}{q(\mathbf{y})})] \\
&= E_q[\log(\frac{P(\mathbf{y}|\mathbf{x},\theta)P(\mathbf{x}|\theta)}{q(\mathbf{y})})] \\
&= E_q[\log(P(\mathbf{x}|\theta))]-E_q[\log(\frac{q(\mathbf{y})}{P(\mathbf{y}|\mathbf{x},\theta)})] \\
&= E_q[\log(P(\mathbf{x}|\theta))]-KL(q(\mathbf{y})\|P(\mathbf{y}|\mathbf{x},\theta)) \\
&= \log(P(\mathbf{x}|\theta)) - KL(q(\mathbf{y})\|P(\mathbf{y}|\mathbf{x},\theta))
\end{aligned}
log ( P ( x ∣ θ )) = log ( y ∑ P ( x , y ∣ θ )) = log ( y ∑ q ( y ) q ( y ) P ( x , y ∣ θ ) ) ≥ E q [ log ( q ( y ) P ( x , y ∣ θ ) )] = E q [ log ( q ( y ) P ( y ∣ x , θ ) P ( x ∣ θ ) )] = E q [ log ( P ( x ∣ θ ))] − E q [ log ( P ( y ∣ x , θ ) q ( y ) )] = E q [ log ( P ( x ∣ θ ))] − K L ( q ( y ) ∥ P ( y ∣ x , θ )) = log ( P ( x ∣ θ )) − K L ( q ( y ) ∥ P ( y ∣ x , θ ))
从上式可以看出,E q [ log ( P ( x , y ∣ θ ) q ( y ) ) ] E_{q} [\log(\frac{P(\mathbf{x},\mathbf{y}|\theta)}{q(\mathbf{y})})] E q [ log ( q ( y ) P ( x , y ∣ θ ) )] 和似然函数 log ( P ( x ∣ θ ) ) \log(P(\mathbf{x}|\theta)) log ( P ( x ∣ θ )) 差了一个 K L ( q ( y ) ∥ P ( y ∣ x , θ ) ) KL(q(\mathbf{y})\|P(\mathbf{y}|\mathbf{x},\theta)) K L ( q ( y ) ∥ P ( y ∣ x , θ )) ,那么可以将 E q [ log ( P ( x , y ∣ θ ) q ( y ) ) ] E_{q} [\log(\frac{P(\mathbf{x},\mathbf{y}|\theta)}{q(\mathbf{y})})] E q [ log ( q ( y ) P ( x , y ∣ θ ) )] 看作是 log ( P ( x ∣ θ ) ) \log(P(\mathbf{x}|\theta)) log ( P ( x ∣ θ )) 的下界。对于 Expectation Step,我们做的是让 q ( y ) = P ( y ∣ x , θ ) q(\mathbf{y})=P(\mathbf{y}|\mathbf{x},\theta) q ( y ) = P ( y ∣ x , θ ) ,即让下界等于似然函数。
然后考虑
log ( P ( x ∣ θ ) ) ≥ E q [ log ( P ( x , y ∣ θ ) q ( y ) ) ] = E q [ log ( P ( x , y ∣ θ ) ) ] − E q [ log ( q ( y ) ) ] = E q [ log ( P ( x , y ∣ θ ) ) ] + H ( q ( y ) ) \begin{aligned}
\log(P(\mathbf{x}|\theta))& \geq E_{q}[\log(\frac{P(\mathbf{x},\mathbf{y}|\theta)}{q(\mathbf{y})})] \\
& = E_q[\log(P(\mathbf{x},\mathbf{y}|\theta))]-E_q[\log(q(\mathbf{y}))] \\
& = E_q[\log(P(\mathbf{x},\mathbf{y}|\theta))]+H(q(\mathbf{y}))
\end{aligned}
log ( P ( x ∣ θ )) ≥ E q [ log ( q ( y ) P ( x , y ∣ θ ) )] = E q [ log ( P ( x , y ∣ θ ))] − E q [ log ( q ( y ))] = E q [ log ( P ( x , y ∣ θ ))] + H ( q ( y ))
可以发现式中 E q [ log ( P ( x , y ∣ θ ) ) ] E_q[\log(P(\mathbf{x},\mathbf{y}|\theta))] E q [ log ( P ( x , y ∣ θ ))] 即为我们在 Maximization Step 中优化的 Q Q Q 函数。通过改变 θ \theta θ 来最大化 Q Q Q 函数,这就相当于提升了似然函数的下界 E q [ log ( P ( x , y ∣ θ ) q ( y ) ) ] E_{q} [\log(\frac{P(\mathbf{x},\mathbf{y}|\theta)}{q(\mathbf{y})})] E q [ log ( q ( y ) P ( x , y ∣ θ ) )] ,同时 θ \theta θ 的改变导致了 P ( y ∣ x , θ ) P(\mathbf{y}|\mathbf{x},\theta) P ( y ∣ x , θ ) 的变化,使得似然函数再次大于它的下界。
该过程可由下图展示,其中 F \mathcal{F} F 函数即为下界 E q [ log ( P ( x , y ∣ θ ) q ( y ) ) ] E_{q} [\log(\frac{P(\mathbf{x},\mathbf{y}|\theta)}{q(\mathbf{y})})] E q [ log ( q ( y ) P ( x , y ∣ θ ) )] .
An alternative view of EM
Overview
记模型的隐变量为 Y Y Y ,观察到的变量为 X X X ,X X X 和 Y Y Y 之间一一对应。
或者从产生式的模型来理解,给定参数 θ \theta θ ,Y Y Y 能够得到分布 q ( Y ) = q ( y ∣ θ ) q(Y)=q(y|\theta) q ( Y ) = q ( y ∣ θ ) ;同时在有了 Y Y Y 之后,能够再产生 X X X ,同样是按照一个分布来生成 q ( x ∣ y , θ ) q(x|y,\theta) q ( x ∣ y , θ ) 。有了上面两点模型假设,理论上就可以得到联合概率分布:q ( x , y ∣ θ ) = q ( x ∣ y , θ ) q ( y ∣ θ ) q(x,y|\theta)=q(x|y,\theta)q(y|\theta) q ( x , y ∣ θ ) = q ( x ∣ y , θ ) q ( y ∣ θ ) ,以及 x x x 的概率分布 q ( x ∣ θ ) = ∫ q ( x , y ∣ θ ) d y q(x|\theta)=\int q(x,y|\theta)\mathrm{d}y q ( x ∣ θ ) = ∫ q ( x , y ∣ θ ) d y
建立了模型之后,我们希望能够拿模型来拟合数据,使得误差最小,即 likelihood 最大。
将数据记为 X N = { x 1 , x 2 , … x N } \mathbf{X}_{N}=\left\{ x_1,x_2, \ldots x_{N} \right\} X N = { x 1 , x 2 , … x N } ,那么可以利用 δ \delta δ 函数写出数据的分布:p ( x ) = δ ( x − x n ) p(x)=\delta(x-x_n) p ( x ) = δ ( x − x n ) 。得到了数据之后,我们希望能够回推,即使用 X X X 来推得 Y Y Y 。将这个逆过程记为分布 p ( y ∣ x ) p(y|x) p ( y ∣ x ) 。
此时可以得到两个联合概率分布:
p ( y ∣ x ) p ( x ) q ( x ∣ y , θ ) q ( y ∣ θ ) p(y|x)p(x) \quad q(x|y,\theta) q(y|\theta)
p ( y ∣ x ) p ( x ) q ( x ∣ y , θ ) q ( y ∣ θ )
其中 p ( x ) p(x) p ( x ) 给定,q ( x ∣ y , θ ) q ( y ∣ θ ) q(x|y,\theta) q(y|\theta) q ( x ∣ y , θ ) q ( y ∣ θ ) 由模型给出,而 p ( y ∣ x ) p(y|x) p ( y ∣ x ) 未知。由于我们希望模型能够刻画数据,因此两个联合概率分布的距离要尽量小。此时就可以使用 KL divergence 来刻画这件事:
min p ( y ∣ x ) , θ K L ( p ( y ∣ x ) p ( x ) ∣ ∣ q ( x ∣ y , θ ) q ( y ∣ θ ) ) \min_{p(y|x), \theta} KL(p(y|x)p(x) || q(x|y,\theta)q(y|\theta))
p ( y ∣ x ) , θ min K L ( p ( y ∣ x ) p ( x ) ∣∣ q ( x ∣ y , θ ) q ( y ∣ θ ))
再利用贝叶斯,q ( x ∣ y , θ ) q ( y ∣ θ ) = q ( x ∣ θ ) q ( y ∣ x , θ ) q(x|y,\theta) q(y|\theta) = q(x|\theta)q(y|x,\theta) q ( x ∣ y , θ ) q ( y ∣ θ ) = q ( x ∣ θ ) q ( y ∣ x , θ ) 。那么从直观上看,可以让 q ( x ∣ θ ) q(x|\theta) q ( x ∣ θ ) 和 p ( x ) p(x) p ( x ) 匹配,让 q ( y ∣ x , θ ) q(y|x,\theta) q ( y ∣ x , θ ) 和 p ( y ∣ x ) p(y|x) p ( y ∣ x ) 匹配。
E-step and M-step
首先单独考察 q ( x ∣ θ ) q(x|\theta) q ( x ∣ θ ) 和 p ( x ) p(x) p ( x )
min θ K L ( p ( x ) ∣ ∣ q ( x ∣ θ ) ) = ∫ p ( x ) log p ( x ) q ( x ∣ θ ) d x = ∫ p ( x ) log p ( x ) d x − ∫ p ( x ) log q ( x ∣ θ ) d x \begin{aligned}
& \min_{\theta} KL(p(x)||q(x|\theta)) \\
= & \int p(x) \log \frac{p(x)}{q(x|\theta)} \mathrm{d}x \\
= & \int p(x) \log p(x) \mathrm{d}x - \int p(x) \log q(x|\theta) \mathrm{d}x
\end{aligned}
= = θ min K L ( p ( x ) ∣∣ q ( x ∣ θ )) ∫ p ( x ) log q ( x ∣ θ ) p ( x ) d x ∫ p ( x ) log p ( x ) d x − ∫ p ( x ) log q ( x ∣ θ ) d x
考虑到 p ( x ) p(x) p ( x ) 为 δ ( x − x n ) \delta(x-x_n) δ ( x − x n ) 的形式,以及 ∫ p ( x ) log p ( x ) d x \int p(x) \log p(x) \mathrm{d}x ∫ p ( x ) log p ( x ) d x 为常数,则
− ∫ p ( x ) log q ( x ∣ θ ) d x = − log q ( x n ∣ θ ) -\int p(x) \log q(x|\theta) \mathrm{d}x = -\log q(x_n|\theta)
− ∫ p ( x ) log q ( x ∣ θ ) d x = − log q ( x n ∣ θ )
但是以上最大似然的形式通常不好求,因此再考虑对 p ( y ∣ x ) p ( x ) p(y|x)p(x) p ( y ∣ x ) p ( x ) 和 q ( x ∣ y , θ ) q ( y ∣ θ ) q(x|y,\theta)q(y|\theta) q ( x ∣ y , θ ) q ( y ∣ θ ) 求 KL divergence。
K L ( p ∣ ∣ q ) = K L ( p ( y ∣ x ) p ( x ) ∣ ∣ q ( x ∣ y , θ ) q ( y ∣ θ ) ) = ∫ p ( y ∣ x ) p ( x ) log p ( y ∣ x ) p ( x ) q ( x ∣ y , θ ) q ( y ∣ θ ) d y d x = ∫ p ( y ∣ x ) p ( x ) log p ( y ∣ x ) p ( x ) q ( y ∣ x , θ ) q ( x ∣ θ ) d y d x = ∫ x p ( x ) ∫ y p ( y ∣ x ) log p ( y ∣ x ) q ( y ∣ x , θ ) d y d x + ∫ x p ( x ) ∫ y p ( y ∣ x ) log p ( x ) q ( x ∣ θ ) d y d x = ∫ x p ( x ) ∫ y p ( y ∣ x ) log p ( y ∣ x ) q ( y ∣ x , θ ) d y d x + ∫ x p ( x ) ∫ y p ( y ∣ x ) ( log p ( x ) − log q ( x ∣ θ ) ) d y d x = ∫ x p ( x ) [ K L ( p ( y ∣ x ) ∣ ∣ q ( y ∣ x , θ ) ) − log q ( x ∣ θ ) ] d x + ∫ x p ( x ) log p ( x ) d x \begin{aligned}
KL(p||q) & = KL(p(y|x)p(x) || q(x|y,\theta)q(y|\theta)) \\
& = \int p(y|x) p(x) \log \frac{p(y|x)p(x)}{q(x|y,\theta)q(y|\theta)}\mathrm{d}y \mathrm{d}x \\
& = \int p(y|x) p(x) \log \frac{p(y|x)p(x)}{q(y|x,\theta)q(x|\theta)} \mathrm{d}y \mathrm{d}x \\
& = \int_{x} p(x) \int_{y} p(y|x) \log \frac{p(y|x)}{q(y|x,\theta)}\mathrm{d}y \mathrm{d}x + \int_{x} p(x) \int_{y} p(y|x) \log \frac{p(x)}{q(x|\theta)} \mathrm{d}y \mathrm{d}x \\
& = \int_{x} p(x) \int_{y} p(y|x) \log \frac{p(y|x)}{q(y|x,\theta)}\mathrm{d}y \mathrm{d}x + \int_{x} p(x) \int_{y} p(y|x) \left( \log p(x) - \log q(x|\theta) \right) \mathrm{d}y \mathrm{d}x \\
& = \int_{x} p(x) [KL(p(y|x) || q(y|x,\theta)) - \log q(x|\theta)] \mathrm{d}x + \int_{x} p(x) \log p(x) \mathrm{d}x
\end{aligned}
K L ( p ∣∣ q ) = K L ( p ( y ∣ x ) p ( x ) ∣∣ q ( x ∣ y , θ ) q ( y ∣ θ )) = ∫ p ( y ∣ x ) p ( x ) log q ( x ∣ y , θ ) q ( y ∣ θ ) p ( y ∣ x ) p ( x ) d y d x = ∫ p ( y ∣ x ) p ( x ) log q ( y ∣ x , θ ) q ( x ∣ θ ) p ( y ∣ x ) p ( x ) d y d x = ∫ x p ( x ) ∫ y p ( y ∣ x ) log q ( y ∣ x , θ ) p ( y ∣ x ) d y d x + ∫ x p ( x ) ∫ y p ( y ∣ x ) log q ( x ∣ θ ) p ( x ) d y d x = ∫ x p ( x ) ∫ y p ( y ∣ x ) log q ( y ∣ x , θ ) p ( y ∣ x ) d y d x + ∫ x p ( x ) ∫ y p ( y ∣ x ) ( log p ( x ) − log q ( x ∣ θ ) ) d y d x = ∫ x p ( x ) [ K L ( p ( y ∣ x ) ∣∣ q ( y ∣ x , θ )) − log q ( x ∣ θ )] d x + ∫ x p ( x ) log p ( x ) d x
E-step 的目标就是将上式中 K L ( p ( y ∣ x ) ∣ ∣ q ( y ∣ x , θ ) ) KL(p(y|x) || q(y|x,\theta)) K L ( p ( y ∣ x ) ∣∣ q ( y ∣ x , θ )) 最小化。同时上式还可以写成
− K L ( p ∣ ∣ q ) = ∫ p ( y ∣ x n ) log q ( x n ∣ y , θ ) q ( y ∣ θ ) p ( y ∣ x n ) d y + ∫ p ( y ∣ x ) p ( x ) log 1 p ( x ) d y d x -KL(p||q) = \int p(y|x_n) \log \frac{q(x_n|y,\theta)q(y|\theta)}{p(y|x_n)} \mathrm{d}y + \int p(y|x) p(x) \log \frac{1}{p(x)} \mathrm{d}y \mathrm{d}x
− K L ( p ∣∣ q ) = ∫ p ( y ∣ x n ) log p ( y ∣ x n ) q ( x n ∣ y , θ ) q ( y ∣ θ ) d y + ∫ p ( y ∣ x ) p ( x ) log p ( x ) 1 d y d x
可以看出式子的第一项就是 Q-function 的形式,M-step 最大化 Q-function,也即是在最小化 p , q p,q p , q 之间的 KL divergence。
Question
If in the E-step, the posterior p ( y ∣ x ) p(y|x) p ( y ∣ x ) cannot be computed analytically, then do you have any ideas to tackle this problem?
在 E-step 中,后验通常不好计算,因为
q ( y ∣ x , θ ) = q ( x ∣ y , θ ) q ( y ∣ θ ) q ( x ∣ θ ) q(y|x,\theta) = \frac{q(x|y,\theta)q(y|\theta)}{q(x|\theta)}
q ( y ∣ x , θ ) = q ( x ∣ θ ) q ( x ∣ y , θ ) q ( y ∣ θ )
中,分布通常需要积分才能获得,很有可能得不到解析解。
一种近似的方式是 sampling,即使用计算机进行分布的模拟,然后采样得到概率分布。
可以给 p ( y ∣ x ) p(y|x) p ( y ∣ x ) 假设一个简单的分布 p ( y ∣ x , ϕ ) p(y|x,\phi) p ( y ∣ x , ϕ ) ,比如假设为高斯分布 G ( y ∣ μ ( x ) , Σ ( x ) ) G(y|\mu(x),\Sigma(x)) G ( y ∣ μ ( x ) , Σ ( x )) 。此时优化问题就从原来的 min p ( y ∣ x ) , θ K L ( p ( y ∣ x ) p ( x ) ∣ ∣ q ( x ∣ y , θ ) q ( y ∣ θ ) ) \min_{p(y|x), \theta} KL(p(y|x)p(x) || q(x|y,\theta)q(y|\theta)) min p ( y ∣ x ) , θ K L ( p ( y ∣ x ) p ( x ) ∣∣ q ( x ∣ y , θ ) q ( y ∣ θ )) 转变为 min ϕ , θ K L ( p ( y ∣ x ) p ( x ) ∣ ∣ q ( x ∣ y , θ ) q ( y ∣ θ ) ) \min_{\phi, \theta} KL(p(y|x)p(x) || q(x|y,\theta)q(y|\theta)) min ϕ , θ K L ( p ( y ∣ x ) p ( x ) ∣∣ q ( x ∣ y , θ ) q ( y ∣ θ )) 。
If in the M-step, the optimization by the Q function cannot be solved by letting the derivative equation equal to zero, then do you have any ideas to tackle this problem?
虽然无法使得 ∂ Q ∂ θ = 0 \frac{\partial Q}{\partial \theta}=0 ∂ θ ∂ Q = 0 ,但是可以用梯度下降法来优化 θ \theta θ 。
Bayesian learning, Maximum A posterior (MAP)
Two philosophical point of view
频率学派:
数据的产生存在一个真实的 θ ∗ \theta^{*} θ ∗ ,我们的工作是通过 max θ P ( x ∣ θ ) \max_{\theta}P(x|\theta) max θ P ( x ∣ θ ) 来接近这个真实的 θ ∗ \theta^{*} θ ∗ 。
引入置信区间 (confidence internal):[ a , b ] [a,b] [ a , b ] ,比如说 95 % 95\% 95% 的置信度
同时认为 θ ^ ( x ) → N → + ∞ θ ∗ \hat{\theta}(x) \xrightarrow{N \rightarrow +\infty} \theta^{*} θ ^ ( x ) N → + ∞ θ ∗ ,为无偏估计。
贝叶斯学派
认为 θ \theta θ 可能是取值空间中的任何值,不管 θ ∗ \theta^{*} θ ∗ 是否存在。
Bayesian Learning
贝叶斯定理:
P ( θ ∣ x ) = P ( x ∣ θ ) P ( θ ) P ( x ) P(\theta|x) = \frac{P(x|\theta)P(\theta)}{P(x)}
P ( θ ∣ x ) = P ( x ) P ( x ∣ θ ) P ( θ )
其中 P ( θ ) P(\theta) P ( θ ) 为参数 θ \theta θ 的先验分布。先验分布可能来自:
已有的知识
均匀分布 (先验基本没有影响)
共轭先验 (conjugate priors):后验和先验有相同的形式,比如都为高斯分布。
贝叶斯学习的工作是最大化后验 (MAP, maximum a posterior)
max θ p ( θ ∣ x ) \max_{\theta}p(\theta|x)
θ max p ( θ ∣ x )
等价于
log p ( x , θ ) = log p ( x ∣ θ ) + log p ( θ ) \log p(x,\theta) = \log p(x|\theta) + \log p(\theta)
log p ( x , θ ) = log p ( x ∣ θ ) + log p ( θ )
可以理解为和似然相比,多了先验知识的影响。在一些机器学习的模型中,会使用到正则项,这些正则项就相当于一些先验知识,即认为模型的参数不会太大。
Example
考虑一堆点,假如只是一个高斯分布 p ( x ∣ θ ) = G ( x ∣ μ , Σ ) p(x|\theta)=G(x|\mu,\Sigma) p ( x ∣ θ ) = G ( x ∣ μ , Σ ) 来表示,那么使用最大似然求解,可以得到 μ {\mu} μ 即为均值,Σ {\Sigma} Σ 即为协方差。
但是如果使用贝叶斯学习求解,并且有先验 p ( μ ) = G ( μ ∣ μ 0 , σ 0 2 ) p(\mu)=G(\mu|\mu_0,\sigma_0^{2}) p ( μ ) = G ( μ ∣ μ 0 , σ 0 2 ) ,此时得到的 μ \mu μ 就会有所不同。
P ( μ ∣ X ) ∝ P ( X ∣ μ , Σ ) P ( μ ∣ μ 0 , σ 2 I ) = ∏ t = 1 ∞ P ( x t ∣ μ , Σ ) ⋅ G ( μ ∣ μ 0 , σ 2 I ) log P ( μ ∣ X ) ∝ ∑ t = 1 N log G ( x t ∣ μ , Σ ) + log G ( μ ∣ μ 0 , σ 2 I ) = ∑ t = 1 N { − d 2 log 2 π − 1 2 log ∣ Σ ∣ − 1 2 ( x t − μ ) T Σ − 1 ( x t − μ ) } − d 2 log 2 π − 1 2 log ∣ σ 2 I ∣ − 1 2 ( μ − μ 0 ) T ( σ 2 I ) − 1 ( μ − μ 0 ) \begin{aligned}
P(\mu|X) &\propto P(X|\mu,\Sigma)P(\mu|\mu_0,\sigma^{2}I) = \prod_{t=1}^{\infty} P(x_t|\mu,\Sigma)\cdot G(\mu|\mu_0,\sigma^{2}I) \\
\log P(\mu|X) &\propto \sum_{t=1}^{N} \log G(x_t|\mu,\Sigma) + \log G(\mu|\mu_0,\sigma^{2} I) \\
&= \sum_{t=1}^{N} \left \{ - \frac{d}{2} \log 2\pi - \frac{1}{2} \log \left\vert \Sigma \right\vert - \frac{1}{2}(x_t-\mu)^{\mathrm{T}} \Sigma^{-1} (x_t-\mu) \right\} \\
& \quad - \frac{d}{2} \log 2\pi - \frac{1}{2}\log \left\vert \sigma^{2}I \right\vert - \frac{1}{2}(\mu-\mu_0)^{\mathrm{T}}(\sigma^{2}I)^{-1}(\mu-\mu_0)
\end{aligned}
P ( μ ∣ X ) log P ( μ ∣ X ) ∝ P ( X ∣ μ , Σ ) P ( μ ∣ μ 0 , σ 2 I ) = t = 1 ∏ ∞ P ( x t ∣ μ , Σ ) ⋅ G ( μ ∣ μ 0 , σ 2 I ) ∝ t = 1 ∑ N log G ( x t ∣ μ , Σ ) + log G ( μ ∣ μ 0 , σ 2 I ) = t = 1 ∑ N { − 2 d log 2 π − 2 1 log ∣ Σ ∣ − 2 1 ( x t − μ ) T Σ − 1 ( x t − μ ) } − 2 d log 2 π − 2 1 log σ 2 I − 2 1 ( μ − μ 0 ) T ( σ 2 I ) − 1 ( μ − μ 0 )
然后对 μ \mu μ 求偏微分
∂ ∂ μ P ( μ ∣ X ) = ∑ t = 1 N { − 1 2 Σ − 1 ( x t − μ ) ( − 1 ) } − 1 2 × 2 σ 0 − 2 ( μ − μ 0 ) = Σ − 1 ( N x ˉ − N μ ) − σ 0 − 2 μ + σ 0 − 2 μ 0 = 0 \begin{aligned}
\frac{\partial }{\partial \mu} P(\mu|X) &= \sum_{t=1}^{N} \left \{ - \frac{1}{2} \Sigma^{-1}(x_t-\mu)(-1) \right \} - \frac{1}{2} \times 2\sigma_0^{-2}(\mu-\mu_0) \\
&= \Sigma^{-1}(N \bar{x}-N \mu) - \sigma_0^{-2}\mu + \sigma_0^{-2}\mu_0 \\
&= 0
\end{aligned}
∂ μ ∂ P ( μ ∣ X ) = t = 1 ∑ N { − 2 1 Σ − 1 ( x t − μ ) ( − 1 ) } − 2 1 × 2 σ 0 − 2 ( μ − μ 0 ) = Σ − 1 ( N x ˉ − N μ ) − σ 0 − 2 μ + σ 0 − 2 μ 0 = 0
⇒ μ = ( Σ − 1 + 1 N σ 0 2 I ) − 1 ( Σ − 1 x ˉ + 1 N σ 0 2 μ 0 ) \Rightarrow \mu = (\Sigma^{-1} + \frac{1}{N\sigma_0^{2}}I)^{-1}(\Sigma^{-1}\bar{x}+\frac{1}{N\sigma_0^{2}}\mu_0)
⇒ μ = ( Σ − 1 + N σ 0 2 1 I ) − 1 ( Σ − 1 x ˉ + N σ 0 2 1 μ 0 )
讨论:
当 N → + ∞ N\rightarrow +\infty N → + ∞ ,有 μ → x ˉ \mu \rightarrow \bar{x} μ → x ˉ
当样本足够大时,likelihood 项占主要作用
当 N ≪ + ∞ , σ 0 2 ≪ 1 N\ll +\infty, \sigma_0^{2}\ll_1 N ≪ + ∞ , σ 0 2 ≪ 1 ,有 μ → μ 0 \mu \rightarrow \mu_0 μ → μ 0
Model Selection
不难发现,K-mean 算法和 GMM 算法都无法确定 cluster 的数量 K K K 取多少合适,如果仅从优化结果来看,两者都是当 K K K 越大时效果越好。因此我们需要一个算法来选择 K K K 。
类比日常生活,做一件事时,我们希望使用更少的时间精力来达到比较好的效果,即边际效应。在模型中,使用足够多的高斯 (以GMM为例) 固然使得 likelihood 更大,但是会增加模型复杂度,增加了计算成本以及过拟合的风险。因此可以把模型的复杂度作为 penalty。
Akaike’s Information Criterion (AIC ): ln p ( X n ∣ Θ ^ K ) − d k \ln p(X_n|\hat{\Theta}_{K})-d_k ln p ( X n ∣ Θ ^ K ) − d k
Bayesian Information Criterion (BIC ): ln p ( X n ∣ Θ ^ K ) − 1 2 d k ln N \ln p(X_n|\hat{\Theta}_{K})-\frac{1}{2}d_k \ln N ln p ( X n ∣ Θ ^ K ) − 2 1 d k ln N
d k d_k d k : number of free parameters
N N N : sample size
算法的工作流程为:
对于 k = 1 , 2 , … , K max k=1,2, \ldots ,K_{\max_{}} k = 1 , 2 , … , K m a x ,计算最大似然:
Θ ^ M L ( k ) = argmax log [ P ( X ∣ Θ , k ) ] \widehat{\Theta}_{ML}(k)=\operatorname{argmax}\log[P(X|\Theta,k)]
Θ M L ( k ) = argmax log [ P ( X ∣Θ , k )]
在确定了参数之后,再选择 k k k 使得 selection criterion (AIC, BIC) 最大:
K ∗ = argmax k J ( Θ ^ M L ( k ) ) K^*=\underset{k}{\operatorname*{argmax}}J(\widehat{\Theta}_{ML}(k))
K ∗ = k argmax J ( Θ M L ( k ))
BIC
在 EM 算法中,我们通过最大化 P ( X ∣ θ ) P(X|\theta) P ( X ∣ θ ) 来寻找合适的参数 θ \theta θ ,那么同理,也可以考虑通过最大化 P ( X ∣ K ) P(X|K) P ( X ∣ K ) 来找到合适的 cluster 的数量 K K K ,即 max K P ( X ∣ K ) \max_{K}P(X|K) max K P ( X ∣ K ) 。
当确定了一个 K K K 之后,参数 θ \theta θ 可能取任意的值,因此当我们需要将所有的 θ \theta θ 都考虑一遍。这是从贝叶斯的角度考虑问题。形式化的
P ( X ∣ K ) = ∫ P ( X ∣ θ , K ) P ( θ ∣ K ) d θ P(X|K) = \int P(X|\theta,K) P(\theta|K) \mathrm{d}\theta
P ( X ∣ K ) = ∫ P ( X ∣ θ , K ) P ( θ ∣ K ) d θ
其中 P ( θ ∣ K ) P(\theta|K) P ( θ ∣ K ) 为参数的先验分布。P ( X ∣ K ) P(X|K) P ( X ∣ K ) 称为 marginal likelihood。但是需要注意到:
( θ ∣ k = 1 ) = { μ , Σ } ( θ ∣ k = 2 ) = { α 1 , μ 1 , Σ 1 , α 2 , μ 2 , Σ 2 } ⋯ (\theta|k=1) = \{ \mu,\Sigma \} \\
(\theta|k=2) = \{ \alpha_1,\mu_1,\Sigma_1,\alpha_2, \mu_2,\Sigma_2 \} \\
\cdots
( θ ∣ k = 1 ) = { μ , Σ } ( θ ∣ k = 2 ) = { α 1 , μ 1 , Σ 1 , α 2 , μ 2 , Σ 2 } ⋯
可以看出想要通过积分得到 P ( X ∣ K ) P(X|K) P ( X ∣ K ) 是非常困难的,因此我们需要一些近似的方法。
P ( X ∣ K ) = ∫ e log [ P ( X ∣ θ , K ) P ( θ ∣ K ) ] d θ P(X|K) = \int e^{\log [P(X|\theta,K)P(\theta|K)]} \mathrm{d} \theta
P ( X ∣ K ) = ∫ e l o g [ P ( X ∣ θ , K ) P ( θ ∣ K )] d θ
将 Q ≡ log [ P ( X ∣ θ , K ) P ( θ ∣ K ) ] Q\equiv \log [P(X|\theta,K)P(\theta|K)] Q ≡ log [ P ( X ∣ θ , K ) P ( θ ∣ K )] 在 θ ^ = arg max θ Q ( θ ) \hat{\theta}=\argmax_{\theta}Q(\theta) θ ^ = arg max θ Q ( θ ) 处做泰勒展开。在 θ ^ \hat{\theta} θ ^ 处展开是为了让展开后的一次项为 0 0 0 ,而二次项可以进行高斯积分,所以我们展开到二次进行近似。
Q ≈ log P ( X ∣ θ ^ , K ) P ( θ ^ ∣ K ) + ( θ − θ ^ ) T ∇ Q ∣ θ = θ ^ + 1 2 ( θ − θ ^ ) H θ ( θ − θ ^ ) Q \thickapprox \log P(X|\hat{\theta},K)P(\hat{\theta}|K) + \left. (\theta-\hat{\theta})^{\mathrm{T}}\nabla Q \right \vert_{\theta=\hat{\theta}} + \frac{1}{2}(\theta-\hat{\theta}) H_{\theta} (\theta-\hat{\theta})
Q ≈ log P ( X ∣ θ ^ , K ) P ( θ ^ ∣ K ) + ( θ − θ ^ ) T ∇ Q θ = θ ^ + 2 1 ( θ − θ ^ ) H θ ( θ − θ ^ )
其中 H θ = ∂ 2 Q ∂ θ ∂ θ T \displaystyle H_{\theta}=\frac{\partial ^{2}Q}{\partial \theta \partial \theta^{\mathrm{T}}} H θ = ∂ θ ∂ θ T ∂ 2 Q 称为 Hessian Matrix。由于 θ ^ \hat{\theta} θ ^ 为极大值点,因此 H θ H_{\theta} H θ 在 θ ^ \hat{\theta} θ ^ 处是负定 (negative definate) 的。由此
P ( X ∣ K ) ≈ P ( X ∣ θ ^ , K ) P ( θ ^ ∣ K ) × ∫ e − 1 2 ( θ − θ ^ ) T ( − H θ ) ( θ − θ ^ ) d θ = P ( X ∣ θ ^ , K ) P ( θ ^ ∣ K ) × ( 2 π ) d k / 2 ∣ − H θ − 1 ∣ 1 2 \begin{aligned}
P(X|K) &\thickapprox P(X|\hat{\theta},K)P(\hat{\theta}|K) \times \int e^{-\frac{1}{2}(\theta-\hat{\theta})^{\mathrm{T}}(-H_{\theta})(\theta-\hat{\theta})}\mathrm{d}\theta \\
&= P(X|\hat{\theta},K)P(\hat{\theta}|K) \times (2\pi)^{d_k / 2} \left\vert -H_{\theta}^{-1} \right\vert ^{\frac{1}{2}}
\end{aligned}
P ( X ∣ K ) ≈ P ( X ∣ θ ^ , K ) P ( θ ^ ∣ K ) × ∫ e − 2 1 ( θ − θ ^ ) T ( − H θ ) ( θ − θ ^ ) d θ = P ( X ∣ θ ^ , K ) P ( θ ^ ∣ K ) × ( 2 π ) d k /2 − H θ − 1 2 1
其中 d k = d i m ( θ ) d_k=dim(\theta) d k = d im ( θ ) 。
从而可以得到
log P ( X ∣ K ) = log P ( X ∣ θ ^ , K ) + log P ( θ ^ ∣ K ) + d k 2 log ( 2 π ) + 1 2 log ∣ − H θ − 1 ∣ \log P(X|K) = \log P(X|\hat{\theta},K) + \log P(\hat{\theta}|K) + \frac{d_k}{2} \log (2\pi) + \frac{1}{2} \log \left\vert -H_{\theta}^{-1} \right\vert
log P ( X ∣ K ) = log P ( X ∣ θ ^ , K ) + log P ( θ ^ ∣ K ) + 2 d k log ( 2 π ) + 2 1 log − H θ − 1
上式中第一项对应的最大似然,第二项对应了先验。如果不考虑先验,比如说令 P ( θ ∣ K ) = 1 P(\theta|K)=1 P ( θ ∣ K ) = 1 ,即取为均匀分布,那么
− H θ = − ∂ 2 log P ( X ∣ θ , K ) ∂ θ ∂ θ T = [ − ∂ 2 log P ( X ∣ θ , K ) ∂ θ i ∂ θ j ] i , j -H_{\theta} = - \frac{\partial^{2} \log P(X|\theta,K) }{\partial \theta \partial \theta^{\mathrm{T}}} = \left[ - \frac{\partial^{2} \log P(X|\theta,K)}{\partial \theta_i \partial \theta_j} \right]_{i,j}
− H θ = − ∂ θ ∂ θ T ∂ 2 log P ( X ∣ θ , K ) = [ − ∂ θ i ∂ θ j ∂ 2 log P ( X ∣ θ , K ) ] i , j
如果观测到的数据 x 1 , x 2 , … x n x_1,x_2, \ldots x_n x 1 , x 2 , … x n 都是独立同分布 (i.i.d) 的,可以得到
log P ( X ∣ θ , K ) = 1 N ∑ t = 1 N N log ( x t ∣ θ , K ) → N → + ∞ E [ N log ( x ∣ θ , k ) ] \log P(X|\theta,K) = \frac{1}{N} \sum_{t=1}^{N} N \log (x_t|\theta,K) \xrightarrow{N \rightarrow +\infty} E[N \log (x|\theta,k)]
log P ( X ∣ θ , K ) = N 1 t = 1 ∑ N N log ( x t ∣ θ , K ) N → + ∞ E [ N log ( x ∣ θ , k )]
那么
− H θ → N → + ∞ − N ∂ 2 E [ N log ( x ∣ θ , k ) ] ∂ θ ∂ θ T ∣ θ = θ ^ ∖ N I θ -H_{\theta} \xrightarrow{N \rightarrow +\infty} - \left . N \frac{\partial^{2} E[N \log (x|\theta,k)] }{\partial \theta \partial \theta^{\mathrm{T}}} \right \vert_{\theta = \hat{\theta}} \setminus N I_{\theta}
− H θ N → + ∞ − N ∂ θ ∂ θ T ∂ 2 E [ N log ( x ∣ θ , k )] θ = θ ^ ∖ N I θ
I θ I_{\theta} I θ 称为 Fisher Information Matrix。最终有
log P ( X ∣ K ) = log P ( X ∣ θ ^ , K ) + d k 2 log ( 2 π ) + 1 2 log ∣ N I θ ∣ = log P ( X ∣ θ ^ , K ) + d k 2 log ( 2 π ) − d k 2 log N − 1 2 log ∣ I θ ∣ \begin{aligned}
\log P(X|K) &= \log P(X|\hat{\theta}, K) + \frac{d_k}{2} \log(2\pi) + \frac{1}{2} \log \left\vert N I_{\theta} \right\vert \\
&= \log P(X|\hat{\theta}, K) + \frac{d_k}{2} \log(2\pi) - \frac{d_k}{2} \log N - \frac{1}{2} \log \left\vert I_{\theta} \right\vert
\end{aligned}
log P ( X ∣ K ) = log P ( X ∣ θ ^ , K ) + 2 d k log ( 2 π ) + 2 1 log ∣ N I θ ∣ = log P ( X ∣ θ ^ , K ) + 2 d k log ( 2 π ) − 2 d k log N − 2 1 log ∣ I θ ∣
式子中的第一项和第三项为主导项,可以发现此时即为 BIC。
VBEM
模型:
p ( Z ∣ π ) = ∏ n = 1 N ∏ k = 1 K π k z n k p ( X ∣ Z , μ , Λ ) = ∏ n = 1 N ∏ k = 1 K N ( x n ∣ μ k , Λ k − 1 ) z n k p(\mathbf{Z}|\boldsymbol{\pi})=\prod_{n=1}^N\prod_{k=1}^K\pi_k^{z_{nk}}\quad p(\mathbf{X}|\mathbf{Z},\boldsymbol{\mu},\boldsymbol{\Lambda})=\prod_{n=1}^N\prod_{k=1}^K\mathcal{N}\left(\mathbf{x}_n|\boldsymbol{\mu}_k,\boldsymbol{\Lambda}_k^{-1}\right)^{z_{nk}}
p ( Z ∣ π ) = n = 1 ∏ N k = 1 ∏ K π k z nk p ( X ∣ Z , μ , Λ ) = n = 1 ∏ N k = 1 ∏ K N ( x n ∣ μ k , Λ k − 1 ) z nk
先验:
p ( π ) = D i r ( π ∣ α 0 ) = C ( α 0 ) ∏ k = 1 K π k α 0 − 1 p ( μ , Λ ) = p ( μ ∣ Λ ) p ( Λ ) = ∏ k = 1 K N ( μ k ∣ m 0 , ( β 0 Λ k ) − 1 ) W ( Λ k ∣ W 0 , ν 0 ) \begin{aligned}
&p(\boldsymbol{\pi})=\mathrm{Dir}(\boldsymbol{\pi}|\boldsymbol{\alpha}_{0})=C(\boldsymbol{\alpha}_{0})\prod_{k=1}^{K}\pi_{k}^{\alpha_{0}-1}\\
&p(\boldsymbol{\mu},\boldsymbol{\Lambda})= p(\boldsymbol{\mu}|\boldsymbol{\Lambda})p(\boldsymbol{\Lambda})\\
&=\prod_{k=1}^{K}\mathcal{N}\left(\boldsymbol{\mu}_{k}|\mathbf{m}_{0},(\beta_{0}\boldsymbol{\Lambda}_{k})^{-1}\right)\mathcal{W}(\boldsymbol{\Lambda}_{k}|\mathbf{W}_{0},\nu_{0})
\end{aligned}
p ( π ) = Dir ( π ∣ α 0 ) = C ( α 0 ) k = 1 ∏ K π k α 0 − 1 p ( μ , Λ ) = p ( μ ∣ Λ ) p ( Λ ) = k = 1 ∏ K N ( μ k ∣ m 0 , ( β 0 Λ k ) − 1 ) W ( Λ k ∣ W 0 , ν 0 )