Skip to content

Commit 6e7ade2

Browse files
committed
Added PMF problem
1 parent 9af7f66 commit 6e7ade2

File tree

9 files changed

+86
-0
lines changed

9 files changed

+86
-0
lines changed

.DS_Store

2 KB
Binary file not shown.

old_repo/Problems/.DS_Store

0 Bytes
Binary file not shown.

questions/182_PMF/description.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Problem
2+
3+
Given a list of integer samples drawn from a discrete distribution, implement a function to compute the empirical Probability Mass Function (PMF). The function should return a list of `(value, probability)` pairs sorted by the value in ascending order. If the input is empty, return an empty list.

questions/182_PMF/example.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"input": "samples = [1, 2, 2, 3, 3, 3]",
3+
"output": "[(1, 0.16666666666666666), (2, 0.3333333333333333), (3, 0.5)]",
4+
"reasoning": "Counts are {1:1, 2:2, 3:3} over 6 samples, so probabilities are 1/6, 2/6, and 3/6 respectively, returned sorted by value."
5+
}

questions/182_PMF/learn.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
# Learn Section
3+
4+
# Probability Mass Function (PMF) — Simple Explanation
5+
6+
A **probability mass function (PMF)** describes how probabilities are assigned to the possible outcomes of a **discrete random variable**.
7+
8+
- It tells you the chance of each specific outcome.
9+
- Each probability is non-negative.
10+
- The total of all probabilities adds up to 1.
11+
12+
## Estimating from data
13+
If the true probabilities are unknown, you can estimate them with an **empirical PMF**:
14+
- Count how often each outcome appears.
15+
- Divide by the total number of observations.
16+
17+
## Example
18+
Observed sequence: `1, 2, 2, 3, 3, 3` (6 outcomes total)
19+
- “1” appears once → estimated probability = 1/6
20+
- “2” appears twice → estimated probability = 2/6 = 1/3
21+
- “3” appears three times → estimated probability = 3/6 = 1/2
22+
23+
24+

questions/182_PMF/meta.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"id": "182",
3+
"title": "Empirical Probability Mass Function (PMF)",
4+
"difficulty": "easy",
5+
"category": "Probability & Statistics",
6+
"video": "",
7+
"likes": "0",
8+
"dislikes": "0",
9+
"contributor": [
10+
{
11+
"profile_link": "https://github.com/jeetmukherjee",
12+
"name": "jeetmukherjee"
13+
}
14+
]
15+
}

questions/182_PMF/solution.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from collections import Counter
2+
3+
def empirical_pmf(samples):
4+
"""
5+
Given an iterable of integer samples, return a list of (value, probability)
6+
pairs sorted by value ascending.
7+
"""
8+
samples = list(samples)
9+
if not samples:
10+
return []
11+
total = len(samples)
12+
cnt = Counter(samples)
13+
result = [(k, cnt[k] / total) for k in sorted(cnt.keys())]
14+
return result

questions/182_PMF/starter_code.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def empirical_pmf(samples):
2+
"""
3+
Given an iterable of integer samples, return a list of (value, probability)
4+
pairs sorted by value ascending.
5+
"""
6+
# TODO: Implement the function
7+
pass

questions/182_PMF/tests.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[
2+
{
3+
"test": "print(empirical_pmf([1, 2, 2, 3, 3, 3]))",
4+
"expected_output": "[(1, 0.16666666666666666), (2, 0.3333333333333333), (3, 0.5)]"
5+
},
6+
{
7+
"test": "print(empirical_pmf([5, 5, 5, 5]))",
8+
"expected_output": "[(5, 1.0)]"
9+
},
10+
{
11+
"test": "print(empirical_pmf([]))",
12+
"expected_output": "[]"
13+
},
14+
{
15+
"test": "print(empirical_pmf([0, 0, 1, 1, 1, 2]))",
16+
"expected_output": "[(0, 0.3333333333333333), (1, 0.5), (2, 0.16666666666666666)]"
17+
}
18+
]

0 commit comments

Comments
 (0)