Quant interview toolkit · coverage and sampling

Why the last missing market state takes so long to appear

A market-data test needs at least one example of each of ten message types. The first few types appear almost immediately. Nine are present before lunch. The team assumes the tenth will arrive just as quickly. Coupon collector mathematics explains why the final missing type is often the slowest part of the job, even when all types are equally common.

Decompose the wait into stages

Imagine independent observations, each equally likely to be any of mm types. After kk distinct types have been seen, a new observation is a previously unseen type with probability (m−k)/m(m-k)/m. The number of observations needed to advance from kk types to k+1k+1 has a geometric distribution with mean m/(m−k)m/(m-k). Add these stage waits from zero observed types through m−1m-1:

E[Tm]=∑k=0m−1mm−k=m∑j=1m1j=mHm.E[T_m] =\sum_{k=0}^{m-1}\frac{m}{m-k} =m\sum_{j=1}^{m}\frac1j =mH_m.

HmH_m is the mmth harmonic number. For ten equally likely message types, H10≈2.929H_{10}\approx2.929, so expected full coverage is about 29.3 observations. That is nearly three times the naive ten-observation minimum. The final stage alone, moving from nine types to ten, has success probability 1/101/10 each observation and expected wait ten. If the type you lack is genuinely rare, the wait can be much longer.

The expectation does not say that a sample of 30 is guaranteed to contain every type. At the moment you have one type missing, the probability of still missing it after another rr independent observations is (1−1/m)r(1-1/m)^r. For m=10m=10, after another 20 observations that probability is 0.920≈12.2%0.9^{20}\approx12.2\%. Long waits are a natural feature of the tail.

Coverage at a fixed sample size

If you collect exactly nn observations, let IjI_j indicate that type jj has appeared. Type jj is absent with probability (1−1/m)n(1-1/m)^n, so linearity of expectation gives

E[distinct types after n]=∑j=1mE[Ij]=m[1−(1−1m)n].E[\text{distinct types after }n] =\sum_{j=1}^m E[I_j] =m\left[1-\left(1-\frac1m\right)^n\right].

At m=10,n=20m=10,n=20, this is 10(1−0.920)≈8.7810(1-0.9^{20})\approx8.78 types. This statement is easier than the full distribution of coverage, and it is often sufficient for planning an exploratory dataset. It also explains a common visual impression: many types appear early, then progress slows. The expected number missing after nn is m(1−1/m)nm(1-1/m)^n.

To bound the chance that at least one type is still missing, use a union bound over the mm missing-type events:

P(some type missing after n)≤m(1−1m)n≤me−n/m.P(\text{some type missing after }n) \le m\left(1-\frac1m\right)^n \le m e^{-n/m}.

This is conservative because missing-type events are not independent, but it gives a simple sample-size target. To make the upper bound at most δ\delta, it is sufficient to take n≥mlog⁡(m/δ)n\ge m\log(m/\delta). For ten types and a 5% target, this bound asks for about 53 observations. It is a guarantee under the equal-probability independent model, not an exact 95th percentile.

Unequal types change the question

Market states and message types are rarely equiprobable. If type jj has probability pjp_j, its probability of being absent after nn independent draws is (1−pj)n(1-p_j)^n, and the expected number of distinct types is ∑j[1−(1−pj)n]\sum_j[1-(1-p_j)^n]. A type with pj=0.001p_j=0.001 has expected waiting time 1,000 draws for its first appearance by itself. The equal-type formula mHmmH_m should not be used to promise complete coverage when one category is much rarer than the rest.

Random sampling may not even be the right strategy for a test suite. If you can deliberately construct a valid example of each type, stratified collection can cover the taxonomy much faster. Random arrivals remain relevant when you need to know how long a passive live monitor might wait before it has seen all cases. The distinction is between coverage by design and coverage by chance.

Define the categories before counting

In a real data audit, classification rules matter. Are a cancel and a cancel-replace one type or two? Does a message count if required fields are corrupted? Do simultaneous messages in one market event count as independent draws? The formula assumes a fixed list of mutually exclusive categories and independent observations from an unchanging distribution. Bursty activity, market hours and regime shifts violate that assumption. A dataset can also contain all ten types but only one example of a crucial rare type; presence is a much weaker requirement than estimating that type’s behavior precisely.

For the related problem of estimating a rare failure rate after zero observed failures, see the rule of three. Coupon collector asks how long until every category appears; rare-event inference asks what an absence tells us about its rate. They are two views of the same thin-data problem.

One sighting is not a test sample

Suppose the tenth message type finally appears on observation 29. The collector has achieved nominal coverage, but a parser tested once on that type has almost no evidence about how it handles variations of the message. If a message type contains optional fields, different order sizes or several venue-specific encodings, “seen once” is an especially weak criterion. A coverage plan should distinguish a first sighting, a minimum number of examples, and a target confidence for an error rate.

For a rare type with appearance probability pjp_j, the number of examples in nn independent draws is binomial with mean npjnp_j. If pj=0.001p_j=0.001, even 1,000 draws give only one expected example, and the probability of zero examples is (0.999)1000≈36.8%(0.999)^{1000}\approx36.8\%. Counting total feed messages without stratifying by type can conceal how little evidence exists for the cases the test most needs. Deliberate sampling is often cheaper than waiting for the passive collector to provide adequate rare-category coverage.

Watch coverage slow as the rarest gaps remain

Every observation is an independent draw from equally likely types. The curve is expected coverage, not a guarantee of full coverage.

Practice

Last categoryWith 20 equally likely message types and exactly one still unseen, how many additional independent messages do you expect to wait?
Show answer

The missing type appears with probability 1/201/20 on each draw. The geometric mean waiting time is 20 additional observations.

Expected coverageAfter 20 independent draws from 20 equally likely types, how many types do you expect to have seen?
Show answer

20[1−(19/20)20]≈12.8320[1-(19/20)^{20}]\approx12.83. Twenty draws do not imply twenty distinct categories because repeats consume observations.