Why Scaling Sample Singletons by 10 Overcounts Rare Queries
Statistics & ExperimentsModel answerLast updated
Explain the bias of scaling sample singleton counts from a 10% log sample, then build, stress-test and simulate better estimators of the true number of once-only queries.
A search engine stores one row per query submission each day. You are given a 10% simple random sample of rows, drawn without replacement, from one day's log, and you want to know how many distinct query strings occurred exactly once in the full day's log. Call these population singletons.
A colleague proposes the shortcut: count the query strings that occur exactly once in the sample and multiply by 10.
Answer the following.
a) Show that the shortcut is biased. State which direction the bias goes and give an intuitive explanation.
b) Describe a better estimator. Relate the sample frequency-of-frequencies counts (the number of distinct queries seen exactly k times in the sample) to the corresponding population counts under binomial thinning. Then propose at least one concrete estimator, such as a Good-Turing/Good-Toulmin style correction, a Chao-type estimator, or a Poisson/negative-binomial mixture fit.
c) Explain how you would compute standard errors or confidence intervals (for example the delta method and the bootstrap), and how you would detect that your model is misspecified when query frequencies are heavy-tailed.
d) Outline a simulation study to compare candidate estimators across realistic traffic distributions.
Aim for a structured answer with formulas where helpful, and be explicit about the trade-offs.
Hints
Hint 1
Let F_j be the number of distinct queries occurring exactly j times in the full log. A query with j occurrences shows up exactly once in the sample with probability j·p·(1−p)^(j−1). What does the expectation of 10·f_1 look like when you sum this over all j?
Hint 2
Under binomial thinning, E[f_k] is a triangular linear function of the F_j. Inverting it exactly gives an alternating series with coefficients growing like 9^k. Think about why that is unbiased yet useless, and what regularization (damping the tail, or a parametric mixture) buys you.
Hint 3
For uncertainty, a naive row-level bootstrap of the sample creates fake duplicates. Think about resampling distinct queries or using a parametric bootstrap from the fitted model. For simulation, vary the tail heaviness (Zipf exponent), the number of distinct queries and the sampling rate, and report bias, RMSE and CI coverage.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Setup
- F_j: number of distinct queries appearing exactly j times in the full log. The target is F_1.
- f_k: number of distinct queries appearing exactly k times in the sample.
- p = 0.1, q = 0.9.
Sampling rows without replacement from a large log is well approximated by independent binomial thinning: a query with population count j has sample count K ~ Bin(j, p). Then
E[f_k] = Σ_{j≥k} F_j · C(j,k) · p^k · q^(j−k).
In matrix form, E[f] = A·F with A upper triangular.
a) Bias of the shortcut
For k = 1,
E[10·f_1] = (1/p) Σ_j F_j · j·p·q^(j−1) = F_1 + Σ_{j≥2} j·q^(j−1)·F_j.
Population singletons contribute exactly F_1, so that part is unbiased. But every query with j ≥ 2 occurrences also has a chance j·p·q^(j−1) of appearing once in the sample, and each such query adds positive mass. The bias is therefore always upward.
The weight j·0.9^(j−1) is 1.8 at j = 2, about 2.4 at j = 3, 3.3 at j = 5, and peaks near 3.9 around j ≈ 9–10. Example: with F_1 = 1M, F_2 = 300k, F_3 = 150k, the shortcut gives roughly 1M + 540k + 365k ≈ 1.9M, almost double the truth.
Intuition: thinning pushes every query down the frequency ladder. A query seen a few times today will usually show up at most once in a 10% sample, so it masquerades as unique. The shortcut treats every sample singleton as a population singleton.
b) A better estimator
Exact inversion (unbiased, unusable)
With G(z) = Σ F_j z^j and H(z) = Σ f_k z^k, we have E[H(z)] = G(q + pz). Inverting gives
F̂_1 = (1/p) · H′(−q/p) = 10 · (f_1 − 18·f_2 + 243·f_3 − …).
This is the Good-Toulmin extrapolation with t = q/p = 9. It is unbiased, but the coefficients alternate and grow like 9^k, so the variance explodes. Stability needs t ≲ 1, i.e. p ≥ 0.5. Intuitively, F_1 includes singletons that never appeared in the sample, so this is an extrapolation and needs regularization.
Option 1: smoothed Good-Toulmin
Damp the tail by multiplying the k-th term by P(L ≥ k), where L is a random truncation point (Poisson or binomial, tuned by cross-validation or a held-out split of the sample). The result is a linear estimator Σ w_k f_k with bounded weights, trading a small bias for a large variance reduction. Guarantees exist for moderate t (roughly up to log n), so at t = 9 it is usable but should be cross-checked.
Option 2: parametric mixture
Model each distinct query's population count J as drawn from a zero-truncated distribution π_j(θ), with D distinct queries in total:
E[f_k] = D · Σ_j π_j(θ) · C(j,k) · p^k · q^(j−k).
Under Poissonization, a query has rate λ ~ G, J ~ Poisson(λ) and K ~ Poisson(pλ). With Gamma mixing both counts are negative binomial with the same shape, which gives closed forms.
Steps:
- Fit θ by maximum likelihood on the observed f_k (k ≥ 1) using the zero-truncated multinomial likelihood.
- Estimate D̂ = S_obs / (1 − P̂(K = 0)), where S_obs is the number of distinct queries in the sample.
- Report F̂_1 = D̂ · π_1(θ̂).
Search traffic is heavy-tailed, so a single negative binomial usually underfits. Prefer a Poisson-lognormal, a finite mixture of NBs fitted by EM, or a Zipf-Mandelbrot family.
Option 3: Chao-style robustness
Chao-type estimators (for example unseen ≈ f_1²/(2·f_2)) use only low-order counts. Analogously, fit the mixture only on k ≤ K (say 10-20) and treat head queries as nuisance. Population singletons come from small rates, so the head carries little information about F_1 but causes most of the misfit.
Recommendation
Report the smoothed Good-Toulmin estimate and the truncated-mixture estimate side by side. Disagreement is itself a diagnostic.
c) Uncertainty and diagnostics
Delta method
- Linear estimators Σ w_k f_k: Var ≈ Σ w_k² · f_k, treating the f_k as roughly Poisson (use the multinomial covariance if conditioning on S_obs).
- Parametric estimators D̂·π_1(θ̂): Var ≈ ∇gᵀ Σ̂ ∇g, where Σ̂ combines the inverse Fisher information of θ̂ with the variance of D̂.
- Build intervals on the log scale so they stay positive and are asymmetric.
Bootstrap
- Do not resample rows of the sample naively: duplicates create fake doubletons and distort f_2, f_3 and so on.
- Better: a parametric bootstrap (simulate f from the fitted model, refit, recompute F̂_1), or a multinomial/Poisson resample of the frequency-of-frequencies vector.
- A subsampling scheme (draw 10% of the 10% and extrapolate with t = 1 to check the method's behaviour) also works as a self-consistency test.
Misspecification diagnostics
- Compare observed f_k with fitted E[f_k] (Pearson residuals, deviance); look for systematic misfit in the tail.
- Sensitivity to truncation K and to the mixing family: if F̂_1 moves a lot, do not trust any single value.
- Hold-out check: split the sample into 5% and 5%, predict the number of new singletons from one half to the other, and compare.
- Heavy tails make the variance of linear estimators dominated by a few large f_k, and make the NB fit chase the head; watch for unstable θ̂ and wide profile likelihoods.
d) Simulation plan
- Generate populations with known F_1: Zipf with exponents between roughly 0.8 and 1.5, Poisson-lognormal, Gamma-Poisson, and a spliced head/tail model fitted to a real log. Vary total rows (10^6 to 10^9) and distinct queries.
- Sample at p = 0.1 without replacement (and also at 0.02, 0.05, 0.3 to see how t = q/p matters).
- Estimators: shortcut (baseline), exact inversion, smoothed Good-Toulmin with several truncation rules, NB mixture, truncated mixtures, Chao-style.
- Metrics over hundreds of replications: bias, RMSE and relative error, CI coverage and width, failure or non-convergence rate, runtime.
- Stress tests: model misspecification (generate from a mixture the fitter does not contain), injected bot traffic with a huge head, and day-of-week or burst effects that violate independence between rows.
- Decision rule: choose the estimator with the best RMSE and acceptable coverage across scenarios, and keep a second estimator as a cross-check in production.
Common mistakes
- Multiplying by 10 and calling it done, or assuming the bias is small because singletons "dominate".
- Using exact inversion and ignoring its variance.
- Resampling rows for the bootstrap.
- Fitting a single NB to a heavy-tailed head and trusting the extrapolated F_1.
- Reporting a symmetric CI that can go negative.