Skip to content
Tutorials Center
My learning

Topics

Lesson 32 of 46 ยท Probabilistic Reasoning

Probability for AI

Learn the probability an AI agent needs: random variables, joint and conditional distributions, Bayes' rule, independence and inference by enumeration, with NumPy code.

Every algorithm so far assumed that the agent knows the truth: the board is fully visible, the action does exactly what it says, the knowledge base is correct. The real world is not like that. A medical test can be wrong, a sensor reading is noisy, and an email may or may not be spam. An agent in such a world cannot be certain, but it can still act sensibly if it reasons with probability.

This lesson gives you the small toolbox that the rest of the chapter uses: random variables, joint and conditional distributions, marginalization, Bayes' rule and independence. All of it is demonstrated with NumPy arrays you can run and change.

Random variables and distributions

A random variable is a quantity whose value is uncertain, for example Flu (is the patient sick?), Fever (does the patient have a temperature?) or Weather. In this chapter every variable is discrete and takes one of a few values. A probability distribution assigns a number between 0 and 1 to each value, and the numbers add up to 1.

Think of probability as a degree of belief. P(Flu = true) = 0.1 means "before looking at any symptoms, the agent believes 10% of patients like this have flu". This is called a prior probability.

The joint distribution assigns a probability to every combination of values of several variables. For three yes/no variables it has 2 ร— 2 ร— 2 = 8 entries. A NumPy array with one axis per variable is a natural way to store it. The joint distribution answers every question about those variables, though it grows exponentially with the number of variables, which is the problem Bayesian networks solve in the next lesson.

Marginals and conditionals

Two operations do almost all the work.

Marginalization removes a variable by summing over its values:

P(Fever = f) = ฮฃ over Flu, Cough of P(Flu, Fever = f, Cough)

In NumPy this is joint.sum(axis=...). Conditioning restricts attention to what you know. The conditional probability of a given b is

P(a | b) = P(a, b) / P(b)

It is the fraction of the "worlds where b holds" in which a also holds. The same equation read the other way is the product rule: P(a, b) = P(a | b) ยท P(b).

graph TD
    J["Joint P(Flu, Fever, Cough)"] -- "sum out variables" --> M["Marginal P(Fever)"]
    J -- "fix the evidence, then normalize" --> C["Conditional P(Flu | Fever = yes)"]

The two basic operations on a joint table: marginalize by summing, condition by slicing and renormalizing.

The following example builds a joint table for flu, fever and cough. We construct it from three small tables (a prior for flu and the chance of each symptom with and without flu) using np.einsum, then answer questions by summing and dividing.

Python A joint distribution as a NumPy array

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The prior chance of flu is 10%, but once the patient has a fever it rises to about 47%. Evidence changes belief, and the rest of the chapter is about doing that systematically.

Bayes' rule

Often we know the probability of the effect given the cause (how often flu produces fever) but we want the reverse: the probability of the cause given the effect. Writing the product rule in both directions and dividing gives Bayes' rule:

P(cause | effect) = P(effect | cause) ยท P(cause) / P(effect)

The denominator P(effect) is what makes the result a proper distribution. We can compute it by summing over all causes, ฮฃ P(effect | cause) ยท P(cause), or simply ignore it and normalize at the end. Always keep these words straight:

Term Name Meaning
P(cause) prior belief before the evidence
P(effect | cause) likelihood how well the cause explains the evidence
P(cause | effect) posterior belief after the evidence

A medical test shows the lesson that people (including doctors) often get wrong. Suppose a test detects a disease in 95% of sick people (sensitivity) and correctly gives a negative result for 90% of healthy people (specificity). If a patient tests positive, how likely is the disease? The answer depends heavily on the base rate, how common the disease is.

Python Bayes' rule and the base-rate effect

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With a prior of 1%, a positive result from a "95% accurate" test means only about an 8.8% chance of disease, because the many healthy people produce more false positives than the few sick people produce true positives. The simulation lands near the formula (0.0858 versus 0.0876), which is the right kind of agreement for a random experiment. A second positive test lifts the belief to about 48%. This sequential updating, where one posterior becomes the next prior, is the heart of the filtering algorithms you will see in Hidden Markov Models and Filtering.

