Law of Total Probability Practice Problem
This data science coding problem helps you practice Conditional Probability & Bayes, law of total probability, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Conditional Probability & Bayes.
- Problem ID: 236
- Problem key: 236-law-of-total-probability
- URL: https://datacrack.app/solve/236-law-of-total-probability
- Difficulty: medium
- Topic: Conditional Probability & Bayes
- Module: Probability
Problem Statement
# π§© Law of Total Probability
---
### π― Goal
Calculate the total probability of an event by adding the probabilities of the paths that lead to it.
---
### π Introduction
The multiplication rule taught you how to calculate one path:
$$
P(A\cap B)=P(B)P(A\mid B)
$$
But sometimes event $A$ can happen in more than one way.
For example, $A$ can happen when $B$ happens, or $A$ can happen when $B$ does not happen.
Those two paths are:
$$
B \rightarrow A
$$
and:
$$
B^c \rightarrow A
$$
The law of total probability adds the two path probabilities:
$$
P(A)=P(A\mid B)P(B)+P(A\mid B^c)P(B^c)
$$
This is useful when the total probability of $A$ is built from separate cases.
---
### π₯ Input
- `p_b`: probability of event $B$.
- `p_a_given_b`: probability of $A$ given $B$.
- `p_a_given_not_b`: probability of $A$ given $B^c$.
### π€ Output
- Return $P(A)$ rounded to 6 decimals.
---
### π» Task
Implement `total_probability(p_b, p_a_given_b, p_a_given_not_b)`.
Use the provided probabilities to calculate the total probability:
$$
P(A)
$$
---
### π§© Starter Code
```python
def total_probability(p_b, p_a_given_b, p_a_given_not_b):
pass
```
---
### π‘ Example
```python
total_probability(0.3, 0.8, 0.2)
```
The function should return the total probability as a decimal rounded to 6 decimals.
---
### β οΈ Common Mistakes
- Forgetting the $B^c$ path.
- Adding conditional probabilities without multiplying by their path probabilities.
- Using $P(B)$ for both paths instead of using $P(B^c)$ for the second path.
Starter Code
def total_probability(p_b, p_a_given_b, p_a_given_not_b):
pass