Sikau

Artificial intelligence · Day 8 · about 40 min

Probability and Bayes

Update beliefs on evidence; filter spam.

A positive test

A disease affects 1 in 100 people. A test catches 90% of cases, but also flags 5% of healthy people. You test positive. How worried should you be? Most people — including many doctors in studies — say about 90%. The real answer is about 15%. Logic deals in certainties; real agents need probability to reason about what's likely.

Today you'll write Bayes' rule and then a naive Bayes spam filter — the method that, after Paul Graham's 2002 essay "A Plan for Spam", made email usable again.

Bayes' rule

Bayes' rule turns what we know — how often an effect follows a cause — into what we want — how likely the cause is given the effect. Start from the prior probability of the cause, P(disease) = 0.01. Multiply by the likelihood P(positive | disease) = 0.9. Divide by the total chance of a positive, counting both true positives and false alarms.

function bayes(prior, likelihood, falseAlarm) {
  const evidence = likelihood * prior + falseAlarm * (1 - prior);
  return (likelihood * prior) / evidence;
}
bayes(0.01, 0.9, 0.05); // ≈ 0.154

Base rates dominate: when a condition is rare, even a good test produces mostly false alarms. Testing again with an independent test is what moves the number.

The rest of this lesson

  • Naive Bayes for textexplain
  • Even oddspredict
  • Scoring a messageorder
  • The parts of Bayesmatch
  • A spam filterwrite code
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 10 lessons of Artificial intelligence