Compute P(X≤k) for a Binomial(n, p) distribution by summing the pmf from i=0 to k. Loop over i in range(k+1), accumulate each term = C(n,i)×p^i× (1−p)^(n−i). For n=5, p=0.5, k=2: three terms (1/32, 5/32, 10/32) sum to 16/32 = 0.5 exactly. Library: scipy.stats.binom.cdf(k, n, p).

By hand

Each iteration computes one pmf term using the same math.comb formula from binomial-probability, then adds it to the running cdf. For n=5, p=0.5, k=2: i=0 → 0.03125, i=1 → 0.15625, i=2 → 0.3125. Running total: 0.03125 → 0.1875 → 0.5. The loop makes the additive structure of the CDF explicit.

naive.py
Replay: real traced execution (multi-file project)
import math
n = 5
p = 0.5
k = 2
cdf = 0.0
for i in range(k + 1):
    term = math.comb(n, i) * p ** i * (1 - p) ** (n - i)
    cdf = cdf + term
print('RESULT:', round(cdf, 6))
  1. import math

    1import math2n = 5
  2. n ← 5

    1import math2n = 53p = 0.5
    values this step5n
  3. p ← 0.5

    2n = 53p = 0.54k = 2
    values this step0.5p
  4. k ← 2

    3p = 0.54k = 25cdf = 0.0
    values this step2k
  5. cdf ← 0.0

    4k = 25cdf = 0.06for i in range(k + 1):
    values this step0.0cdf
  6. i ← 0

    5cdf = 0.06for i in range(k + 1):7    term = math.comb(n, i) * p ** i * (1 - p) ** (n - i)
    values this step0i
  7. term ← 0.03125

    6for i in range(k + 1):7    term = math.comb(n, i) * p ** i * (1 - p) ** (n - i)8    cdf = cdf + term
    values this step0.03125term
  8. cdf ← 0.03125

    7    term = math.comb(n, i) * p ** i * (1 - p) ** (n - i)8    cdf = cdf + term9print('RESULT:', round(cdf, 6))
    values this step0.0 0.03125cdf
  9. i ← 1

    5cdf = 0.06for i in range(k + 1):7    term = math.comb(n, i) * p ** i * (1 - p) ** (n - i)
    values this step0 1i
  10. term ← 0.15625

    6for i in range(k + 1):7    term = math.comb(n, i) * p ** i * (1 - p) ** (n - i)8    cdf = cdf + term
    values this step0.03125 0.15625term
  11. cdf ← 0.1875

    7    term = math.comb(n, i) * p ** i * (1 - p) ** (n - i)8    cdf = cdf + term9print('RESULT:', round(cdf, 6))
    values this step0.03125 0.1875cdf
  12. i ← 2

    5cdf = 0.06for i in range(k + 1):7    term = math.comb(n, i) * p ** i * (1 - p) ** (n - i)
    values this step1 2i
  13. term ← 0.3125

    6for i in range(k + 1):7    term = math.comb(n, i) * p ** i * (1 - p) ** (n - i)8    cdf = cdf + term
    values this step0.15625 0.3125term
  14. cdf ← 0.5

    7    term = math.comb(n, i) * p ** i * (1 - p) ** (n - i)8    cdf = cdf + term9print('RESULT:', round(cdf, 6))
    values this step0.1875 0.5cdf
  15. for i in range(k + 1):

    5cdf = 0.06for i in range(k + 1):7    term = math.comb(n, i) * p ** i * (1 - p) ** (n - i)
  16. stdout ← RESULT: 0.5

    8    cdf = cdf + term9print('RESULT:', round(cdf, 6))
    values this stepRESULT: 0.5stdout

With the library

scipy.stats.binom.cdf(k, n, p) computes the same cumulative sum. Note argument order: k first, then n and p (same as binom.pmf).

library.py
from scipy.stats import binom
from dalib.display import set_display
set_display()

n = 5
k = 2
p = 0.5
cdf = binom.cdf(k, n, p)
print('n:', n, ' k:', k, ' p:', p)
print('RESULT:', round(cdf, 6))
n: 5  k: 2  p: 0.5
RESULT: 0.5

Implementation notes

  • CDF is the sum of pmf values up to and including k: P(X≤k) = Σ P(X=i) for i=0..k. This loop explicitly shows what the CDF adds up.
  • p=0.5 and n=5 make the result exactly 0.5 by symmetry: the binomial is symmetric around n/2=2.5, so P(X≤2) = P(X≥3) = 0.5.
  • For the survival function P(X>k) use binom.sf(k, n, p) or compute 1 − cdf. For a specific interval P(a<X≤b) compute cdf(b)−cdf(a).
  • Cross-reference: binomial-probability (this chapter) for the single pmf term that each loop iteration computes.