變分貝葉斯方法 是近似貝葉斯推理 與機器學習 中較難積分 的一系列技術,通常用於由觀測變量及未知參數 、潛變量 組成的複雜統計模型 ,之間存在的各種關係可用圖模式 描述。貝葉斯推理中,參數和潛變量一般歸為「未觀察變量」。變分貝葉斯方法主要用於兩個目的:
提供未觀測變量後驗概率 的分析近似,以便對變量統計推斷
得出觀測數據的邊緣似然 的下界 (即給定模型的數據的邊緣概率 ,邊緣化未觀測變量)。通常用於模型選擇 ,一般認為給定模型的邊緣似然越高,擬合效果越好,產生數據的可能性越大(另見貝葉斯因子 )。
就目的1(近似後驗概率),變分貝葉斯是蒙特卡洛法 (特別是馬爾可夫鏈蒙特卡洛 ,如吉布斯採樣 )的替代,可採用完全貝葉斯的方法,對難以取值或抽樣 的複雜分佈 進行統計推斷 。蒙特卡洛技術利用一組樣本對精確後驗值進行數值近似,而變分貝葉斯可求近似後驗的局部最優的精確解析解。
變分貝葉斯可視作最大期望算法 (EM)的推廣,從估計參數的單一最似然值的最大後驗概率 (MAP估計),推廣到計算參數及潛變量的整個近似後驗分佈 的完全貝葉斯估計。貝葉斯估計同EM一樣,也能找到一組最優參數值,且與EM有相同的交替結構——基於不能求解析解的相依方程組。
很多應用中,變分貝葉斯能更快得到與吉布斯採樣精度相當的解,但推導迭代方程組需要更多工作。即便是很多概念上非常簡單的模型也如此,下面以只有2個參數、沒有潛變量的基本不分層模型為例說明。
變分 推斷中,數據
X
{\displaystyle \mathbf {X} }
的一組未觀測變量
Z
=
{
Z
1
…
Z
n
}
{\displaystyle \mathbf {Z} =\{Z_{1}\dots Z_{n}\}}
的後驗分佈近似於所謂變分分佈
Q
(
Z
)
{\displaystyle Q(\mathbf {Z} )}
P
(
Z
∣
X
)
≈
Q
(
Z
)
.
{\displaystyle P(\mathbf {Z} \mid \mathbf {X} )\approx Q(\mathbf {Z} ).}
分佈
Q
(
Z
)
{\displaystyle Q(\mathbf {Z} )}
屬於在形式上比
P
(
Z
∣
X
)
{\displaystyle P(\mathbf {Z} \mid \mathbf {X} )}
簡單的分佈族(如高斯分佈族),這是為使
Q
(
Z
)
{\displaystyle Q(\mathbf {Z} )}
與真實後驗
P
(
Z
∣
X
)
{\displaystyle P(\mathbf {Z} \mid \mathbf {X} )}
更相似。
相似性用不相似函數
d
(
Q
;
P
)
{\displaystyle d(Q;P)}
衡量,推斷(inference)通過求分佈
Q
(
Z
)
{\displaystyle Q(\mathbf {Z} )}
使
d
(
Q
;
P
)
{\displaystyle d(Q;P)}
最小化進行。
最常見的變分貝葉斯法以Q 與P 的KL散度 作為不相似函數,很適合最小化。KL散度的定義是
D
K
L
(
Q
∥
P
)
≜
∑
Z
Q
(
Z
)
log
Q
(
Z
)
P
(
Z
∣
X
)
.
{\displaystyle D_{\mathrm {KL} }(Q\parallel P)\triangleq \sum _{\mathbf {Z} }Q(\mathbf {Z} )\log {\frac {Q(\mathbf {Z} )}{P(\mathbf {Z} \mid \mathbf {X} )}}.}
注意Q 和P 是相反的。反向KL散度在概念上類似於期望最大化算法 (KL散度則產生期望傳播 算法)。
變分技術通常用於近似以下參數:
P
(
Z
∣
X
)
=
P
(
X
∣
Z
)
P
(
Z
)
P
(
X
)
=
P
(
X
∣
Z
)
P
(
Z
)
∫
Z
P
(
X
,
Z
′
)
d
Z
′
{\displaystyle P(\mathbf {Z} \mid \mathbf {X} )={\frac {P(\mathbf {X} \mid \mathbf {Z} )P(\mathbf {Z} )}{P(\mathbf {X} )}}={\frac {P(\mathbf {X} \mid \mathbf {Z} )P(\mathbf {Z} )}{\int _{\mathbf {Z} }P(\mathbf {X} ,\mathbf {Z} ')\,d\mathbf {Z} '}}}
對
Z
{\displaystyle \mathbf {Z} }
邊緣化(以計算分母中的
P
(
X
)
{\displaystyle P(\mathbf {X} )}
)通常是難解的,因為
Z
{\displaystyle \mathbf {Z} }
的搜索空間在組合上可能很大。因此,我們用
Q
(
Z
)
≈
P
(
Z
∣
X
)
{\displaystyle Q(\mathbf {Z} )\approx P(\mathbf {Z} \mid \mathbf {X} )}
求近似。
考慮到
P
(
Z
∣
X
)
=
P
(
X
,
Z
)
P
(
X
)
{\displaystyle P(\mathbf {Z} \mid \mathbf {X} )={\frac {P(\mathbf {X} ,\mathbf {Z} )}{P(\mathbf {X} )}}}
,KL散度也可寫作
D
K
L
(
Q
∥
P
)
=
∑
Z
Q
(
Z
)
[
log
Q
(
Z
)
P
(
Z
,
X
)
+
log
P
(
X
)
]
=
∑
Z
Q
(
Z
)
[
log
Q
(
Z
)
−
log
P
(
Z
,
X
)
]
+
∑
Z
Q
(
Z
)
[
log
P
(
X
)
]
{\displaystyle D_{\mathrm {KL} }(Q\parallel P)=\sum _{\mathbf {Z} }Q(\mathbf {Z} )\left[\log {\frac {Q(\mathbf {Z} )}{P(\mathbf {Z} ,\mathbf {X} )}}+\log P(\mathbf {X} )\right]=\sum _{\mathbf {Z} }Q(\mathbf {Z} )\left[\log Q(\mathbf {Z} )-\log P(\mathbf {Z} ,\mathbf {X} )\right]+\sum _{\mathbf {Z} }Q(\mathbf {Z} )\left[\log P(\mathbf {X} )\right]}
因為
Q
(
Z
)
{\displaystyle Q(\mathbf {Z} )}
是分佈,所以
∑
Z
Q
(
Z
)
=
1
{\displaystyle \sum _{\mathbf {Z} }Q(\mathbf {Z} )=1}
;又由於
P
(
X
)
{\displaystyle P(\mathbf {X} )}
對於
Z
{\displaystyle \mathbf {Z} }
是常數,所以
D
K
L
(
Q
∥
P
)
=
∑
Z
Q
(
Z
)
[
log
Q
(
Z
)
−
log
P
(
Z
,
X
)
]
+
log
P
(
X
)
{\displaystyle D_{\mathrm {KL} }(Q\parallel P)=\sum _{\mathbf {Z} }Q(\mathbf {Z} )\left[\log Q(\mathbf {Z} )-\log P(\mathbf {Z} ,\mathbf {X} )\right]+\log P(\mathbf {X} )}
根據(離散隨機變量 )期望 的定義,上式可以寫作
D
K
L
(
Q
∥
P
)
=
E
Q
[
log
Q
(
Z
)
−
log
P
(
Z
,
X
)
]
+
log
P
(
X
)
{\displaystyle D_{\mathrm {KL} }(Q\parallel P)=\mathbb {E} _{\mathbf {Q} }\left[\log Q(\mathbf {Z} )-\log P(\mathbf {Z} ,\mathbf {X} )\right]+\log P(\mathbf {X} )}
重排為
log
P
(
X
)
=
D
K
L
(
Q
∥
P
)
−
E
Q
[
log
Q
(
Z
)
−
log
P
(
Z
,
X
)
]
=
D
K
L
(
Q
∥
P
)
+
L
(
Q
)
{\displaystyle \log P(\mathbf {X} )=D_{\mathrm {KL} }(Q\parallel P)-\mathbb {E} _{\mathbf {Q} }\left[\log Q(\mathbf {Z} )-\log P(\mathbf {Z} ,\mathbf {X} )\right]=D_{\mathrm {KL} }(Q\parallel P)+{\mathcal {L}}(Q)}
由於對數證據
log
P
(
X
)
{\displaystyle \log P(\mathbf {X} )}
對於Q 是定值,最大化末項
L
(
Q
)
{\displaystyle {\mathcal {L}}(Q)}
會使Q 對P 的KL散度最小化。適當選擇Q 可以讓
L
(
Q
)
{\displaystyle {\mathcal {L}}(Q)}
更容易計算,更容易最大化,因此既有後驗
P
(
Z
∣
X
)
{\displaystyle P(\mathbf {Z} \mid \mathbf {X} )}
的解析近似Q ,也有對數證據
log
P
(
X
)
{\displaystyle \log P(\mathbf {X} )}
的下界
L
(
Q
)
{\displaystyle {\mathcal {L}}(Q)}
(KL散度非負)。
下界
L
(
Q
)
{\displaystyle {\mathcal {L}}(Q)}
與熱力學自由能 類似,也稱作(負)變分自由能 ,因為它也可以表為負能量
E
Q
[
log
P
(
Z
,
X
)
]
{\displaystyle \operatorname {E} _{Q}[\log P(\mathbf {Z} ,\mathbf {X} )]}
加Q 的熵 。
L
(
Q
)
{\displaystyle {\mathcal {L}}(Q)}
項也稱作證據下界 (ELBO),是數據的對數證據的下界。
根據布雷格曼散度 (KL散度是其特例)的廣義勾股定理,可以證明[ 1] [ 2]
布雷格曼散度 的廣義勾股定理[ 2]
D
K
L
(
Q
∥
P
)
≥
D
K
L
(
Q
∥
Q
∗
)
+
D
K
L
(
Q
∗
∥
P
)
,
∀
Q
∗
∈
C
{\displaystyle D_{\mathrm {KL} }(Q\parallel P)\geq D_{\mathrm {KL} }(Q\parallel Q^{*})+D_{\mathrm {KL} }(Q^{*}\parallel P),\forall Q^{*}\in {\mathcal {C}}}
其中
C
{\displaystyle {\mathcal {C}}}
是凸集,且
Q
=
Q
∗
≜
arg
min
Q
∈
C
D
K
L
(
Q
∥
P
)
.
{\displaystyle Q=Q^{*}\triangleq \arg \min _{Q\in {\mathcal {C}}}D_{\mathrm {KL} }(Q\parallel P).}
時等號成立。這時,全局最小值
Q
∗
(
Z
)
=
q
∗
(
Z
1
∣
Z
2
)
q
∗
(
Z
2
)
=
q
∗
(
Z
2
∣
Z
1
)
q
∗
(
Z
1
)
{\displaystyle Q^{*}(\mathbf {Z} )=q^{*}(\mathbf {Z} _{1}\mid \mathbf {Z} _{2})q^{*}(\mathbf {Z} _{2})=q^{*}(\mathbf {Z} _{2}\mid \mathbf {Z} _{1})q^{*}(\mathbf {Z} _{1})}
,其中
Z
=
{
Z
1
,
Z
2
}
{\displaystyle \mathbf {Z} =\{\mathbf {Z_{1}} ,\mathbf {Z_{2}} \}}
可以如下求得:[ 1]
q
∗
(
Z
2
)
=
P
(
X
)
ζ
(
X
)
P
(
Z
2
∣
X
)
exp
(
D
K
L
(
q
∗
(
Z
1
∣
Z
2
)
∥
P
(
Z
1
∣
Z
2
,
X
)
)
)
=
1
ζ
(
X
)
exp
E
q
∗
(
Z
1
∣
Z
2
)
(
log
P
(
Z
,
X
)
q
∗
(
Z
1
∣
Z
2
)
)
,
{\displaystyle q^{*}(\mathbf {Z} _{2})={\frac {P(\mathbf {X} )}{\zeta (\mathbf {X} )}}{\frac {P(\mathbf {Z} _{2}\mid \mathbf {X} )}{\exp(D_{\mathrm {KL} }(q^{*}(\mathbf {Z} _{1}\mid \mathbf {Z} _{2})\parallel P(\mathbf {Z} _{1}\mid \mathbf {Z} _{2},\mathbf {X} )))}}={\frac {1}{\zeta (\mathbf {X} )}}\exp \mathbb {E} _{q^{*}(\mathbf {Z} _{1}\mid \mathbf {Z} _{2})}\left(\log {\frac {P(\mathbf {Z} ,\mathbf {X} )}{q^{*}(\mathbf {Z} _{1}\mid \mathbf {Z} _{2})}}\right),}
其中歸一化常數為
ζ
(
X
)
=
P
(
X
)
∫
Z
2
P
(
Z
2
∣
X
)
exp
(
D
K
L
(
q
∗
(
Z
1
∣
Z
2
)
∥
P
(
Z
1
∣
Z
2
,
X
)
)
)
=
∫
Z
2
exp
E
q
∗
(
Z
1
∣
Z
2
)
(
log
P
(
Z
,
X
)
q
∗
(
Z
1
∣
Z
2
)
)
.
{\displaystyle \zeta (\mathbf {X} )=P(\mathbf {X} )\int _{\mathbf {Z} _{2}}{\frac {P(\mathbf {Z} _{2}\mid \mathbf {X} )}{\exp(D_{\mathrm {KL} }(q^{*}(\mathbf {Z} _{1}\mid \mathbf {Z} _{2})\parallel P(\mathbf {Z} _{1}\mid \mathbf {Z} _{2},\mathbf {X} )))}}=\int _{\mathbf {Z} _{2}}\exp \mathbb {E} _{q^{*}(\mathbf {Z} _{1}\mid \mathbf {Z} _{2})}\left(\log {\frac {P(\mathbf {Z} ,\mathbf {X} )}{q^{*}(\mathbf {Z} _{1}\mid \mathbf {Z} _{2})}}\right).}
ζ
(
X
)
{\displaystyle \zeta (\mathbf {X} )}
項在實踐中常稱作證據下界(ELBO),因為
P
(
X
)
≥
ζ
(
X
)
=
exp
(
L
(
Q
∗
)
)
{\displaystyle P(\mathbf {X} )\geq \zeta (\mathbf {X} )=\exp({\mathcal {L}}(Q^{*}))}
,[ 1] 如上所示。
交換
Z
1
,
Z
2
{\displaystyle \mathbf {Z} _{1},\ \mathbf {Z} _{2}}
的角色,可以迭代計算真實模型邊際
P
(
Z
1
∣
X
)
,
P
(
Z
2
∣
X
)
{\displaystyle P(\mathbf {Z} _{1}\mid \mathbf {X} ),\ P(\mathbf {Z} _{2}\mid \mathbf {X} )}
的近似
q
∗
(
Z
1
)
,
q
∗
(
Z
2
)
{\displaystyle q^{*}(\mathbf {Z} _{1}),\ q^{*}(\mathbf {Z} _{2})}
。這種迭代保證單調收斂,[ 1] 但收斂到的
Q
∗
{\displaystyle Q^{*}}
只是
D
K
L
(
Q
∥
P
)
{\displaystyle D_{\mathrm {KL} }(Q\parallel P)}
的局部極小值。
若約束空間
C
{\displaystyle {\mathcal {C}}}
被限制在獨立空間內,即
q
∗
(
Z
1
∣
Z
2
)
=
q
∗
(
Z
1
)
{\displaystyle q^{*}(\mathbf {Z} _{1}\mid \mathbf {Z} _{2})=q^{*}(\mathbf {Z_{1}} )}
,則上述迭代將成為所謂平均場近似(mean field approximation)
Q
∗
(
Z
)
=
q
∗
(
Z
1
)
q
∗
(
Z
2
)
,
{\displaystyle Q^{*}(\mathbf {Z} )=q^{*}(\mathbf {Z} _{1})q^{*}(\mathbf {Z} _{2}),}
。
變分分佈
Q
(
Z
)
{\displaystyle Q(\mathbf {Z} )}
常被假定為潛變量的某劃分 的因式分解,即對潛變量
Z
{\displaystyle \mathbf {Z} }
的劃分
Z
1
…
Z
M
{\displaystyle \mathbf {Z} _{1}\dots \mathbf {Z} _{M}}
,
Q
(
Z
)
=
∏
i
=
1
M
q
i
(
Z
i
∣
X
)
{\displaystyle Q(\mathbf {Z} )=\prod _{i=1}^{M}q_{i}(\mathbf {Z} _{i}\mid \mathbf {X} )}
由變分法 可以證明,對每個因子
q
j
{\displaystyle q_{j}}
的最佳分佈
q
j
∗
{\displaystyle q_{j}^{*}}
(就KL散度最小的分佈而言,如上所述)滿足[ 3]
q
j
∗
(
Z
j
∣
X
)
=
e
E
q
−
j
∗
[
ln
p
(
Z
,
X
)
]
∫
e
E
q
−
j
∗
[
ln
p
(
Z
,
X
)
]
d
Z
j
{\displaystyle q_{j}^{*}(\mathbf {Z} _{j}\mid \mathbf {X} )={\frac {e^{\operatorname {E} _{q_{-j}^{*}}[\ln p(\mathbf {Z} ,\mathbf {X} )]}}{\int e^{\operatorname {E} _{q_{-j}^{*}}[\ln p(\mathbf {Z} ,\mathbf {X} )]}\,d\mathbf {Z} _{j}}}}
其中
E
q
−
j
∗
[
ln
p
(
Z
,
X
)
]
{\displaystyle \operatorname {E} _{q_{-j}^{*}}[\ln p(\mathbf {Z} ,\mathbf {X} )]}
是數據與潛變量的對數聯合概率 的期望值 ,是相對於不屬於劃分的所有變量的
q
∗
{\displaystyle q^{*}}
而取。關於
q
j
∗
(
Z
j
∣
X
)
{\displaystyle q_{j}^{*}(\mathbf {Z} _{j}\mid \mathbf {X} )}
的推導見[ 4] 的引理 4.1。
實踐中,通常用對數表示:
ln
q
j
∗
(
Z
j
∣
X
)
=
E
q
−
j
∗
[
ln
p
(
Z
,
X
)
]
+
constant
{\displaystyle \ln q_{j}^{*}(\mathbf {Z} _{j}\mid \mathbf {X} )=\operatorname {E} _{q_{-j}^{*}}[\ln p(\mathbf {Z} ,\mathbf {X} )]+{\text{constant}}}
上式中的常數與歸一化常數 (
q
j
∗
{\displaystyle q_{j}^{*}}
上述表達式中的分母)有關,通常通過檢查來恢復,因為表達式的其餘部分通常是已知的分佈(如正態分佈 、伽馬分佈 等)。
利用期望的性質,式
E
q
−
j
∗
[
ln
p
(
Z
,
X
)
]
{\displaystyle \operatorname {E} _{q_{-j}^{*}}[\ln p(\mathbf {Z} ,\mathbf {X} )]}
通常可簡為潛變量先驗分佈 的固定超參數 及不屬於當前劃分的潛變量(即不屬於
Z
j
{\displaystyle \mathbf {Z} _{j}}
的潛變量)的期望值的函數(有時還包括方差 等高階矩 )。這就在某一划分的變量分佈參數同其他劃分的變量的期望值之間產生了循環依賴 ,自然就需要類似期望最大化算法 的迭代 法,以某種方式(也許隨機)初始化潛變量期望(可能還有高階矩),再利用當前期望依次計算每個分佈的參數、適當設置新算出的分佈的期望。這種算法保證收斂。[ 5]
換一種說法,對每個變量劃分,簡化其中變量分佈的表達式、研究分佈與相關變量的函數依賴關係,通常就能確定分佈的族(反過來確定了常數)。分佈的參數公式可用先驗分佈的超參數(已知常數)表示,也可用其他劃分中變量函數的期望表示。通常這些期望可簡為變量本身的期望值函數(即平均值 );有時也會出現變量平方(與變量方差 有關)或更高次冪()高階矩 )的期望。其他變量的分佈一般來自已知族,相關期望的公式可查到;但公式取決於分佈的參數,參數又取決於其他變量的期望,所以每個變量的分佈參數公式都可表為一群變量間非線性相互依賴的方程。通常不能直接求解,不過可以用簡單的迭代算法,大多數時候都能保證收斂。
用對偶公式圖解坐標上升變分推理算法[ 4]
下面的定理稱作變分推理對偶公式,[ 4] 解釋了變分貝葉斯方法中變分分佈的一些重要性質。
Theorem 考慮兩概率空間
(
Θ
,
F
,
P
)
,
(
Θ
,
F
,
Q
)
{\displaystyle (\Theta ,{\mathcal {F}},P),\ (\Theta ,{\mathcal {F}},Q)}
,其中
Q
≪
P
{\displaystyle Q\ll P}
。假設存在共同的主導概率測度
λ
{\displaystyle \lambda }
使
P
≪
λ
,
Q
≪
λ
{\displaystyle P\ll \lambda ,\ Q\ll \lambda }
。令h 是
(
Θ
,
F
,
P
)
{\displaystyle (\Theta ,{\mathcal {F}},P)}
上的任意實值隨機變量 ,滿足
h
∈
L
1
(
P
)
{\displaystyle h\in L_{1}(P)}
,則下列等式成立
log
E
P
[
exp
h
]
=
sup
Q
≪
P
{
E
Q
[
h
]
−
D
KL
(
Q
∥
P
)
}
.
{\displaystyle \log E_{P}[\exp h]={\text{sup}}_{Q\ll P}\{E_{Q}[h]-D_{\text{KL}}(Q\parallel P)\}.}
此外,若且唯若(supremum)
q
(
θ
)
p
(
θ
)
=
exp
h
(
θ
)
E
P
[
exp
h
]
,
{\displaystyle {\frac {q(\theta )}{p(\theta )}}={\frac {\exp h(\theta )}{E_{P}[\exp h]}},}
關於概率測度Q 幾乎是確定的,其中
p
(
θ
)
=
d
P
/
d
λ
,
q
(
θ
)
=
d
Q
/
d
λ
{\displaystyle p(\theta )=dP/d\lambda ,\ q(\theta )=dQ/d\lambda }
分別表示概率測度P 、Q 對
λ
{\displaystyle \lambda }
的Radon–Nikodym導數,右式才取上界。
考慮簡單的不分層貝葉斯模型,由來自正態分佈 (均值 、方差 未知)的獨立同分佈 觀測量組成。[ 6] 下面我們將詳細介紹這模型,以說明變分貝葉斯方法的工作原理。
為了數學上的方便,下例中我們用精度 (即方差 倒數;多元正態分佈時是協方差矩陣 的逆。理論上精度和方差等價,因為兩者間是雙射 )。
將共軛先驗 分佈置於未知均值
μ
{\displaystyle \mu }
、精度
τ
{\displaystyle \tau }
,即均值也遵循高斯分佈,而精度遵循伽馬分佈 。
τ
∼
Gamma
(
a
0
,
b
0
)
μ
|
τ
∼
N
(
μ
0
,
(
λ
0
τ
)
−
1
)
{
x
1
,
…
,
x
N
}
∼
N
(
μ
,
τ
−
1
)
N
=
number of data points
{\displaystyle {\begin{aligned}\tau &\sim \operatorname {Gamma} (a_{0},b_{0})\\\mu |\tau &\sim {\mathcal {N}}(\mu _{0},(\lambda _{0}\tau )^{-1})\\\{x_{1},\dots ,x_{N}\}&\sim {\mathcal {N}}(\mu ,\tau ^{-1})\\N&={\text{number of data points}}\end{aligned}}}
先驗分佈超參數
μ
0
,
λ
0
,
a
0
{\displaystyle \mu _{0},\lambda _{0},a_{0}}
、
b
0
{\displaystyle b_{0}}
是已知定值,可設為小正數,以得到較寬的先驗分佈,表明我們對
μ
{\displaystyle \mu }
、
τ
{\displaystyle \tau }
的先驗分佈一無所知。
已知N 個數據點
X
=
{
x
1
,
…
,
x
N
}
{\displaystyle \mathbf {X} =\{x_{1},\ldots ,x_{N}\}}
,而我們的目標是推斷參數
μ
,
τ
{\displaystyle \mu ,\ \tau }
的後驗分佈
q
(
μ
,
τ
)
=
p
(
μ
,
τ
∣
x
1
,
…
,
x
N
)
{\displaystyle q(\mu ,\tau )=p(\mu ,\tau \mid x_{1},\ldots ,x_{N})}
。
所有變量的聯合概率 可重寫為
p
(
X
,
μ
,
τ
)
=
p
(
X
∣
μ
,
τ
)
p
(
μ
∣
τ
)
p
(
τ
)
{\displaystyle p(\mathbf {X} ,\mu ,\tau )=p(\mathbf {X} \mid \mu ,\tau )p(\mu \mid \tau )p(\tau )}
個體因子是
p
(
X
∣
μ
,
τ
)
=
∏
n
=
1
N
N
(
x
n
∣
μ
,
τ
−
1
)
p
(
μ
∣
τ
)
=
N
(
μ
∣
μ
0
,
(
λ
0
τ
)
−
1
)
p
(
τ
)
=
Gamma
(
τ
∣
a
0
,
b
0
)
{\displaystyle {\begin{aligned}p(\mathbf {X} \mid \mu ,\tau )&=\prod _{n=1}^{N}{\mathcal {N}}(x_{n}\mid \mu ,\tau ^{-1})\\p(\mu \mid \tau )&={\mathcal {N}}\left(\mu \mid \mu _{0},(\lambda _{0}\tau )^{-1}\right)\\p(\tau )&=\operatorname {Gamma} (\tau \mid a_{0},b_{0})\end{aligned}}}
其中
N
(
x
∣
μ
,
σ
2
)
=
1
2
π
σ
2
e
−
(
x
−
μ
)
2
2
σ
2
Gamma
(
τ
∣
a
,
b
)
=
1
Γ
(
a
)
b
a
τ
a
−
1
e
−
b
τ
{\displaystyle {\begin{aligned}{\mathcal {N}}(x\mid \mu ,\sigma ^{2})&={\frac {1}{\sqrt {2\pi \sigma ^{2}}}}e^{\frac {-(x-\mu )^{2}}{2\sigma ^{2}}}\\\operatorname {Gamma} (\tau \mid a,b)&={\frac {1}{\Gamma (a)}}b^{a}\tau ^{a-1}e^{-b\tau }\end{aligned}}}
假設
q
(
μ
,
τ
)
=
q
(
μ
)
q
(
τ
)
{\displaystyle q(\mu ,\tau )=q(\mu )q(\tau )}
,即後驗分佈因式分解為
μ
,
τ
{\displaystyle \mu ,\ \tau }
的獨立因子。這種假設是變分貝葉斯方法的基礎。事實上,真正的後驗分佈並非如此(這種簡單情形我們知道它是正態-伽馬分佈 ),因此所得結果將是近似值。
則
ln
q
μ
∗
(
μ
)
=
E
τ
[
ln
p
(
X
∣
μ
,
τ
)
+
ln
p
(
μ
∣
τ
)
+
ln
p
(
τ
)
]
+
C
=
E
τ
[
ln
p
(
X
∣
μ
,
τ
)
]
+
E
τ
[
ln
p
(
μ
∣
τ
)
]
+
E
τ
[
ln
p
(
τ
)
]
+
C
=
E
τ
[
ln
∏
n
=
1
N
N
(
x
n
∣
μ
,
τ
−
1
)
]
+
E
τ
[
ln
N
(
μ
∣
μ
0
,
(
λ
0
τ
)
−
1
)
]
+
C
2
=
E
τ
[
ln
∏
n
=
1
N
τ
2
π
e
−
(
x
n
−
μ
)
2
τ
2
]
+
E
τ
[
ln
λ
0
τ
2
π
e
−
(
μ
−
μ
0
)
2
λ
0
τ
2
]
+
C
2
=
E
τ
[
∑
n
=
1
N
(
1
2
(
ln
τ
−
ln
2
π
)
−
(
x
n
−
μ
)
2
τ
2
)
]
+
E
τ
[
1
2
(
ln
λ
0
+
ln
τ
−
ln
2
π
)
−
(
μ
−
μ
0
)
2
λ
0
τ
2
]
+
C
2
=
E
τ
[
∑
n
=
1
N
−
(
x
n
−
μ
)
2
τ
2
]
+
E
τ
[
−
(
μ
−
μ
0
)
2
λ
0
τ
2
]
+
E
τ
[
∑
n
=
1
N
1
2
(
ln
τ
−
ln
2
π
)
]
+
E
τ
[
1
2
(
ln
λ
0
+
ln
τ
−
ln
2
π
)
]
+
C
2
=
E
τ
[
∑
n
=
1
N
−
(
x
n
−
μ
)
2
τ
2
]
+
E
τ
[
−
(
μ
−
μ
0
)
2
λ
0
τ
2
]
+
C
3
=
−
E
τ
[
τ
]
2
{
∑
n
=
1
N
(
x
n
−
μ
)
2
+
λ
0
(
μ
−
μ
0
)
2
}
+
C
3
{\displaystyle {\begin{aligned}\ln q_{\mu }^{*}(\mu )&=\operatorname {E} _{\tau }\left[\ln p(\mathbf {X} \mid \mu ,\tau )+\ln p(\mu \mid \tau )+\ln p(\tau )\right]+C\\&=\operatorname {E} _{\tau }\left[\ln p(\mathbf {X} \mid \mu ,\tau )\right]+\operatorname {E} _{\tau }\left[\ln p(\mu \mid \tau )\right]+\operatorname {E} _{\tau }\left[\ln p(\tau )\right]+C\\&=\operatorname {E} _{\tau }\left[\ln \prod _{n=1}^{N}{\mathcal {N}}\left(x_{n}\mid \mu ,\tau ^{-1}\right)\right]+\operatorname {E} _{\tau }\left[\ln {\mathcal {N}}\left(\mu \mid \mu _{0},(\lambda _{0}\tau )^{-1}\right)\right]+C_{2}\\&=\operatorname {E} _{\tau }\left[\ln \prod _{n=1}^{N}{\sqrt {\frac {\tau }{2\pi }}}e^{-{\frac {(x_{n}-\mu )^{2}\tau }{2}}}\right]+\operatorname {E} _{\tau }\left[\ln {\sqrt {\frac {\lambda _{0}\tau }{2\pi }}}e^{-{\frac {(\mu -\mu _{0})^{2}\lambda _{0}\tau }{2}}}\right]+C_{2}\\&=\operatorname {E} _{\tau }\left[\sum _{n=1}^{N}\left({\frac {1}{2}}(\ln \tau -\ln 2\pi )-{\frac {(x_{n}-\mu )^{2}\tau }{2}}\right)\right]+\operatorname {E} _{\tau }\left[{\frac {1}{2}}(\ln \lambda _{0}+\ln \tau -\ln 2\pi )-{\frac {(\mu -\mu _{0})^{2}\lambda _{0}\tau }{2}}\right]+C_{2}\\&=\operatorname {E} _{\tau }\left[\sum _{n=1}^{N}-{\frac {(x_{n}-\mu )^{2}\tau }{2}}\right]+\operatorname {E} _{\tau }\left[-{\frac {(\mu -\mu _{0})^{2}\lambda _{0}\tau }{2}}\right]+\operatorname {E} _{\tau }\left[\sum _{n=1}^{N}{\frac {1}{2}}(\ln \tau -\ln 2\pi )\right]+\operatorname {E} _{\tau }\left[{\frac {1}{2}}(\ln \lambda _{0}+\ln \tau -\ln 2\pi )\right]+C_{2}\\&=\operatorname {E} _{\tau }\left[\sum _{n=1}^{N}-{\frac {(x_{n}-\mu )^{2}\tau }{2}}\right]+\operatorname {E} _{\tau }\left[-{\frac {(\mu -\mu _{0})^{2}\lambda _{0}\tau }{2}}\right]+C_{3}\\&=-{\frac {\operatorname {E} _{\tau }[\tau ]}{2}}\left\{\sum _{n=1}^{N}(x_{n}-\mu )^{2}+\lambda _{0}(\mu -\mu _{0})^{2}\right\}+C_{3}\end{aligned}}}
上面的推導中,
C
,
C
2
,
C
3
{\displaystyle C,\ C_{2},\ C_{3}}
指對
μ
{\displaystyle \mu }
為常數的值。注意
E
τ
[
ln
p
(
τ
)
]
{\displaystyle \operatorname {E} _{\tau }[\ln p(\tau )]}
項不是
μ
{\displaystyle \mu }
的函數,無論
μ
{\displaystyle \mu }
的值是多少,它都不變。因此,第三行中可以把它吸收到末尾的常數項,第七行也如此。
最後一行是
μ
{\displaystyle \mu }
的二次多項式。由於這是
q
μ
∗
(
μ
)
{\displaystyle q_{\mu }^{*}(\mu )}
的對數,我們可以看到
q
μ
∗
(
μ
)
{\displaystyle q_{\mu }^{*}(\mu )}
本身是正態分佈 。
通過一些繁瑣的計算(展開括號里的平方、分離出涉及
μ
{\displaystyle \mu }
、
μ
2
{\displaystyle \mu ^{2}}
的項、對
μ
{\displaystyle \mu }
應用配方法 ),可得到正態分佈的參數:
ln
q
μ
∗
(
μ
)
=
−
E
τ
[
τ
]
2
{
∑
n
=
1
N
(
x
n
−
μ
)
2
+
λ
0
(
μ
−
μ
0
)
2
}
+
C
3
=
−
E
τ
[
τ
]
2
{
∑
n
=
1
N
(
x
n
2
−
2
x
n
μ
+
μ
2
)
+
λ
0
(
μ
2
−
2
μ
0
μ
+
μ
0
2
)
}
+
C
3
=
−
E
τ
[
τ
]
2
{
(
∑
n
=
1
N
x
n
2
)
−
2
(
∑
n
=
1
N
x
n
)
μ
+
(
∑
n
=
1
N
μ
2
)
+
λ
0
μ
2
−
2
λ
0
μ
0
μ
+
λ
0
μ
0
2
}
+
C
3
=
−
E
τ
[
τ
]
2
{
(
λ
0
+
N
)
μ
2
−
2
(
λ
0
μ
0
+
∑
n
=
1
N
x
n
)
μ
+
(
∑
n
=
1
N
x
n
2
)
+
λ
0
μ
0
2
}
+
C
3
=
−
E
τ
[
τ
]
2
{
(
λ
0
+
N
)
μ
2
−
2
(
λ
0
μ
0
+
∑
n
=
1
N
x
n
)
μ
}
+
C
4
=
−
E
τ
[
τ
]
2
{
(
λ
0
+
N
)
μ
2
−
2
(
λ
0
μ
0
+
∑
n
=
1
N
x
n
λ
0
+
N
)
(
λ
0
+
N
)
μ
}
+
C
4
=
−
E
τ
[
τ
]
2
{
(
λ
0
+
N
)
(
μ
2
−
2
(
λ
0
μ
0
+
∑
n
=
1
N
x
n
λ
0
+
N
)
μ
)
}
+
C
4
=
−
E
τ
[
τ
]
2
{
(
λ
0
+
N
)
(
μ
2
−
2
(
λ
0
μ
0
+
∑
n
=
1
N
x
n
λ
0
+
N
)
μ
+
(
λ
0
μ
0
+
∑
n
=
1
N
x
n
λ
0
+
N
)
2
−
(
λ
0
μ
0
+
∑
n
=
1
N
x
n
λ
0
+
N
)
2
)
}
+
C
4
=
−
E
τ
[
τ
]
2
{
(
λ
0
+
N
)
(
μ
2
−
2
(
λ
0
μ
0
+
∑
n
=
1
N
x
n
λ
0
+
N
)
μ
+
(
λ
0
μ
0
+
∑
n
=
1
N
x
n
λ
0
+
N
)
2
)
}
+
C
5
=
−
E
τ
[
τ
]
2
{
(
λ
0
+
N
)
(
μ
−
λ
0
μ
0
+
∑
n
=
1
N
x
n
λ
0
+
N
)
2
}
+
C
5
=
−
1
2
(
λ
0
+
N
)
E
τ
[
τ
]
(
μ
−
λ
0
μ
0
+
∑
n
=
1
N
x
n
λ
0
+
N
)
2
+
C
5
{\displaystyle {\begin{aligned}\ln q_{\mu }^{*}(\mu )&=-{\frac {\operatorname {E} _{\tau }[\tau ]}{2}}\left\{\sum _{n=1}^{N}(x_{n}-\mu )^{2}+\lambda _{0}(\mu -\mu _{0})^{2}\right\}+C_{3}\\&=-{\frac {\operatorname {E} _{\tau }[\tau ]}{2}}\left\{\sum _{n=1}^{N}(x_{n}^{2}-2x_{n}\mu +\mu ^{2})+\lambda _{0}(\mu ^{2}-2\mu _{0}\mu +\mu _{0}^{2})\right\}+C_{3}\\&=-{\frac {\operatorname {E} _{\tau }[\tau ]}{2}}\left\{\left(\sum _{n=1}^{N}x_{n}^{2}\right)-2\left(\sum _{n=1}^{N}x_{n}\right)\mu +\left(\sum _{n=1}^{N}\mu ^{2}\right)+\lambda _{0}\mu ^{2}-2\lambda _{0}\mu _{0}\mu +\lambda _{0}\mu _{0}^{2}\right\}+C_{3}\\&=-{\frac {\operatorname {E} _{\tau }[\tau ]}{2}}\left\{(\lambda _{0}+N)\mu ^{2}-2\left(\lambda _{0}\mu _{0}+\sum _{n=1}^{N}x_{n}\right)\mu +\left(\sum _{n=1}^{N}x_{n}^{2}\right)+\lambda _{0}\mu _{0}^{2}\right\}+C_{3}\\&=-{\frac {\operatorname {E} _{\tau }[\tau ]}{2}}\left\{(\lambda _{0}+N)\mu ^{2}-2\left(\lambda _{0}\mu _{0}+\sum _{n=1}^{N}x_{n}\right)\mu \right\}+C_{4}\\&=-{\frac {\operatorname {E} _{\tau }[\tau ]}{2}}\left\{(\lambda _{0}+N)\mu ^{2}-2\left({\frac {\lambda _{0}\mu _{0}+\sum _{n=1}^{N}x_{n}}{\lambda _{0}+N}}\right)(\lambda _{0}+N)\mu \right\}+C_{4}\\&=-{\frac {\operatorname {E} _{\tau }[\tau ]}{2}}\left\{(\lambda _{0}+N)\left(\mu ^{2}-2\left({\frac {\lambda _{0}\mu _{0}+\sum _{n=1}^{N}x_{n}}{\lambda _{0}+N}}\right)\mu \right)\right\}+C_{4}\\&=-{\frac {\operatorname {E} _{\tau }[\tau ]}{2}}\left\{(\lambda _{0}+N)\left(\mu ^{2}-2\left({\frac {\lambda _{0}\mu _{0}+\sum _{n=1}^{N}x_{n}}{\lambda _{0}+N}}\right)\mu +\left({\frac {\lambda _{0}\mu _{0}+\sum _{n=1}^{N}x_{n}}{\lambda _{0}+N}}\right)^{2}-\left({\frac {\lambda _{0}\mu _{0}+\sum _{n=1}^{N}x_{n}}{\lambda _{0}+N}}\right)^{2}\right)\right\}+C_{4}\\&=-{\frac {\operatorname {E} _{\tau }[\tau ]}{2}}\left\{(\lambda _{0}+N)\left(\mu ^{2}-2\left({\frac {\lambda _{0}\mu _{0}+\sum _{n=1}^{N}x_{n}}{\lambda _{0}+N}}\right)\mu +\left({\frac {\lambda _{0}\mu _{0}+\sum _{n=1}^{N}x_{n}}{\lambda _{0}+N}}\right)^{2}\right)\right\}+C_{5}\\&=-{\frac {\operatorname {E} _{\tau }[\tau ]}{2}}\left\{(\lambda _{0}+N)\left(\mu -{\frac {\lambda _{0}\mu _{0}+\sum _{n=1}^{N}x_{n}}{\lambda _{0}+N}}\right)^{2}\right\}+C_{5}\\&=-{\frac {1}{2}}(\lambda _{0}+N)\operatorname {E} _{\tau }[\tau ]\left(\mu -{\frac {\lambda _{0}\mu _{0}+\sum _{n=1}^{N}x_{n}}{\lambda _{0}+N}}\right)^{2}+C_{5}\end{aligned}}}
注意上面所有步驟用二次和公式都能化簡。
即
q
μ
∗
(
μ
)
∼
N
(
μ
∣
μ
N
,
λ
N
−
1
)
μ
N
=
λ
0
μ
0
+
N
x
¯
λ
0
+
N
λ
N
=
(
λ
0
+
N
)
E
τ
[
τ
]
x
¯
=
1
N
∑
n
=
1
N
x
n
{\displaystyle {\begin{aligned}q_{\mu }^{*}(\mu )&\sim {\mathcal {N}}(\mu \mid \mu _{N},\lambda _{N}^{-1})\\\mu _{N}&={\frac {\lambda _{0}\mu _{0}+N{\bar {x}}}{\lambda _{0}+N}}\\\lambda _{N}&=(\lambda _{0}+N)\operatorname {E} _{\tau }[\tau ]\\{\bar {x}}&={\frac {1}{N}}\sum _{n=1}^{N}x_{n}\end{aligned}}}
q
τ
∗
(
τ
)
{\displaystyle q_{\tau }^{*}(\tau )}
的推導大致相同,簡潔起見略去細節。
ln
q
τ
∗
(
τ
)
=
E
μ
[
ln
p
(
X
∣
μ
,
τ
)
+
ln
p
(
μ
∣
τ
)
]
+
ln
p
(
τ
)
+
constant
=
(
a
0
−
1
)
ln
τ
−
b
0
τ
+
1
2
ln
τ
+
N
2
ln
τ
−
τ
2
E
μ
[
∑
n
=
1
N
(
x
n
−
μ
)
2
+
λ
0
(
μ
−
μ
0
)
2
]
+
constant
{\displaystyle {\begin{aligned}\ln q_{\tau }^{*}(\tau )&=\operatorname {E} _{\mu }[\ln p(\mathbf {X} \mid \mu ,\tau )+\ln p(\mu \mid \tau )]+\ln p(\tau )+{\text{constant}}\\&=(a_{0}-1)\ln \tau -b_{0}\tau +{\frac {1}{2}}\ln \tau +{\frac {N}{2}}\ln \tau -{\frac {\tau }{2}}\operatorname {E} _{\mu }\left[\sum _{n=1}^{N}(x_{n}-\mu )^{2}+\lambda _{0}(\mu -\mu _{0})^{2}\right]+{\text{constant}}\end{aligned}}}
兩側取指數,可見
q
τ
∗
(
τ
)
{\displaystyle q_{\tau }^{*}(\tau )}
服從伽馬分佈 。
q
τ
∗
(
τ
)
∼
Gamma
(
τ
∣
a
N
,
b
N
)
a
N
=
a
0
+
N
+
1
2
b
N
=
b
0
+
1
2
E
μ
[
∑
n
=
1
N
(
x
n
−
μ
)
2
+
λ
0
(
μ
−
μ
0
)
2
]
{\displaystyle {\begin{aligned}q_{\tau }^{*}(\tau )&\sim \operatorname {Gamma} (\tau \mid a_{N},b_{N})\\a_{N}&=a_{0}+{\frac {N+1}{2}}\\b_{N}&=b_{0}+{\frac {1}{2}}\operatorname {E} _{\mu }\left[\sum _{n=1}^{N}(x_{n}-\mu )^{2}+\lambda _{0}(\mu -\mu _{0})^{2}\right]\end{aligned}}}
回憶前幾節的結論:
q
μ
∗
(
μ
)
∼
N
(
μ
∣
μ
N
,
λ
N
−
1
)
μ
N
=
λ
0
μ
0
+
N
x
¯
λ
0
+
N
λ
N
=
(
λ
0
+
N
)
E
τ
[
τ
]
x
¯
=
1
N
∑
n
=
1
N
x
n
{\displaystyle {\begin{aligned}q_{\mu }^{*}(\mu )&\sim {\mathcal {N}}(\mu \mid \mu _{N},\lambda _{N}^{-1})\\\mu _{N}&={\frac {\lambda _{0}\mu _{0}+N{\bar {x}}}{\lambda _{0}+N}}\\\lambda _{N}&=(\lambda _{0}+N)\operatorname {E} _{\tau }[\tau ]\\{\bar {x}}&={\frac {1}{N}}\sum _{n=1}^{N}x_{n}\end{aligned}}}
q
τ
∗
(
τ
)
∼
Gamma
(
τ
∣
a
N
,
b
N
)
a
N
=
a
0
+
N
+
1
2
b
N
=
b
0
+
1
2
E
μ
[
∑
n
=
1
N
(
x
n
−
μ
)
2
+
λ
0
(
μ
−
μ
0
)
2
]
{\displaystyle {\begin{aligned}q_{\tau }^{*}(\tau )&\sim \operatorname {Gamma} (\tau \mid a_{N},b_{N})\\a_{N}&=a_{0}+{\frac {N+1}{2}}\\b_{N}&=b_{0}+{\frac {1}{2}}\operatorname {E} _{\mu }\left[\sum _{n=1}^{N}(x_{n}-\mu )^{2}+\lambda _{0}(\mu -\mu _{0})^{2}\right]\end{aligned}}}
每種情形下,某變量的分佈參數取決於對另一變量的期望。可用正態分佈和伽馬分佈矩期望的標準公式推廣期望:
E
[
τ
∣
a
N
,
b
N
]
=
a
N
b
N
E
[
μ
∣
μ
N
,
λ
N
−
1
]
=
μ
N
E
[
X
2
]
=
Var
(
X
)
+
(
E
[
X
]
)
2
E
[
μ
2
∣
μ
N
,
λ
N
−
1
]
=
λ
N
−
1
+
μ
N
2
{\displaystyle {\begin{aligned}\operatorname {E} [\tau \mid a_{N},b_{N}]&={\frac {a_{N}}{b_{N}}}\\\operatorname {E} \left[\mu \mid \mu _{N},\lambda _{N}^{-1}\right]&=\mu _{N}\\\operatorname {E} \left[X^{2}\right]&=\operatorname {Var} (X)+(\operatorname {E} [X])^{2}\\\operatorname {E} \left[\mu ^{2}\mid \mu _{N},\lambda _{N}^{-1}\right]&=\lambda _{N}^{-1}+\mu _{N}^{2}\end{aligned}}}
大多數時候將這些公式應用到上述方程不困難,但
b
N
{\displaystyle b_{N}}
需要更多工作:
b
N
=
b
0
+
1
2
E
μ
[
∑
n
=
1
N
(
x
n
−
μ
)
2
+
λ
0
(
μ
−
μ
0
)
2
]
=
b
0
+
1
2
E
μ
[
(
λ
0
+
N
)
μ
2
−
2
(
λ
0
μ
0
+
∑
n
=
1
N
x
n
)
μ
+
(
∑
n
=
1
N
x
n
2
)
+
λ
0
μ
0
2
]
=
b
0
+
1
2
[
(
λ
0
+
N
)
E
μ
[
μ
2
]
−
2
(
λ
0
μ
0
+
∑
n
=
1
N
x
n
)
E
μ
[
μ
]
+
(
∑
n
=
1
N
x
n
2
)
+
λ
0
μ
0
2
]
=
b
0
+
1
2
[
(
λ
0
+
N
)
(
λ
N
−
1
+
μ
N
2
)
−
2
(
λ
0
μ
0
+
∑
n
=
1
N
x
n
)
μ
N
+
(
∑
n
=
1
N
x
n
2
)
+
λ
0
μ
0
2
]
{\displaystyle {\begin{aligned}b_{N}&=b_{0}+{\frac {1}{2}}\operatorname {E} _{\mu }\left[\sum _{n=1}^{N}(x_{n}-\mu )^{2}+\lambda _{0}(\mu -\mu _{0})^{2}\right]\\&=b_{0}+{\frac {1}{2}}\operatorname {E} _{\mu }\left[(\lambda _{0}+N)\mu ^{2}-2\left(\lambda _{0}\mu _{0}+\sum _{n=1}^{N}x_{n}\right)\mu +\left(\sum _{n=1}^{N}x_{n}^{2}\right)+\lambda _{0}\mu _{0}^{2}\right]\\&=b_{0}+{\frac {1}{2}}\left[(\lambda _{0}+N)\operatorname {E} _{\mu }[\mu ^{2}]-2\left(\lambda _{0}\mu _{0}+\sum _{n=1}^{N}x_{n}\right)\operatorname {E} _{\mu }[\mu ]+\left(\sum _{n=1}^{N}x_{n}^{2}\right)+\lambda _{0}\mu _{0}^{2}\right]\\&=b_{0}+{\frac {1}{2}}\left[(\lambda _{0}+N)\left(\lambda _{N}^{-1}+\mu _{N}^{2}\right)-2\left(\lambda _{0}\mu _{0}+\sum _{n=1}^{N}x_{n}\right)\mu _{N}+\left(\sum _{n=1}^{N}x_{n}^{2}\right)+\lambda _{0}\mu _{0}^{2}\right]\\\end{aligned}}}
這樣就可以寫出參數方程如下,不需要期望:
μ
N
=
λ
0
μ
0
+
N
x
¯
λ
0
+
N
λ
N
=
(
λ
0
+
N
)
a
N
b
N
x
¯
=
1
N
∑
n
=
1
N
x
n
a
N
=
a
0
+
N
+
1
2
b
N
=
b
0
+
1
2
[
(
λ
0
+
N
)
(
λ
N
−
1
+
μ
N
2
)
−
2
(
λ
0
μ
0
+
∑
n
=
1
N
x
n
)
μ
N
+
(
∑
n
=
1
N
x
n
2
)
+
λ
0
μ
0
2
]
{\displaystyle {\begin{aligned}\mu _{N}&={\frac {\lambda _{0}\mu _{0}+N{\bar {x}}}{\lambda _{0}+N}}\\\lambda _{N}&=(\lambda _{0}+N){\frac {a_{N}}{b_{N}}}\\{\bar {x}}&={\frac {1}{N}}\sum _{n=1}^{N}x_{n}\\a_{N}&=a_{0}+{\frac {N+1}{2}}\\b_{N}&=b_{0}+{\frac {1}{2}}\left[(\lambda _{0}+N)\left(\lambda _{N}^{-1}+\mu _{N}^{2}\right)-2\left(\lambda _{0}\mu _{0}+\sum _{n=1}^{N}x_{n}\right)\mu _{N}+\left(\sum _{n=1}^{N}x_{n}^{2}\right)+\lambda _{0}\mu _{0}^{2}\right]\end{aligned}}}
注意
λ
N
{\displaystyle \lambda _{N}}
和
b
N
{\displaystyle b_{N}}
存在相互依賴關係,自然產生了類似最大期望算法 的算法:
計算
∑
n
=
1
N
x
n
,
∑
n
=
1
N
x
n
2
.
{\displaystyle \sum _{n=1}^{N}x_{n},\ \sum _{n=1}^{N}x_{n}^{2}.}
用所得值計算
μ
N
,
a
N
.
{\displaystyle \mu _{N},\ a_{N}.}
初始化
λ
N
{\displaystyle \lambda _{N}}
為任意值。
用
λ
N
{\displaystyle \lambda _{N}}
的現值和其它參數的已知值計算
b
N
{\displaystyle b_{N}}
。
用
b
N
{\displaystyle b_{N}}
的現值和其它參數的已知值計算
λ
N
{\displaystyle \lambda _{N}}
。
重複最後兩部,直到收斂(即直到兩值的更新量不超過閾值)。
然後就有了後驗參數近似分佈的超參數值,可用於計算後驗的任何屬性,如均值、方差、95%最高密度區域(包含總概率95%的最小區間)等等。
可以證明這種算法保證收斂到局部極大值。
還要注意,後驗分佈與相應的先驗分佈有同樣的形式。只需假設分佈可以分解,分佈形式便隨之而來。事實證明,後驗、先驗形式相同並非巧合,而是先驗分佈為指數族 成員時的一般結果,大多數標準分佈都如此。
The on-line textbook: Information Theory, Inference, and Learning Algorithms (頁面存檔備份 ,存於互聯網檔案館 ), by David J.C. MacKay provides an introduction to variational methods (p. 422).
A Tutorial on Variational Bayes (頁面存檔備份 ,存於互聯網檔案館 ). Fox, C. and Roberts, S. 2012. Artificial Intelligence Review, doi :10.1007/s10462-011-9236-8 .
Variational-Bayes Repository A repository of research papers, software, and links related to the use of variational methods for approximate Bayesian learning up to 2003.
Variational Algorithms for Approximate Bayesian Inference (頁面存檔備份 ,存於互聯網檔案館 ), by M. J. Beal includes comparisons of EM to Variational Bayesian EM and derivations of several models including Variational Bayesian HMMs.
High-Level Explanation of Variational Inference (頁面存檔備份 ,存於互聯網檔案館 ) by Jason Eisner may be worth reading before a more mathematically detailed treatment.
Copula Variational Bayes inference via information geometry (pdf) (頁面存檔備份 ,存於互聯網檔案館 ) by Tran, V.H. 2018. This paper is primarily written for students. Via Bregman divergence , the paper shows that Variational Bayes is simply a generalized Pythagorean projection of true model onto an arbitrarily correlated (copula) distributional space, of which the independent space is merely a special case.
An in depth introduction to Variational Bayes note (頁面存檔備份 ,存於互聯網檔案館 ). Nguyen, D. 2023