Skip to content
PostBayesian Network / Lecture

Bayesian-05-Parameter Learning

2025-12-21
Back to Blog

Bayesian Network Parameter Learning

The Maximum Likelihood Estimation (MLE)

Bayesian Maximum A Posterior (MAP)

  1. θ is a continuous random variable that takes values in [0,1].

  2. Assumption: prior probability distribution of θ: p(θ).
    It is our beliefs about the parameter before seeing the data.

  3. [Output] The posterior probability distribution of θ:

    p(θD)

    It is the updated distribution of the parameter after seeing the data.
    It gives us an estimate, i.e., the mean of the distribution, but also measures uncertainty about the estimate, i.e., standard deviation.

  4. Prediction of Dn+1 using the i.i.d. property:

    P(Dn+1=HD)=P(Dn+1=Hθ,D)p(θD)dθ=P(Dn+1=Hθ)p(θD)dθ=θp(θD)dθ

    Full Bayesian: Take expectation over θ through the integral.

  5. The computation of the integral of the full Bayesian estimate can be complicated.
    We can use maxp(θD) instead.

  6. Bayesian methods involve a degree of subjectivity, as the choice of the prior can influence the results (posterior), especially when the data is sparse or the model is complex, while MLE does not.

P(Dn+1=HD)=θMAP=argmaxp(θD)

The value of the parameter that maximizes the posterior distribution.

Recall Bayes' Theorem:

P(θD)=P(Dθ)p(θ)P(D)

Where P(Dθ) is the likelihood L(θD) of the observed data D, and P(D) does not depend on θ, acting as a constant.


整个推导分为三个阶段:

  1. 准备阶段:定义似然函数和先验分布。
  2. 更新阶段:利用贝叶斯定理计算后验分布。
  3. 预测阶段:通过全贝叶斯积分得出最终公式。

第一阶段:准备原材料

我们需要两个核心组件:

1. 似然函数 (Likelihood) P(Dθ) 这是描述数据如何产生的。假设掷硬币服从二项分布,参数 θ 代表正面概率。

P(D|θ)=θnH(1θ)nT

2. 先验分布 (Prior) p(\theta$) 我们选择 Beta 分布 作为先验,因为它和二项分布是“共轭”的(数学形式相似,好计算)。

p(θ)=Γ(α)Γ(αH)Γ(αT)θαH1(1θ)αT1
  • 为了书写方便,我们把前面的 Gamma 函数系数统称为常数 Cprior
  • αH,αT 是先验的超参数(虚拟计数)。

第二阶段:计算后验分布 (Posterior)

根据贝叶斯定理:

p(θ|D)=P(D|θ)p(θ)P(D)P(D|θ)p(θ)

由于分母 P(D)θ 无关,我们可以先忽略常数,只看与 θ 有关的核心部分 。

步骤:

  1. 将似然和先验相乘:
p(θ|D)[θnH(1θ)nT][θαH1(1θ)αT1]
  1. 合并同类项(指数相加):
p(θ|D)θ(nH+αH)1(1θ)(nT+αT)1

结论: 观察这个式子,它的形式依然是一个 Beta 分布! 新的参数变成了:

  • 新正面计数αH=αH+nH
  • 新反面计数αT=αT+nT

所以,后验分布的完整概率密度函数(加上归一化常数)是:

p(θ|D)=Γ(n+α)Γ(nH+αH)Γ(nT+αT)新的归一化常数 CnewθnH+αH1(1θ)nT+αT1

(注:这里 n=nH​+nT​ 是总数据量,α=αH​+αT​ 是总先验量)


第三阶段:全贝叶斯预测 (Prediction)

这是最关键的一步。我们要预测下一次是正面,不是只取一个 θ,而是对所有可能的 θ 求加权平均(积分)。

目标公式

P(Dn+1=H|D)=01P(Dn+1=H|θ)p(θ|D)dθ

因为 P(Dn+1=H|θ)=θ,所以变成:

=01θp(θ|D)dθ

推导步骤:

  1. 代入后验分布
=01θ[CnewθnH+αH1(1θ)nT+αT1]dθ
  1. 合并 θ: 积分里的 θ 和后面的 θ...1 合并,1 消失了:
