Implementing 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(B)P(A∩B)12345P_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
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)P(B∣A)⋅P(A)Where:
- P(A∣B) - probability of A given B (our goal);
- P(B∣A) - probability of B given A;
- P(A) - prior probability of A;
- P(B) - total probability of B.
Expanding P(B)
P(B)=P(B∣A)P(A)+P(B∣¬A)P(¬A)123456789101112P_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
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.
Q1. What does conditional probability $P(A|B)$ represent?
answer_1 = 'c'
print(f"Quiz 1 answer: {answer_1}")
✅ c) Probability of A happening given B has occurred
Q2. Which is the correct formula for conditional probability?
answer_2 = 'b'
print(f"Quiz 2 answer: {answer_2}")
✅ b) $P(A|B) = P(A \cap B) / P(B)$
Q3. If $P(A \cap B) = 0.3$ and $P(B) = 0.5$, what is $P(A|B)$?
P_A_and_B = 0.3
P_B = 0.5
P_A_given_B = P_A_and_B / P_B
print(f"Quiz 3 answer: P(A|B) = {P_A_given_B:.1f} # Correct answer: 0.6")
✅ Answer: 0.6
Q4. In Bayes’ Theorem, what does $P(B|A)$ represent?
answer_4 = 'b'
print(f"Quiz 4 answer: {answer_4}")
✅ b) Probability of B given A
Q5. What role does $P(B)$ play in Bayes’ Theorem?
answer_5 = 'c'
print(f"Quiz 5 answer: {answer_5}")
✅ c) Normalization factor
Q6. Calculate $P(B)$ given: $P(A)=0.01$, $P(B|A)=0.99$, $P(B|\neg A)=0.05$
P_A = 0.01
P_B_given_A = 0.99
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(f"Quiz 6 answer: P(B) = {P_B:.4f} # Correct answer: 0.0594")
✅ Answer: 0.0594
Q7. Using the same values, calculate $P(A|B)$
P_A_given_B = (P_B_given_A * P_A) / P_B
print(f"Quiz 7 answer: P(A|B) = {P_A_given_B:.4f} # Correct answer: 0.1672")
✅ Answer: 0.1672
Q8. Why is Bayes’ Theorem useful in real-world problems?
answer_8 = 'b'
print(f"Quiz 8 answer: {answer_8}")
✅ b) It helps update our belief about A when new evidence B is observed
---
Хочеш, я з цього файлу зроблю ще й **міні-ноутбук у стилі Jupyter** (Markdown + кодові блоки), щоб його можна було одразу запускати як інтерактивний урок?
1. Which of the following is the correct formula for conditional probability?
2. What this code will output?
3. What role does P(B)
play?
4. What this code will output?
5. Why is Bayes' Theorem useful in real-world problems like medical testing or spam filtering?
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Можеш пояснити ще раз, чому навіть при позитивному тесті ймовірність мати хворобу така низька?
Поясни, як саме працює формула Байєса на прикладі з тестом.
Можеш коротко підсумувати різницю між умовною ймовірністю та теоремою Байєса?
Awesome!
Completion rate improved to 1.89
Implementing Conditional Probability & Bayes' Theorem in Python
Sveip for å vise menyen
Conditional Probability
Conditional probability measures the chance of an event happening given another event has already occurred.
Formula:
P(A∣B)=P(B)P(A∩B)12345P_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
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)P(B∣A)⋅P(A)Where:
- P(A∣B) - probability of A given B (our goal);
- P(B∣A) - probability of B given A;
- P(A) - prior probability of A;
- P(B) - total probability of B.
Expanding P(B)
P(B)=P(B∣A)P(A)+P(B∣¬A)P(¬A)123456789101112P_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
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.
Q1. What does conditional probability $P(A|B)$ represent?
answer_1 = 'c'
print(f"Quiz 1 answer: {answer_1}")
✅ c) Probability of A happening given B has occurred
Q2. Which is the correct formula for conditional probability?
answer_2 = 'b'
print(f"Quiz 2 answer: {answer_2}")
✅ b) $P(A|B) = P(A \cap B) / P(B)$
Q3. If $P(A \cap B) = 0.3$ and $P(B) = 0.5$, what is $P(A|B)$?
P_A_and_B = 0.3
P_B = 0.5
P_A_given_B = P_A_and_B / P_B
print(f"Quiz 3 answer: P(A|B) = {P_A_given_B:.1f} # Correct answer: 0.6")
✅ Answer: 0.6
Q4. In Bayes’ Theorem, what does $P(B|A)$ represent?
answer_4 = 'b'
print(f"Quiz 4 answer: {answer_4}")
✅ b) Probability of B given A
Q5. What role does $P(B)$ play in Bayes’ Theorem?
answer_5 = 'c'
print(f"Quiz 5 answer: {answer_5}")
✅ c) Normalization factor
Q6. Calculate $P(B)$ given: $P(A)=0.01$, $P(B|A)=0.99$, $P(B|\neg A)=0.05$
P_A = 0.01
P_B_given_A = 0.99
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(f"Quiz 6 answer: P(B) = {P_B:.4f} # Correct answer: 0.0594")
✅ Answer: 0.0594
Q7. Using the same values, calculate $P(A|B)$
P_A_given_B = (P_B_given_A * P_A) / P_B
print(f"Quiz 7 answer: P(A|B) = {P_A_given_B:.4f} # Correct answer: 0.1672")
✅ Answer: 0.1672
Q8. Why is Bayes’ Theorem useful in real-world problems?
answer_8 = 'b'
print(f"Quiz 8 answer: {answer_8}")
✅ b) It helps update our belief about A when new evidence B is observed
---
Хочеш, я з цього файлу зроблю ще й **міні-ноутбук у стилі Jupyter** (Markdown + кодові блоки), щоб його можна було одразу запускати як інтерактивний урок?
1. Which of the following is the correct formula for conditional probability?
2. What this code will output?
3. What role does P(B)
play?
4. What this code will output?
5. Why is Bayes' Theorem useful in real-world problems like medical testing or spam filtering?
Takk for tilbakemeldingene dine!