AI voor Iedereen

Search

Search chapters and demos

🎲 Probability

🎰 Why probability in AI?

🧠 Logic alone is not enough

In the previous chapter we reasoned with hard rules: if Anna comes, Bram comes too. That works perfectly for chess, sudoku and class timetables, because there the rules are fixed and the full state is known.

The real world is rarely that tidy. A doctor does not know for sure which disease is behind a cough. A historian does not know for sure who wrote an anonymous pamphlet. Your smartphone does not know for sure which word you want to type after "I'll have a sandwich with". Here "if X, then Y" is no longer enough. The question becomes: "how sure am I that X is true?"

Classical logic gets stuck on questions like these. One exception, one inaccurate sensor or one wrong assumption, and the whole line of reasoning collapses. That is why AI systems need a language for calculating with uncertainty: probability theory. Instead of "it is going to rain", a probabilistic system says "there is an 80% chance of rain". Instead of getting stuck on a strange measurement, it says "this is unlikely, but not impossible", and it takes both possibilities into account.

🌫️ Where does uncertainty come from?

Uncertainty creeps into almost every AI problem in four ways:

  • 🧩 Incomplete knowledge: you do not know everything. A card player cannot see the opponent's cards, and a historian has only a handful of sources left.
  • πŸ“‘ Inaccurate observations: a thermometer, a GPS or the camera of a self-driving car never measures the world with perfect accuracy. Is that spot on the road a pedestrian or a shadow?
  • 🎲 Unpredictable effects: the same action does not always have the same effect. A medicine often works, but not always. A robot that wants to drive 10 centimeters forward may drive 9.8.
  • πŸ—£οΈ Ambiguity: one observation can have several meanings. The same symptoms fit different diseases, and the word "bank" can mean a financial institution or the side of a river.

We often want an AI system to be able to reason about that doubt (make a decision without knowing everything) and to learn from noisy data. Probabilities are the language in which you write down that doubt and calculate with it.

Probability Definition

A probability is a number between 0 and 1 that says how likely something is. A probability of 0 means "impossible", a probability of 1 (or 100%) means "certain", and a probability of 0.8 means "in about 8 out of 10 cases".

You write the probability of an event AA as P(A)P(A). So you write the probability of rain tomorrow as P(Rain)P(\text{Rain}), and you read P(Rain)=0.8P(\text{Rain}) = 0.8 as "the probability of rain is 80%".

If you put all possible outcomes side by side (rain or no rain; 1, 2, 3, 4, 5 or 6 on a die), their probabilities always add up to exactly 1. After all, something is guaranteed to happen.

An image recognizer rarely just says "dog". Its answer is a probability distribution over all labels: 80% dog, 15% cat, 5% something else. A language model predicts a distribution over its entire vocabulary for the next word and picks from it. A weather model gives "an 80% chance of rain". In short, probability distributions are the standard form in which modern AI systems give their answers.

1️⃣ One variable

πŸ”’ Probability by counting

If all outcomes are equally likely, the probability of an event is the number of favorable outcomes divided by the number of possible outcomes.

  • 🎲 A fair die has 6 outcomes, one of which is a 3: P(D=3)=16β‰ˆ16.7%P(D{=}3) = \frac{1}{6} \approx 16.7\%.
  • πŸƒ A deck of cards has 52 cards, 13 of which are hearts: P(hearts)=1352=14=25%P(\text{hearts}) = \frac{13}{52} = \frac{1}{4} = 25\%.
  • πŸ‘‘ There are 4 kings: P(king)=452β‰ˆ7.7%P(\text{king}) = \frac{4}{52} \approx 7.7\%. There is only one king of hearts: P(kingΒ ofΒ hearts)=152β‰ˆ1.9%P(\text{king of hearts}) = \frac{1}{52} \approx 1.9\%.

What does such a percentage mean in practice? One answer: "if I roll very often, the 3 comes up in about one sixth of the rolls". Test that yourself in the simulator below. Click the die to roll it; on the right you see how often each outcome has come up so far (the relative frequency). After a few rolls, click "Fast" to let the computer do the rolling.

Loading demo

