Lesson 10 of 15
Bernoulli & Binomial Distributions
Counting Successes
A Bernoulli() trial has two outcomes: success (probability ) and failure (probability ).
A Binomial(, ) random variable counts successes in independent Bernoulli trials:
The factor counts the number of ways to arrange successes in positions.
import math
def binom_pmf(n, p, k):
return math.comb(n, k) * p**k * (1 - p)**(n - k)
def binom_cdf(n, p, k):
return sum(binom_pmf(n, p, i) for i in range(k + 1))
# P(exactly 3 heads in 10 fair flips)
print(round(binom_pmf(10, 0.5, 3), 4)) # 0.1172
print(round(binom_cdf(10, 0.5, 3), 4)) # 0.1719
Why This Distribution Is Everywhere
- Quality control: number of defective items in a batch
- Clinical trials: number of patients who respond to treatment
- Surveys: number of yes responses out of respondents
- A/B testing: number of conversions out of visitors
Your Task
Implement binomial_full(n, p, k) that prints PMF, CDF, , and , each rounded to 4 decimal places.
Pyodide loading...
Loading...
Click "Run" to execute your code.