Bayesian Network Parameter Learning
The Maximum Likelihood Estimation (MLE)
Bayesian Maximum A Posterior (MAP)
is a continuous random variable that takes values in . Assumption: prior probability distribution of
: .
It is our beliefs about the parameter before seeing the data.[Output] The posterior probability distribution of
: 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.Prediction of
using the i.i.d. property: Full Bayesian: Take expectation over
through the integral. The computation of the integral of the full Bayesian estimate can be complicated.
We can useinstead. 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.
The value of the parameter that maximizes the posterior distribution.
Recall Bayes' Theorem:
Where
整个推导分为三个阶段:
- 准备阶段:定义似然函数和先验分布。
- 更新阶段:利用贝叶斯定理计算后验分布。
- 预测阶段:通过全贝叶斯积分得出最终公式。
第一阶段:准备原材料
我们需要两个核心组件:
1. 似然函数 (Likelihood)
2. 先验分布 (Prior)
- 为了书写方便,我们把前面的 Gamma 函数系数统称为常数
。 是先验的超参数(虚拟计数)。
第二阶段:计算后验分布 (Posterior)
根据贝叶斯定理:
由于分母
步骤:
- 将似然和先验相乘:
- 合并同类项(指数相加):
结论: 观察这个式子,它的形式依然是一个 Beta 分布! 新的参数变成了:
- 新正面计数:
- 新反面计数:
所以,后验分布的完整概率密度函数(加上归一化常数)是:
(注:这里 n=nH+nT 是总数据量,α=αH+αT 是总先验量)
第三阶段:全贝叶斯预测 (Prediction)
这是最关键的一步。我们要预测下一次是正面,不是只取一个
目标公式:
因为
推导步骤:
- 代入后验分布:
- 合并
: 积分里的 和后面的 合并, 消失了:
利用 Beta 函数的定义求解积分: 数学上已知:
。 对比我们的积分项: - 指数
对应公式里的 - 指数
对应公式里的
- 指数
所以,积分部分的结果是:
- 整合所有项: 现在把前面的常数
和刚才算出的积分结果乘起来:
化简 (利用 Γ(x+1)=xΓ(x)):
- 消去
。 - 将
展开为 。 - 将分母的
展开为 。
- 消去
式子变为:
- 消去
。 - 消去
。
最终结果:
Example
Single coin toss prediction after observing
Where
The prior belief of getting a head for the next toss is:
So after observing
(a) As
Or:
(b) Suppose
Case 1:
The observed data is too small, so the prior dominates.
Case 2:
The data prevail.
MLE for Multinomial distribution
- Variable with multiple values
- Parameter space
, such that - MLE:
MAP for Multinomial distribution
Bayesian estimation of Bayesian networks with single multi-valued variable:
with different values and with and
For dataset
data cases
Then the multinomial likelihood function is:
Conjugate family for multinomial likelihood: Dirichlet distributions, which generalize the Beta distribution.
A Dirichlet distribution
is parameterized by parameters: , 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:
Where
(when , it is Beta distribution).
For all
, we have: If the prior probability distribution is a Dirichlet distribution
, then the posterior probability: Results in an updated Dirichlet distribution
. Bayesian estimation results in the following prediction:
The dataset is of size
and of the data cases take value . Therefore, after taking data into consideration, now our updated belief on is: The Bayesian prediction converges to the MLE estimate when
.
Parameter Estimation in General Bayesian networks:
variables (nodes): Number of states of
: , where Number of configurations of parents of
: , where is the number of configurations of the parents.
For example, if a variablehas two binary-valued parents, then there are 4 different combinations of the parents, . Parameters to be estimated:
Where:
: Index of variable , : An outcome from , , where ( is two if is binary-valued). : The index of parents ( has no parents, then ). Parameter vectors:
And note that:
: Vector of parameters for a node:
: Vector of parameters for a node with given parent :
- 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

Figure 6: DAG
- All the three variables are binary of values
- We have the following 12 parameters
- For nodes
, the number configuration of parents is , which is empty (no parents), so the parameters related to
- For nodes

