KatInterview

Correcting Sampling Bias When Counting Children Per Household

Arrays & HashingAuto-graded · 16 test cases
EasyAuto-gradedArrays & Hashingprobabilitysampling-biasweighted-estimation

Last 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 where survey_counts[i] is the number of children surveyed who reported living in a household of size family_sizes[i].
  • family_sizes — a list of positive integers listing the distinct household sizes (each at least 1), in the same order as survey_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) <= 100
  • 1 <= family_sizes[i] <= 1000 for all i
  • 1 <= survey_counts[i] <= 10^6 for all i
  • All values in family_sizes are distinct.
  • target_size is guaranteed to appear exactly once in family_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.