The dashed line shows the expected 16.7%. After 60 rolls the bars still differ quite a bit from it. After 600 rolls they are already close, and after 6,000 rolls even closer. The more often you repeat, the closer the relative frequency gets to the "true" probability. That is the frequentist view of probability: probability is what you see in the long run when you repeat something endlessly.

Many questions, however, cannot be repeated endlessly. "How likely is it that the Dutch writer Multatuli wrote this anonymous pamphlet?" or "Will it rain tomorrow?" are one-off questions. For those there is the Bayesian view: a probability is a degree of belief. "I am 70% sure that it will rain tomorrow" is then a perfectly meaningful statement, even though there is only one tomorrow. Ideally, it then actually rains on 70% of the occasions when you thought there was a 70% chance of rain. This is the view AI systems usually take: a model has a belief about the world and adjusts it as soon as new information comes in.

πŸ“‹ Probability distribution

A fair die has six outcomes, each with probability 16\frac{1}{6}. Such a list of all outcomes with their probabilities is called a probability distribution.

Probability distribution Definition

A probability distribution is a list of all possible outcomes of a variable, with the probability of each outcome. Because one of the outcomes is certain to happen, those probabilities add up to exactly 1 (100%).

A loaded die LL has the same six outcomes, but a different distribution. Suppose it shows a 6 in half of the rolls:

OutcomeFair die DDLoaded die LL
116.7%10%
216.7%10%
316.7%10%
416.7%10%
516.7%10%
616.7%50%
Total100%100%

Both columns are valid probability distributions, because the probabilities add up to 100%. The shape of the distribution tells you something about the world: the second die cannot be trusted.

πŸ“ Normalization

An AI system rarely starts with probabilities. It starts by counting: how often did each word occur, how many customers bought bread, how many patients with a fever had the flu? To go from such counts to probabilities, you have to normalize.

Normalization Definition

To normalize is to divide every count by the total, so that the numbers add up to 1 and thus form a valid probability distribution.

A playlist with 20 rock songs, 50 pop songs and 30 jazz songs (100 in total) thus becomes the distribution 20%, 50%, 30%. The probability that "shuffle" picks a pop song is 50100=0.5\frac{50}{100} = 0.5.

The table below contains raw counts. Click "Normalize" to turn them into a probability distribution.

Loading demo

Counting and normalizing is at the heart of a lot of machine learning: a model counts patterns in data and turns those counts into probabilities. In naive Bayes and autocomplete you see that happen literally.

πŸ”§ Calculating with probabilities

Once you have a probability distribution, you want to ask it questions: "What is the probability of a 3 or a 6?", "What is the probability that I do not roll a 6?" Two rules are enough for that: the addition rule and the complement rule.

  • Addition rule. The probability of "A or B" is the sum of both probabilities, minus the probability that they both happen. That subtraction is needed because otherwise you count the overlap twice.

    With one die there is no overlap: you cannot roll a 3 and a 6 at the same time. Such events are called mutually exclusive events or disjoint events, and for them you can simply add: P(D=3Β orΒ D=6)=16+16=13P(D{=}3 \text{ or } D{=}6) = \frac{1}{6} + \frac{1}{6} = \frac{1}{3}.

    Often there is overlap. Take a fruit basket with ten pieces of fruit: πŸŽπŸ“πŸ’πŸŠπŸ‡πŸ‘πŸ₯₯πŸŒπŸ‹πŸ. Three pieces are red (πŸŽπŸ“πŸ’), six are round (πŸŽπŸ’πŸŠπŸ‡πŸ‘πŸ₯₯), and two are both (πŸŽπŸ’).

    Venn diagram of red and round fruit, with overlap


    If you simply add 30% and 60%, you get 90%, but then you have counted the apple and the cherry twice. The correct probability of "red or round" is 310+610βˆ’210=710=70%\frac{3}{10} + \frac{6}{10} - \frac{2}{10} = \frac{7}{10} = 70\%: seven of the ten pieces are red, round or both.

  • Complement rule. The probability that something does not happen is 1 minus the probability that it does: P(notΒ A)=1βˆ’P(A)P(\text{not } A) = 1 - P(A). If you know there is a 20% chance of rain tomorrow, you also know there is an 80% chance that it stays dry. You write "not A" as Β¬A\lnot A; in probability tables you also see βœ… for "yes" and ❌ for "no".

