1. Home
  2. Explore
  3. Computer Science
  4. Bayesian Networks Overview

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Bayesian Networks Overview

Bayesian Networks


Recap: Factor Graphs

Factor graphs provide a framework for modeling problems with multiple variables. A factor graph consists of:

  • A set of variables (e.g., locations of objects at different times, colors of regions)

  • A set of factors that depend on certain subsets of variables, specifying preferences or constraints

The weight of a complete assignment to all variables is the product of all factors: Weight(x)=∏jfj(xj)\text{Weight}(\mathbf{x}) = \prod_{j} f_j(\mathbf{x}_j)Weight(x)=∏j​fj​(xj​)

This embodies the principle: specify locally, optimize globally. It is easy to define local interactions between variables via factors, but the goal is to find a globally optimal solution.

Object tracking example:

  • Variables: location of an unobserved object at each time step iii

  • Transition factors: encourage the object to remain close between successive time steps (no teleportation)

  • Observation factors: incorporate noisy sensor readings, encouraging the estimated location to match observations

Last week’s algorithms (backtracking search, beam search, Gibbs sampling) could find the maximum weight assignment. However, factors are often defined arbitrarily. Bayesian networks give these factors a probabilistic meaning.


Probability Review

A random variable is a variable whose value is unknown. A joint distribution over a set of random variables assigns a probability to every possible combination of values. For example, let SSS be sunshine and RRR be rain. The joint distribution P(S,R)P(S,R)P(S,R) is a table:

SSS

RRR

Probability

0

0

0.2

0

1

0.08

1

0

0.7

1

1

0.02

Notation: uppercase letters denote random variables (S,RS, RS,R), lowercase letters denote values (s,rs, rs,r). The expression P(S=s,R=r)P(S=s, R=r)P(S=s,R=r) is a single number, while P(S,R)P(S,R)P(S,R) denotes the entire distribution.

Marginal distribution: to find the distribution of a subset of variables (query variables), sum over all values of the other variables. For P(S)P(S)P(S):

  • P(S=0)=P(S=0,R=0)+P(S=0,R=1)=0.2+0.08=0.28P(S=0) = P(S=0,R=0) + P(S=0,R=1) = 0.2 + 0.08 = 0.28P(S=0)=P(S=0,R=0)+P(S=0,R=1)=0.2+0.08=0.28

  • P(S=1)=P(S=1,R=0)+P(S=1,R=1)=0.7+0.02=0.72P(S=1) = P(S=1,R=0) + P(S=1,R=1) = 0.7 + 0.02 = 0.72P(S=1)=P(S=1,R=0)+P(S=1,R=1)=0.7+0.02=0.72

This is marginalization.

Conditional distribution: given evidence (some variables observed), restrict to matching rows and renormalize. For P(S∣R=1)P(S \mid R=1)P(S∣R=1):

  • Select rows where R=1R=1R=1: probabilities 0.08 and 0.02

  • Sum = 0.1, normalize: P(S=0∣R=1)=0.08/0.1=0.8P(S=0 \mid R=1) = 0.08 / 0.1 = 0.8P(S=0∣R=1)=0.08/0.1=0.8, P(S=1∣R=1)=0.2P(S=1 \mid R=1) = 0.2P(S=1∣R=1)=0.2

Probabilistic inference is the task of answering queries of the form:

P(Query∣Evidence)P(\text{Query} \mid \text{Evidence})P(Query∣Evidence)

where Query is a set of variables of interest, Evidence is a set of observed values, and any other variables are marginalized out.


The Alarm Network – First Bayesian Network

Consider three random variables: Burglary (BBB), Earthquake (EEE), Alarm (AAA).

Step 1: Define variables

  • BBB: burglary occurring (0 or 1)

  • EEE: earthquake occurring (0 or 1)

  • AAA: alarm sounding (0 or 1)

Step 2: Draw directed edges (reflecting causal influence)

  • Burglary causes Alarm: B→AB \to AB→A

  • Earthquake causes Alarm: E→AE \to AE→A

  • BBB and EEE are independent (no edge between them)