=Cnew01θnH+αH(1θ)nT+αT1dθ
  1. 利用 Beta 函数的定义求解积分: 数学上已知:01xa1(1x)b1dx=Γ(a)Γ(b)Γ(a+b)。 对比我们的积分项:

    • 指数 nH+αH 对应公式里的 a1a=nH+αH+1
    • 指数 nT+αT1 对应公式里的 b1b=nT+αT

所以,积分部分的结果是:

Γ(nH+αH+1)Γ(nT+αT)Γ(nH+αH+1+nT+αT)
  1. 整合所有项: 现在把前面的常数 Cnew 和刚才算出的积分结果乘起来:
=Γ(n+α)Γ(nH+αH)Γ(nT+αT)CnewΓ(nH+αH+1)Γ(nT+αT)Γ(n+α+1)积分结果
  1. 化简 (利用 Γ(x+1)=xΓ(x))

    • 消去 Γ(nT+αT)
    • Γ(nH+αH+1) 展开为 (nH+αH)Γ(nH+αH)
    • 将分母的 Γ(n+α+1) 展开为 (n+α)Γ(n+α)

式子变为:

=Γ(n+α)Γ(nH+αH)(nH+αH)Γ(nH+αH)(n+α)Γ(n+α)
  • 消去 Γ(n+α)
  • 消去 Γ(nH+αH)

最终结果

P(Dn+1=H|D)=nH+αHn+α

Example

Single coin toss prediction after observing nH heads and nT tails

P(Dn+1=HD)=01θp(θD)dθ=01θθnH+αH1(1θ)nT+αT1dθ=01θnH+αH(1θ)nT+αT1dθ=nH+αHn+α

Where n=nH+nT and α=αH+αT.
The prior belief of getting a head for the next toss is:

p(Dn+1=H)=p(Di=H)=αHα

So after observing D, our updated belief on the probability of the next toss being heads is now:

P(Dn+1=HD)=nH+αHn+α

(a) As n:

P(Dn+1=HD)=nH+αHn+αnHnH+nT=θ

Or:

P(θ=nHnH+nT)=1

(b) Suppose αH=αT=100, so the prior of p(θ)=0.5.

Case 1: D1: nH=3 and n=10

P(Dn+1=HD1)=3+10010+200=1032100.5

The observed data is too small, so the prior dominates.

Case 2: D2: nH=30,000 and n=100,000

P(Dn+1=HD2)=30,000+100100,000+200=30,100100,2000.3

The data prevail.

MLE for Multinomial distribution

  • Variable with multiple values Ω={x1,x2,,xr}
  • Parameter space Θ=(θ1,,θr), such that i=1rθi=1
  • MLE:
θi=nin

MAP for Multinomial distribution

Bayesian estimation of Bayesian networks with single multi-valued variable:

  • Ω={x1,x2,,xr} with r different values r2
  • θi=P(X=xi) and θ=(θ1,θ2,,θr) with θi0 and i=1rθi=1

For dataset D with N cases D1,D2,,DN,
data cases Di take value xiΩX for all i=1,2,,N.

Then the multinomial likelihood function is:

L(θ;D)=P(Dθ)=i=1NP(Diθ)=i=1NP(X=xiθ)=i=1rθini
  • Conjugate family for multinomial likelihood: Dirichlet distributions, which generalize the Beta distribution.

    • A Dirichlet distribution Dir(α1,α2,,αr) is parameterized by r parameters: α1,α2,,αr, which are called pseudo-counts. The total α of pseudo-counts reflects how confident we are in our prior, and is also called equivalent sample size.

    • Density function is:

      Γ(α)i=1rΓ(αi)i=1rθiαi1

      Where α=i=1rαi (when r=2, it is Beta distribution).

  • For all i, we have:

    01θiΓ(α)i=1rΓ(αi)i=1rθiαi1dθ1dθr=αiα
  • If the prior probability distribution is a Dirichlet distribution Dir(α1,,αr), then the posterior probability:

    p(θD)i=1rθiαi+ni1

    Results in an updated Dirichlet distribution Dir(α1+n1,α2+n2,,αr+nr).

  • Bayesian estimation results in the following prediction:

    P(Dn+1=xiD)=θip(θD)dθ=ni+αin+α

    The dataset is of size n and ni of the data cases take value xi. Therefore, after taking data D into consideration, now our updated belief on P(Dn+1=xi) is:

    P(Dn+1=xiD)=ni+αin+α
  • The Bayesian prediction converges to the MLE estimate when n.


