Binomial Coin-Bias Estimation and Overdispersion Diagnostics

Medium · Statistics & Math · Data Scientist · Technical Screen

Prompt

Work through both parts of the following probability and statistical modeling scenario:

Part 1: Estimating Coin Bias and Batch Moments

Suppose an experiment records 80 independent, identically distributed (i.i.d.) tosses of a single coin, and 56 of those tosses land Heads.

  1. Derive the Maximum Likelihood Estimate (p_hat) for the coin's probability of landing Heads.
  2. Treating p_hat as the coin's Heads probability, compute the expected count of Heads and the variance across a new sequence of 8 tosses.
  3. Name the probability distribution governing that 8-toss count, list the assumptions needed for the distribution to hold, and explain what goes wrong with estimation and uncertainty if your initial experiment had only 2 tosses instead of 80.

Part 2: Diagnosing Batch-Level Count Dispersion

Next, you inspect an observational table of 800 separate batches, where every batch records the number of Heads observed across 8 coin tosses. Across the 800 batches, the sample mean is 4.0 Heads and the sample variance is 5.5.

  1. Could a standard Binomial model plausibly generate this dataset? Support your answer mathematically using the Binomial variance identity Var(X) = n * p * (1 - p).
  2. Name the statistical phenomenon shown by this mismatch between mean and variance, explain what broken physical assumption could cause it, and propose an appropriate model adjustment.

Scope & Expectations

  • Derive p_hat directly from the log-likelihood function rather than quoting the sample fraction without proof, and distinguish a single Bernoulli trial from a Binomial sum.
  • Check all four Binomial validity conditions explicitly and analyze boundary collapse when the initial sample has only 2 tosses.
  • Prove why no probability p lets an 8-toss Binomial model reach variance 5.5, then connect the excess variation to hierarchical or correlated-trial count models.