Step 3: Define local conditional distributions

  • P(B)P(B)P(B): prior probability of burglary, e.g., P(B=1)=εP(B=1) = \varepsilonP(B=1)=ε, P(B=0)=1−εP(B=0) = 1-\varepsilonP(B=0)=1−ε

  • P(E)P(E)P(E): similarly P(E=1)=εP(E=1) = \varepsilonP(E=1)=ε, P(E=0)=1−εP(E=0) = 1-\varepsilonP(E=0)=1−ε

  • P(A∣B,E)P(A \mid B, E)P(A∣B,E): probability of alarm given both causes. For a perfect alarm:

    • If no burglary and no earthquake: alarm does not sound P(A=0∣B=0,E=0)=1P(A=0 \mid B=0,E=0)=1P(A=0∣B=0,E=0)=1, P(A=1∣...)=0P(A=1 \mid ...)=0P(A=1∣...)=0

    • If either or both occur: alarm sounds with high probability

Step 4: Define the joint distribution

The joint distribution over all variables is the product of local conditional distributions:

P(B,E,A)=P(B)⋅P(E)⋅P(A∣B,E)P(B, E, A) = P(B) \cdot P(E) \cdot P(A \mid B, E)P(B,E,A)=P(B)⋅P(E)⋅P(A∣B,E)

This is a factor graph with one factor per variable. The factor for AAA connects AAA and its parents B,EB, EB,E (moralizing the parents). Important: one factor per variable, not per edge.

Explaining away – a key reasoning pattern:

  • Initially P(B=1)=εP(B=1) = \varepsilonP(B=1)=ε (very low)

  • After hearing alarm A=1A=1A=1, P(B∣A=1)P(B \mid A=1)P(B∣A=1) rises significantly (e.g., 0.51 for ε=0.05\varepsilon=0.05ε=0.05)

  • But if we also learn that an earthquake occurred (E=1E=1E=1), P(B∣A=1,E=1)P(B \mid A=1, E=1)P(B∣A=1,E=1) drops back to ε\varepsilonε

Why? The two causes compete to explain the effect. Conditioning on one cause reduces the probability of the other. This is explaining away, a natural human reasoning phenomenon that is precisely captured in the Bayesian network.

Definition: A Bayesian network is a directed acyclic graph (DAG) over random variables, where each node has a local conditional distribution given its parents, and the joint distribution is the product of all local conditionals.

P(X1,X2,…,Xn)=∏i=1nP(Xi∣Parents(Xi))P(X_1, X_2, \dots, X_n) = \prod_{i=1}^n P(X_i \mid \text{Parents}(X_i))P(X1​,X2​,…,Xn​)=∏i=1n​P(Xi​∣Parents(Xi​))

Key Properties of Bayesian Networks

1. Consistency of Sub‑Bayesian Networks (Marginalization)

If a leaf node (no children) is marginalized out, the resulting distribution is exactly the Bayesian network obtained by removing that node and its incoming edges. This can be seen algebraically: summing over a leaf node’s values, the factor sums to 1 and disappears. Thus marginalization becomes a purely graphical operation – simply drop the node.

In contrast, in a general factor graph, such a factor would remain because it is not guaranteed to sum to 1.

2. Consistency of Local Conditionals

The local conditional distribution P(Xi∣Parents(Xi))P(X_i \mid \text{Parents}(X_i))P(Xi​∣Parents(Xi​)) that is defined as part of the network equals the distribution derived from the joint distribution via the laws of probability. This means the defined local probabilities are exactly the conditional probabilities implied by the full joint distribution – no conflict.


More Examples: Cold and Allergies

Variables: Cold (CCC), Allergies (AAA), Coughing (HHH), Itchy eyes (III).

Edges:

  • Cold and Allergies both cause Coughing (HHH depends on CCC and AAA)

  • Allergies cause Itchy eyes (III depends on AAA)

Local conditional distributions:

  • P(C)P(C)P(C), P(A)P(A)P(A) (priors)

  • P(H∣C,A)P(H \mid C, A)P(H∣C,A)

  • P(I∣A)P(I \mid A)P(I∣A)

Joint distribution: P(C,A,H,I)=P(C)⋅P(A)⋅P(H∣C,A)⋅P(I∣A)P(C, A, H, I) = P(C) \cdot P(A) \cdot P(H \mid C, A) \cdot P(I \mid A)P(C,A,H,I)=P(C)⋅P(A)⋅P(H∣C,A)⋅P(I∣A)

