Horvitz-Thompson Event Rate from Weighted Inspection Records
Arrays & HashingAuto-graded · 18 test casesLast updated
Estimate a population-wide event rate from samples drawn with known unequal inclusion probabilities, using inverse-probability weighting divided by the population size.
Horvitz-Thompson Event Rate from Weighted Inspection Records
A population contains population_size units. Only some of them were inspected, and each inspected unit had a known probability of being picked for inspection. You are given one record for every inspected unit, and each record says whether the event of interest occurred for that unit.
Implement a function that estimates the event rate of the whole population, meaning the expected fraction of all units in which the event occurs.
Input
population_size: an integer, the total number of units in the population (inspected or not).sampled_records: a list of records, one per inspected unit. Each record is a two-element list[inclusion_probability, event]:inclusion_probability: the probability that this unit was selected for inspection, with0 < inclusion_probability <= 1.event:1if the event occurred for this unit,0otherwise.
Task
Use inverse-probability weighting. Each inspected unit stands in for 1 / inclusion_probability units of the population. Estimate the total number of events in the population as the sum of event / inclusion_probability over all inspected records. The estimated rate is that total divided by population_size (a known denominator).
Edge cases
- If
population_size <= 0, return0.0. - If
sampled_recordsis empty, return0.0.
Return the estimate as a float.
Constraints
population_sizeis an integer.- Every
inclusion_probabilityis in(0, 1]. - Every
eventis0or1. - The denominator is the known population size, not the number of inspected records and not a sum of weights.
- Results are compared to the expected values with ordinary floating-point tolerance.
Examples
Example 1
Input: population_event_rate(1000, [[1, 1], [1, 0], [0.1, 1], [0.1, 0], [0.1, 1]])
Output: 0.021
Example 2
Input: population_event_rate(100, [[1, 1], [1, 0], [0.25, 1], [0.25, 0]])
Output: 0.05
Example 3
Input: population_event_rate(500, [[1, 1], [0.5, 1], [0.5, 0], [0.2, 1]])
Output: 0.016
Hints
Hint 1
Think about how many population units each inspected unit represents when its chance of being picked was p.
Hint 2
A unit with an event contributes 1/p to the estimated number of events; a unit without an event contributes nothing.
Hint 3
Divide the weighted event total by the known population size, not by the number of sampled records, and handle the empty or non-positive cases first.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
When units are selected with unequal probabilities, a plain average over the sample over-represents units that were easy to select. Inverse-probability weighting corrects this: a unit selected with probability p represents about 1 / p units of the population.
- Handle the edge cases: if
population_size <= 0or there are no records, return0.0. - For each record
[p, event], addevent / pto a running total. This is the estimated number of events in the whole population. - Divide the total by
population_size, which is a known constant, to get the rate.
Why it is correct
For a unit i with inclusion probability p_i, the indicator that it was inspected has expectation p_i. So (inspected_i * event_i) / p_i has expected value event_i. Summing over all units gives an unbiased estimate of the total number of events, and dividing by the fixed population size gives an unbiased estimate of the rate. Because the denominator is the known population size, there is no ratio-of-estimates bias, unlike dividing by the sum of weights.
Check on the first sample: the events are 1/1 + 1/0.1 + 1/0.1 = 21, and 21 / 1000 = 0.021.
Complexity
- Time: O(n) for n sampled records.
- Space: O(1) extra.
Reference solution
from typing import List
def population_event_rate(population_size: int, sampled_records: List[List[float]]) -> float:
if population_size <= 0 or not sampled_records:
return 0.0
estimated_events = sum(event / p for p, event in sampled_records)
return estimated_events / population_size