Section 2 · Lesson 2

The binomial distribution

Model the number of conversions out of n visitors with scipy.stats.binom, and avoid the classic "at least" off-by-one.

35 min 3 practice tasks
The Binomial Distribution and Test, Clearly Explained!!! by StatQuest with Josh Starmer on YouTube

Every visitor to a landing page is a tiny yes/no experiment: convert or not. The binomial distribution answers the natural follow-up: out of n visitors, how many conversions should I expect, and how much will that number bounce around by pure chance?

When does it apply?

The count of successes X follows a binomial distribution when:

  1. each trial has two outcomes (convert / don’t),
  2. the number of trials n is fixed in advance,
  3. every trial has the same success probability p,
  4. trials are independent (one visitor’s choice doesn’t affect another’s).

The formula

P(X = k) = C(n, k) * p**k * (1 - p)**(n - k)
C(n, k)  = n! / (k! * (n - k)!)      # ways to pick which k trials succeed

mean     = n * p
variance = n * p * (1 - p)
sd       = sqrt(n * p * (1 - p))

p**k * (1 - p)**(n - k) is the probability of one specific sequence with k successes; C(n, k) counts how many such sequences exist.

In Python with SciPy

scipy.stats has an object for each common distribution, with the same method names:

Call Meaning
binom.pmf(k, n, p) P(X = k), the “probability mass function”
binom.cdf(k, n, p) P(X ≤ k), cumulative, includes k
binom.sf(k, n, p) P(X > k) = 1 - cdf(k), “survival function”, excludes k
binom.mean(n, p), binom.std(n, p) mean and SD
from scipy import stats

n, p = 20, 0.10                  # 20 visitors, 10% conversion rate
stats.binom.pmf(0, n, p)         # P(no conversions) ≈ 0.12
stats.binom.cdf(3, n, p)         # P(3 or fewer)
stats.binom.sf(4, n, p)          # P(5 or more), note the 4!
stats.binom.mean(n, p)           # 2.0

The off-by-one is the classic bug: “at least k” is sf(k - 1), because sf(k) starts counting above k. For whole-number distributions, always ask yourself whether k itself should be in.

Reading the numbers

With 20 visitors and a 10% rate, getting zero conversions happens about 12% of the time. That is not rare at all. Small samples produce very noisy conversion counts, and the binomial SD tells you how noisy: the SD of the rate (X / n) is sqrt(p * (1 - p) / n), which shrinks only with the square root of n.

In the real world

  • Don’t kill campaigns too early. A campaign with 40 clicks and no conversions at an expected 5% rate has a 0.95^40 ≈ 13% chance of that result even if nothing is wrong.
  • Sanity-check experiments. If a test was meant to split traffic 50/50, the number of users in B should be binomial with p = 0.5. A wildly unlikely split (“sample ratio mismatch”) usually means a tracking or assignment bug.
  • Watch the assumptions. Repeat visitors, bot traffic or a mid-test price change all break “independent, same p”. Then the binomial understates how variable your counts really are.
  • The last task previews hypothesis testing: “if nothing changed, how likely is a result this extreme?” That question is the heart of section 4.

Practice

Write your answer, press Run to see results and Check answer to grade it. Shortcuts: Ctrl/⌘ + Enter run, Shift + Enter check.

1

Exactly 5, at most 2

A small campaign sends n = 50 visitors to a page that converts with probability p = 0.10. Assume visitors convert independently. With scipy.stats.binom, create (each rounded to 4 decimals):

  • p_exactly_5: P(X = 5), exactly 5 conversions
  • p_at_most_2: P(X ≤ 2), at most 2 conversions
Need a hint?
  1. stats.binom.pmf(k, n, p) is P(X = k).
  2. stats.binom.cdf(k, n, p) is P(X ≤ k), so it includes k itself.
Python 3
from scipy import stats

n, p = 50, 0.10

p_exactly_5 = None
p_at_most_2 = None
print(p_exactly_5, p_at_most_2)
Files in the working directory
ab_test.csv (4,000 rows)
user_id, variant, device, time_on_page_sec, converted, revenue
daily_stats.csv (1,561 rows)
campaign_id, date, impressions, clicks, spend, conversions, revenue
orders.csv (900 rows)
id, customer_id, campaign_id, order_date, amount, status
campaigns.csv (40 rows)
id, advertiser_id, name, channel, objective, start_date, end_date, daily_budget, status
2

At least 25 conversions?

A campaign gets n = 200 clicks and each click converts with probability p = 0.08. Create:

  • expected: the expected number of conversions, n × p
  • sd: the standard deviation of the number of conversions, sqrt(n × p × (1 - p)),

rounded to 2 decimals

  • p_at_least_25: P(X ≥ 25), rounded to 4 decimals

Careful: binom.sf(k, n, p) is P(X > k), strictly greater than k.

Need a hint?
  1. stats.binom.mean(n, p) and stats.binom.std(n, p) give the same mean and SD as the formulas.
  2. P(X ≥ 25) = P(X > 24) = stats.binom.sf(24, n, p), which equals 1 - stats.binom.cdf(24, n, p).
Python 3
import math
from scipy import stats

n, p = 200, 0.08

expected = 0
sd = 0
p_at_least_25 = stats.binom.sf(25, n, p)  # is this "at least 25"?
print(expected, sd, p_at_least_25)
Files in the working directory
ab_test.csv (4,000 rows)
user_id, variant, device, time_on_page_sec, converted, revenue
daily_stats.csv (1,561 rows)
campaign_id, date, impressions, clicks, spend, conversions, revenue
orders.csv (900 rows)
id, customer_id, campaign_id, order_date, amount, status
campaigns.csv (40 rows)
id, advertiser_id, name, channel, objective, start_date, end_date, daily_budget, status
3

Could variant B's result be luck?

In ab_test.csv, suppose variant B's true conversion rate were the same as variant A's observed rate. How surprising would B's result be?

  • rate_a: A's conversion rate (mean of converted for variant "A")
  • n_b: the number of users in variant B
  • conv_b: the number of conversions in variant B
  • p_as_extreme: P(X ≥ conv_b) for X ~ Binomial(n_b, rate_a), rounded to 6 decimals

(This treats A's rate as exactly known, which is a simplification. Section 4 does it properly.)

Need a hint?
  1. len(b) counts rows; b["converted"].sum() counts conversions.
  2. P(X ≥ k) = stats.binom.sf(k - 1, n, p).
Python 3
import pandas as pd
from scipy import stats

ab = pd.read_csv("ab_test.csv")
a = ab[ab["variant"] == "A"]
b = ab[ab["variant"] == "B"]

rate_a = None
n_b = None
conv_b = None
p_as_extreme = None
print(rate_a, n_b, conv_b, p_as_extreme)
Files in the working directory
ab_test.csv (4,000 rows)
user_id, variant, device, time_on_page_sec, converted, revenue
daily_stats.csv (1,561 rows)
campaign_id, date, impressions, clicks, spend, conversions, revenue
orders.csv (900 rows)
id, customer_id, campaign_id, order_date, amount, status
campaigns.csv (40 rows)
id, advertiser_id, name, channel, objective, start_date, end_date, daily_budget, status