The Math Behind the Methods

Conditional Independence and Probabilistic Graphical Models

Understand conditional independence through numbers, read chains, forks and colliders, and see how graphs shrink a model.

Key takeaways

  • X and Y are conditionally independent given Z when p(x, y | z) = p(x | z) p(y | z) for every z that can occur.
  • Independence and conditional independence do not imply each other.
  • Conditioning on the middle of a chain or the source of a fork blocks dependence, while conditioning on a collider creates it.
  • A directed graphical model factorizes the joint distribution into one conditional per variable given its parents.
  • The graph encodes assumptions, and arrows are not causal claims unless you argue for that separately.

Two variables are conditionally independent given a third if, once you know the third, learning one tells you nothing more about the other. A probabilistic graphical model draws variables as nodes and uses the missing connections to state which of these independences hold.

The payoff is practical. A joint distribution over many variables is too large to write down or estimate, but if each variable interacts directly with only a few others, it breaks into small pieces.

The definition

Write X ⊥ Y | Z for "X is independent of Y given Z". Two equivalent statements, where the conditioning is defined:

p(x, y | z) = p(x | z) · p(y | z)
p(x | y, z) = p(x | z)

The second is the intuitive one. If you already know z, then y adds nothing about x.

Compare marginal independence, p(x, y) = p(x) p(y), where nothing is conditioned on. Variables can have either property without the other, as the next examples show.

A shared cause

Say Z records whether a day is hot, with probability 0.5. X records whether ice cream sales are high and Y whether the pool is busy. Given a hot day, each happens with probability 0.8. Given a cool day, 0.2. Given Z, the two are independent.

P(X = 1)        = 0.5 · 0.8 + 0.5 · 0.2        = 0.50
P(Y = 1)        = 0.50 as well
P(X = 1, Y = 1) = 0.5 · 0.8² + 0.5 · 0.2²      = 0.34
P(X) · P(Y)     = 0.25
P(Y = 1 | X = 1) = 0.34 / 0.50                 = 0.68

Without knowing the weather, high ice cream sales raise the chance of a busy pool from 0.50 to 0.68. Once you know the weather, they tell you nothing further. These numbers are invented to show the arithmetic.

Explaining away

Now reverse the arrows. Let A and B be independent causes, each present with probability 0.1, and let an alarm C sound exactly when at least one is present.

P(A = 1)                  = 0.10
P(C = 1)                  = 1 − 0.9²            = 0.19
P(A = 1 | C = 1)          = 0.10 / 0.19         ≈ 0.53
P(A = 1 | C = 1, B = 1)   = 0.10

A and B start independent. After you hear the alarm, they become dependent: finding out that B is present removes the reason to suspect A. This is explaining away.

The same effect appears as selection bias. If your dataset includes only cases that passed a filter influenced by two factors, those factors can appear related within the data even though they are unrelated in the population.

Chains, forks and colliders

Structure Shape A and B with C unobserved A and B given C
Chain A → C → B Generally dependent Independent
Fork A ← C → B Generally dependent Independent
Collider A → C ← B Independent Generally dependent

"Generally" is needed because particular probability values can cancel a dependence that the graph permits. Conditioning on a descendant of a collider, such as a noisy report of the alarm, also opens the connection.

These three cases extend to a rule called d-separation. A path between two variables is blocked by a set of observed variables if it passes through an observed non-collider, or through a collider where neither the collider nor any of its descendants is observed. If every path is blocked, the independence holds in every distribution that fits the graph.

What observing the middle variable does
Chain A influences C, which influences B A and B are linked at first Observing C blocks the link Fork C is a shared source of A and B A and B are linked at first Observing C blocks the link Collider A and B both feed into C A and B start independent Observing C creates a link Chain A influences C, which influences B A and B are linked at first Observing C blocks the link Fork C is a shared source of A and B A and B are linked at first Observing C blocks the link Collider A and B both feed into C A and B start independent Observing C creates a link

Factorizing a joint distribution

A Bayesian network is a directed graph with no cycles. The joint distribution is the product of each variable's distribution given its parents, the nodes pointing into it.

p(x₁, …, x_d) = ∏ᵢ p(xᵢ | parents of xᵢ)

fork above:      p(z) p(x | z) p(y | z)
collider above:  p(a) p(b) p(c | a, b)

