Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Implementing Conditional Probability & Bayes' Theorem in Python | Probability & Statistics
Mathematics for Data Science

bookImplementing Conditional Probability & Bayes' Theorem in Python

Conditional Probability

Conditional probability measures the chance of an event happening given another event has already occurred.

Formula:

P(A∣B)=P(A∩B)P(B)P(A \mid B) = \frac{P(A \cap B)}{P(B)}
12345
P_A_and_B = 0.1 # Probability late AND raining P_B = 0.2 # Probability raining P_A_given_B = P_A_and_B / P_B print(f"P(A|B) = {P_A_given_B:.2f}") # Output: 0.5
copy

Interpretation: if it is raining, there's a 50% chance you will be late to work.

Bayes' Theorem

Bayes' Theorem helps us find $P(A|B)$ when it's hard to measure directly, by relating it to $P(B|A)$ which is often easier to estimate.

Formula:

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

Where:

  • P(A∣B)P(A \mid B) - probability of A given B (our goal);
  • P(B∣A)P(B \mid A) - probability of B given A;
  • P(A)P(A) - prior probability of A;
  • P(B)P(B) - total probability of B.

Expanding P(B)P(B)

P(B)=P(B∣A)P(A)+P(B∣¬A)P(¬A)P(B) = P(B \mid A) P(A) + P(B \mid \neg A) P(\neg A)
123456789101112
P_A = 0.01 # Disease prevalence P_not_A = 1 - P_A P_B_given_A = 0.99 # Sensitivity P_B_given_not_A = 0.05 # False positive rate # Total probability of testing positive P_B = (P_B_given_A * P_A) + (P_B_given_not_A * P_not_A) print(f"P(B) = {P_B:.4f}") # Output: 0.0594 # Apply Bayes’ Theorem P_A_given_B = (P_B_given_A * P_A) / P_B print(f"P(A|B) = {P_A_given_B:.4f}") # Output: 0.1672
copy

Interpretation: Even if you test positive, there is only about a 16.7% chance you actually have the disease.

Key Takeaways

  • Conditional probability finds the chance of A happening when we know B occurred;
  • Bayes' Theorem flips conditional probabilities to update beliefs when direct measurement is difficult;
  • Both are essential in data science, medical testing, and machine learning.
question mark

What this code will output?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 4

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain the difference between joint, marginal, and conditional probability?

How does Bayes' theorem help in real-world scenarios like medical testing?

Can you walk me through the calculations in the code examples step by step?

Awesome!

Completion rate improved to 1.96

bookImplementing Conditional Probability & Bayes' Theorem in Python

Swipe to show menu

Conditional Probability

Conditional probability measures the chance of an event happening given another event has already occurred.

Formula:

P(A∣B)=P(A∩B)P(B)P(A \mid B) = \frac{P(A \cap B)}{P(B)}
12345
P_A_and_B = 0.1 # Probability late AND raining P_B = 0.2 # Probability raining P_A_given_B = P_A_and_B / P_B print(f"P(A|B) = {P_A_given_B:.2f}") # Output: 0.5
copy

Interpretation: if it is raining, there's a 50% chance you will be late to work.

Bayes' Theorem

Bayes' Theorem helps us find $P(A|B)$ when it's hard to measure directly, by relating it to $P(B|A)$ which is often easier to estimate.

Formula:

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

Where:

  • P(A∣B)P(A \mid B) - probability of A given B (our goal);
  • P(B∣A)P(B \mid A) - probability of B given A;
  • P(A)P(A) - prior probability of A;
  • P(B)P(B) - total probability of B.

Expanding P(B)P(B)

P(B)=P(B∣A)P(A)+P(B∣¬A)P(¬A)P(B) = P(B \mid A) P(A) + P(B \mid \neg A) P(\neg A)
123456789101112
P_A = 0.01 # Disease prevalence P_not_A = 1 - P_A P_B_given_A = 0.99 # Sensitivity P_B_given_not_A = 0.05 # False positive rate # Total probability of testing positive P_B = (P_B_given_A * P_A) + (P_B_given_not_A * P_not_A) print(f"P(B) = {P_B:.4f}") # Output: 0.0594 # Apply Bayes’ Theorem P_A_given_B = (P_B_given_A * P_A) / P_B print(f"P(A|B) = {P_A_given_B:.4f}") # Output: 0.1672
copy

Interpretation: Even if you test positive, there is only about a 16.7% chance you actually have the disease.

Key Takeaways

  • Conditional probability finds the chance of A happening when we know B occurred;
  • Bayes' Theorem flips conditional probabilities to update beliefs when direct measurement is difficult;
  • Both are essential in data science, medical testing, and machine learning.
question mark

What this code will output?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 4
some-alt