Bayes’ Theorem Practice Problem
This data science coding problem helps you practice Conditional Probability & Bayes, bayes’ theorem, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Conditional Probability & Bayes.
- Problem ID: 235
- Problem key: 235-bayes-theorem
- URL: https://datacrack.app/solve/235-bayes-theorem
- Difficulty: medium
- Topic: Conditional Probability & Bayes
- Module: Probability
Problem Statement
# 🧩 Bayes’ Theorem
---
### 🎯 Goal
Use Bayes’ theorem to reverse a conditional probability.
---
### 📖 Introduction
Sometimes we know:
$$
P(B \mid A)
$$
but we want:
$$
P(A \mid B)
$$
These are not the same direction.
Bayes’ theorem gives a way to reverse the conditioning:
$$
P(A\mid B)=\frac{P(B\mid A)P(A)}{P(B)}
$$
The pieces have names:
- **Prior**: $P(A)$, what we believed before seeing $B$.
- **Likelihood**: $P(B\mid A)$, how likely the evidence is if $A$ is true.
- **Evidence**: $P(B)$, how common the evidence is overall.
- **Posterior**: $P(A\mid B)$, the updated probability after seeing $B$.
In this problem, $P(B)$ is given directly.
---
### 📥 Input
- `p_a`: prior probability $P(A)$.
- `p_b_given_a`: likelihood $P(B\mid A)$.
- `p_b`: evidence probability $P(B)$.
Assume `p_b` is greater than 0.
### 📤 Output
- Return the posterior probability $P(A\mid B)$ rounded to 6 decimals.
---
### 💻 Task
Implement `bayes_theorem(p_a, p_b_given_a, p_b)`.
Your function should calculate:
$$
P(A\mid B)=\frac{P(B\mid A)P(A)}{P(B)}
$$
---
### 🧩 Starter Code
```python
def bayes_theorem(p_a, p_b_given_a, p_b):
pass
```
---
### 💡 Example
```python
bayes_theorem(0.3, 0.8, 0.38)
```
The function should return the posterior probability as a decimal rounded to 6 decimals.
---
### ⚠️ Common Mistakes
- Confusing $P(B\mid A)$ with $P(A\mid B)$.
- Forgetting to multiply the likelihood by the prior.
- Treating the evidence $P(B)$ as optional.
Starter Code
def bayes_theorem(p_a, p_b_given_a, p_b):
pass