Bayesian Networks in AI

Last Updated 17 Aug, 2026
Quick Answer

What is a Bayesian network in AI?

A Bayesian network is a graph that represents uncertain variables and their probabilistic dependencies. It allows an AI system to update its belief about unobserved variables after observing new evidence.

  • How nodes, edges, and conditional probability tables represent dependencies
  • How conditional probability enables belief updating from new evidence
  • How to compute inference and joint probabilities using Python

A Bayesian network represents how different uncertain events depend on each other, so an AI system can update its beliefs about one thing after observing evidence about another.

What Is a Bayesian Network?

A Bayesian network is a graph that represents a set of variables and how they probabilistically depend on one another. Each variable is a node, and each arrow shows a direct influence from one variable to another. Together with a probability table at each node, this graph lets a system compute the chance of anything in the network, given whatever evidence it has observed.

Conditional Probability Recap

Bayesian networks are built entirely out of conditional probabilities, so it helps to recap the idea first.

Conditional probability, written P(A | B), is the chance of A being true given that B is already known to be true. It's the mathematical tool behind "updating your belief" once new evidence comes in.

Nodes and Edges: Representing Dependencies

In a Bayesian network, each node is a variable, and each edge is a direct dependency — an arrow from Rain to WetGrass means Rain directly influences whether the grass is wet.

Conditional Probability Tables (CPTs)

Every node has a conditional probability table (CPT) that gives the probability of that node's values for every combination of its parents' values. A node with no parents, like Rain, just has its own prior probability.

Simple Worked Example

This is the classic "Sprinkler" network: rain makes the sprinkler less likely to run, and both rain and the sprinkler can make the grass wet.

  • P(Rain = True) = 0.2
  • P(Sprinkler = True | Rain = True) = 0.01, P(Sprinkler = True | Rain = False) = 0.4
  • P(WetGrass = True | Sprinkler, Rain): 0.99 if both, 0.9 if only Sprinkler, 0.8 if only Rain, 0.0 if neither

Example: Computing Probabilities with a Bayesian Network

This code computes P(WetGrass = True) by summing over every combination of Rain and Sprinkler, then uses Bayes' rule to find P(Rain = True | WetGrass = True) — how much observing wet grass should raise our belief in rain.

P_rain = {True: 0.2, False: 0.8}

P_sprinkler_given_rain = {
    True: {True: 0.01, False: 0.99},
    False: {True: 0.40, False: 0.60},
}

P_wetgrass_given_sprinkler_rain = {
    (True, True): 0.99,
    (True, False): 0.90,
    (False, True): 0.80,
    (False, False): 0.00,
}

def joint_prob(rain, sprinkler, wetgrass):
    p_r = P_rain[rain]
    p_s = P_sprinkler_given_rain[rain][sprinkler]
    p_w = P_wetgrass_given_sprinkler_rain[(sprinkler, rain)] if wetgrass else \
        (1 - P_wetgrass_given_sprinkler_rain[(sprinkler, rain)])
    return p_r * p_s * p_w

p_wetgrass_true = sum(
    joint_prob(rain, sprinkler, True)
    for rain in [True, False]
    for sprinkler in [True, False]
)

p_rain_and_wet = sum(
    joint_prob(True, sprinkler, True)
    for sprinkler in [True, False]
)

p_rain_given_wet = p_rain_and_wet / p_wetgrass_true

print(f"P(WetGrass=True) = {p_wetgrass_true:.4f}")
print(f"P(Rain=True, WetGrass=True) = {p_rain_and_wet:.4f}")
print(f"P(Rain=True | WetGrass=True) = {p_rain_given_wet:.4f}")
print(f"P(Rain=True) [prior, no evidence] = {P_rain[True]:.4f}")

Output:

P(WetGrass=True) = 0.4484
P(Rain=True, WetGrass=True) = 0.1604
P(Rain=True | WetGrass=True) = 0.3577
P(Rain=True) [prior, no evidence] = 0.2000

Explanation:

Before seeing any evidence, the chance of rain is just the prior, 20%. But once we observe that the grass is wet, that chance nearly doubles to about 35.8%. This is exactly what a Bayesian network is for: it takes evidence about one variable (WetGrass) and correctly propagates it back through the graph to update belief in another variable (Rain) it's connected to.

Quick Summary

ConceptMeaning
NodeA random variable, like Rain or WetGrass
EdgeA direct dependency between two variables
CPTTable giving a node's probabilities given its parents
InferenceUsing evidence to update belief in another variable

Frequently Asked Questions

In a Bayesian network, each node represents a random variable, and each directed edge represents a direct probabilistic dependency or influence from one variable to another.

A Conditional Probability Table (CPT) lists the probability of each possible value of a node given every combination of values of its parent nodes.

A node with no parents does not depend on other variables in the graph and simply holds its own prior probability.

When new evidence is observed, the network uses Bayes' rule to propagate the observation through the graph, updating the prior probability of connected variables into a revised conditional probability.