Binomial: Exactly k Versus At Most k Successes

Probability and StatisticsEasy+10 XP+10

Binomial: Exactly k Versus At Most k Successes

A Binomial variable counts the number of successes in n independent Bernoulli(p) trials. Its PMF gives the probability of exactly k successes; its CDF gives the probability of at most k.

P(X=k)=(nk)pk(1−p)n−kP(X = k) = \binom{n}{k} p^k (1-p)^{n-k}P(X=k)=(kn​)pk(1−p)n−k

Task: write binomial_pmf_cdf(n, p, k) returning [pmf, cdf], each rounded to 4 decimal places, where cdf = P(X \le k).

  • cdf is the running sum of the pmf from 0 up to and including k.
  • With n = 1, this collapses back to a plain Bernoulli trial.

Examples

n = 10
p = 0.5
k = 4
returns [0.2051, 0.377]
n = 5
p = 0.2
k = 2
returns [0.2048, 0.9421]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run