Skip to content
DeepForgeby svx
All problems
MediumStatisticsst-118

Post-Stratification Weights

Problem

For each unit, return the post-stratification weight population_proportion / sample_proportion of its stratum, where the sample proportion is the unit count in that stratum divided by n. Return [] for empty input and 0.0 for a stratum missing from the population table.

Hint

Post-stratification reweights the sample to match known population margins.

Test cases

case 1
in: [["a","a","b","b","b"],{"a":0.5,"b":0.5}]
expected: [1.25,1.25,0.8333333333333334,0.8333333333333334,0.8333333333333334]
case 2
in: [["a"],{"a":1}]
expected: [1]
case 3
in: [[],{}]
expected: []
case 4
in: [["a","b"],{"a":0.25,"b":0.75}]
expected: [0.5,1.5]
case 5
in: [["x"],{}]
expected: [0]
Starter codePython
def post_stratification_weights(strata, population_proportions):
    # Your code here
    pass

5 test cases · run your code against them in the editor