The savings are large. A full joint table over 10 binary variables has 2¹⁰ − 1 = 1,023 free probabilities. A chain over the same variables needs 1 for the first and 2 for each of the other nine, 19 in total.

Fewer parameters mean less data needed and less computation, at the price of assuming that the missing arrows really are unimportant. Bayesian networks in detail covers learning and querying them.

Models you may already use

  • Naive Bayes assumes features are independent given the class. For d binary features and two classes it needs 1 + 2d probabilities. Why it often still works looks at the gap between assumption and reality.
  • Hidden Markov models assume the next hidden state depends on the past only through the current state, and each observation depends only on its own state. See states and emissions.
  • Autoregressive models use the product rule, p(x₁) p(x₂ | x₁) and so on, which is exact on its own. Assumptions enter through how each conditional is parameterized and how far back it looks.
  • Latent variable models make observed variables independent given hidden ones, as in mixtures and factor analysis.

Undirected graphs

A Markov random field uses undirected edges. The joint distribution is a product of nonnegative potential functions over fully connected groups of nodes, divided by a constant Z that makes the total 1. Z sums over all configurations and is often the expensive part.

Reading independence is simpler here: two sets of variables are independent given a third whenever the third cuts every path between them. A conditional random field applies this to labels given an observed input, so the input itself need not be modeled.

In either kind of graph, a node's Markov blanket is a set that shields it from everything else once observed. In an undirected graph it is the node's neighbors. In a directed graph it consists of the parents, the children and the children's other parents, who are included because observing a shared child opens a collider.

Inference and its cost

Inference means computing the distribution of some variables given observed ones. The factorization lets you push sums inward so that each involves only a few factors, a procedure called variable elimination. On graphs shaped like trees, message passing gives exact answers efficiently. On densely connected graphs, exact inference becomes intractable in general, and you turn to Monte Carlo sampling or variational approximations.

You can verify small examples by listing every configuration.

from itertools import product

joint = {}
for a, b in product([0, 1], repeat=2):
    c = int(a or b)
    joint[(a, b, c)] = (0.1 if a else 0.9) * (0.1 if b else 0.9)

def prob(query, given):
    match = lambda k, cond: all(k[i] == v for i, v in cond.items())
    den = sum(p for k, p in joint.items() if match(k, given))
    num = sum(p for k, p in joint.items() if match(k, {**given, **query}))
    return num / den

print(prob({0: 1}, {2: 1}))          # about 0.526
print(prob({0: 1}, {2: 1, 1: 1}))    # 0.1

Where the probabilities come from

A graph supplies structure. You still need the numbers in each factor.

When every variable is observed in a directed network with table-based conditionals, maximum likelihood is counting. The estimate of p(child = k | parents = j) is the number of rows with that combination divided by the number of rows with those parent values. Each variable can be fitted on its own.

Counting breaks down when a node has many parents. A binary node with 10 binary parents needs 2¹⁰ = 1,024 conditional probabilities, and many parent combinations may never appear. You can add pseudo-counts, or replace the table with a compact function such as a logistic regression on the parents.

When some variables are hidden, the counts are unknown. Expectation-maximization alternates between estimating expected counts and updating the parameters. It improves the likelihood at each round but can settle on a local optimum, so try several starting points.

Learning the graph itself is harder. The number of possible graphs grows extremely fast with the number of variables, and several graphs can fit the same distribution equally well. Search methods that score graphs, and methods built on independence tests, both help, provided you treat their output as a proposal to examine.

Cautions

Arrows and causes. A chain A → C → B, the reversed chain and the fork imply the same single independence, so observational data alone cannot distinguish them. Causal reading needs further assumptions or experiments, as discussed in causal inference basics.

Testing independence. With continuous or many-valued conditioning variables, each value has few examples, and reliable tests need assumptions about the form of the relationships. Failing to detect dependence is not proof of independence.

Controlling for everything. Adding every available variable as a feature or adjustment can open a collider path and manufacture an association. For pure prediction this may be harmless while conditions stay the same. For explanation or decisions it can mislead.

Start a graph from what you know about how the data arise, list the independences it implies, and check the ones your data can test. When a check fails, ask which missing arrow or unrecorded variable could explain it before adding edges at random.

Written for 4MachineLearning with AI writing tools and not reviewed line by line by a person. Check anything you rely on against a textbook or primary source. Report an error.

Keep reading

All 26 articles in The Math Behind the Methods →

Latest news on this topic