Skip to content
DeepForgeby svx
All problems
MediumStatisticsst-119

Raking One Step

Problem

Perform one multiplicative raking update on a 2D table of counts: w_ij = counts_ij * (row_target_i / row_margin_i) * (col_target_j / col_margin_j). Return a zeros table when the targets do not match the shape or any row or column margin is zero, and [] for an empty table.

Hint

One raking step rescales rows and columns simultaneously.

Test cases

case 1
in: [[[10,20],[30,40]],[40,60],[50,50]]
expected: [[16.666666666666664,22.22222222222222],[32.14285714285714,28.571428571428573]]
case 2
in: [[[5,5],[5,5]],[10,10],[10,10]]
expected: [[5,5],[5,5]]
case 3
in: [[],[10],[10]]
expected: []
case 4
in: [[[1,2],[3,4]],[0,10],[5,5]]
expected: [[0,0],[5.357142857142857,4.761904761904762]]
case 5
in: [[[1,2]],[3],[1,5]]
expected: [[1,5]]
Starter codePython
def raking_one_step(counts, row_targets, col_targets):
    # Your code here
    pass

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