10 - Bayesian Networks: Exact Inference
Once we have constructed a Bayesian Network that represents the probabilistic model of our domain, the primary task is to use it to answer questions. This process is called Probabilistic Inference.
Inference involves calculating a useful quantity—usually a conditional probability—from a joint probability distribution, given some observed evidence. For example, given Judea Pearl's classic Alarm Network: If Mary calls me to tell me that my house alarm is ringing, how likely is it that there is a burglar?
There are two primary types of exact queries we ask:
- Posterior Probability: — What is the probability distribution of a query variable given the evidence?
- Most Likely Explanation (Maximum a Posteriori): — What is the single most likely state of the query variables given the evidence?
Probabilistic Inference Methods
The main algorithms used for inference in Bayesian Networks fall into two categories:
- Exact Inference: Algorithms that calculate the exact mathematical probabilities.
- Inference by Enumeration (Exponential complexity)
- Variable Elimination (Worst-case exponential complexity, but often much better in practice)
- Approximate Inference: Algorithms that estimate probabilities (e.g., Sampling methods like Markov Chain Monte Carlo).
It is important to note that Exact Inference in Bayesian Networks is generally NP-Hard.
1. Inference by Enumeration
Inference by Enumeration is the most fundamental, brute-force way to perform exact inference.
Let our domain consist of:
- Evidence variables with observed values .
- A Query variable .
- Hidden variables (everything else).
The algorithm proceeds in three steps:
- Join: Multiply together all the relevant conditional probability tables (CPTs) from the Bayesian Network to form the full joint distribution, but only select the entries consistent with the observed evidence .
- Marginalize: Sum out all the Hidden variables to get the joint distribution of just the Query and the Evidence: .
- Normalize: Divide the resulting table by the sum of its entries to obtain the conditional probability .
The Problem with Enumeration
Consider the Alarm Network (Variables: ). To calculate :
While straightforward to write, enumeration is incredibly slow. Because you join up the entire joint distribution before you sum out any hidden variables, you generate massive intermediate tables. If you have variables with domain size , you are creating a table of size .
2. Factors and Pointwise Products
To improve upon enumeration, we need a mathematical abstraction. We introduce the concept of Factors.
A factor is a multi-dimensional array (a table) that stores non-negative numbers. It represents a "slice" or a "piece" of a probability distribution.
- is a factor over , let's call it .
- is a factor over and , let's call it .
- An initial local CPT in a Bayes Net is just a factor.
If we assign a value to a variable (e.g., we observe evidence), we "select" a slice of the array. The assigned variable is no longer a missing dimension; the factor shrinks.
Pointwise Product
The fundamental operation on factors is the pointwise product (or "multiplying the tables"). If we have and , their pointwise product yields a new factor :
3. Variable Elimination
Why is inference by enumeration so slow? Because we wait until the very end to sum out the hidden variables.
The core idea behind the Variable Elimination (VE) algorithm is to interleave joining and marginalizing. We sum out (eliminate) hidden variables as early as mathematically possible, which shrinks the intermediate factors and saves enormous amounts of computational time and space.
The Two Basic Operations
- Join Factors: Take all factors that mention a specific variable, and multiply them together using the pointwise product. This creates a single, larger new factor.
- Eliminate (Marginalize): Take a factor, and sum out a hidden variable. This is a projection operation that shrinks the factor to a smaller one, completely removing the hidden variable from the network.
Note: In mathematics, this is simply exploiting the distributive property: . By summing early, we avoid doing multiple multiplications.
The Variable Elimination Algorithm
Setup: Start with the initial factors, which are the local CPTs of the Bayesian Network. If you have observed evidence (e.g., ), immediately instantiate that evidence in the factors. The initial factor simply becomes .
Execution: While there are still hidden variables (variables that are neither the Query nor the Evidence):
- Pick a hidden variable .
- Join all factors that mention .
- Eliminate (sum out) from the resulting joined factor.
Finish: Once all hidden variables are eliminated, you will be left with one or more factors that only mention the Query variable .
- Join all remaining factors.
- Normalize the resulting table to sum to 1. This gives exactly .
4. Complexity and Variable Ordering
In Variable Elimination, you must pick hidden variables to eliminate one by one. The algorithm works correctly no matter what order you choose to eliminate the hidden variables.
However, the computational and space complexity of the algorithm is entirely determined by the largest factor generated during the process. The size of a factor is the number of entries in its table (e.g., a factor over binary variables has entries).
The elimination ordering can drastically affect the size of the largest factor.
Example of Ordering Impact
Consider a network structured like a star: A single hidden central node , which points to query nodes , which in turn point to evidence nodes . We want to query .
- Ordering 1 (Bad): If we eliminate first, we must join all factors mentioning . Since connects to every , we create a massive factor over . Summing out leaves a factor over variables, requiring a table of size .
- Ordering 2 (Good): If we eliminate through first, and save for last, we only ever join small factors. For example, to eliminate , we join and and sum out , leaving a small factor strictly over . The maximum factor generated at any point only has 2 variables (size ).
Does there always exist an ordering that results only in small factors? No. For highly connected, complex networks, exact inference remains NP-Hard, and large factors are unavoidable. However, choosing a smart elimination ordering (often using greedy heuristics like "eliminate the variable that results in the smallest new factor") makes Exact Inference vastly superior to basic Enumeration in practice.
Summary
- Inference by Enumeration is a mathematically correct but highly inefficient brute-force algorithm that joins the entire joint distribution before summing out hidden variables.
- Factors are multi-dimensional arrays representing slices of probability distributions.
- Variable Elimination interleaves the joining of factors and the marginalization (elimination) of hidden variables. By summing out variables as early as possible, it avoids building massive intermediate tables.
- The elimination ordering critically determines the maximum factor size generated during VE. While optimal ordering can reduce complexity from exponential to linear in certain networks, Exact Inference remains NP-Hard in the worst case.