Given evidence of coughing (H=1H=1H=1) and itchy eyes (I=1I=1I=1), probabilistic inference can compute P(C∣H=1,I=1)P(C \mid H=1, I=1)P(C∣H=1,I=1). In one setting with P(C=1)=0.1P(C=1)=0.1P(C=1)=0.1, P(A=1)=0.2P(A=1)=0.2P(A=1)=0.2, the resulting probability of a cold is 0.13. This is another instance of explaining away: observing itchy eyes (evidence of allergies) lowers the probability of cold, even though allergies were not directly observed.


Probabilistic Programs

A probabilistic program is a different way to specify a Bayesian network. Each variable is generated by a simple command that draws from a distribution, often conditioned on previously drawn variables. Running the program produces one sample from the joint distribution; the program itself defines the distribution over all variables.

Example – Alarm network as a program:

B = Bernoulli(ε)
E = Bernoulli(ε)
A = B or E   // deterministic function

Example – Object tracking:

X0 = (0,0)
for t = 1 to T:
    with probability α: X_t = X_{t-1} + (1,0)   // move right
    with probability 1-α: X_t = X_{t-1} + (0,-1) // move down

This defines a Markov chain where each XtX_tXt​ depends only on Xt−1X_{t-1}Xt−1​.

Probabilistic inference can be done by rejection sampling: run the program many times, keep only those runs that match the observed evidence, and examine the distribution of query variables.

Programs allow complex models to be specified succinctly, leveraging programming concepts like loops, functions, and recursion. They offer a modular way to define large Bayesian networks.


Common Probabilistic Models

Markov Model

Generate a sequence where each element depends on the previous one. Used for language modeling, time series.

Hidden Markov Model (HMM)

At each time step ttt:

  • Generate hidden state HtH_tHt​ given Ht−1H_{t-1}Ht−1​ (Markov chain)

  • Generate observation EtE_tEt​ given HtH_tHt​

The hidden states are not observed; inference aims to recover them from observations. Historically used for speech recognition.

Factorial HMM

Multiple independent hidden chains (e.g., two objects), but a single combined observation at each time step. This is more complex but captures multiple simultaneous processes.

Naive Bayes

For classification:

  • Generate a label YYY from a prior

  • For each feature iii, generate XiX_iXi​ independently given YYY

Despite the naive independence assumption, it often works well for text classification.

Latent Dirichlet Allocation (LDA)

A topic model for documents:

  • For each document, generate a distribution over topics θ\thetaθ

  • For each word position, generate a topic ZZZ from θ\thetaθ, then generate a word from that topic

This captures documents covering multiple topics.

Medical Diagnosis System

  • Generate diseases (hidden) from prior

  • For each symptom (observation), generate it based on the diseases present

Inference: given symptoms, find most likely diseases.

Stochastic Block Model (Social Networks)

  • Each node has a type (e.g., politician, scientist)

  • For each pair of nodes, generate an edge (interaction) based on their types

Inference: given observed network, infer the types.

All these models share a structure: there are hidden variables (what we want to infer) and observed variables (evidence). The Bayesian network is built from the hidden variables to the observations – it describes how the clean, hidden structure generates the messy data. Then probabilistic inference reverses that: from observations back to hidden variables.


Probabilistic Inference – A Step‑by‑Step Approach

Given a Bayesian network and a query P(Query∣Evidence)P(\text{Query} \mid \text{Evidence})P(Query∣Evidence), the naive approach (using the full joint table) is exponential. Instead, we can exploit the graph structure.

Example: Simple Markov chain X1→X2→X3→X4X_1 \to X_2 \to X_3 \to X_4X1​→X2​→X3​→X4​, query P(X3∣X2=5)P(X_3 \mid X_2 = 5)P(X3​∣X2​=5).

Using algebra: P(X3∣X2=5)∝∑X1,X4P(X1)P(X2=5∣X1)P(X3∣X2=5)P(X4∣X3)P(X_3 \mid X_2=5) \propto \sum_{X_1, X_4} P(X_1) P(X_2=5 \mid X_1) P(X_3 \mid X_2=5) P(X_4 \mid X_3)P(X3​∣X2​=5)∝∑X1​,X4​​P(X1​)P(X2​=5∣X1​)P(X3​∣X2​=5)P(X4​∣X3​)

