Skip to content
DeepForgeby svx
All problems
HardProbabilitypr-229

Effective Sample Size

Problem

Effective sample size of a weight vector after normalizing:

ESS = 1 / sum(w_i^2).

Return 0.0 for an empty vector or one with zero total weight.

Hint

ESS is maximal for uniform weights and drops toward 1 for degenerate ones.

Test cases

case 1
in: [[0.25,0.25,0.25,0.25]]
expected: 4
case 2
in: [[1]]
expected: 1
case 3
in: [[0.5,0.5]]
expected: 2
case 4
in: [[0.9,0.1]]
expected: 1.2195121951219512
case 5
in: [[0,0]]
expected: 0
Starter codePython
def effective_sample_size(weights):
    # Your code here
    pass

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