Bayesian network intuition
A Bayesian network represents a probability distribution using a directed acyclic graph (DAG) and local conditional probabilities. It is useful when several uncertain observations are related. For example, a fictional outdoor display might have a wet surface and poor visibility because it is raining. The observations provide evidence about rain; they do not prove it. The model makes its assumptions explicit so you can update probabilities instead of treating a guess as a fact.
Read a three-variable network
Let R mean rain, W a wet surface and V poor visibility. Each variable is either true or false. The graph has two arrows, R → W and R → V, and no directed cycles. Rain has no parents. The other two variables each have rain as their parent. We choose a prior P(R)=1/4. The probability of a wet surface is 4/5 with rain and 1/5 without it; poor visibility is 3/5 with rain and 1/10 without it. These are invented probabilities, not weather measurements. Each false outcome has probability one minus the corresponding true outcome.
Conditional independence and factorization
The model assumes W and V are independent given R: once you know whether it is raining, knowing W does not change the distribution of V. Thus a joint world has probability P(r,w,v)=P(r)P(w|r)P(v|r), where lowercase letters denote specific Boolean values. For all three true, multiply (1/4)(4/5)(3/5)=3/25. Without rain, wet and poor has probability (3/4)(1/5)(1/10)=3/200. Add these worlds to get P(W,V)=27/200. Marginally P(W)=7/20 and P(V)=9/40; their product 63/800 is not 27/200. Conditional independence is not unconditional independence. A shared cause can make observations related.
from fractions import Fractionfrom itertools import product
def weather_joint(prior, wet, poor): # All inputs are probabilities for True, indexed by rain status. rows = {} for rain, surface, visibility in product((False, True), repeat=3): r = prior if rain else 1 - prior w = wet[rain] if surface else 1 - wet[rain] v = poor[rain] if visibility else 1 - poor[rain] rows[rain, surface, visibility] = r * w * v return rows
rows = weather_joint(Fraction(1, 4), {False: Fraction(1, 5), True: Fraction(4, 5)}, {False: Fraction(1, 10), True: Fraction(3, 5)})print(sum(rows.values()))print(rows[True, True, True])print(sum(p for (_, wet, poor), p in rows.items() if wet and poor))The outputs are 1, 3/25 and 27/200. The first is a normalization check: all possible worlds have total probability one. Fraction is a standard-library type that stores exact rational numbers; use integer numerator and denominator rather than a float to avoid introducing binary rounding. itertools.product creates every triple of Boolean values. The function assumes exact rational probabilities between zero and one and complete tables for both rain values; this is not a general-purpose network parser.
Update beliefs from evidence
To calculate P(R|W), divide the probability of rain and wet surface by the probability of wet surface. The numerator is 1/5 and denominator 7/20, yielding 4/7. With both W and V observed true, the numerator is 3/25 and denominator 27/200, yielding 8/9. Unobserved variables are summed over, not automatically set to false. Evidence can move beliefs in either direction. A query inconsistent with possible evidence has probability zero; evidence with probability zero makes the conditional probability undefined, and the teaching function returns None rather than inventing a result.
from fractions import Fractionfrom itertools import product
def weather_joint(prior, wet, poor): # All inputs are probabilities for True, indexed by rain status. rows = {} for rain, surface, visibility in product((False, True), repeat=3): r = prior if rain else 1 - prior w = wet[rain] if surface else 1 - wet[rain] v = poor[rain] if visibility else 1 - poor[rain] rows[rain, surface, visibility] = r * w * v return rows
def probability(rows, query, evidence): # query and evidence map tuple positions to Boolean values. def matches(world, conditions): return all(world[i] == value for i, value in conditions.items()) denominator = sum(p for world, p in rows.items() if matches(world, evidence)) if denominator == 0: return None numerator = sum(p for world, p in rows.items() if matches(world, evidence) and matches(world, query)) return numerator / denominator
rows = weather_joint(Fraction(1, 4), {False: Fraction(1, 5), True: Fraction(4, 5)}, {False: Fraction(1, 10), True: Fraction(3, 5)})print(probability(rows, {0: True}, {1: True}))print(probability(rows, {0: True}, {1: True, 2: True}))print(probability(rows, {0: False}, {0: True}))Outputs: 4/7, 8/9, 0. Index 0 refers to rain, 1 to wet surface, and 2 to poor visibility. The query and evidence dictionaries specify which positions have known values. The third call asks for no rain when rain is known, so the numerator is zero but the denominator is positive. A missing observation is not the same as an observed false value.
When the network is a bad model
A sprinkler or fog could affect wetness and visibility even when rain is known. If the assumed independence fails, multiplying the two local probabilities may misrepresent evidence. Arrows in a probability model do not establish a causal effect by themselves; observational dependencies require additional causal assumptions before interventions can be predicted. Enumeration grows exponentially with the number of Boolean variables. Larger models can exploit factorization with variable elimination or use approximate methods; neither approach repairs an incorrect model. Report assumptions, probability sources and uncertainty.
Practice
Compute P(R|W=false) by hand, then check with probability(rows,{0:True},{1:False}). It is 1/13. Next set P(W=true|R) and P(W=true|not R) both to zero and ask for the posterior given wetness. Explain why None is appropriate. In the next lesson, turn a modeled probability into a decision using explicitly chosen utilities.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Bayesian network intuition — understanding check
Apply the chapter concepts to new situations.
Which directed graph represents the lesson weather network, with rain R as the shared parent of wetness W and poor visibility V?
What does conditional independence of W and V given R mean in the stated model?
What is P(R=true,W=true,V=true) under the given model?
When computing P(R=true | W=true) and visibility is unobserved, how is V handled?
If wetness has zero probability under all rain states, what is P(R|W=true)?