forked from Open-Deep-ML/DML-OpenProblem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path182.json
More file actions
42 lines (42 loc) · 2.93 KB
/
Copy path182.json
File metadata and controls
42 lines (42 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
{
"id": "182",
"title": "Empirical Probability Mass Function (PMF)",
"difficulty": "easy",
"category": "Probability & Statistics",
"video": "",
"likes": "0",
"dislikes": "0",
"contributor": [
{
"profile_link": "https://github.com/jeetmukherjee",
"name": "jeetmukherjee"
}
],
"description": "## Problem\n\nGiven 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.",
"learn_section": "\n# Learn Section\n\n# Probability Mass Function (PMF) — Simple Explanation\n\nA **probability mass function (PMF)** describes how probabilities are assigned to the possible outcomes of a **discrete random variable**.\n\n- It tells you the chance of each specific outcome. \n- Each probability is non-negative. \n- The total of all probabilities adds up to 1.\n\n## Estimating from data\nIf the true probabilities are unknown, you can estimate them with an **empirical PMF**:\n- Count how often each outcome appears. \n- Divide by the total number of observations. \n\n## Example\nObserved sequence: `1, 2, 2, 3, 3, 3` (6 outcomes total)\n- “1” appears once → estimated probability = 1/6 \n- “2” appears twice → estimated probability = 2/6 = 1/3 \n- “3” appears three times → estimated probability = 3/6 = 1/2 \n\n\n ",
"starter_code": "def empirical_pmf(samples):\n \"\"\"\n Given an iterable of integer samples, return a list of (value, probability)\n pairs sorted by value ascending.\n \"\"\"\n # TODO: Implement the function\n pass",
"solution": "from collections import Counter\n\ndef empirical_pmf(samples):\n \"\"\"\n Given an iterable of integer samples, return a list of (value, probability)\n pairs sorted by value ascending.\n \"\"\"\n samples = list(samples)\n if not samples:\n return []\n total = len(samples)\n cnt = Counter(samples)\n result = [(k, cnt[k] / total) for k in sorted(cnt.keys())]\n return result",
"example": {
"input": "samples = [1, 2, 2, 3, 3, 3]",
"output": "[(1, 0.16666666666666666), (2, 0.3333333333333333), (3, 0.5)]",
"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."
},
"test_cases": [
{
"test": "print(empirical_pmf([1, 2, 2, 3, 3, 3]))",
"expected_output": "[(1, 0.16666666666666666), (2, 0.3333333333333333), (3, 0.5)]"
},
{
"test": "print(empirical_pmf([5, 5, 5, 5]))",
"expected_output": "[(5, 1.0)]"
},
{
"test": "print(empirical_pmf([]))",
"expected_output": "[]"
},
{
"test": "print(empirical_pmf([0, 0, 1, 1, 1, 2]))",
"expected_output": "[(0, 0.3333333333333333), (1, 0.5), (2, 0.16666666666666666)]"
}
]
}