Skip to content
DeepForgeby svx
All problems
EasyML Fundamentalsml-298

Effective Sample Size for Importance Weights

Problem

Compute the effective sample size of a weight vector: (sum(w))^2 / sum(w^2).

Return 0.0 when the sum of squares is 0 or the input is empty.

Hint

Unequal weights reduce the effective sample size below the raw count.

Test cases

case 1
in: [[1,1,1,1]]
expected: 4
case 2
in: [[1,2,3]]
expected: 2.5714285714285716
case 3
in: [[0,0]]
expected: 0
case 4
in: [[]]
expected: 0
Starter codePython
def effective_sample_size(weights):
    # Your code here
    pass

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