Binomial Probability P(X = k) in n Trials

Basics
BasicsBinomialDistributionsGoogleMetaJane Street

Problem

Implement binomial_pmf(n, k, p) returning the exact Binomial probability P(X=k)=(nk)pk(1−p)n−kP(X = k) = \binom{n}{k} p^k (1 - p)^{n - k} of observing k successes in n independent Bernoulli(p) trials.

Why It Matters: The Binomial distribution models click counts, conversion counts, defect counts, and coin-flip hypotheses across fixed trial sizes.

Function Signature

import math

def binomial_pmf(n: int, k: int, p: float) -> float:

Sample Input & Output

Example 1: Exactly 2 Heads in 4 Fair Coin Flips

Input:

n = 4, k = 2, p = 0.5

Expected Output:

0.375

Explanation: C(4, 2) * 0.5^2 * 0.5^2 = 6 * 0.0625 = 0.375.

Example 2: Zero Successes (k = 0)

Input:

n = 3, k = 0, p = 0.2

Expected Output:

0.512

Explanation: C(3, 0) * 1 * 0.8^3 = 0.512.

Constraints & Edge Cases

  • Return exact probability as a float.
  • Use math.comb(n, k) for exact binomial coefficients.
  • Guaranteed 0 <= k <= n and 0.0 <= p <= 1.0.

Constraints & Evaluation Criteria

  • Return exact probability as a float.

Hints

Python 3
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
n=4, k=2, p=0.5
Expected Output / Criteria
0.375