Probability from a Sample Space Practice Problem
This data science coding problem helps you practice Probability Fundamentals, probability from a sample space, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Probability Fundamentals.
- Problem ID: 219
- Problem key: 219-probability-from-a-sample-space
- URL: https://datacrack.app/solve/219-probability-from-a-sample-space
- Difficulty: easy
- Topic: Probability Fundamentals
- Module: Probability
Problem Statement
# 🧩 Probability from a Sample Space
---
### 🎯 Goal
Calculate the probability of an event when every outcome in the sample space is equally likely.
---
### 📖 Introduction
A **random experiment** is something where the result is uncertain before it happens.
Examples:
- rolling a die
- flipping a coin
- drawing one card
Each possible result is called an **outcome**.
The set of **all possible outcomes** is called the **sample space**, written as:
$$
\Omega
$$
For example, when rolling one fair six-sided die, the only possible outcomes are 1, 2, 3, 4, 5, and 6. Therefore, the sample space is:
$$
\Omega = \{1,2,3,4,5,6\}
$$
An **event** is a group of outcomes from the sample space that match something we are interested in.
For example, the event "roll an even number" contains all even outcomes:
$$
A = \{2,4,6\}
$$
If all outcomes are equally likely, then:
$$
P(A)=\frac{|A|}{|\Omega|}
$$
---
### 🔍 Explanation of Symbols
| Symbol | Meaning |
|:--:|:--|
| $\Omega$ | sample space, the set of all possible outcomes |
| $A$ | event, the set of favorable outcomes |
| $\lvert A\rvert$ | number of outcomes in event $A$ |
| $\lvert \Omega\rvert$ | total number of outcomes in the sample space |
| $P(A)$ | probability that event $A$ happens |
---
### 📥 Input
- `sample_space`: a non-empty set containing all possible outcomes.
- `event`: a set of favorable outcomes. It must be a subset of `sample_space`.
### 📤 Output
- Return the probability of the event rounded to 6 decimals.
---
### 💻 Task
Implement `event_probability(sample_space, event)`.
Your function should:
1. Count the outcomes in `event`.
2. Count the outcomes in `sample_space`.
3. Calculate:
$$
P(A)=\frac{|A|}{|\Omega|}
$$
4. Return the probability rounded to 6 decimals.
---
### 🧩 Starter Code
```python
def event_probability(sample_space, event):
pass
```
---
### 💡 Example
```python
event_probability(
sample_space={1, 2, 3, 4, 5, 6},
event={2, 4, 6}
)
```
Expected output:
```python
0.5
```
---
### ⚠️ Common Mistakes
- Dividing by the size of the event instead of the sample space.
- Providing an event containing outcomes outside the sample space. By definition, an event must be a subset of the sample space.
- Forgetting that probability is favorable outcomes divided by all possible outcomes.
Starter Code
def event_probability(sample_space, event):
pass