Testing Independence Practice Problem
This data science coding problem helps you practice Independence, testing independence, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Independence.
- Problem ID: 238
- Problem key: 238-testing-independence
- URL: https://datacrack.app/solve/238-testing-independence
- Difficulty: easy
- Topic: Independence
- Module: Probability
Problem Statement
# 🧩 Testing Independence
---
### 🎯 Goal
Determine whether two events are independent by calculating their probabilities from a sample space.
---
### 📖 Introduction
In the previous problem, you learned that if two events are independent, then:
$$
P(A\cap B)=P(A)P(B)
$$
This gives us a test.
But in real problems, we are not always given $P(A)$, $P(B)$, and $P(A\cap B)$ directly.
Sometimes we are given:
- the full sample space
- event $A$
- event $B$
Then we calculate the probabilities ourselves and check whether the independence rule is true.
---
### 📥 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$.
### 📤 Output
Return `True` if the events are independent, otherwise return `False`.
---
### 💻 Task
Implement `test_independence(sample_space, event_a, event_b)`.
Use the sample space and events to determine whether $A$ and $B$ are independent.
Return `True` if they are independent, otherwise return `False`.
---
### 🧩 Starter Code
```python
def test_independence(sample_space, event_a, event_b):
"""
Test whether two events are independent using outcomes from a sample space.
"""
# Convert the events to sets so we can find their shared outcomes.
event_a_set = set(event_a)
event_b_set = set(event_b)
pass
```
---
### 💡 Example
```python
sample_space = [1, 2, 3, 4, 5, 6]
event_a = [2, 4, 6]
event_b = [4, 5, 6]
test_independence(sample_space, event_a, event_b)
```
Expected output:
```python
False
```
---
### ⚠️ Common Mistakes
- Comparing the number of outcomes in $A$ and $B$ instead of comparing probabilities.
- Forgetting to find the shared outcomes in both events.
- Thinking overlapping events are automatically independent.
- Forgetting that the expected overlap under independence is $P(A)P(B)$.
Starter Code
def test_independence(sample_space, event_a, event_b):
"""
Test whether two events are independent using outcomes from a sample space.
"""
# Convert the events to sets so we can find their shared outcomes.
event_a_set = set(event_a)
event_b_set = set(event_b)
pass