Sometimes "the probability that something does not happen" is much easier to calculate than the probability that it does. For example: what is the probability of rolling at least one six with two dice? Simply adding 16+16\frac{1}{6} + \frac{1}{6} is wrong: you would then count a double six twice, just like the apple and the cherry in the fruit basket.

This is a good place to apply the complement rule. "At least one six" is the opposite of "no six at all", and because the dice are independent, you can multiply to get that probability: P(noΒ six)=56β‹…56=2536P(\text{no six}) = \frac{5}{6} \cdot \frac{5}{6} = \frac{25}{36}. So P(atΒ leastΒ oneΒ six)=1βˆ’2536=1136β‰ˆ30.6%P(\text{at least one six}) = 1 - \frac{25}{36} = \frac{11}{36} \approx 30.6\%.

πŸ‘₯ Multiple variables

So far we looked at one variable at a time: a die, a card, a playlist. In the real world, more is always happening at once: it rains and it is windy, someone has a fever and a cough, an email contains the word "free" and three exclamation marks. An AI system combines such variables all the time. A diagnosis system looks at age, symptoms and test results together, and a language model at all the previous words of a sentence.

As a running example we take a small case from the supermarket. For 100 customers, you kept track of whether they bought πŸ§€ cheese and whether they bought 🍞 bread:

βœ…πŸž Buys bread❌🍞 No breadTotal
βœ…πŸ§€ Buys cheese30535
βŒπŸ§€ No cheese105565
Total4060100

🌍 Joint distribution

Divide all counts by 100 (normalize!) and the table becomes a joint distribution: it holds a probability for every combination of values. 30% of the customers buy cheese and bread, 5% cheese without bread, 10% bread without cheese and 55% neither. Together that is 100%, because every customer falls into exactly one cell.

Joint distribution Definition

The joint distribution of several variables gives, for every possible combination of values, the probability that exactly that combination occurs. All probabilities in the table add up to 1.

You write the probability that AA and BB occur together as P(A,B)P(A, B) (sometimes also as P(A∩B)P(A \cap B), "AA intersection BB"). The order does not matter: P(A,B)=P(B,A)P(A, B) = P(B, A).

This table is the ideal of every probabilistic AI system: if you know it completely, you can answer any question about cheese and bread from it (see below). The problem is that it explodes. Just like a truth table, it lists every combination: with two yes/no variables that is 4 cells, with ten already 210=10242^{10} = 1024, and with the hundreds of variables of a real problem more cells than there are atoms in the universe. A large part of machine learning is about approximating that table cleverly without ever writing it out in full. We come back to this in the chapters on Bayesian networks and naive Bayes.

You can also draw the same table as a Venn diagram. Move the sliders below and predict how the diagram changes.

In a Venn diagram you draw a circle for event AA and a circle for event BB. The overlap stands for "A and B", everything inside the two circles together for "A or B", and the area outside the circles for "neither".

Loading demo

πŸ“Š Marginalization

What if you just want to know what percentage of the customers buy bread, with or without cheese? Then you add up the "Buys bread" column: 30+10=4030 + 10 = 40 customers, so P(Bread)=40%P(\text{Bread}) = 40\%. That is why the totals at the edge (the margin) of the table are called marginal probabilities, and the adding up itself is called marginalization.

Marginalization Definition

Marginalizing means removing a variable from a joint distribution by adding up the probabilities for all the different values of that variable. You are really saying: "I don't care whether cheese was bought; add all the cases together". What remains is the probability distribution of the variable you are interested in.

Look at the margins of the cheese-and-bread table below, and change a few probabilities to see how the margins change along with them. The row and column totals of the table show exactly the marginal probabilities of one variable, with the other one summed out. The drop-down menu has other situations too.

Loading demo

In AI you often work with dozens of variables at once. Marginalization lets you zoom in on the few variables that matter at that moment and "reason away" the rest.

For two variables XX and YY with joint distribution P(X,Y)P(X, Y), you write that adding up as a rule:

Marginal probability Rule
P(X)=βˆ‘yP(X,Y=y)=P(X,y1)+P(X,y2)+…+P(X,yn)P(X) = \sum_{y} P(X, Y{=}y) = P(X, y_1) + P(X, y_2) + \ldots + P(X, y_n)

