Skip to content
PostBayesian Network / Lecture

Bayesian-03-Variable-Elimination

2025-11-06
Back to Blog

Bayesian Network Variable Elimination

Inference in graphical models involves computing conditional probability answers to queries:

  • Compute the posterior probability distributions, given values e for some evidence nodes E
  • Belief updating or probabilistic inference P(QE=e)

Types of inference:

  1. Exact inference: variable elimination
  2. Approximate inference

Naive inference algorithm for computing conditional probability P(QE=e) in a Bayesian network, involves the following 3 steps:

  1. Step 1: Get joint probability distribution P(X) over the set X of ALL variables by multiplying conditional probability (chain rule)
  2. Step 2: Marginalization: to find P(Q,E) and then P(E)P(Q,E)=XQEP(X)P(E)=QP(Q,E)XQE is the set of non-query, non-evidence variables.
  3. Step 3: Renormalization: Calculate the conditional probability: based on the evidence E=eP(QE=e)=P(Q,E=e)P(E=e)

To perform inference effectively:

  1. Structure of the Bayesian network: DAG and CPDs
  2. Conditional independence assertion
  3. Factorization of the joint distribution

Conditional Probability Query

Posterior Probability Query

It means finding the probability of some variable(s) after knowing certain evidence.

  • (a) Given the values of some variables (the evidence).
  • (b) Compute the posterior probability of other variables — used for prediction, explanation, or reasoning.
  • (c) Example:
    If John and Mary both called the police because of an alarm, what’s the probability that there was actually an earthquake?
P(E=yJ=y,M=y)

General form of query

P(QE=e)=P(Q,e)P(e)

(a) Notations:

  • Q is a list of query variables, sometimes we use letter Y as well.
  • E is a list (set) of evidence variables
  • e is the corresponding list observed values
  • W=XQE: random variables that are neither query nor evidence,
  • X are ALL the random variables of the joint distribution P.

(b) Computing probabilities

i. P(y,e), an instantiation can be computed by summing out all entries in the joint (summing over w) excluding Y and E where P(y,e,w) is an entry in the full joint distributions.

P(y,e)=wP(y,e,w)

ii. The probability of evidence P(e) is computed directly by summing over all possible values of Y.

P(e)=yP(y,e)

(c) Deriving conditional probabilities

P(ye)=P(y,e)P(e)

if Y has k different values {y1,y2,...,yk}, then we can compute the joint probability

P(y1,e),P(y2,e),...,P(yk,e)

and renormalizing the entries to 1 by dividing the sum of them yP(y,e) which is less than 1.

(d) Inference: the process of computing the answer to a query.

Types of exact inferences

(a) Predictive/Causal inference:

Determining the likelihood of an effect given a cause. From causes to effects
If burglary B happens, will Mary report alarm P(MB=y)?

(b) Diagnostic (evidential/abductive)

It seeks to identify the causes given an observed effect. From effect to causes:
Mary calls M, what’s wrong, what is the probability there was a burglary? P(BM=y).

(c) Inter-causal inference (explaining away)

Inter-causal inference deals with the relationship between different causes of a common effect. It can demonstrate how one cause can reduce the probability of another cause given the effect.
Between causes of a common effect

P(BA=y)>P(BA=y,E=y)

Earthquake E=y explains away (reduces) Burglary B=y when an alarm is triggered. Conversely,

P(BA=y)<P(BA=y,E=n)

(d) Mixed inference

  • P(AJ=y,E=y) simultaneous use of diagnostic and causal inference
  • P(BJ=y,E=n) simultaneous use of diagnostic and inter-causal inference

Naive Inference

  • P(QE=e)
  1. Step 1: Get the joint distribution: P(X)
  2. Step 2: Marginalize: to compute the marginal probability of a given variable in our model after summing everything else out
P(Q,E)=XQEP(X)P(E)=QP(Q,E)

e.g.,

P(B,J,M)=E,AP(B,E,A,J,M)
  1. Step 3: Condition inference (Baye’s Theorem):
P(QE=e)=P(Q,E=e)P(E=e)
  • The naive algorithm does not make use of the factorization, and has exponential complexity (bad!!).
  • Eliminating a variable X1 from the joint distribution P(X1,X2,...,Xm) is to compute:
P(X2,...,Xm)=X1P(X1,X2,...,Xm)

But the complexity is exponential in m.
We need better ways to eliminate variables with much less computation using factorization. How to use the factorization to reduce the complexity.

Variable Elimination Simple Chain Example

We have a simple Bayesian network:

ABCD

and we want to compute

P(D)=A,B,CP(A)P(B|A)P(C|B)P(D|C)

Compute P(D) efficiently by reducing repeated summations.

