Conditional Probability from a Sample Space Practice Problem
This data science coding problem helps you practice Conditional Probability & Bayes, conditional probability from a sample space, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Conditional Probability & Bayes.
- Problem ID: 231
- Problem key: 231-conditional-probability-from-a-sample-space
- URL: https://datacrack.app/solve/231-conditional-probability-from-a-sample-space
- Difficulty: easy
- Topic: Conditional Probability & Bayes
- Module: Probability
Problem Statement
# 🧩 Conditional Probability from a Sample Space
---
### 🎯 Goal
Calculate a conditional probability by restricting the sample space to the event that is already known to have happened.
---
### 📖 Introduction
In Probability Fundamentals, you calculated probability by comparing an event to the full sample space:
$$
P(A)=\frac{|A|}{|\Omega|}
$$
Conditional probability changes the question.
When we write:
$$
P(A \mid B)
$$
we read it as:
```text
probability of A given B
```
The phrase **given B** means we already know event $B$ happened.
So we no longer compare $A$ to the full sample space. We only look inside $B$.
That means:
```text
Among the outcomes inside B, how many are also inside A?
```
---
### 📥 Input
- `sample_space`: all possible outcomes.
- `event_a`: outcomes in event $A$.
- `event_b`: outcomes in event $B$.
Assume `event_b` is not empty.
### 📤 Output
- Return $P(A \mid B)$ rounded to 6 decimals.
---
### 💻 Task
Implement `conditional_probability_from_sample_space(sample_space, event_a, event_b)`.
Use the provided sample space and events to calculate:
$$
P(A \mid B)
$$
---
### 🧩 Starter Code
```python
def conditional_probability_from_sample_space(sample_space, event_a, event_b):
sample_space = set(sample_space)
event_a = set(event_a)
event_b = set(event_b)
pass
```
---
### 💡 Example
```python
conditional_probability_from_sample_space(
sample_space=[1, 2, 3, 4, 5, 6],
event_a=[2, 4, 6],
event_b=[4, 5, 6]
)
```
The function should return the conditional probability as a decimal rounded to 6 decimals.
---
### ⚠️ Common Mistakes
- Dividing by the full sample space instead of dividing by the size of $B$.
- Counting all outcomes in $A$ instead of only the outcomes that are in both $A$ and $B$.
- Reading $P(A \mid B)$ as if $A$ happened first. It means $B$ is the condition we are restricting to.
Starter Code
def conditional_probability_from_sample_space(sample_space, event_a, event_b):
sample_space = set(sample_space)
event_a = set(event_a)
event_b = set(event_b)
pass