Weighted Mean, Variance and Survey Weights

easy
Unit 2 · Statistics II0/3 to milestone

Survey respondents and logged events often carry weights: one respondent may represent 500 people, another 2,000. Unweighted averages then give the wrong answer.

Implement:

  • weighted_mean(x, w): ∑wixi/∑wi\sum w_i x_i / \sum w_i
  • weighted_var(x, w): the population-style weighted variance ∑wi(xi−xˉw)2/∑wi\sum w_i (x_i - \bar x_w)^2 / \sum w_i
  • effective_sample_size(w): Kish's formula (∑w)2/∑w2(\sum w)^2 / \sum w^2. Uneven weights mean less information than the raw count suggests.

Raise ValueError for negative weights, mismatched lengths, or weights that sum to 0.

#statistics#surveys#numpy2 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Weighted Mean, Variance and Survey Weights · TechGradify