Lab 1 of 4about 35 minutes
Check an AI detector by drawing
It uses what you learned in Probability, Conditional probability and Bayes' rule and Random variables and distributions.
Start here
Run itNumPy for the draws, and matplotlib for the bars.
1The job, drawn
Run itFrom Probability
The agent's job from prob-basics: ten steps, each right nine times in ten. Run the cell. It draws 100,000 runs with a seeded generator, and prints the share where all ten steps went right, beside 0.9 to the tenth.
The draw lands near the worked-out answer, not on it. Every lab of this course does both: work it out, then draw it and count.
2The detector's bars
Run itFrom Random variables and distributions
The detector from prob-bayes, a little more real. It gives every essay a score from 1 to 5, drawn from a distribution: one set of bars for people's essays, one for AI-written ones. It flags a score of 4 or more. Run the cell: it draws both sets and the line.
Each set adds up to 1, as a distribution does. A set that adds up to 0.99 stops NumPy's draws: ValueError: Probabilities do not sum to 1.
3A hundred thousand essays
Your turnFrom Conditional probability and Bayes' rule and Random variables and distributions
Draw the essays, one in ten AI-written. rng.choice(scores, size=n, p=...) draws n scores, each as often as its bar says. Every essay gets a score from each set of bars, and keeps the one that fits who wrote it, as np.where did in prob-bayes.
Hint
p= takes one set of bars. In np.where, the first choice is for the essays where is_ai is True.
One way to do it
def draw_essays(ai_share, n=100_000, seed=0):
rng = np.random.default_rng(seed)
is_ai = rng.random(n) < ai_share
ai_score = rng.choice(scores, size=n, p=ai_bars)
people_score = rng.choice(scores, size=n, p=people_bars)
score = np.where(is_ai, ai_score, people_score)
return is_ai, score
is_ai, score = draw_essays(0.1)
flag = score >= line
print(is_ai.sum(), flag.sum())4How often each kind is flagged
Your turnFrom Conditional probability and Bayes' rule and Random variables and distributions
Count it in a smaller pile: the flags of the AI essays alone, then of the people's (~is_ai turns every True to False, and back). Then work it out from the bars: the chances past the line, added up. A score is 4 or 5, never both, so "or" adds.
Hint
Put the pile you want inside the brackets. The people's line is the AI line with the other bars.
One way to do it
caught = flag[is_ai].mean()
blamed = flag[~is_ai].mean()
caught_bars = ai_bars[scores >= line].sum()
blamed_bars = people_bars[scores >= line].sum()
print(round(caught, 3), round(caught_bars, 3))
print(round(blamed, 3), round(blamed_bars, 3))5AI-written and flagged
Your turnFrom Probability and Conditional probability and Bayes' rule
Count the essays that are both: AI-written and flagged. Then multiply. One in ten is AI-written, and of those, caught_bars get flagged.
Now try is_ai.mean() * flag.mean(): the share AI-written times the share of all essays flagged, as if one told you nothing about the other. It gives about 0.014, far from the count. The flag depends on who wrote the essay, so the two are not independent.
Hint
& keeps the essays where both are True. The second share is the one you worked out from the bars in step 4.
One way to do it
both = (is_ai & flag).mean()
multiplied = 0.1 * caught_bars
print(round(both, 4), round(multiplied, 4))6How often a flag is right
Your turnFrom Conditional probability and Bayes' rule
Now turn it around. Keep the flagged pile, and take the share of it that AI wrote. Then Bayes' rule in one line, with the bars' numbers: the right flags, over all the flags.
Hint
The right flags are AI essays times the share caught. All the flags add the people's essays times the share blamed.
One way to do it
right = is_ai[flag].mean()
bayes = caught_bars * 0.1 / (caught_bars * 0.1 + blamed_bars * 0.9)
print(round(right, 3), round(bayes, 3))7When AI essays are rare
On your ownFrom Conditional probability and Bayes' rule and Random variables and distributions
On your own now. AI essays get rarer: one in fifty. Draw again with draw_essays(0.02). Move the line from 1 up to 5, and stop at the first line where a flag is right at least 9 times in 10. Keep that line in best_line, the share of right flags there in right_there, and the share of AI essays it still catches in caught_there.
Hint
Loop over scores, and in each turn make the flags and their share that AI wrote, as in step 6. break stops the loop at the first line that is good enough.
One way to do it
rare_ai, rare_score = draw_essays(0.02)
for best_line in scores:
rare_flag = rare_score >= best_line
right_there = rare_ai[rare_flag].mean()
if right_there >= 0.9:
break
caught_there = rare_flag[rare_ai].mean()
print(best_line, round(right_there, 3), round(caught_there, 3))
print(rare_flag[rare_ai].sum(), "of", rare_ai.sum(), "AI essays caught")Run on Pyodide 314, Python 3.14, NumPy 2.4 and matplotlib 3.10. Checked on .
