Distributions
Discrete CDF
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))
import math
1import math2n = 5n ← 5
1import math2n = 53p = 0.5values this step5np ← 0.5
2n = 53p = 0.54k = 2values this step0.5pk ← 2
3p = 0.54k = 25cdf = 0.0values this step2kcdf ← 0.0
4k = 25cdf = 0.06for i in range(k + 1):values this step0.0cdfi ← 0
5cdf = 0.06for i in range(k + 1):7 term = math.comb(n, i) * p ** i * (1 - p) ** (n - i)values this step0iterm ← 0.03125
6for i in range(k + 1):7 term = math.comb(n, i) * p ** i * (1 - p) ** (n - i)8 cdf = cdf + termvalues this step0.03125termcdf ← 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.03125cdfi ← 1
5cdf = 0.06for i in range(k + 1):7 term = math.comb(n, i) * p ** i * (1 - p) ** (n - i)values this step0 → 1iterm ← 0.15625
6for i in range(k + 1):7 term = math.comb(n, i) * p ** i * (1 - p) ** (n - i)8 cdf = cdf + termvalues this step0.03125 → 0.15625termcdf ← 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.1875cdfi ← 2
5cdf = 0.06for i in range(k + 1):7 term = math.comb(n, i) * p ** i * (1 - p) ** (n - i)values this step1 → 2iterm ← 0.3125
6for i in range(k + 1):7 term = math.comb(n, i) * p ** i * (1 - p) ** (n - i)8 cdf = cdf + termvalues this step0.15625 → 0.3125termcdf ← 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.5cdffor 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)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 compute1 − cdf. For a specific interval P(a<X≤b) computecdf(b)−cdf(a). - Cross-reference:
binomial-probability(this chapter) for the single pmf term that each loop iteration computes.