The marginal probability of XX is the sum of the joint probabilities over all values yy of the variable YY that you remove. For the example: P(Bread)=P(Bread,Cheese)+P(Bread,Β¬Cheese)=0.30+0.10=0.40P(\text{Bread}) = P(\text{Bread}, \text{Cheese}) + P(\text{Bread}, \lnot\text{Cheese}) = 0.30 + 0.10 = 0.40.

The summation sign βˆ‘\sum simply means "add them all up". The same notation describes the normalization requirement of every distribution with finitely many outcomes (a discrete probability distribution): βˆ‘xP(X=x)=1\sum_{x} P(X{=}x) = 1, and for a joint distribution βˆ‘x,yP(X=x,Y=y)=1\sum_{x, y} P(X{=}x, Y{=}y) = 1.

πŸ” Conditional probability

Now for the most important step of the chapter. Suppose you see at the checkout that a customer has cheese in their basket. What is the probability that there is bread in it too?

You now only need to look at the "Buys cheese" row: 35 customers, 30 of them with bread. So the probability is 3035β‰ˆ85.7%\frac{30}{35} \approx 85.7\%, quite a bit higher than the 40% for a random customer. That is a conditional probability: the probability of something, given that you already know something else.

Conditional probability Definition

The conditional probability P(A∣B)P(A \mid B) is the probability that AA is true when you already know that BB is true. You read the bar ∣\mid as "given": P(Bread∣Cheese)P(\text{Bread} \mid \text{Cheese}) is "the probability of bread, given cheese".

You calculate it by looking only at the cases in which BB happened, and checking which fraction of those is also AA. As a formula, you divide the joint probability by the probability of the condition:

P(A∣B)=P(A,B)P(B)P(A \mid B) = \frac{P(A, B)}{P(B)}

For the example: P(Bread∣Cheese)=P(Bread,Cheese)P(Cheese)=0.300.35β‰ˆ85.7%P(\text{Bread} \mid \text{Cheese}) = \frac{P(\text{Bread}, \text{Cheese})}{P(\text{Cheese})} = \frac{0.30}{0.35} \approx 85.7\%.

So conditioning is two things at once: cutting away (all customers without cheese drop out of the picture) and normalizing again (the remaining 35 customers form the new 100%). Within the "Buys cheese" row, the conditional probabilities therefore add up neatly to 1: 85.7% with bread and 14.3% without.

The table below shows those two steps: the yellow row is the condition, the green cell is the joint probability that you divide by it. Use the "Edit question" button to ask other questions too.

Loading demo

The probability before you knew anything (P(Bread)=40%P(\text{Bread}) = 40\%) is called the prior probability. The probability after you saw the evidence (P(Bread∣Cheese)β‰ˆ85.7%P(\text{Bread} \mid \text{Cheese}) \approx 85.7\%) is called the posterior probability. Knowledge cuts away possibilities and so shifts your probabilities. That is the whole idea of reasoning under uncertainty, and you do it yourself all the time:

πŸ•΅οΈ Guess Who? Example

In the game Guess Who? (external link) you guess which of the 24 characters your opponent picked by asking yes/no questions. Beforehand, each character has probability 124\frac{1}{24}. You ask "Is your person wearing a hat?" and the answer is yes. You flip down all the characters without a hat: their probability becomes 0. If five hat wearers are left, the probability of each of them jumps to 15\frac{1}{5}.

Every question is a condition, every flipped-down card a possibility that is cut away, and the probabilities of what is left are divided up again. A probabilistic AI system does exactly the same: start with a prior probability, receive evidence, cross out possibilities and calculate the posterior probability.

Almost every AI model calculates a conditional probability. A model gets to see something (the evidence) and gives a probability distribution over what it concludes from it:

  • πŸ“§ A spam filter calculates P(spam∣wordsΒ inΒ theΒ email)P(\text{spam} \mid \text{words in the email}).
  • 🩺 A diagnosis system calculates P(disease∣symptomsΒ andΒ testΒ results)P(\text{disease} \mid \text{symptoms and test results}).
  • πŸ’¬ A language model such as ChatGPT calculates P(nextΒ word∣textΒ soΒ far)P(\text{next word} \mid \text{text so far}).
  • 🎬 A recommender system calculates P(youΒ likeΒ thisΒ film∣whatΒ youΒ watchedΒ before)P(\text{you like this film} \mid \text{what you watched before}), just as the supermarket infers from the cheese that you want bread.
