Correcting Sampling Bias When Counting Children Per Household
Arrays & HashingAuto-graded · 16 test casesLast updated
Tests whether a candidate can identify and reverse length-biased sampling to recover true household-level proportions from child-level survey data.
Background
Imagine a researcher walks around a playground and asks children one question: "How many kids live in your house?" The responses look reasonable at first glance, but there is a hidden problem: households with more children send more kids to the playground, so they show up in the data more often. Correcting for this over-representation is necessary before making any claim about the distribution of household sizes across the town.
Task
You are given survey data collected from children. Each child reported how many children live in their household. The data is summarized as a list of counts paired with the corresponding household sizes.
Your goal is to estimate the true probability that a uniformly random household contains exactly target_size children, after correcting for the fact that larger households contributed more respondents to the survey.
How the correction works
A household with k children is k times as likely to contribute a respondent to a child-level survey as a single-child household. To recover the household-level distribution, divide each observed count by the corresponding household size. This gives a value proportional to the number of households of that size in the population. Sum all such adjusted values, then divide the target size's adjusted value by that sum to obtain the estimated probability.
More precisely: let w[i] = survey_counts[i] / family_sizes[i] for each index i. The estimated probability for target_size is:
sum of w[i] for all i where family_sizes[i] == target_size
──────────────────────────────────────────────────────────
sum of w[i] for all i
Inputs
survey_counts— a list of integers wheresurvey_counts[i]is the number of children surveyed who reported living in a household of sizefamily_sizes[i].family_sizes— a list of positive integers listing the distinct household sizes (each at least 1), in the same order assurvey_counts.target_size— an integer representing the household size whose probability you want to estimate.
Output
Return a single float: the estimated probability that a randomly chosen household has exactly target_size children.
Constraints
1 <= len(survey_counts) == len(family_sizes) <= 1001 <= family_sizes[i] <= 1000for alli1 <= survey_counts[i] <= 10^6for alli- All values in
family_sizesare distinct. target_sizeis guaranteed to appear exactly once infamily_sizes.- Every household has at least one child (no childless households exist).
- Children are assumed to be sampled uniformly at random from all children in the population.
- The return value is a single float computed by the formula above; no rounding or special tie-breaking is needed.
Examples
Example 1
Input: estimate_household_probability([50, 20, 30], [1, 2, 3], 1)
Output: 0.7142857142857143
Hints
Hint 1
A family with k children appears in a child-level survey roughly k times as often as a single-child family — think about what you need to divide by to undo that effect.
Hint 2
After dividing each count by its household size, you have numbers proportional to how many households of each type exist. These are your 'household weights'.
Hint 3
Once you have the household weights, finding the probability for a specific size is just that size's weight divided by the sum of all weights — a straightforward normalization step.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The core insight is that sampling children is not the same as sampling households. A household with k children is k times more likely to place a respondent in a child-level survey than a household with 1 child. As a result, the raw survey proportions reflect the child-weighted distribution, not the household distribution.
To reverse this bias, apply inverse-probability weighting: divide each observed count by the corresponding household size. The resulting values are proportional to how many households of each type exist in the population — specifically, count[i] / size[i] estimates the relative number of households with size[i] children.
Once you have these household weights, normalize them to get probabilities. The estimated probability for target_size is that size's household weight divided by the sum of all household weights.
Why this is correct
Suppose there are n_k households of size k. In a child-uniform sample of N children, the expected number of respondents from size-k households is N * (k * n_k) / total_children. Dividing the observed count by k removes the factor of k, leaving a quantity proportional to n_k. Normalizing across all sizes recovers the household probability.
Worked example: survey_counts = [50, 20, 30], family_sizes = [1, 2, 3].
| Size | Count | Count / Size (household weight) |
|---|---|---|
| 1 | 50 | 50 |
| 2 | 20 | 10 |
| 3 | 30 | 10 |
| — | — | Total = 70 |
P(size = 1) = 50 / 70 = 5/7 ≈ 0.714.
The naive answer (50/100 = 0.5) is wrong because it treats a child sample as a household sample — the classic length-biased sampling trap.
Complexity
- Time: O(n), where n is the length of
family_sizes. One pass to compute weights, one pass to sum them, one pass to find the target. - Space: O(n) for the list of household weights.
from typing import List
def estimate_household_probability(survey_counts: List[int], family_sizes: List[int], target_size: int) -> float:
# Dividing each count by household size corrects for length-biased sampling.
household_weights = [count / size for count, size in zip(survey_counts, family_sizes)]
total_weight = sum(household_weights)
target_weight = sum(
weight
for weight, size in zip(household_weights, family_sizes)
if size == target_size
)
return target_weight / total_weight