Lesson 2 of 116 min
Conditional probability and Bayes' rule
A chance once you know something is a share of a smaller pile, and turning it around gives another pile, whose answer depends on how common the cause is.
After this lesson you can update a probability when you learn something new, and turn "how likely is this evidence" into "how likely is this cause".
Loading the video…
Refreshers
An idea from an earlier lesson shows up here when the video leans on it.
In real code
What the video left out, as you will meet it in your own code.
The names of the rule's parts
Books and code give each share of the rule a name. How common AI essays are before any flag is the prior. A flag, given AI, is the likelihood. AI, given a flag, is the posterior. The rule comes from counting the essays that are AI-written and flagged two ways: 0.9 of 0.1, or 0.67 of 0.135.
p_ai = 0.1 # the prior
p_flag_ai = 0.9 # the likelihood
p_flag = 0.9 * 0.1 + 0.05 * 0.9 # every flag, as a share
p_ai_flag = p_flag_ai * p_ai / p_flag # the posterior
print(round(p_flag, 3), round(p_ai_flag, 3))
print(round(0.9 * 0.1, 3), round(90 / 1000, 3), round(2/3 * 0.135, 3))0.135 0.667 0.09 0.09 0.09
A second flag
A second detector flags the same essay. Use the rule again, with the first answer as the new prior: 0.67 becomes 0.97. This holds only when the two detectors make their mistakes apart. Two detectors trained the same way often flag the same essays by people, and then the second flag tells you much less.
def given_flag(base, caught, wrong):
right = caught * base
return right / (right + wrong * (1 - base))
first = given_flag(0.1, 0.9, 0.05)
second = given_flag(first, 0.9, 0.05)
print(round(first, 3), round(second, 3))0.667 0.973
Check the second flag with a draw
Draw the essays, give each one two flags that don't depend on each other, and keep the essays both detectors flagged. The share AI wrote lands near the 0.973 the rule gave.
import numpy as np
rng = np.random.default_rng(0)
n = 100_000
ai = rng.random(n) < 0.1
one = np.where(ai, rng.random(n) < 0.9, rng.random(n) < 0.05)
two = np.where(ai, rng.random(n) < 0.9, rng.random(n) < 0.05)
print(ai[one & two].mean())0.9742803209060877
The same rule, in odds
Odds say one count against the other: 1 AI essay to 9 by people. A flag is 18 times likelier on an AI essay (0.9 over 0.05). Multiply the odds by 18 and you get 2 to 1, which is 2 in 3 again. In odds, each new flag is one more multiplication.
before = 1 / 9 # 1 AI essay to 9 by people
ratio = 0.9 / 0.05 # a flag, 18 times likelier on AI
after = before * ratio
print(round(ratio), round(after, 3), round(after / (1 + after), 3))18 2.0 0.667
Run on Python 3.13 and NumPy 2.5.5. Checked on .
Quiz
Two questions on this lesson, and one from earlier. Answer as often as you like.
1 of 3
From earlier: Probability
An AI agent does a job in five steps. Each step goes right 9 times in 10, whatever the other steps did. What is the chance the whole job goes right?
2 of 3
A spam filter catches 9 spam emails in 10. It flags 1 good email in 20 by mistake. It flags an email. How sure can you be that it's spam?
3 of 3
Of 1,000 essays, 200 were written by AI. The detector flags 180 of them, and 40 of the 800 written by people. An essay is flagged. What is the chance that AI wrote it?
The ideas in this lesson
- Conditional probability
- A chance once you know something: keep only the cases that fit what you know, and take the share.
- Base rate
- How common something is before any test. When it is rare, most flags can be wrong, even from a good test.
- Bayes' rule
- It turns a chance around: from how often a thing gets flagged, to how often a flag means the thing.
It leans on
Transcript
What the video says, part by part. Press a time to watch from there.
How sure is a flag?
Here's a question. A detector checks essays for AI writing. It catches most AI essays. It rarely flags a person's essay by mistake. It flags your essay. How sure can you be that AI wrote it?
The detector's numbers
Say it catches nine AI essays in ten. And one essay in twenty by a person gets flagged by mistake. Make your guess.
A thousand essays
Let's count. Picture a thousand essays, one dot each. Say one essay in ten was written by AI. That's a hundred, drawn as diamonds.
The other nine hundred were written by people.
A smaller pile
So what does "catches nine in ten" mean, in the dots?
It's only about the AI essays. Keep just those hundred. The detector flags ninety of them.
So once you know AI wrote an essay, the chance of a flag is ninety out of a hundred. That's zero point nine.
A chance once you know something is a share of a smaller pile. It's called a conditional probability. Here, the chance of a flag, given AI-written.
The people's essays
The people's essays work the same way. Keep the nine hundred. One in twenty gets flagged: forty-five. The chance of a flag, given written by a person, is zero point zero five.
Turn it around
But you don't know who wrote your essay. You only see the flag. So now the smaller pile is every flagged essay. Ninety flags on AI essays, and forty-five on people's. No essay is both, so the two add up: a hundred and thirty-five flags. What share of them did AI write?
Ninety of the hundred and thirty-five. That's about zero point six seven... two in three. Not zero point nine.
"A flag, given AI" is one pile. "AI, given a flag" is another pile.
Why not zero point nine
The detector is good. So why not zero point nine?
Because there are many more essays by people. A small share of a big pile is still a lot of essays.
One in twenty of nine hundred makes forty-five wrong flags. That's next to ninety right ones. How common something is, before any test, is called its base rate. Here, one essay in ten.
The base rate decides
What if AI essays were rarer? Say one in fifty. Now there are twenty diamonds... and the detector flags eighteen.
There are nine hundred and eighty essays by people... and it flags forty-nine. So, eighteen right flags, out of sixty-seven. About one in four.
It's the same detector. And now most of its flags are wrong. The base rate decides.
Bayes' rule
Is there a rule, so you don't have to draw dots?
Yes. It's the same count, written with the detector's numbers.
On top: how often AI essays get flagged, times how common they are. That's the right flags.
Underneath: those right flags, plus the flags on people's essays.
Work it out, and it's zero point six seven again. That's Bayes' rule. It turns "a flag, given AI" into "AI, given a flag".
A real detector
Do real detectors look like this? In twenty twenty-three, a big AI lab put one out. It caught about a quarter of AI text. And it flagged about one in eleven texts by people, by mistake.
Your turn. If one essay in ten is AI-written, what share of its flags are right?
Out of a thousand essays, that's twenty-six right flags, and eighty-one wrong ones.
So about one flag in four is right.
The lab took it down that same year, because it was wrong so often.
In code
Now in real code. Draw a hundred thousand essays, each one AI-written one time in ten.
Then the flags. An AI essay is caught nine times in ten. An essay by a person is flagged one time in twenty. Every essay gets the flag that fits who wrote it. Now keep only the flagged essays.
Then take the share that AI wrote. What will it print?
Zero point six seven zero. Close to the two in three we counted.
Make AI essays one in fifty, and it prints zero point two six one.
That's close to zero point two seven.
A draw lands near the count, not on it.
The same check works for any test of something rare: spam, a stolen card, a rare illness.
Before you trust a flag, ask how common the thing is.
Two piles
So a chance once you know something is the share in a smaller pile. Turn the question around, and you get another pile. Bayes' rule counts it. And the base rate decides how much a flag is worth.
How common it is decides
A flag means less when the thing is rare. Now try it on this lesson's page, and take the quiz. Make the detector's flags right nine times in ten.
You use this lesson in Lab 1: Check an AI detector by drawing, after Random variables and distributions.
Facts last checked on .