πŸ“– Guessing the next word Example

Take the sentence "The dog of the man of the neighbors of the cat bit the man of the cat of the neighbors" and count, for each word, which word follows it. "The" is followed by a word seven times: twice by "man", twice by "cat", twice by "neighbors" and once by "dog". Normalize those counts and you have conditional probabilities: P("man"∣"the")=27β‰ˆ28.6%P(\text{"man"} \mid \text{"the"}) = \frac{2}{7} \approx 28.6\%.

Loading demo

This little table is already a tiny language model. Train the same model on the collected poems of Emily Dickinson or on billions of web pages, and the table becomes gigantic, but the principle stays the same: count how often each word follows a context, and normalize. Such a language model lets you do simple probability calculations on texts. The chapter on autocomplete goes deeper into this.

πŸ”€ Independent or dependent?

Knowing about the cheese changed the probability of bread from 40% to 85.7%. But not all information helps. If you roll a black (BB) and a gray (GG) die, the outcome of the black one says nothing about the gray one: P(G=6∣B=6)=P(G=6)=16P(G{=}6 \mid B{=}6) = P(G{=}6) = \frac{1}{6}. The gray die does not "know" what the black one did. Such variables are called independent.

Independence Definition

Two variables are independent if knowing one does not change your probability of the other: P(A∣B)=P(A)P(A \mid B) = P(A). So you learn nothing new about AA by knowing BB.

For independent variables, the joint probability is simply the product of the separate probabilities: P(A,B)=P(A)β‹…P(B)P(A, B) = P(A) \cdot P(B).

The probability of two sixes with two dice is 16β‹…16=136\frac{1}{6} \cdot \frac{1}{6} = \frac{1}{36}. After all, there are 6Γ—6=366 \times 6 = 36 equally likely combinations, exactly one of which is a double six. The same holds for every combination: the joint distribution of two fair dice is a table of 36 cells, each with probability 136β‰ˆ2.78%\frac{1}{36} \approx 2.78\%.

Cheese and bread, on the other hand, are dependent: knowing that someone bought cheese changes the probability of bread drastically.

  • P(Bread)=40%P(\text{Bread}) = 40\%: the probability for a random customer.
  • P(Bread∣Cheese)β‰ˆ85.7%P(\text{Bread} \mid \text{Cheese}) \approx 85.7\%: someone who buys cheese usually buys bread too.
  • P(Bread∣¬Cheese)=1065β‰ˆ15.4%P(\text{Bread} \mid \lnot\text{Cheese}) = \frac{10}{65} \approx 15.4\%: someone who buys no cheese rarely buys bread.

βœ–οΈ Product rule and chain rule

For dependent variables you cannot simply multiply. Still, you can always build up the probability of "cheese and bread" in two steps: first the probability that someone buys cheese (35%), then the probability that this person also buys bread (85.7%). Together: 0.35β‹…0.857=0.300.35 \cdot 0.857 = 0.30, exactly the 30% from the table. That split is called the product rule.

Product rule Rule

P(A,B)=P(A∣B)β‹…P(B)P(A, B) = P(A \mid B) \cdot P(B)

The probability that AA and BB happen together is the probability of BB times the probability of AA given BB. This is nothing but the definition of conditional probability, turned around: multiply both sides of P(A∣B)=P(A,B)P(B)P(A \mid B) = \frac{P(A, B)}{P(B)} by P(B)P(B).

If AA and BB are independent, then P(A∣B)=P(A)P(A \mid B) = P(A) and you get P(A,B)=P(A)β‹…P(B)P(A, B) = P(A) \cdot P(B) again.

πŸ”— Chain rule

The same trick works for more than two variables. You write the probability of a whole combination as a chain of conditional probabilities: the probability of the first, times the probability of the second given the first, times the probability of the third given the first two, and so on. That is called the chain rule.

For three variables you apply the product rule twice:

P(A,B,C)=P(A∣B,C)β‹…P(B,C)=P(A∣B,C)β‹…P(B∣C)β‹…P(C)\begin{align*} P(A, B, C) &= P(A \mid B, C) \cdot P(B, C) \\ &= P(A \mid B, C) \cdot P(B \mid C) \cdot P(C) \end{align*}

Independencies make the chain shorter. If BB is independent of CC (written BβŠ₯CB \perp C), then P(B∣C)=P(B)P(B \mid C) = P(B) and one condition drops out. With Bayesian networks you will see how to exploit such independencies systematically, and with naive Bayes how a model even simply assumes them to keep the table small.

πŸ”„ Bayes' theorem

Often you know a conditional probability in the "wrong" direction. A doctor knows from experience how often the flu causes a fever: P(Fever∣Flu)P(\text{Fever} \mid \text{Flu}). But the patient with a fever wants to know the reverse: P(Flu∣Fever)P(\text{Flu} \mid \text{Fever}). A historian knows how often a known author uses certain words, but wants to know who wrote an anonymous pamphlet (authorship attribution). A spam filter knows how often spam emails contain the word "free", but has to decide whether an email with that word is spam.

Each time, you know P(Evidence∣Cause)P(\text{Evidence} \mid \text{Cause}) and you are looking for P(Cause∣Evidence)P(\text{Cause} \mid \text{Evidence}). Bayes' theorem is the tool for turning that arrow around. It is the foundation of many AI systems and the most important formula of this chapter. We start with an example and simply count.

πŸ”₯ The fire alarm

The fire alarm goes off. Should you panic? You know that the alarm almost always goes off when there is a real fire: P(Alarm∣Fire)=99%P(\text{Alarm} \mid \text{Fire}) = 99\%. But what you really want to know is P(Fire∣Alarm)P(\text{Fire} \mid \text{Alarm}): the alarm is going off, so is there a fire?

To turn that arrow around, you need three numbers:

  • πŸ”₯ How often is there a fire? Fires are rare, say 1 day in 1,000 (0.1%). This is the base rate, the prior probability P(Fire)P(\text{Fire}).
  • πŸ”” How often does the alarm go off when there is a fire? Almost always: 99%. This is the sensitivity P(Alarm∣Fire)P(\text{Alarm} \mid \text{Fire}).
  • 🚨 How often does the alarm go off without a fire? Burnt toast, steam from the shower: say on 1% of the days. This is the false positive rate (FPR) P(Alarm∣¬Fire)P(\text{Alarm} \mid \lnot\text{Fire}).

Now look at 1,000 days, as in the grid below. On 1 day there is a fire, and the alarm goes off (red). On the other 999 days there is no fire, yet the alarm still goes off on about 10 of them (blue). So the alarm goes off on 11 days, and on only 1 of those 11 days is there really a fire:

P(Fire∣Alarm)=111β‰ˆ9%P(\text{Fire} \mid \text{Alarm}) = \frac{1}{11} \approx 9\%
Loading demo

Only 9%, even though the alarm seemed 99% reliable. The reason: fire is so rare that the few false alarms far outnumber the real fire. Play with the sliders: make fires more frequent, or the alarm more precise, and see what happens to the posterior probability.

The lesson of Bayes: don't forget the base rate. How rare the cause was beforehand counts just as much as how strong the evidence is. You see exactly the same structure in medical tests. A test that detects 99% of the people who have a disease still gives mostly false positives when the disease is rare. A positive result then does not mean "you are sick", but "your probability has gone up, take a second test".

πŸ“œ Bayes' theorem as a formula

Bayes' theorem writes down what we just counted in a single formula.

Bayes' theorem Rule

Bayes' theorem turns a conditional probability around. In words: the probability of a cause given the evidence equals the probability of the evidence given the cause, times how often the cause occurs beforehand, divided by how often the evidence occurs in general.