The effect of different priors on smoothing our parameter estimates. The graph shows the estimate of
- It is similar for nodes
, the number configuration of parents is , which is also empty, so the parameters related to - For nodes
, there are 4 different configurations of parents, - Data
- Given a complete case
: a vector of values, one for each variable - Given a set of complete data cases:
- Given a complete case
Case for Complete Data sets:
| 1 | 1 | 1 |
| 1 | 1 | 2 |
| 1 | 1 | 2 |
| 1 | 2 | 2 |
| 1 | 2 | 2 |
| 1 | 2 | 2 |
| 2 | 1 | 1 |
| 2 | 1 | 1 |
| 2 | 1 | 1 |
| 2 | 1 | 2 |
| 2 | 2 | 1 |
| 2 | 2 | 1 |
| 2 | 2 | 2 |
| 2 | 2 | 2 |
| 2 | 2 | 2 |
| 2 | 2 | 2 |
- Find the ML estimates of the parameters
Log-likelihood function:
- For each term
if , we will need these three parameters: , , for .
- Define the characteristic function to identify the parameters involved with given data
When
Logarithm:
Define another function of number of data cases where
and We have the MLE for
- Results:MLE for
- ...
Example:
| 1 | 1 | 1 |
| 1 | 1 | 2 |
| 1 | 1 | 2 |
| 1 | 2 | 2 |
| 1 | 2 | 2 |
| 1 | 2 | 2 |
| 2 | 1 | 1 |
| 2 | 1 | 1 |
| 2 | 1 | 1 |
| 2 | 1 | 2 |
| 2 | 2 | 1 |
| 2 | 2 | 1 |
| 2 | 2 | 2 |
| 2 | 2 | 2 |
| 2 | 2 | 2 |
| 2 | 2 | 2 |
- Summary
is a random variable - Prior
: Dirichlet distribution - Posterior
also produce Dirichlet distribution
Case for Incomplete Data sets
| 1 | 1 | 1 |
| ? | 1 | 2 |
| 1 | ? | ? |
| 2 | 1 | 1 |
| 2 | 1 | 1 |
| 2 | 1 | 2 |
| 2 | ? | 1 |
| ? | 2 | ? |
Incomplete data set ? indicates missing values
| 1 | 1 | 1 | |
| 2 | 2 | 2 | |
| 1 | - | 1 | |
| 2 | - | 1 |
| 1 | 1 | 1 | |
| 2 | 2 | 2 | |
| 1 | 1 | 1 | |
| 1 | 2 | 1 | |
| 2 | 1 | 2 | |
| 2 | 2 | 2 |
Missing at random (MAR) assumption: actually value of X and the even X-is-missing (
given all the observed variables, the fact that X is missing
Expectation-maximization (EM) algorithm:
- An iterative algorithm
- start with an initial estimation
- at each iteration
- expectation: complete the data based on
- maximization: using the completed dataset re-estimate the parameter
- expectation: complete the data based on
- after data completion, we obtain a dataset with complete data cases
- EM re-estimates the parameters using the complete data set.
- at each iteration
Example: consider an incomplete data case
- 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
Worked Example:
Problem Definition: We wish to estimate parameters for the chain
Dataset:
Step 1: Initialization
We begin with an initial guess for the parameters
| 1 | 2 | |
|---|---|---|
| 1/2 | 1/2 |
| 1 | 2 | |
|---|---|---|
| 1 | 2/3 | 1/3 |
| 2 | 1/3 | 2/3 |
| 1 | 2 | |
|---|---|---|
| 1 | 1/3 | 2/3 |
| 2 | 2/3 | 1/3 |
Step 2: Iteration 1 - E-Step (Data Completion) We calculate the weights for the missing values in
Case D3=(1,?,1): We calculate the likelihood of the missing variable
Path A (
): Path B (
): Total Probability:
Weights:
Case D4=(2,?,2): Similarly, given observed
Path A (
): Path B (
): Weights:
Resulting Completed Data Table:
| Case ID | Weight | |||
|---|---|---|---|---|
| 1 | 1 | 1 | 1 | |
| 2 | 2 | 2 | 1 | |
| 1 | 1 | 1 | 4/5 | |
| 1 | 2 | 1 | 1/5 | |
| 2 | 1 | 2 | 1/5 | |
| 2 | 2 | 2 | 4/5 |
Step 3: Iteration 1 - M-Step (Parameter Re-estimation)
We compute the new parameters
1. Update
- Count(
) - Count(
) - Total samples = 4
2. Update
For
(Total weight = 2.0): : Weight is Prob : Weight is Prob
For
(Total weight = 2.0): : Weight is Prob : Weight is Prob
Updated Parameters
| 0.5 | |
| 0.5 |
| 1 | 2 | |
|---|---|---|
| 9/10 | 1/10 | |
| 1/10 | 9/10 |
| 1 | 2 | |
|---|---|---|
| 9/10 | 1/10 | |
| 1/10 | 9/10 |
Key takeaway
Simple MLE and MAP for coin and Dice
- MLE
- Multinomial MLE
- MAP
- Multinomial MAP
Estimate CPD without Missing Value
Estimate CPD by MLE
- 每个节点的 CPD 独立计算 (得益于网络的局部分解)。
- 只需一次遍历数据集进行计数统计。
- 若某父节点配置未出现(计数=0),概率为 0,可用 Laplace 平滑避免:加 1(或伪计数
)到分子分母。
优点:
- 简单、 高效、大样本时准确。
缺点:
- 小样本易过拟合,
- 不确定性度量,
- 无先验。
Estimate CPD by Bayesian Estimation
是 MLE 中的真实计数。 是先验提供的伪计数(pseudo-counts)。
Estimate CPD with Missing Value
EM算法的原理
- EM是一种迭代优化算法,旨在最大化不完整数据的对数似然函数。
- 假设缺失值为MAR(Missing At Random),即缺失机制只依赖于观测到的变量。
EM算法步骤(基于完整数据MLE的迭代)
- 初始化
选取初始参数(可以随机初始化、使用完整部分数据估计,或设为均匀分布)。 - E步(Expectation,期望步)
使用当前参数计算每个缺失值的条件期望概率。 - 将不完整实例“软分割”成多个完整实例(每个配置的权重为其条件概率)。
- 由此得到一个加权的“完整”数据集。
- M步(Maximization,最大化步)
在加权的完整数据集上执行加权MLE(使用加权频率计数),得到新的参数。 - 本质上是将参数估计公式中的计数替换为加权计数。
Example
本案例演示从完整数据中估计条件概率分布(CPD)的过程。
假设三个二值变量 X1、X2、X3(取值 0 或 1),贝叶斯网络结构为 X1 → X2 → X3。
联合概率分解为:
数据集
有 4 个完整观测实例:
| 实例 | X1 | X2 | X3 |
|---|---|---|---|
| D1 | 1 | 1 | 1 |
| D2 | 1 | 0 | 1 |
| D3 | 0 | 1 | 0 |
| D4 | 0 | 0 | 0 |
参数估计(最大似然估计 MLE)
P(X1)
P(X2 | X1)
- 当
时:计数 有 1 个(D1),总计 2 个 → - 当 X1=0 时:计数 X2=1 有 1 个(D3),总计 2 个 →
- 当
P(X3 | X1, X2)