Step-by-step walkthrough

  1. Part 1: Name the Binomial model and its four assumptions

    Start from a single toss and build up to a batch of n tosses:

    • A Bernoulli(p) trial models one coin toss with two possible outcomes: 1 (Heads) with probability p, and 0 (Tails) with probability 1 - p. Its expected value is p and its variance is p * (1 - p).
    • Adding up n such tosses gives X = X_1 + X_2 + ... + X_n, the total count of Heads in n tosses. When the tosses are independent and identically distributed (i.i.d.) — meaning no toss influences another and every toss shares the exact same Heads probability p — X follows a Binomial(n, p) distribution:
    P(X = k) = C(n, k) * p^k * (1 - p)^(n - k)     for k = 0, 1, ..., n
    E[X]     = n * p
    Var(X)   = n * p * (1 - p)
    

    where C(n, k) = n! / (k! * (n - k)!) counts how many sequences of n tosses contain exactly k Heads.

    You can remember the four assumptions required for Binomial(n, p) to be valid with BINS:

    1. Binary outcomes: Every toss lands either Heads or Tails (no edge stands or missing trials).
    2. Independent trials: Learning the outcome of one toss does not change the probability of Heads on any other toss (no streak or alternation effects).
    3. Number of trials n fixed in advance: You decide to toss n times before starting (as opposed to tossing until you see 4 Heads, which would follow a Negative Binomial model).
    4. Same probability p on every trial: The coin and tossing mechanism stay identical across all tosses (no coin wear, swapping coins, or changing tossers).
  2. Part 1: Derive the Maximum Likelihood Estimate (p_hat = 0.70)

    Maximum Likelihood Estimation (MLE) finds the parameter value p that makes the observed data (56 Heads and 24 Tails across 80 tosses) as probable as possible.

    Write the likelihood function L(p) — the probability of observing 56 Heads out of 80 tosses treated as a function of the unknown p — and take its natural logarithm so exponents turn into easy sums (because log is strictly increasing, log L(p) and L(p) peak at the exact same p):

    L(p)          = C(80, 56) * p^56 * (1 - p)^24
    log L(p)      = log C(80, 56) + 56 * log(p) + 24 * log(1 - p)
    
    Differentiate with respect to p and set to 0:
    d/dp log L(p) = 56 / p - 24 / (1 - p) = 0
    56 * (1 - p)  = 24 * p
    56 - 56 * p   = 24 * p
    56            = 80 * p
    p_hat         = 56 / 80 = 0.70
    

    To verify this critical point is a maximum rather than a minimum, check the second derivative: -56 / p^2 - 24 / (1 - p)^2 is strictly negative for every p in (0, 1) (and at the boundaries p = 0 or p = 1, L(p) = 0). In general, the Binomial MLE is always the sample proportion heads / tosses.

    In an interview, pair any point estimate with its standard error (SE) — the typical sampling wiggle of p_hat across repeated 80-toss experiments:

    import math
    
    heads = 56
    tosses = 80
    p_hat = heads / tosses
    standard_error = math.sqrt(p_hat * (1.0 - p_hat) / tosses)
    ci_low = p_hat - 1.96 * standard_error
    ci_high = p_hat + 1.96 * standard_error
    
    print(p_hat, round(standard_error, 4))          # 0.7 0.0512
    print(round(ci_low, 3), round(ci_high, 3))      # 0.6 0.8
    
  3. Part 1: Expected Heads and variance in 8 tosses (plus the 2-toss edge case)

    Now toss this coin n = 8 times, treating p = p_hat = 0.70 as the coin's Heads probability, so X ~ Binomial(8, 0.70). Because expectations always add (E[X_1 + ... + X_8] = 8 * E[X_1]) and variances add when variables are independent (Var(X_1 + ... + X_8) = 8 * Var(X_1)):

    Expected Heads:  E[X]   = n * p           = 8 * 0.70        = 5.6 Heads
    Variance:        Var(X) = n * p * (1 - p) = 8 * 0.70 * 0.30 = 1.68
    Standard dev.:   SD(X)  = sqrt(1.68)                        ≈ 1.30 Heads
    

    (As a bonus detail: 1.68 treats p = 0.70 as known without error; if you propagate the estimation uncertainty from our 80-toss sample using a uniform Beta(1, 1) prior, the predictive variance rises slightly by about 9.4% to 1.84.)

    Edge case — what if Part 1 had only n = 2 tosses? With 2 tosses, p_hat can only take three values: 0.0 (0 Heads), 0.5 (1 Head), or 1.0 (2 Heads). Even a completely unbiased coin (p = 0.5) lands Heads twice 0.5^2 = 25% of the time and Tails twice another 25% of the time — meaning half the time (50%), MLE hits a boundary (0 or 1)! If you observe 2 Heads out of 2:

    • p_hat = 1.0, so the plug-in Binomial formula predicts 8.0 Heads in 8 tosses with zero variance (8 * 1.0 * 0.0 = 0) and zero standard error (sqrt(1.0 * 0.0 / 2) = 0), claiming total certainty from two tosses!
    • How to fix tiny-sample collapse: Report an exact Clopper-Pearson 95% confidence interval ([0.025^(1/2), 1.0] = [0.158, 1.000] for 2/2 Heads) or use Bayesian smoothing (for example, Laplace's uniform Beta(1, 1) prior adds one pseudo-Head and one pseudo-Tail, pulling the estimate from 2/2 = 1.0 toward (2 + 1) / (2 + 2) = 0.75).
  4. Part 2: Why variance 5.5 is impossible for any Binomial(8, p)

    In Part 2, we observe N = 800 batches of n = 8 tosses with sample mean 4.0 and sample variance 5.5. This dataset cannot come from a standard Binomial(8, p) distribution, for three reinforcing reasons:

    1. Matching the mean breaks the variance: For Binomial(8, p), E[X] = 8 * p = 4.0 pins p = 0.5. Plugging n = 8 and p = 0.5 into Var(X) = n * p * (1 - p) gives 8 * 0.5 * 0.5 = 2.0. The observed variance 5.5 is 5.5 / 2.0 = 2.75 times too large.
    2. Universal variance ceiling for n = 8: Even if you tried changing p, note that p * (1 - p) = 0.25 - (p - 0.5)^2 <= 0.25 for every probability p in [0, 1]. Therefore, no Binomial(8, p) distribution can ever have a variance above 8 * 0.25 = 2.0!
    3. Far outside 800-batch sampling noise: Under Binomial(8, 0.5), the fourth central moment E[(X - 4)^4] is 11.0, so the sample variance across 800 independent batches has a standard error of sqrt((11.0 - 2.0^2) / 800) ≈ 0.0935. Observing 5.5 instead of 2.0 is over 37 standard errors away (equivalently, the classical dispersion statistic D = (N - 1) * s^2 / 2.0 = 799 * 5.5 / 2.0 = 2,197.25 sits enormously above its expected value of 799).
    import math
    
    num_batches = 800
    batch_size = 8
    sample_mean = 4.0
    sample_var = 5.5
    
    p_implied = sample_mean / batch_size
    binomial_var = batch_size * p_implied * (1.0 - p_implied)          # 2.0 (max possible)
    # Exact SE of s^2 under Binomial(8, 0.5), where fourth central moment mu_4 = 11.0
    mu_4 = binomial_var * (1.0 + (3 * batch_size - 6) * p_implied * (1.0 - p_implied))
    se_sample_var = math.sqrt((mu_4 - binomial_var ** 2) / num_batches)
    z_exact = (sample_var - binomial_var) / se_sample_var
    
    print(binomial_var, round(se_sample_var, 4), round(z_exact, 1))    # 2.0 0.0935 37.4
    
  5. Part 2: Diagnose overdispersion and see how broken assumptions create it

    When observed count variance exceeds the theoretical variance allowed by the assumed distribution, the phenomenon is called overdispersion (or extra-binomial variation). Note a helpful nuance: "variance > mean" is the threshold for Poisson overdispersion (where Var = mean). For a Binomial distribution, Var = n * p * (1 - p) is already smaller than mean = n * p — and here 5.5 exceeds both 2.0 and 4.0!

    Visually, both distributions center at 4.0, but overdispersion hollows out the middle and pushes far more batches into the extreme tails (0, 1, 7, 8 Heads):

    Expected batch counts across 800 batches of 8 tosses (both have mean = 4.0):
    
    Heads k :     0    1    2    3    4    5    6    7    8
    Binomial:     3   25   88  175  219  175   88   25    3   (Var = 2.0)
    Beta-Bin:    59   84   98  105  108  105   98   84   59   (Var = 5.5)
    

    Overdispersion tells us that assumption I (independence) or assumption S (same p) of BINS failed:

    • Cause A — p varies from batch to batch (heterogeneity): Different batches use coins with different biases (for example, warped coins or different mechanical flippers). If batch biases have average E[p] = mu and spread Var(p), the law of total variance (Var(X) = E[Var(X | p)] + Var(E[X | p])) simplifies to:
    Var(X) = n * mu * (1 - mu) + n * (n - 1) * Var(p)
    
    Plugging in `n = 8`, `mu = 0.5`, and `Var(X) = 5.5` gives `2.0 + 56 * Var(p) = 5.5`, so `Var(p) = 3.5 / 56 = 0.0625`. For instance, a 50/50 mix of two trick coins with `p = 0.25` and `p = 0.75` produces exact overall mean `4.0` and exact variance `5.5`!
    
    • Cause B — Positive correlation within each batch (streaks): Every coin has marginal p = 0.5, but tosses inside the same 8-toss batch tend to repeat (Cov(X_i, X_j) > 0). With common pairwise correlation rho between any two tosses in a batch:
    Var(X) = n * p * (1 - p) * (1 + (n - 1) * rho)
    
    Setting `2.0 * (1 + 7 * rho) = 5.5` yields `rho = 1.75 / 7 = 0.25`.
    
  6. Part 2: Adjust the model with a Beta-Binomial or overdispersed GLM

    To model bounded counts in {0, 1, ..., 8} with extra spread, replace the single-parameter Binomial with one of three standard extensions (after first verifying data hygiene — confirming every batch really had 8 tosses rather than mixed batch sizes!):

    1. Beta-Binomial model (primary generative fix): A two-stage hierarchical model where each batch first draws its own bias p_i ~ Beta(alpha, beta) on [0, 1] and then generates X_i | p_i ~ Binomial(n, p_i). Writing mu = alpha / (alpha + beta) for the mean probability and rho = 1 / (alpha + beta + 1) for the intra-batch correlation gives E[X] = n * mu and Var(X) = n * mu * (1 - mu) * (1 + (n - 1) * rho). Matching the sample mean (4.0) and variance (5.5) gives alpha = beta = 1.5 exactly (and in production you refine (alpha, beta) via maximum likelihood across all 800 batch counts):
    batch_size = 8
    sample_mean = 4.0
    sample_var = 5.5
    
    mu = sample_mean / batch_size                                      # 0.5
    base_var = batch_size * mu * (1.0 - mu)                            # 2.0
    rho = (sample_var / base_var - 1.0) / (batch_size - 1)             # 0.25
    concentration = 1.0 / rho - 1.0                                    # alpha + beta = 3.0
    alpha = mu * concentration
    beta = (1.0 - mu) * concentration
    
    print(round(rho, 3), round(alpha, 2), round(beta, 2))              # 0.25 1.5 1.5
    
    1. Quasi-Binomial GLM (for regression and inference): Keep the Binomial mean n * p while scaling the variance to phi * n * p * (1 - p) with estimated dispersion factor phi = 5.5 / 2.0 = 2.75. Regression coefficients stay unchanged, while standard errors are multiplied by sqrt(2.75) ≈ 1.66 so confidence intervals and p-values stop being falsely narrow.
    2. Binomial Generalized Linear Mixed Model (GLMM) or Markov model: Add a batch-level random intercept logit(p_i) = beta_0 + u_i with u_i ~ Normal(0, sigma^2) when batch covariates exist, or fit a Markov transition chain if ordered toss sequences show within-batch streaks.

Intuition & how it works

Imagine comparing two rooms where 800 people each toss a coin 8 times. In Room A, everyone flips an identical unbiased coin (p = 0.5), so almost every batch lands near the middle (2 to 6 Heads) and 0 or 8 Heads rarely occurs. In Room B, half the people secretly hold a tails-heavy coin (p = 0.25) and half hold a heads-heavy coin (p = 0.75): the room-wide average is still 4.0 Heads per batch, yet outcomes scatter heavily toward 0, 1 and 7, 8! Checking only the average makes Room A and Room B look identical; checking the variance (the average squared distance from the mean) immediately exposes the hidden mix.

A naive analyst sees an average of 4.0 Heads out of 8 tosses and blindly fits Binomial(8, 0.5) because the sample mean matches a fair coin. That fails because in a Binomial(n, p) distribution — the distribution of total successes across n independent binary trials sharing the same success probability p — a single parameter p locks both the mean (n * p) and the variance (n * p * (1 - p)). Once n = 8 is fixed, no single value of p can ever produce a variance larger than 8 * 0.5 * 0.5 = 2.0, so an observed variance of 5.5 proves the single-coin i.i.d. assumptions are broken.

Walk through the numbers across both parts step by step:

  • Part 1 (56 Heads in 80 i.i.d. tosses): Maximum Likelihood Estimation (MLE) — choosing p to maximize the probability of the observed sample — gives p_hat = 56 / 80 = 0.70. Across 8 future tosses of that coin (Binomial(8, 0.70)), expected Heads is 8 * 0.70 = 5.6 and variance is 8 * 0.70 * 0.30 = 1.68.
  • Part 2 (800 batches of 8 tosses, mean 4.0, variance 5.5): Mean 4.0 implies p = 0.5 and theoretical Binomial variance 2.0. Observed variance 5.5 is 2.75x higher (overdispersion), so we switch to a Beta-Binomial distribution — a hierarchical model where each batch draws its own p ~ Beta(1.5, 1.5) before tossing 8 times — which matches both mean 4.0 and variance 5.5.

Key points

  • Bernoulli vs. Binomial & BINS assumptions: One toss is a Bernoulli(p) variable (1 with probability p, 0 with probability 1 - p); the sum of n tosses is Binomial(n, p) provided all four BINS assumptions hold: Binary outcomes, Independent trials, Number of trials n fixed in advance, and Same p across all tosses (i.i.d.).
  • Part 1 MLE and uncertainty: Maximizing log L(p) = const + 56 * log(p) + 24 * log(1 - p) gives p_hat = 56 / 80 = 0.70, with standard error (typical sampling fluctuation of the estimator) sqrt(0.70 * 0.30 / 80) ≈ 0.0512 and approximate 95% confidence interval [0.600, 0.800].
  • Part 1 8-toss moments & 2-toss edge case: Under Binomial(8, 0.70), E[X] = 5.6 and Var(X) = 1.68 (SD ≈ 1.30). If Part 1 only had 2 tosses, p_hat could only be 0, 0.5, or 1.0, and observing 2/2 Heads (25% chance even for an unbiased coin!) collapses plug-in variance and Wald standard error to 0; use exact Clopper-Pearson intervals ([0.158, 1.0]) or a Bayesian prior (Beta(1, 1) shrinks 2/2 to 0.75).
  • Part 2 Binomial impossibility: Because p * (1 - p) <= 0.25 for all p, Binomial(8, p) variance is capped at 2.0. Across 800 batches, sample variance has a standard error of ~0.0935, so 5.5 sits over 37 standard errors above 2.0 — ruling out random noise completely.
  • Overdispersion causes and Beta-Binomial fix: Overdispersion (observed count variance exceeding the model's theoretical variance) arises either when p varies across batches (Var(X) = 2.0 + 56 * Var(p) gives Var(p) = 0.0625) or when tosses within a batch correlate positively (Var(X) = 2.0 * (1 + 7 * rho) gives rho = 0.25). Fit a Beta-Binomial(n=8, alpha=1.5, beta=1.5) model, a Quasi-Binomial GLM (phi = 2.75, inflating standard errors by sqrt(2.75) ≈ 1.66), or a mixed-effects logistic model.

Common mistakes

  • Trying to pick a different p to make Binomial(8, p) reach variance 5.5: Not realizing n * p * (1 - p) peaks at p = 0.5 (Var = 2.0) and only gets smaller as p moves toward 0 or 1.
  • Applying Poisson or unbounded Normal distributions to 8-toss counts: Counts in {0, ..., 8} have a hard ceiling at 8; a Poisson distribution assumes unbounded counts (and at mean 4.0 predicts variance 4.0, which still misses 5.5).
  • Confusing Var(X_1 + ... + X_8) with Var(8 * X_1): Summing 8 independent tosses gives 8 * p * (1 - p) = 1.68, whereas 8^2 * p * (1 - p) = 13.44 would mean flipping one coin once and multiplying the result by 8 (perfect correlation rho = 1).
  • Trusting plug-in MLE and Wald intervals at tiny sample sizes (n = 2): Reporting p_hat = 1.0 ± 0.0 after 2 Heads ignores severe boundary collapse.
  • Using naive Binomial standard errors on overdispersed batch data: Pooling 800 batches as 6,400 independent tosses understates the true standard error of the overall mean by sqrt(2.75) ≈ 1.66x, inflating false-positive rates in hypothesis tests and confidence intervals.

Follow-up questions

  • How would you run a formal statistical test to check whether the coin in Part 1 is unbiased? Set the null hypothesis H0: p = 0.5 against the two-sided alternative H1: p != 0.5 at significance level alpha = 0.05 before inspecting the outcomes. Under Binomial(80, 0.5), summing the exact upper-tail probabilities for k >= 56 and doubling for both tails gives a p-value (the probability under H0 of seeing an outcome at least as extreme as 56 Heads) of 0.00045. A normal-approximation z-test with null standard error sqrt(0.5 * 0.5 / 80) ≈ 0.0559 yields z = (0.70 - 0.50) / 0.0559 ≈ 3.58 (p = 0.00035, or 0.00053 with continuity correction); since p < 0.05, we reject H0 and conclude the coin favors Heads.
  • How does positive serial correlation between tosses (such as hot-hand streaks) change the batch variance formula? When tosses correlate, the variance of the sum includes all pairwise covariance terms: Var(X) = sum Var(X_i) + 2 * sum_{i < j} Cov(X_i, X_j). If every pair of tosses in an 8-toss batch shares positive correlation rho, variance inflates to n * p * (1 - p) * (1 + (n - 1) * rho) — for instance, rho = 0.2 at p = 0.5 lifts variance from 2.0 to 2.0 * 2.4 = 4.8 (and a Markov streak chain that repeats the previous toss 70% of the time lifts variance to ~4.11). Although the sample proportion p_hat remains unbiased when marginal p stays constant, effective sample size shrinks to roughly n / (1 + (n - 1) * rho), making naive Binomial standard errors overly optimistic.
  • In a Bayesian analysis, how does the choice of prior shape your estimate of p when only a handful of tosses are available? Placing a conjugate prior distribution Beta(alpha, beta) (representing initial beliefs before data as alpha pseudo-Heads and beta pseudo-Tails) on p produces a posterior distribution Beta(alpha + h, beta + n - h) after seeing h Heads in n tosses. Its posterior mean (alpha + h) / (alpha + beta + n) blends the prior mean and p_hat, weighting the data by n / (alpha + beta + n). With 2 Heads in 2 tosses (p_hat = 1.0), a uniform Beta(1, 1) prior pulls the estimate to 3 / 4 = 0.75 and a skeptical Beta(10, 10) prior pulls it to 12 / 22 ≈ 0.545; by n = 80 (56 Heads), the observed data dominate and both priors settle near p_hat (0.695 and 0.660 vs. 0.700).

Follow-up questions

Similar questions

More Cargurus interview questions

All 2 Cargurus questions →