Constants can be pulled out, and sums simplify because ∑X4P(X4∣X3)=1\sum_{X_4} P(X_4 \mid X_3) = 1∑X4​​P(X4​∣X3​)=1 and ∑X1P(X1)P(X2=5∣X1)\sum_{X_1} P(X_1) P(X_2=5 \mid X_1)∑X1​​P(X1​)P(X2​=5∣X1​) is just a constant (which cancels when normalizing). Result: P(X3∣X2=5)=P(X3∣X2=5)P(X_3 \mid X_2=5) = P(X_3 \mid X_2=5)P(X3​∣X2​=5)=P(X3​∣X2​=5) – the local conditional. This is expected from the consistency property.

To make inference systematic, use the following five-step procedure:

  1. Remove (marginalize) non-ancestors of the query and evidence variables. Only keep variables that are ancestors of either query or evidence (including the variables themselves). Downstream variables (descendants) can be dropped because they sum to 1 when marginalized.

  2. Convert the remaining Bayesian network into a factor graph. Create one factor per variable, with that variable and its parents as scope (moralize the parents). This removes directionality and makes the algebra clearer.

  3. Condition on the evidence. For each observed variable, remove it as a variable and replace all factors that mention it with a partially evaluated factor (set the variable to its observed value).

  4. Marginalize out disconnected components. Any factor that does not involve any query variable is a constant and can be dropped.

  5. Do the actual work. You are left with a small factor graph over the query variables and possibly some other variables (e.g., ancestors that are not observed). Compute the desired conditional probability by:

    • Multiply all remaining factors

    • Marginalize (sum) over any variables that are not query variables

    • Normalize (since the final expression is proportional to the desired probability)

Example: Alarm network – query P(B∣A=1)P(B \mid A=1)P(B∣A=1).

  • Step 1: All variables (B,E,AB, E, AB,E,A) are ancestors of query or evidence → no removal.

  • Step 2: Factor graph: factors P(B)P(B)P(B), P(E)P(E)P(E), P(A∣B,E)P(A \mid B, E)P(A∣B,E).

  • Step 3: Condition on A=1A=1A=1: remove AAA, replace P(A∣B,E)P(A \mid B, E)P(A∣B,E) with P(A=1∣B,E)P(A=1 \mid B, E)P(A=1∣B,E).

  • Step 4: All factors involve BBB or EEE – no disconnected component.

  • Step 5: Compute P(B∣A=1)∝∑EP(B)P(E)P(A=1∣B,E)P(B \mid A=1) \propto \sum_E P(B) P(E) P(A=1 \mid B, E)P(B∣A=1)∝∑E​P(B)P(E)P(A=1∣B,E). Marginalize over EEE: $$f(B) = \sum_E P(E) P(A=1 \mid B, E)$ThenThenThenP(B \mid A=1) \propto P(B) \cdot f(B)$. Normalize.

With ε=0.05\varepsilon = 0.05ε=0.05, this yields P(B=1∣A=1)=12−ε≈0.51P(B=1 \mid A=1) = \frac{1}{2-\varepsilon} \approx 0.51P(B=1∣A=1)=2−ε1​≈0.51.

This procedure leverages the graph structure to minimize manual algebra. With practice, steps 1 and 4 become almost visual, and one can jump directly to step 5.


Summary

  • Bayesian networks provide a probabilistic interpretation to factor graphs by using local conditional distributions. The joint distribution is a product of these local probabilities, and the factors are not arbitrary but satisfy the sum-to-one property.

  • Probabilistic inference – computing P(query∣evidence)P(\text{query} \mid \text{evidence})P(query∣evidence) – can be performed by exploiting the graph structure via marginalization, conditioning, and factor graph operations.

  • Probabilistic programs offer a modular, intuitive way to define Bayesian networks and to understand the generative process behind data.

  • Many common models (HMM, naive Bayes, LDA, etc.) are special cases of Bayesian networks.

  • Explaining away is a key non‑intuitive reasoning pattern that arises naturally when multiple causes compete to explain an effect.

The next lecture will focus on automating step 5 – performing the necessary sums efficiently without manual algebra.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent