Log inStart free
LESSON 4.03 · 6 STAGES · 3 CONTROLS · 3 CHECKS

Sampling and selection bias

Watch who gets into the data bend one simple average four ways.


EXPLAINER · 4.03 · SAMPLE BAY
01
Stage 1 of 6 · All customers
Each bar is one customer's stay in months. The true mean of all 24 customers is 12 months.
Explain it simply

Each bar is one customer. Its length shows how many months they stayed before they left. Filled bars are heavy users, and hollow bars are light users. Heavy users stay 18 months on average, light users 6. The dashed line marks the true mean of all 24 customers: 12 months.

24 bars, one per customer, from 1 to 30 months long. Light users average 6 months and heavy users 18. The true mean is 12 months.

stay: filled heavy user, hollow light usertrue mean, todayestimate from the databias, skipped or cut staynot in the data
Toy: 24 customers, one joins each month · light users stay 6 months on average, heavy users 18 · fixed shares, so every mean is exact
HOVER OR TAP A SHAPE TO SEE ITS NUMBERS
light 1 in 4 · heavy 3 in 4
How the survey reaches the customers.
month 24
The month when the company reads its data.
months per exit
How to count a stay that has not ended.
STAGE 1 / 6
MATH · STAGE 1HOVER A SYMBOL OR A SHAPE
μ=1N∑i=1NLi\sy{mu}{param}{\mu} = \frac{1}{\sy{n}{data}{N}} \sum_{i=1}^{N} \sy{L}{data}{L_i}
μ=28824=12\mu = \frac{288}{24} = 12

The true mean adds every stay and divides by all 24 customers.

Show the code ▾Hide the code ▴ · Try it yourself · Python 3
light = [14, 11, 9, 7, 2, 4, 8, 5, 2, 3, 1, 6]      # stays in months, mean 6
heavy = [13, 27, 19, 16, 11, 21, 9, 15, 30, 6, 26, 23]  # stays in months, mean 18

# (kind, join month, stay, k): customer 2k is light, 2k + 1 heavy
customers = []
for k in range(12):
    customers += [('light', 2 * k, light[k], k), ('heavy', 2 * k + 1, heavy[k], k)]

def mean(xs):
    return sum(xs) / len(xs)

print(mean([c[2] for c in customers]))               # 12.0  the true mean

reach = {'light': 1, 'heavy': 3}                     # in-app survey: quarters of each group it reaches
first = {1: [0], 2: [0, 2], 3: [0, 1, 2]}
asked = [c for c in customers if c[3] % 4 in first[reach[c[0]]]]
print(mean([c[2] for c in asked]))                   # 15.0  the sample mean

def weighted(rows):                                  # each answer counts 1/p times
    w = [4 / reach[c[0]] for c in rows]
    return sum(wi * c[2] for wi, c in zip(w, rows)) / sum(w)

print(round(weighted(asked), 2))                     # 12.0  the weights undo the sampling bias
answers = [c for c in asked if c[2] > 3]             # leavers within 3 months skip the survey
print(round(weighted(answers), 2))                   # 17.0  selection bias: the weights cannot fix it

today = 24                                           # the month the company reads its data
still = [c for c in customers if c[2] > today - c[1]]
print(len(still), round(mean([c[2] for c in still]), 2)) # 8 19.25  survivors only

seen = [min(c[2], today - c[1]) for c in customers]  # censored at today
exits = [c[2] <= today - c[1] for c in customers]
print(sum(seen), sum(exits))                         # 195 16
print(round(sum(seen) / len(seen), 2))               # 8.12  count as ended
print(round(mean([t for t, e in zip(seen, exits) if e]), 2)) # 8.38  drop them
print(round(sum(seen) / sum(exits), 2))              # 12.19  months per exit

Copy it into a file or a Python shell and run it. Change reach and today to match the controls.

Common mix-ups

MYTHToday's customers show how long a customer stays.

FIXToday's table holds only survivors. The short stays left first, so the 8 survivors average 19.3 months, not 12.

MYTHDropping the unfinished stays removes the censoring problem.

FIXDropping them removes the longest stays, so the mean falls to 8.4 months. Months per exit gives 12.2.

Predict, then reveal
0 / 3 REVEALED
Q01Which survey gives a sample mean below the true 12 months?
1 · GUESS2 · TRY IT IN THE DIAGRAM
Q02If today moves to month 36, does counting unfinished stays as ended get closer to 12?
1 · GUESS2 · TRY IT IN THE DIAGRAM
Q03Which way of counting unfinished stays lands nearest the true mean?
1 · GUESS2 · TRY IT IN THE DIAGRAM

Takeaways

  1. If you know how a sample was drawn, weights undo sampling bias.
  2. If the outcome decides who is in the data, weights cannot fix it.
  3. Count a censored stay's months, but never count it as an exit.