Representing Uncertain Knowledge

The definition

A Bayesian network is a directed acyclic graph (DAG) where

  • each node is a random variable;
  • each arrow \(X \to Y\) means "X directly influences Y" (X is a parent of Y);
  • each node \(X_i\) has a conditional probability table (CPT) \(\mathbf{P}(X_i \mid \text{Parents}(X_i))\).
Burglary Earthquake Alarm JohnCalls MaryCalls P(b)=0.001 P(e)=0.002 B E │ P(a|B,E) T T │ 0.95T F │ 0.94F T │ 0.29F F │ 0.001 A │ P(j|A)T │ 0.90F │ 0.05 A │ P(m|A)T │ 0.70F │ 0.01
The burglar-alarm network (Pearl; AIMA Fig. 14.2). Each CPT row only needs P(true), since P(false) = 1 − P(true).

Look at what the network says, and at what it doesn't say:

  • Burglary and Earthquake both directly influence the Alarm.
  • John and Mary only react to the Alarm. They don't see burglars or feel earthquakes directly. If John notices the burglar himself, the model is wrong, and we'd need an arrow B → J.
  • All the messy details (Mary's headphones, John's ringtone confusion) are summarized in the CPT numbers: 0.70 rather than 1.0, and 0.05 rather than 0.

The semantics: the joint factorizes

Here is the key equation. A Bayes net represents the joint distribution as the product of the local CPTs:

$$ \boxed{\; P(x_1, \dots, x_n) = \prod_{i=1}^{n} P\big(x_i \mid \text{parents}(X_i)\big) \;} $$

Compare this with the always-true chain rule from §4.1: \(P(x_1,\dots,x_n) = \prod_i P(x_i \mid x_1, \dots, x_{i-1})\). The Bayes net replaces "all previous variables" with "just my parents." That replacement is valid exactly when each variable is conditionally independent of its other predecessors given its parents. That's the assumption a Bayes net makes, and the arrows are where you write it down.

Worked: probability of a full world

The alarm sounds, both John and Mary call, and there is neither a burglary nor an earthquake:

$$ P(j, m, a, \neg b, \neg e) = P(j\mid a)\,P(m\mid a)\,P(a \mid \neg b, \neg e)\,P(\neg b)\,P(\neg e) $$ $$ = 0.90 \times 0.70 \times 0.001 \times 0.999 \times 0.998 \approx 0.000628. $$

Every full assignment's probability is a product of exactly one entry from each CPT.

import itertools

# The burglar-alarm network (AIMA Fig 14.2). Each node: (parents, P(node=True | parent values))
NET = {
    "Burglary":   ((), {(): 0.001}),
    "Earthquake": ((), {(): 0.002}),
    "Alarm":      (("Burglary", "Earthquake"),
                   {(True, True): 0.95, (True, False): 0.94, (False, True): 0.29, (False, False): 0.001}),
    "JohnCalls":  (("Alarm",), {(True,): 0.90, (False,): 0.05}),
    "MaryCalls":  (("Alarm",), {(True,): 0.70, (False,): 0.01}),
}
ORDER = ["Burglary", "Earthquake", "Alarm", "JohnCalls", "MaryCalls"]   # parents before children

def p(var, value, assignment):
    parents, cpt = NET[var]
    pt = cpt[tuple(assignment[q] for q in parents)]
    return pt if value else 1 - pt

def joint(assignment):
    """Chain rule for Bayes nets: product of each node's CPT entry given its parents."""
    result = 1.0
    for var in ORDER:
        result *= p(var, assignment[var], assignment)
    return result

# P(j, m, a, not b, not e): the alarm went off with no burglar and no earthquake, both call
x = dict(Burglary=False, Earthquake=False, Alarm=True, JohnCalls=True, MaryCalls=True)
print("P(j, m, a, ~b, ~e) = %.6f" % joint(x))

total = sum(joint(dict(zip(ORDER, vals))) for vals in itertools.product([True, False], repeat=5))
print("sum over all 32 worlds =", round(total, 10))
print("numbers needed: full joint =", 2**5 - 1, " Bayes net =", sum(2 ** len(NET[v][0]) for v in ORDER))
P(j, m, a, ~b, ~e) = 0.000628
sum over all 32 worlds = 1.0
numbers needed: full joint = 31  Bayes net = 10

Compactness: why this is a big deal

A Boolean node with \(k\) parents needs \(2^k\) numbers, one P(true) per combination of parent values. With \(n\) nodes and at most \(k\) parents each, the network needs at most \(n \cdot 2^k\) numbers instead of \(2^n - 1\).

\(n\) variables, ≤ \(k=5\) parentsFull joint \(2^n - 1\)Bayes net ≤ \(n \cdot 2^k\)
5 (burglary)3110 (actual)
30~1.07 billion960
100~\(10^{30}\)3,200

Fewer numbers also means less data needed to learn them and less overfitting. That's the bias–variance tradeoff again (§2.3). A sparse network makes strong independence assumptions (some bias) but has far fewer parameters to estimate (much less variance).

Building a network: order matters

The recipe (AIMA §14.2):

  1. Choose an ordering of the variables, \(X_1, \dots, X_n\).
  2. For each \(X_i\) in turn, choose as parents a minimal set of earlier variables such that \(P(X_i \mid X_1,\dots,X_{i-1}) = P(X_i \mid \text{Parents}(X_i))\).
  3. Write the CPT.

Any ordering gives a correct network, but not an equally compact one. Use the causal order (causes before effects: B, E, A, J, M) and you get 10 numbers, all easy to estimate. Use the diagnostic order M, J, A, B, E and you need extra arrows. J depends on M (if Mary called, the alarm probably went off). A depends on both J and M. B depends on A. And E depends on A and B, because once you know the alarm rang, a burglary "explains" it and makes an earthquake less likely. That gives 13 numbers, and they are strange ones like P(e | a, b), which no expert can estimate intuitively. Ordering M, J, E, B, A is even worse: 31 numbers, no savings at all.

Rule of thumb

Draw arrows from causes to effects. Causal models are usually the sparsest, their CPTs are the most natural to estimate ("how often does an earthquake trigger the alarm?"), and they stay valid when the situation changes. A new alarm model only changes one CPT.

Compact CPTs: noisy-OR

A node with many parents still needs \(2^k\) numbers. When the parents are independent possible causes, the noisy-OR model needs only \(k\). Each cause \(i\) alone fails to trigger the effect with probability \(q_i\), and the failures are independent:

$$ P(\neg \text{effect} \mid \text{causes that are true}) = \prod_{i\, \text{true}} q_i. $$

Example: Bolt is late (effect) if it rains (\(q = 0.6\)), if its battery is low (\(q = 0.2\)), or both. Then P(late | rain, low battery) = \(1 - 0.6 \times 0.2 = 0.88\).