Bayesian Networks

Bayesian Networks Explained in Detail

A detailed guide to Bayesian networks: how their graphs and probability tables work, how inference and learning are done, and where they are used.

On this page
  1. The parts of a Bayesian network
  2. How the graph encodes independence
  3. A worked example: the burglary alarm network
  4. Explaining away and independence
  5. How inference works
  6. Learning a network from data
  7. Where Bayesian networks are used
  8. Strengths and limits
  9. How they compare with related models
  10. Tools for building one
  11. Common questions
  12. Key takeaways

A Bayesian network is a model of how several uncertain things depend on one another. Each thing is a node, arrows show which nodes directly influence others, and every node carries a table of probabilities. Together they let you ask questions such as “How likely is a burglary, given that two neighbors called?” and get a numerical answer. The name comes from Thomas Bayes, whose rule for updating a probability after new evidence is central to how these networks reason. Judea Pearl did much to develop the framework in the 1980s.

This guide explains the parts of a network, how the graph encodes independence, a worked example, how inference and learning work, where networks are used, and their limits. If you only want the short definition, start with “What is meant by Bayesian network?”

The parts of a Bayesian network

Four ideas make up the structure.

Nodes are random variables, which are quantities whose value is uncertain. A node might be a yes-or-no fact such as “Burglary”, a category such as “Weather: sunny, cloudy or rainy”, or a measurement. Simple textbook networks usually use discrete values, which keeps the probability tables finite.

Edges are the arrows between nodes. An arrow from A to B says that A has a direct influence on B, and A is then a parent of B. An edge is a modeling choice, and it carries an assumption: a missing arrow claims that two nodes do not influence each other directly.

Acyclic means the graph has no loops. Start at any node and follow the arrows, and you can never return to where you began. A graph of arrows with no cycles is called a directed acyclic graph, or DAG. This property lets the probabilities be combined in a clean order.

Conditional probability tables (CPTs) hold the numbers. Each node has a table giving its probability for every combination of its parents’ values. A node with no parents has a plain table of prior probabilities.

How the graph encodes independence

Without any structure, a joint probability distribution over several variables is very large. Five yes-or-no variables have 32 combinations of values, so the full joint table needs 31 independent numbers. A Bayesian network stores only the table for each node, conditioned on its parents. When each node has few parents, that can need far fewer numbers.

The graph also determines how the full probability is built. The probability of one complete scenario is the product of each node’s probability, given its parents’ values:

P(X1, X2, …, Xn) = P(X1 | parents of X1) × P(X2 | parents of X2) × … × P(Xn | parents of Xn)

This factorization is what makes networks practical.

The graph also states independence claims. Each node is independent of its non-descendants, meaning the nodes it cannot reach by following arrows, once its parents are known. Learning about those nodes adds nothing to what you expect of the node, as long as you already know its parents.

A worked example: the burglary alarm network

This example is a standard textbook case from Russell and Norvig’s Artificial Intelligence: A Modern Approach. Its numbers are illustrative.

Five variables are involved. Burglary (B) and earthquake (E) can each set off the alarm (A). The alarm can lead two neighbors, John (J) and Mary (M), to call the homeowner. The arrows run from B and E to A, and from A to J and M.

The alarm’s table gives its probability for each combination of burglary and earthquake:

Burglary Earthquake P(alarm)
yes yes 0.95
yes no 0.94
no yes 0.29
no no 0.001

The other tables are simpler. The probability of a burglary is 0.001 and of an earthquake is 0.002. The neighbors’ calls depend only on the alarm:

Alarm P(John calls) P(Mary calls)
yes 0.90 0.70
no 0.05 0.01

A full scenario. Suppose the alarm sounds, both neighbors call, and there is neither a burglary nor an earthquake. Multiply the relevant entries:

P(J, M, A, not B, not E) = P(not B) × P(not E) × P(A | not B, not E) × P(J | A) × P(M | A) = 0.999 × 0.998 × 0.001 × 0.90 × 0.70 ≈ 0.00063

A question. What is the probability of a burglary, given that both neighbors call? The calls depend on the alarm, and the alarm could have come from either cause. The network sums over the values of the unseen variables and compares the cases for burglary. The answer is about 0.28. A burglary is rare, with a prior of 0.001, but two calls raise its probability to roughly 28 percent. The calls are not proof, because the tables allow for calls without an alarm and for alarms that never prompt a call.

New evidence. Now suppose we learn that an earthquake happened. The probability of a burglary falls to roughly 0.3 percent. The earthquake already explains the alarm, so a burglary is no longer needed as the explanation.

Explaining away and independence

Burglary and earthquake are independent in this network. Without any information about the alarm, learning about one tells you nothing about the other. Once the alarm is observed, they become dependent. Each is a possible cause of the alarm, so confirming one makes the other less likely. This effect is called explaining away, and it is one of the least intuitive features of Bayesian networks.

Which variables are independent, with or without evidence, can be read from the graph with a rule called d-separation. You do not need the rule to use a network, but it tells you exactly what the graph claims.