P(B∣A)=P(A∣B)β‹…P(B)P(A)P(B \mid A) = \frac{P(A \mid B) \cdot P(B)}{P(A)}
  • P(B)P(B) is the prior probability: how likely BB is before you know AA. For the fire alarm: P(Fire)=0.001P(\text{Fire}) = 0.001.
  • P(A∣B)P(A \mid B) is the likelihood: how likely the evidence is if BB is true. Here: P(Alarm∣Fire)=0.99P(\text{Alarm} \mid \text{Fire}) = 0.99.
  • P(A)P(A) is the total probability of the evidence: how often AA occurs, with or without BB. Here the alarm goes off on 11 of the 1,000 days, so P(Alarm)β‰ˆ0.011P(\text{Alarm}) \approx 0.011.
  • P(B∣A)P(B \mid A) is the posterior probability: what you want to know. Here: P(Fire∣Alarm)=0.99β‹…0.0010.011β‰ˆ0.09P(\text{Fire} \mid \text{Alarm}) = \frac{0.99 \cdot 0.001}{0.011} \approx 0.09.

Above all, remember the shape of the numerator: likelihood times prior. Strong evidence (P(A∣B)P(A \mid B) high) cannot simply make up for a rare cause (P(B)P(B) low). The denominator P(A)P(A) is only a normalization: it makes sure that the posterior probabilities over all possible causes add up to 1 again.

βš–οΈ Deriving Bayes' theorem

Bayes' theorem is not a new assumption: it follows directly from the product rule. You can split the joint probability P(A,B)P(A, B) in two ways:

P(A,B)=P(A∣B)β‹…P(B)andP(A,B)=P(B∣A)β‹…P(A)P(A, B) = P(A \mid B) \cdot P(B) \qquad \text{and} \qquad P(A, B) = P(B \mid A) \cdot P(A)

So the two right-hand sides are equal:

P(B∣A)β‹…P(A)=P(A∣B)β‹…P(B)P(B \mid A) \cdot P(A) = P(A \mid B) \cdot P(B)

Divide both sides by P(A)P(A) and you have Bayes' theorem:

P(B∣A)=P(A∣B)β‹…P(B)P(A)P(B \mid A) = \frac{P(A \mid B) \cdot P(B)}{P(A)}

🧰 Computing everything from the joint distribution

Everything you saw in this chapter starts from the same table. If you know the full joint distribution, you can derive every other probability from it:

  • Marginal probabilities by adding up rows or columns.
  • Conditional probabilities by dividing a joint probability by a marginal probability.
  • Reversed conditional probabilities with Bayes' theorem.
  • Complements and or-probabilities with the complement rule and the addition rule.

Practice below with the cheese-and-bread table, or pick another probability table at the top left. Set a question with the "Edit question" button, work out the answer yourself and check your calculation against the one in the demo.

  1. Marginalization: what is P(Bread)P(\text{Bread})? (Add up the "Bread=βœ…" column.)
  2. Conditional probability: what is P(Cheese∣Bread)P(\text{Cheese} \mid \text{Bread})? (Divide the joint probability by the marginal probability of bread.)
  3. Reading off: what is P(Β¬Cheese,Β¬Bread)P(\lnot\text{Cheese}, \lnot\text{Bread})? (Read off the cell.)
  4. Reversing: what is P(Bread∣Cheese)P(\text{Bread} \mid \text{Cheese})? Compare with question 2: the direction of the bar makes a big difference.
  5. Complement: what is P(Β¬Cheese)P(\lnot\text{Cheese})? (Add up the "Cheese=❌" row, or calculate 1βˆ’P(Cheese)1 - P(\text{Cheese}).)
Loading demo

⚠️ Pitfalls: when probabilities mislead

Probabilities are treacherous, for people and for AI systems alike. Three pitfalls come up so often that you need to recognize them. They have one thing in common: the bare numbers seem to say something, but only when you look at the context (what is rare, what is related to what, which variable can you not see?) is your conclusion correct. A model that sees only numbers walks right into them.

πŸ“‰ Correlation is not causation

What goes wrong? Two things occur together, and you conclude that one causes the other.

Example. On days when a lot of ice cream is sold, more people drown. Does ice cream cause drowning? No. Does drowning make people want ice cream? Not that either. There is a hidden third variable, a confounding variable (or confounder): warm weather. When it is warm, people eat ice cream and go swimming. Cheese and bread are also related without one causing the other; the sandwich is the hidden reason.