Warning: Do not confuse P(positive | disease) (95%) with P(disease | positive) (8.8% here). Mixing up a conditional with its reverse is called the base-rate fallacy or the prosecutor's fallacy, and it is a classic reasoning error.

Independence and conditional independence

Variables A and B are independent if knowing one tells you nothing about the other: P(A, B) = P(A) ยท P(B). Independence is valuable because it lets a big joint table be stored as small separate tables. A table of 10 independent yes/no variables needs 10 numbers rather than 1,023.

Truly independent variables are rare; conditional independence is the common and more useful form. A and B are conditionally independent given C when, once you know C, learning B adds nothing about A:

P(A, B | C) = P(A | C) ยท P(B | C)

In the flu model, fever and cough are not independent in general (if a patient has a fever, flu is more likely, so a cough becomes more likely too), but they are independent given the flu status. Check it numerically in the first example's joint array: for Flu = 1 the product of the marginals matches the joint entry exactly (0.56 and 0.56), while without the conditioning it does not (0.074 against 0.0425).

Inference by enumeration

The simplest general way to answer a query is inference by enumeration: take the joint table, keep only the cells that match the evidence, sum out the variables you do not care about (the hidden variables), and normalize. The Joint class below does this for any number of named variables.

The model is a small garden: rain (rainy day), sprinkler (the automatic sprinkler ran) and wet (the grass is wet). The sprinkler is less likely to run on a rainy day, and wet grass can have either cause.

Python Inference by enumeration over a named joint table

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Read the three probabilities in order. Rain starts at 30%. Seeing wet grass raises it to about 49%. But if we also learn the sprinkler ran, rain drops to about 5.6%: the sprinkler explains away the wet grass. When one effect has two possible causes, learning that one cause is present makes the other less necessary. This pattern appears throughout probabilistic AI. (In this model rain and sprinkler are already dependent by construction, as the last line shows; in a model where two causes are independent a priori, observing their common effect makes them dependent.)

Enumeration is simple and exact, but it needs the whole joint table, whose size is 2^n for n yes/no variables. For 30 variables that is over a billion numbers. Exploiting conditional independence to avoid that table is exactly what a Bayesian network does.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does marginalizing a variable out of a joint distribution mean?

    Marginalization sums the joint over every value of the variable you want to remove, leaving the distribution of the remaining variables.
  2. 2. A test has 95% sensitivity and 90% specificity, and the disease affects 1% of people. Why is P(disease | positive) only about 9%?

    With a low base rate, 10% false positives among 99% healthy people outnumber 95% true positives among 1% sick people. The prior matters.
  3. 3. Which statement about conditional independence is correct?

    Once C is known, B gives no extra information about A. This is different from plain independence and is the basis of Bayesian networks.
  4. 4. In Bayes' rule, what is the likelihood?

    The likelihood P(effect | cause) says how well a cause predicts the observed evidence. The prior is P(cause) and the posterior is P(cause | effect).
  5. 5. What is the main drawback of inference by enumeration over the full joint table?

    n yes/no variables need 2^n entries. That is why compact representations such as Bayesian networks matter.

Summary

  • A probability distribution over a discrete random variable assigns numbers in [0, 1] that sum to 1; the joint distribution covers all combinations of several variables.
  • Marginalize by summing out variables; condition by fixing the evidence and normalizing; the product rule links joint and conditional probabilities.
  • Bayes' rule turns P(effect | cause) into P(cause | effect); base rates matter enormously.
  • Independence lets large joints factor into small tables; conditional independence is the common and useful form.
  • Inference by enumeration slices, sums and normalizes the joint table; it is exact but exponential in the number of variables.
  • Observing a common effect can make independent causes compete ("explaining away").

Next: Bayesian Networks.