Parameter Estimation in General Bayesian networks:

  • n variables (nodes): X1,X2,,Xn

  • Number of states of Xi: 1,2,,ri, where ri=|ΩXi|

  • Number of configurations of parents of Xi: 1,2,,qi, where qi=|ΩPaXi| is the number of configurations of the parents.
    For example, if a variable Xi has two binary-valued parents, then there are 4 different combinations of the parents, qi=4.

  • Parameters to be estimated:

θijk=P(Xi=jPaXi=k)

Where:

  • i: Index of variable Xi, i=1,2,,n

  • j: An outcome from ΩXi, Xi=j, where j=1,2,,ri (ri is two if Xi is binary-valued).

  • k: The index of parents k=1,,qi (Xi has no parents, then k=1).

  • Parameter vectors:

θ={θijki=1,2,,n;j=1,2,,ri;k=1,,qi}

And note that:

jθijk=1for all i,k
  • θi..: Vector of parameters for a node:
P(XiPaXi)θi..={θijkj=1,2,,ri;k=1,,qi}
  • θi.k: Vector of parameters for a node with given parent k:
P(XiPaXi=k)θi.k={θi.kj=1,2,,ri}
  • Global likelihood decomposition:
    We can maximize each local likelihood function independently of the rest and then combine the solutions to get an MLE solution.

Example: three node v structure

Image of a Directed Acyclic Graph (DAG) with nodes  and  pointing to node

Figure 6: DAG

  • All the three variables are binary of values {1,2}
  • We have the following 12 parameters θ
    • For nodes X1, the number configuration of parents is q1=1, which is empty (no parents), so the parameters related to X1θ111=P(X1=1)θ121=P(X1=2)

L3U3gR

The effect of different priors on smoothing our parameter estimates. The graph shows the estimate of θ (y-axis) after seeing different number of samples (x-axis). The graph below the x-axis shows the particular sequence of tosses. The solid line corresponds to the MLE estimate, and the remaining ones to Bayesian estimates with different strengths and uniform prior means. The large-dashline corresponds to Beta(1;1), the small-dash line to Beta(5;5), and the dotted line to Beta(10;10).

  • It is similar for nodes X2, the number configuration of parents is q2=1, which is also empty, so the parameters related to X2θ211=P(X2=1)θ221=P(X2=2)
  • For nodes X3, there are 4 different configurations of parents, q3=22
    • PaXi=1:(X1=1,X2=1)

      θ311=P(X3=1X1=1,X2=1)θ321=P(X3=2X1=1,X2=1)
    • PaXi=2:(X1=1,X2=2)

      θ312=P(X3=1X1=1,X2=2)θ322=P(X3=2X1=1,X2=2)
    • PaXi=3:(X1=2,X2=1)

      θ313=P(X3=1X1=2,X2=1)θ323=P(X3=2X1=2,X2=1)
    • PaXi=4:(X1=2,X2=2)

      θ314=P(X3=1X1=2,X2=2)θ324=P(X3=2X1=2,X2=2)
  • Data
    • Given a complete case Dl: a vector of values, one for each variable
    • D1=(X1=1,X2=2,X3=2)
    • Given a set of complete data cases: D={D1,D2,,Dn}

Case for Complete Data sets:

X1X2X3
111
112
112
122
122
122
211
211
211
212
221
221
222
222
222
222
  • Find the ML estimates of the parameters θ
Log-likelihood function:
(θ;D)=logL(θ;D)=logl=1nP(Dlθ)=l=1nlogP(Dlθ)
  • For each term logP(Dlθ) if Dl=(1,2,2), we will need these three parameters: θ111=P(X1=1), θ221=P(X2=2), θ322=P(X3=2X1=1,X2=2) for PaXi=2.
