KatInterview

Population Size from Two-Sample Overlap

Arrays & HashingAuto-graded · 19 test cases
MediumAuto-gradedArrays & Hashingstatisticsestimationprobability

Last updated

Apply the capture-recapture estimator to infer total population size from overlapping detection counts.

You are analyzing detection data from two independent screening methods applied to the same hidden population. Each method flags a subset of items, and some items are flagged by both.

You are given:

  • only_first: count of items flagged exclusively by the first method
  • only_second: count of items flagged exclusively by the second method
  • overlap: count of items flagged by both methods
  • population_size: the known total size of the universe from which items are drawn

Your task is to estimate the total number of items in the hidden target category using the Lincoln-Petersen estimator, then compute the prevalence rate (the proportion of the universe belonging to the target category).

Return a dictionary with two keys:

  • "estimated_total": the estimated total count in the target category (float)
  • "prevalence": the estimated proportion of the universe in the target category (float)

Calculation details:

  • First compute the total flagged by each method: first_total = only_first + overlap and second_total = only_second + overlap
  • The Lincoln-Petersen estimate is: estimated_total = (first_total × second_total) / overlap
  • Prevalence is: prevalence = estimated_total / population_size

Constraints

  • 0 ≤ only_first, only_second ≤ 10⁶
  • 1 ≤ overlap ≤ 10⁶ (overlap is always at least 1)
  • 1 ≤ population_size ≤ 10⁹
  • The two methods together flag at least overlap items

Examples

Example 1

Input: estimate_population_from_overlap(800, 600, 200, 1000000)

Output: {'estimated_total': 4000, 'prevalence': 0.004}

Example 2

Input: estimate_population_from_overlap(100, 80, 20, 500000)

Output: {'estimated_total': 600, 'prevalence': 0.0012}

Example 3

Input: estimate_population_from_overlap(0, 50, 10, 10000)

Output: {'estimated_total': 60, 'prevalence': 0.006}

Hints

Hint 1

The core insight is that the overlap rate observed in one method's sample should reflect the overall capture rate of the other method across the entire hidden population.

Hint 2

Calculate the total captured by each method first—don't use the 'only' counts directly in the formula.

Hint 3

The estimator assumes both methods sample the hidden population independently and with uniform probability.