Conditional Independence Practice Problem
This data science coding problem helps you practice Independence, conditional independence, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Independence.
- Problem ID: 239
- Problem key: 239-conditional-independence
- URL: https://datacrack.app/solve/239-conditional-independence
- Difficulty: medium
- Topic: Independence
- Module: Probability
Problem Statement
# 🧩 Conditional Independence
---
### 🎯 Goal
Determine whether two events are independent after restricting attention to a third event.
---
### 📖 Introduction
Ordinary independence asks whether $A$ and $B$ are independent in the full sample space.
Conditional independence asks a more specific question:
```text
After we only look inside C, are A and B independent within that smaller group?
```
So we do not test the whole sample space first.
We first restrict attention to event $C$.
Inside $C$, the independence rule becomes:
$$
P(A\cap B \mid C)=P(A\mid C)P(B\mid C)
$$
This means $A$ and $B$ are conditionally independent given $C$ if their actual overlap inside $C$ matches the overlap we would expect from multiplying their conditional probabilities.
This is different from ordinary independence because every probability is calculated **inside C only**.
---
### 📥 Input
- `sample_space`: a list of all possible outcomes.
- `event_a`: a list of outcomes in event $A$.
- `event_b`: a list of outcomes in event $B$.
- `event_c`: a list of outcomes in event $C$.
### 📤 Output
Return `True` if $A$ and $B$ are conditionally independent given $C$, otherwise return `False`.
---
### 💻 Task
Implement `test_conditional_independence(sample_space, event_a, event_b, event_c)`.
Restrict the sample space to event $C$, then determine whether $A$ and $B$ are independent within that restricted group.
Return `True` if they are conditionally independent given $C$, otherwise return `False`.
---
### 🧩 Starter Code
```python
def test_conditional_independence(sample_space, event_a, event_b, event_c):
# Convert the events to sets so we can restrict and intersect them.
event_a_set = set(event_a)
event_b_set = set(event_b)
event_c_set = set(event_c)
```
---
### 💡 Example
```python
sample_space = [1, 2, 3, 4, 5, 6, 7, 8]
event_a = [1, 2, 5, 6]
event_b = [1, 3, 5, 7]
event_c = [1, 2, 3, 4]
test_conditional_independence(sample_space, event_a, event_b, event_c)
```
Expected output:
```python
True
```
---
### ⚠️ Common Mistakes
- Testing independence in the full sample space instead of inside $C$.
- Counting outcomes outside $C$ when calculating conditional probabilities.
- Forgetting to find the outcomes that are in $A$, $B$, and $C$ at the same time.
- Returning calculated probabilities instead of only returning `True` or `False`.
Starter Code
def test_conditional_independence(sample_space, event_a, event_b, event_c):
# Convert the events to sets so we can restrict and intersect them.
event_a_set = set(event_a)
event_b_set = set(event_b)
event_c_set = set(event_c)