logP(D4θ)=logP(X1=1,X2=2,X3=2)=log[P(X1=1θ)P(X2=2θ)P(X3=2X1=1,X2=2,θ)]=logθ111+logθ221+logθ322
  • Define the characteristic function to identify the parameters involved with given data Dlχ(i,j,k:Dl)={1Xi=j,PaXi=k0otherwise

When l=4, the 4th data case: D4=(1,2,2) $$ \chi(i, j, k: D_4) = \chi(2, 2, 1: D_4) = \chi(3, 2, 2: D_4) = 1 $$ $$ \chi(i, j, k: D_4) = 0 $$

  • Logarithm:

    logP(Dlθ)=i,j,kχ(i,j,k:Dl)logθijk
  • Define another function of number of data cases where Xi=j and PaXi=k

    mijk=lχ(i,j,k:Dl)
  • We have the MLE for θijk

θijk=mijkjmijk
  • Results:MLE for
    • P(X1=1)=6/16
    • P(X2=1)=7/16
    • P(X3=1X1=2,X2=2)=2/6
    • ...

Example:

X1X2X3
111
112
112
122
122
122
211
211
211
212
221
221
222
222
222
222
  • Summary
    • θ is a random variable
    • Prior p(θ): Dirichlet distribution
    • Posterior p(θD) also produce Dirichlet distribution

Case for Incomplete Data sets

X1X2X3
111
?12
1??
211
211
212
2?1
?2?

Incomplete data set ? indicates missing values

X1X2X3
D1111
D2222
D31-1
D42-1
X1X2X3
D1111
D2222
D3,1111
D3,2121
D4,1212
D4,2222

Missing at random (MAR) assumption: actually value of X and the even X-is-missing (α) are conditionally independent given other observed variables

P(X|α,E)=P(X|E)

given all the observed variables, the fact that X is missing α gives no additional information about the value.

Expectation-maximization (EM) algorithm:
  • An iterative algorithm
  • start with an initial estimation θ0
    • at each iteration t
      • expectation: complete the data based on θt
      • maximization: using the completed dataset re-estimate the parameter θt+1
    • after data completion, we obtain a dataset with complete data cases
    • EM re-estimates the parameters using the complete data set.
Example: consider an incomplete data case D3=(1,?,?)
  1. General Concept: Splitting Incomplete Cases

In the E-step, an incomplete data point is not assigned a single value. Instead, it is split into multiple "partial data cases," each representing a possible completion. The weight of each partial case corresponds to the posterior probability of that specific completion given the observed data and current parameters.

Illustrative Example: Consider a data point D3=(1,?,?) where both X2 and X3 are missing. With binary variables, there are 2×2=4 possible completions. The EM algorithm splits D3 into 4 partial cases, weighting them by their conditional probabilities:

D3=(1,?,?){w1(1,1,1)where w1=P(X2=1,X3=1X1=1,θt)w2(1,2,1)where w2=P(X2=2,X3=1X1=1,θt)w3(1,1,2)where w3=P(X2=1,X3=2X1=1,θt)w4(1,2,2)where w4=P(X2=2,X3=2X1=1,θt)

Worked Example: X1X2X3

Problem Definition: We wish to estimate parameters for the chain X1X2X3 using an incomplete dataset where X2 is missing in some cases.

Dataset:

  • D1=(1,1,1)
  • D2=(2,2,2)
  • D3=(1,?,1)
  • D4=(2,?,2)

Step 1: Initialization (θ0)

We begin with an initial guess for the parameters θ0.

X112
P(X1)1/21/2
X1X212
12/31/3
21/32/3
X2X312
11/32/3
22/31/3

Step 2: Iteration 1 - E-Step (Data Completion) We calculate the weights for the missing values in D3 and D4 using the current parameters θ0.

Case D3​=(1,?,1): We calculate the likelihood of the missing variable X2 being 1 or 2, given observed X1=1,X3=1.

  • Path A (X2=1): P(1)P(1|1)P(1|1)=122323=418

  • Path B (X2=2): P(1)P(2|1)P(1|2)=121313=118

  • Total Probability: 518

  • Weights:

    • w(X2=1)=4/185/18=45
    • w(X2=2)=1/185/18=15

Case D4​=(2,?,2): Similarly, given observed X1=2,X3=2:

  • Path A (X2=1): P(2)P(1|2)P(2|1)=121313=118

  • Path B (X2=2): P(2)P(2|2)P(2|2)=122323=418

  • Weights:

    • w(X2=1)=15
    • w(X2=2)=45

Resulting Completed Data Table:

Case IDX1X2X3Weight
D11111
D22221
D3,11114/5
D3,21211/5
D4,12121/5
D4,22224/5

Step 3: Iteration 1 - M-Step (Parameter Re-estimation)

We compute the new parameters θ1 using the weighted counts from the completed table.

1. Update P(X1):

  • Count(X1=1) =1(D1)+4/5(D3,1)+1/5(D3,2)=2
  • Count(X1=2) =1(D2)+1/5(D4,1)+4/5(D4,2)=2
  • Total samples = 4
  • θX1=11=24=0.5

2. Update P(X2X1):

  • For X1=1 (Total weight = 2.0):

    • X2=1: Weight is 1(D1)+4/5(D3,1)=1.8 Prob =1.82.0=910
    • X2=2: Weight is 1/5(D3,2)=0.2 Prob =0.22.0=110
  • For X1=2 (Total weight = 2.0):

    • X2=1: Weight is 1/5(D4,1)=0.2 Prob =0.22.0=110
    • X2=2: Weight is 1(D2)+4/5(D4,2)=1.8 Prob =1.82.0=910

Updated Parameters θ1:

P(X1)
X1=10.5
X1=20.5
X1X212
X1=19/101/10
X1=21/109/10
X2X312
X2=19/101/10
X2=21/109/10

Key takeaway

Simple MLE and MAP for coin and Dice

  • MLE nHnH+nT
  • Multinomial MLE θi=nin
  • MAP nH+αHn+α
  • Multinomial MAP P(Dn+1=xiD)=ni+αin+α

Estimate CPD without Missing Value

Estimate CPD by MLE

P(X=xPa(X)=pa)=计数(X=xPa(X)=pa)计数(Pa(X)=pa)
  • 每个节点的 CPD 独立计算 (得益于网络的局部分解)。
  • 只需一次遍历数据集进行计数统计。
  • 若某父节点配置未出现(计数=0),概率为 0,可用 Laplace 平滑避免:加 1(或伪计数 α)到分子分母。

优点:

  • 简单、 高效、大样本时准确。

缺点:

  • 小样本易过拟合,
  • 不确定性度量,
  • 无先验。

Estimate CPD by Bayesian Estimation

θ^ijk=mijk+αijkjmijk+jαijk
  • mijk 是 MLE 中的真实计数。
  • αijk 是先验提供的伪计数(pseudo-counts)。

Estimate CPD with Missing Value

EM算法的原理

  • EM是一种迭代优化算法,旨在最大化不完整数据的对数似然函数。
  • 假设缺失值为MAR(Missing At Random),即缺失机制只依赖于观测到的变量。

EM算法步骤(基于完整数据MLE的迭代)

  1. 初始化
    选取初始参数 θ(可以随机初始化、使用完整部分数据估计,或设为均匀分布)。
  2. E步(Expectation,期望步)
    使用当前参数 θ 计算每个缺失值的条件期望概率。
    • 将不完整实例“软分割”成多个完整实例(每个配置的权重为其条件概率)。
    • 由此得到一个加权的“完整”数据集。
  3. M步(Maximization,最大化步)
    在加权的完整数据集上执行加权MLE(使用加权频率计数),得到新的参数 θ
    • 本质上是将参数估计公式中的计数替换为加权计数。

Example

本案例演示从完整数据中估计条件概率分布(CPD)的过程。

假设三个二值变量 X1、X2、X3(取值 0 或 1),贝叶斯网络结构为 X1 → X2 → X3。

联合概率分解为:

P(X1,X2,X3)=P(X1)×P(X2|X1)×P(X3|X1,X2)
数据集

有 4 个完整观测实例:

实例X1X2X3
D1111
D2101
D3010
D4000
参数估计(最大似然估计 MLE)
  1. P(X1)
    P(X1=1)=24=0.5P(X1=0)=0.5

  2. P(X2 | X1)

    • X1=1 时:计数 X2=1 有 1 个(D1),总计 2 个 → P(X2=1|X1=1)=12=0.5
      P(X2=0|X1=1)=0.5
    • 当 X1=0 时:计数 X2=1 有 1 个(D3),总计 2 个 → P(X2=1|X1=0)=0.5
      P(X2=0|X1=0)=0.5
  3. P(X3 | X1, X2)

    • (X1=1,X2=1)D1X3=1P(X3=1|X1=1,X2=1)=1.0
    • (X1=1,X2=0)D2X3=1P(X3=1|X1=1,X2=0)=1.0
    • (X1=0,X2=1)D3X3=0P(X3=1|X1=0,X2=1)=0.0
    • (X1=0,X2=0)D4X3=0P(X3=1|X1=0,X2=0)=0.0