Why this matters for AI. Models are champions at detecting associations, but they only see what occurs together in the data, not what causes what. A model that sees ice cream sales and drownings could "conclude" that banning ice cream saves lives. That is why you need people with domain knowledge to tell causality apart from mere association.

πŸ” Turning the arrow around

What goes wrong? You confuse P(Evidence∣Hypothesis)P(\text{Evidence} \mid \text{Hypothesis}) with P(Hypothesis∣Evidence)P(\text{Hypothesis} \mid \text{Evidence}), and so you forget the base rate. That is called the base-rate fallacy, exactly the mistake that Bayes' theorem prevents.

Example. In a court case, the prosecutor says: "The probability that a random innocent person matches this DNA trace is 1 in a million. The suspect matches. So he is guilty with 99.9999% certainty." This is called the prosecutor's fallacy (external link). The figure of 1 in a million is P(match∣innocent)P(\text{match} \mid \text{innocent}): how likely the evidence is for an innocent person. The judge wants to know P(innocent∣match)P(\text{innocent} \mid \text{match}), and that is something completely different. Just count: in a country with 10 million inhabitants, on average 10 innocent people match the trace. If the suspect was found by searching a database, without any other evidence, he is one of about eleven matching people, and the probability that he is guilty is closer to 1 in 11 than to 99.9999%. Just like the fire alarm: one real fire among ten false alarms.

Why this matters for AI. A classification model that is "99% accurate" still produces mostly false alarms for a rare category (fraud, a rare disease, a wanted person at an airport). Anyone who ignores the base rate overestimates what the model is saying. In the chapter on model evaluation you learn how to evaluate such models fairly.

πŸͺ€ Simpson's paradox: combining groups can reverse a trend

What goes wrong? A trend that holds in every subgroup reverses as soon as you lump the groups together.

Example. A classic study of kidney stone treatments (Charig et al., 1986) (external link), here with simplified figures. Two treatments were each given to 100 patients:

Success rateSmall stonesLarge stonesAll patients
Treatment A90% (9 of 10)50% (45 of 90)54% (54 of 100)
Treatment B80% (72 of 90)30% (3 of 10)75% (75 of 100)

Treatment A does better for small stones and better for large stones, but overall B seems better. How is that possible? A was mostly used for large (difficult) stones, B mostly for small (easy) ones. The size of the stone is the hidden variable that distorts the overall picture: B looks good because B got the easy cases, not because B works better.

Simpson's paradox Definition

Simpson's paradox (external link) occurs when a relationship that holds in every subgroup disappears or reverses as soon as you combine the subgroups. The cause is a hidden variable that is related to both the choice (A or B) and the outcome.

Why this matters for AI. A model that trains on the combined data "learns" that B is better and would recommend B to every patient, while A is better for everyone. That is how unintentionally biased models come about. The remedy: split the data by the relevant variables, use domain knowledge and map out causal relationships explicitly, for example with Bayesian networks.

🎯 Calibration

Suppose the weather forecaster says: "There is a 50% chance of rain tomorrow". That does not mean "it will rain for half of the day tomorrow". It means: "of all the days that resemble the forecast for tomorrow, it rained on about half".

That is the idea of calibration: good probability forecasts are right in the long run. If the forecaster predicts "20% chance" 100 times and it rains on about 20 of those days, the forecaster is perfectly calibrated.

Calibration Definition

A prediction system is calibrated if, over all predictions to which it assigns a probability pp, the event really occurs in about a fraction pp of the cases.

Ideally, people and AI systems estimate their own uncertainty well. Modern AI systems (especially neural networks), however, are often overconfident (external link): some networks report 99% certainty but are right in only 80% of those cases. In critical domains such as medicine, finance and safety, that is dangerous. Calibration is a form of honesty toward users: the system has to admit how uncertain it is, and estimate that uncertainty as well as possible.

πŸ•ΉοΈ Demos

πŸ—£οΈ Feedback

Just like machine learning algorithms, we also love learning from your feedback to improve the course.

Let us know how informative and how difficult you found this chapter, and where it could be better.

How informative did you find this chapter?
Was the difficulty of this chapter right for your level of knowledge and skill? This chapter was...
Anonymous with random ID