Skip to content
DeepForgeby svx
All problems
MediumStatisticsst-112

Trimmed Variance

Problem

Return the population variance of the data after removing the k smallest and k largest values. Return 0.0 when data is empty or 2*k >= n. With k = 0 this is the ordinary population variance.

Hint

Sort, trim both tails, then average squared deviations.

Test cases

case 1
in: [[1,2,3,4,5,100],1]
expected: 1.25
case 2
in: [[1,2,3,4,5],0]
expected: 2
case 3
in: [[1,2,3,4],1]
expected: 0.25
case 4
in: [[1,2],1]
expected: 0
case 5
in: [[],0]
expected: 0
Starter codePython
def trimmed_variance(data, k):
    # Your code here
    pass

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