How inference works

Inference means computing the probability of some variables, the query, given the values of others, the evidence. It is the main job of a Bayesian network once it is built. The word has a narrower meaning here than in “What is inference in the context of AI models?”, where it usually means running a trained model on new input.

Exact methods give the true answer:

  • Enumeration adds up every joint probability that matches the evidence. It is easy to follow, but the work grows exponentially with the number of unobserved variables.
  • Variable elimination sums out one variable at a time and reuses intermediate results. It is a common basis for exact inference software.
  • Junction tree methods convert the network into a tree of variable groups, so that many queries can share one set of calculations.

Approximate methods handle larger networks:

  • Sampling generates random scenarios that agree with the network and keeps the ones that match the evidence. Likelihood weighting and Gibbs sampling are two examples.
  • Loopy belief propagation passes messages along the edges. It can give useful approximate answers on graphs with loops, but it is not guaranteed to converge to the correct values.

Exact inference is hard in general. It is NP-hard for arbitrary networks, so no single method is fast for every network. Sparse networks, with few connections, are usually much easier.

Learning a network from data

A network can be built from expert knowledge, learned from data, or both. Learning has two parts.

Parameter learning estimates the numbers in each table when the graph is fixed. The simplest method counts how often each combination of values appears and divides by the total. With few records the counts are unreliable, so practitioners often add a small amount to each count, a step called smoothing. A Bayesian approach starts from a prior distribution over the probabilities and updates it with the data. This is where Bayesian statistics enters, and the choice between the two approaches is part of a long debate. For the main points, see “Why is Bayesian statistics controversial?”

Structure learning works out the arrows. Two families are common:

  • Constraint-based methods test which variables are independent in the data and look for a graph that agrees with those results. The PC algorithm is a well-known example.
  • Score-based methods give each candidate graph a score that rewards a good fit and penalizes complexity, then search for a high-scoring graph. Finding the best-scoring graph is NP-hard in general, so practical tools use heuristic search.

Two caveats matter. First, different graphs can encode exactly the same independence claims, so the data may not settle the direction of every arrow. Second, arrows are often drawn to mean cause, but a Bayesian network encodes probabilistic dependence. Reading arrows as causes needs extra assumptions and a causal framework built on top of the basic network.

Where Bayesian networks are used

Bayesian networks have been applied in:

  • Medical diagnosis and decision support, where symptoms and test results feed into probabilities for possible conditions.
  • Fault diagnosis in machinery, electronics and computer systems.
  • Risk and reliability analysis, where many uncertain factors combine.
  • Bioinformatics, including modeling relationships between genes.
  • Text classification and spam filtering, usually through the simpler naive Bayes model.

These uses share a need for reasoning under uncertainty when evidence is incomplete. That is why Bayesian networks are often cited as one of the classic answers to “How to handle uncertainty in AI?”

Strengths and limits

Strengths:

  • They state uncertainty as probabilities rather than as a single yes or no.
  • They handle missing evidence. You can ask about any variable given any subset of observations.
  • They reason in both directions, from causes to effects (prediction) and from effects back to causes (diagnosis).
  • They can combine expert knowledge with data, and the graph is readable enough to inspect and explain.

Limits:

  • Tables grow quickly. A node with k yes-or-no parents needs 2 to the power of k rows.
  • Continuous variables need special distributions or must be split into ranges.
  • Exact inference can be slow for densely connected networks.
  • Structure learning needs a lot of data and is computationally hard.
  • The results are only as good as the graph and the numbers. A wrong independence assumption can mislead, and tables estimated from little data can be unreliable.
  • Naive Bayes is the simplest network. The class variable is the parent of every feature, and the features are assumed independent once the class is known. That assumption is strong, but the model is fast and often works well for text.
  • Hidden Markov models handle sequences by modeling hidden states that change over time and produce observations. They are a special case of a dynamic Bayesian network.
  • Neural networks learn patterns from data and are often very accurate, but their learned weights do not form a readable set of probability tables. Bayesian networks are easier to inspect, but they usually need more hand design or structure learning.

Tools for building one

Several libraries let you build, learn and query networks. pgmpy is a Python library, and bnlearn is an R package. Graphical tools, both commercial and free, also exist. Check the current documentation, since features change between releases.

Common questions

Readers often ask whether modern AI systems are Bayesian networks. Some AI systems use them, but the large language models behind chatbots do not work this way. For that specific case, see “Is ChatGPT Bayesian?”

Key takeaways

  • A Bayesian network is a directed acyclic graph of variables, with a probability table at each node.
  • Multiplying the tables gives the full joint probability, and the graph encodes independence.
  • Given evidence, inference updates the probabilities of the other variables, sometimes sharply, as explaining away shows.
  • Exact inference is hard in general, so approximate methods are common for large networks.
  • Learning a network means estimating its tables and finding its structure, and both are harder than they look.

Keep exploring

Guides from other topics

All topics →