Normalize Raw Frequencies Into a Valid PMF

Basics
BasicsPMFDistributionsGoogleMetaJane Street

Problem

Implement normalize_pmf(counts) converting a list of non-negative frequency counts (with sum(counts) > 0) into a valid Probability Mass Function summing to 1.0.

Why It Matters: Normalizing raw histogram or token counts into a PMF is the foundation of empirical distributions, n-gram language models, and categorical sampling.

Function Signature

def normalize_pmf(counts: list[int]) -> list[float]:

Sample Input & Output

Example 1: Die Roll Frequencies

Input:

counts = [2, 3, 5]

Expected Output:

[0.2, 0.3, 0.5]

Explanation: Total is 10, so probabilities are [2/10, 3/10, 5/10] = [0.2, 0.3, 0.5].

Example 2: Including Zero-Count Buckets

Input:

counts = [0, 4, 0, 4]

Expected Output:

[0.0, 0.5, 0.0, 0.5]

Explanation: Zero-count outcomes map cleanly to 0.0 while total normalizes to [0.0, 0.5, 0.0, 0.5].

Constraints & Edge Cases

  • Return a list of floats summing to 1.0.
  • Preserve exact list order and length.
  • Every entry lies in [0.0, 1.0] and sums to 1.0.

Constraints & Evaluation Criteria

  • Return a list of floats summing to `1.0`.

Hints

Python 3
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
[2, 3, 5]
Expected Output / Criteria
[0.2, 0.3, 0.5]