0Pricing
Learn AI with Python · Lesson

Probability and Bayes Theorem

Joint/conditional probability, independence, Bayes theorem with real AI examples.

Probability and Bayes Theorem is a free Learn AI with Python lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn AI with Python learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Probability Basics

A probability is a number from 0 to 1 measuring how likely an event is. Conditional probability P(A|B) is the chance of A given that B happened.

Conditional Probability

P(A|B) = P(A and B) / P(B). Conditioning narrows the sample space to the cases where B is true.

p_a_and_b = 0.12
p_b = 0.30
print(p_a_and_b / p_b)   # P(A | B) = 0.4

Bayes Theorem

Bayes theorem reverses a conditional: P(A|B) = P(B|A) * P(A) / P(B). It updates a prior belief P(A) into a posterior after observing evidence B.

Naming the Pieces

Prior P(A): belief before evidence. Likelihood P(B|A): how well A explains the evidence. Posterior P(A|B): updated belief. Evidence P(B): a normalizing total.

Law of Total Probability

The denominator P(B) is found by summing over all cases: P(B) = P(B|A)*P(A) + P(B|not A)*P(not A). This expands the evidence into known parts.

p_a = 0.01            # prior
p_b_given_a = 0.99   # likelihood
p_b_given_not_a = 0.05
p_not_a = 1 - p_a
p_b = p_b_given_a * p_a + p_b_given_not_a * p_not_a
print(p_b)

Medical Test Example

A disease affects 1 percent of people. A test is 99 percent sensitive but has a 5 percent false-positive rate. What is the chance you are sick GIVEN a positive test?

posterior = (p_b_given_a * p_a) / p_b
print(round(posterior, 3))   # about 0.167

The Surprising Result

Despite a 99 percent accurate test, a positive result means only a 17 percent chance of disease. The rare base rate (1 percent prior) dominates, the classic base-rate fallacy.

Independence

Two events are independent when P(A|B) = P(A), so observing B tells you nothing about A. Then P(A and B) = P(A) * P(B).

p_a, p_b = 0.5, 0.5
print(p_a * p_b)   # 0.25 if independent (e.g. two fair coins)

The Monty Hall Problem

Three doors hide one car. You pick one; the host opens a different door revealing a goat, then offers a switch. Counterintuitively, switching wins 2/3 of the time.

Simulating Monty Hall

A simulation confirms the math. Switching wins whenever your first pick was wrong, which happens 2 out of 3 times.

import random
def trial(switch):
    car = random.randint(0, 2)
    pick = random.randint(0, 2)
    if switch:
        return pick != car   # switching wins if first pick was wrong
    return pick == car

wins = sum(trial(True) for _ in range(100000))
print(wins / 100000)   # about 0.667

Why Switching Wins

Your initial pick is right 1/3 of the time. The host's reveal concentrates the remaining 2/3 onto the single unopened door, so switching captures that 2/3 probability.

Quick Check

Test your probability reasoning.

Recap

Probability essentials:

  • Conditional: P(A|B) = P(A and B)/P(B)
  • Bayes: P(A|B) = P(B|A)P(A)/P(B) updates prior to posterior
  • Law of total probability expands P(B)
  • Base-rate fallacy: rare conditions yield low posteriors despite accurate tests
  • Monty Hall: switching wins 2/3 of the time

Frequently asked questions

Is the “Probability and Bayes Theorem” lesson free?

Yes — the full text of “Probability and Bayes Theorem” is free to read here on the web, and the Learn AI with Python course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn AI with Python course, upgrade to CoddyKit PRO.

What will I learn in “Probability and Bayes Theorem”?

Joint/conditional probability, independence, Bayes theorem with real AI examples. You practise Learn AI with Python with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn AI with Python?

No prior experience is required. Learn AI with Python on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Probability and Bayes Theorem” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn AI with Python lesson?

Yes. Every Learn AI with Python lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Descriptive Statistics and Distributions
  2. Probability and Bayes Theorem
  3. Hypothesis Testing
  4. Correlation and Covariance
← Back to Learn AI with Python