Population Size from Two-Sample Overlap
Arrays & HashingAuto-graded · 19 test casesLast 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 methodonly_second: count of items flagged exclusively by the second methodoverlap: count of items flagged by both methodspopulation_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 + overlapandsecond_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
overlapitems
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.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem applies the Lincoln-Petersen estimator, a classical method from ecology adapted to estimate hidden population sizes when two independent samples overlap.
The key reasoning:
- Each detection method captures some fraction of the hidden target population
- The overlap between methods tells us how complete each method's coverage is
- If method B recaptures a fraction R/C of method A's finds, we infer that method A found fraction M/N of the total population
Mathematically, we treat the second method as a "resample" of the first. The proportion of the second method's catches that were also caught by the first (overlap / second_total) should approximate the first method's overall capture rate (first_total / estimated_total).
Setting these equal and solving:
overlap / second_total ≈ first_total / estimated_total
estimated_total = (first_total × second_total) / overlap
The most common error is using the "only" counts directly in the formula. The estimator requires the total captured by each method, which includes the overlap.
Time Complexity: O(1) — arithmetic operations only
Space Complexity: O(1) — constant space for variables and output dictionary
from typing import Dict
def estimate_population_from_overlap(only_first: int, only_second: int, overlap: int, population_size: int) -> Dict[str, float]:
first_total = only_first + overlap
second_total = only_second + overlap
estimated_total = (first_total * second_total) / overlap
prevalence = estimated_total / population_size
return {
"estimated_total": estimated_total,
"prevalence": prevalence,
}