Naive Method

  • 做法:先求出完整的联合分布 P(A,B,C,D),再逐一消除变量。
  • 复杂度计算23+22+2   - 23:消除 A 时,需要处理关于 (B,C,D) 的因子,大小为 2×2×2=8。   - 22:消除 B 时,需要处理关于 (C,D) 的因子,大小为 2×2=4。   - 2:消除 C 时,需要处理关于 (D) 的因子,大小为 2
  • 缺点:复杂度随变量增加呈 指数级增长 (O(2n))。

Variable Elimination

  • 做法:利用因子分解,每次只对局部相关的变量求和。
  • 复杂度计算2+2+2   - 2:计算 AP(A)P(B|A),只涉及 2 个变量,生成关于 B 的因子。   - 2:计算 BP(C|B)τ(B),只涉及 2 个变量,生成关于 C 的因子。   - 2:计算 CP(D|C)τ(C),只涉及 2 个变量,生成关于 D 的因子。
  • 优点:复杂度随变量增加呈 线性增长 (O(n×2))。

Theorem

Suppose F is a factorization of a joint distribution P(X1,X2,...,Xn), then the result of eliminating (F,X1) is a factorization of the marginal probability distribution P(X2,X3,...,Xn).

假设你有一个由很多小因子(函数)相乘构成的系统。当你对其中一个变量 X1 求和(即“消除”它)时,那些不包含 X1 的因子就像常数一样,可以从求和符号中提取出来。 最终剩下的部分是一个新的函数 h,它和原来剩下的因子一起,构成了剩余变量的联合分布。

Proof:
Suppose P(X1,X2,...,Xn)=f1×f2××fn and X1 appears in and only in factors f1,f2,...,fk, then
the factors where X1 does not appear are fk+1,fk+2,...,fn.

P(X2,X3,...,Xn)=X1P(X1,X2,...,Xn)=X1i=1nfi=X1i=1kfii=k+1nfi=(X1i=1kfi)i=k+1nfi=hi=k+1nfi

Factor Marginalization

Definition of factor marginalization: Let X be a set of variables and YX. Let ϕ(X,Y) be a factor, we define the factor marginalization of Y in ϕ (denoted as Yϕ), to be a factor ψ over X as:

ψ(X)=Yϕ(X,Y)

We sum up entries in the table where the values of X match up. The process of marginalizing a joint distribution P(X,Y) onto X in a Bayesian network is summing out Y in the factor corresponding to P.

Procedure of Variable Elimination (F,X):

  • Input: F: a list of functions; X: a variable to be eliminated.
  • Output: another list of factor functions F:
  1. Remove from the F all the functions, say f1,f2,...,fk, that involve X.
  2. Compute new function g=i=1kfi, e.g., P(A)P(BA) and k=2.
  3. Compute new function h=Xg, e.g., ψ1(B), which is a marginal probability distribution.
  4. Add new function h to F to replace f1,f2,...,fk.
  5. Return F as F.

Sum-product inference task: Compute: Xi=1kfi This can be more effective than: XP(X1,X2,...,Xn)!

Variable Elimination Algorithm VE(F,Q,E,e,ρ)

  • Inputs:

    • F: the list of conditional probability tables in a BN; set of factors
    • Q: a list of query variables;
    • E: a list of observed variables;
    • e: observed values;
    • ρ: eliminating order of variables which are not in QE
  • Output: P(QE=e), the query answer

  • While ρ is not empty

    • eliminate the first variable Z from ρ
    • call function eliminate(F,Z) endwhile.
  • Set h to be the product of all the factors in F

  • Instantiate observed variables in h to their observed values

  • Re-normalization: h(Q)Qh(Q)

  • Return

Example

Conditional Probability Inference

graphviz
digraph G {
    // Set global graph, node, and edge attributes
    graph [rankdir=TB];
    node [shape=circle, style=filled, color=lightblue, fontname="Helvetica", fontsize=12];
    edge [color=black];
    // Define nodes
    A [label="A"];
    B [label="B"];
    C [label="C"];
    D [label="D"];
    E [label="E"];
    F [label="F"];
    // Define edges
    A -> D;
    B -> D;
    B -> E;
    C -> E;
    D -> F;
    E -> F;
}

Query P(A|F=0)

This Bayesian network (BN) factorizes P(A,B,C,D,E,F) into the following form:

P(A,B,C,D,E,F)=P(A)P(B)P(C)P(DA,B)P(EB,C)P(FD,E)

So the initial factorization:

F={P(A),P(B),P(C),P(DA,B),P(EB,C),P(FD,E)}

Elimination order for the non-query, non-evidence variables: ρ:C,E,B,D

Inference process:

  • Step 1: Eliminate all non-query variables:
    • Eliminate C (furthest): So we have the new marginal function:

      ψ1(B,E)=CP(C)P(EB,C)F={P(A),P(B),P(DA,B),P(FD,E),ψ1(B,E)}

      Assuming all variables are binary, the cost of eliminating C is 23 (index 3 is the number of variables involved in this process).

    • Eliminate E: Only two factors contain E in F, while three factors contain B:

      ψ2(B,D,F)=EP(FD,E)ψ1(B,E)F={P(A),P(B),P(DA,B),ψ2(B,D,F)}

      The cost of eliminating E is 24.

    • Eliminate B:

      ψ3(A,D,F)=BP(B)P(DA,B)ψ2(B,D,F)F={P(A),ψ3(A,D,F)}
    • Eliminate D:

      ψ4(A,F)=Dψ3(A,D,F)F={P(A),ψ4(A,F)}
  • Step 2: Deal with the evidence variable:
    • g(A,F)=P(A)ψ4(A,F)
  • Step 3: Instantiation for F=0:
    • h(A)=g(A,F=0)
    • This h(A) is an un-normalized factor, because Ah(A) may not be 1.
  • Step 4: Re-normalization and obtain the conditional probability:
    • P(AF=0)=h(A)Ah(A)

Marginal Probability Inference

graphviz
digraph BayesianNetwork {
    // Node attributes
    node [shape=ellipse, fontname="Helvetica", fontsize=12, style=solid];

    // Nodes
    Coherence;
    Difficulty;
    Intelligence;
    Grade;
    SAT;
    Letter;
    Job;
    Happy;

    // Edges
    Coherence -> Difficulty;
    Difficulty -> Grade;
    Intelligence -> Grade;
    Intelligence -> SAT;
    Grade -> Letter;
    SAT -> Job;
    Letter -> Job;
    Job -> Happy;
    Difficulty -> Happy;
}

(a) The chain rule for this Student BN asserts that

P(C,D,I,G,S,L,H)=P(C)P(DC)P(ID)P(GI,D)P(SI)P(LG)P(JL,S)P(HG,J)=ϕC(C)ϕD(D,C)ϕI(ID)ϕG(G,I,D)ϕS(S,I)ϕL(L,G)ϕJ(J,L,S)ϕH(H,G,J)

So, we have the list of function factors:

F={ϕC(C),ϕD(D,C),ϕI(I),ϕG(G,I,D),ϕS(S,I),ϕL(L,G),ϕJ(J,L,S),ϕH(H,G,J)}

(b) Apply VE algorithm to compute P(J)

  • p: C,D,I,H,G,S,L
  • q: J

i. Eliminating C: Compute the factors with sum-production operation

ψ1(C,D)=ϕC(C)ϕD(D,C)τ1(D)=Cψ1(C,D)

In this case, τ1(D) is simply the marginal distribution of D. (This is a trivial case; the others may NOT necessarily be the marginal distribution function). Now we have eliminated one factor that involves D, ϕD(D,C)=P(DC), and introduced the new factor τ1(D) that involves D. Now the list of factors are:

F={τ1(D),ϕI(I),ϕG(G,I,D),ϕS(S,I),ϕL(L,G),ϕJ(J,L,S),ϕH(H,G,J)}

ii. Eliminating D:

ψ2(G,I,D)=ϕG(G,I,D)τ1(D)τ2(G,I)=Dψ2(G,I,D)

Now the list of factors are:

F={τ2(G,I),ϕI(I),ϕS(S,I),ϕL(L,G),ϕJ(J,L,S),ϕH(H,G,J)}

iii. Eliminating I:

There are three factors involving I: τ2(G,I),ϕI(I),ϕS(S,I).

ψ3(G,I,S)=ϕI(I)ϕC(G,I,D)ϕS(S,I)τ3(G,S)=Iψ3(G,I,S)

Now the list of factors are:

F={τ3(G,S),ϕL(L,G),ϕJ(J,L,S),ϕH(H,G,J)}

iv. Eliminating H:

There is only one factor involving H: ϕH(H,G,J).

ψ4(G,J,H)=ϕH(H,G,J)τ4(G,J)=Hψ4(G,J,H)

Note that:

ϕH(H,G,J)=P(HG,J)τ4(G,J)=Hψ4(G,J,H)=HP(HG,J)=1

We generated this new factor, but it does not complicate the algorithm. Now the list of factors are:

F={τ3(G,S),τ4(G,J),ϕL(L,G),ϕJ(J,L,S)}

v. Eliminating G:

There are three factors related to G:

ψ5(G,J,L,S)=τ3(G,S)τ4(G,J)ϕL(L,G)τ5(J,L,S)=Gψ5(G,J,L,S)

So now the list of factors are:

F={τ5(J,L,S),ϕJ(J,L,S)}

vi. Eliminating S:

ψ6(J,L,S)=τ5(J,L,S)ϕJ(J,L,S)τ6(J,L)=Sψ6(J,L,S)

So now the list of factors are:

F={τ6(J,L)}

vii. Eliminating L:

τ7(J)=Lτ6(J,L)

Then we renormalize τ7 to obtain P(J).


  • We can use any elimination ordering, but some are obviously better than the others.
  • The optimal elimination ordering: the one with minimum cost.