All problems
James-Stein Estimator
Problem
Return the James-Stein shrunken means grand + max(0, 1 - (k-3)*sigma_sq/(n*sum((m-grand)^2)))*(m-grand) for k group means. When k < 3, n <= 0, or all means equal, return the original means unchanged.
Hint
The shrinkage factor is clamped at zero so estimates never overshoot the grand mean.
Test cases
case 1
in: [[12,10,8],10,4,10]
expected: [12,10,8]
case 2
in: [[12,11,9,8],10,4,10]
expected: [11.92,10.96,9.04,8.08]
case 3
in: [[15,10,5],10,1,10]
expected: [15,10,5]
case 4
in: [[20,0,10,10],10,100,1]
expected: [15,5,10,10]
case 5
in: [[20,0,10,10],10,100,0.1]
expected: [10,10,10,10]
Starter codePython
def james_stein(means, grand_mean, sigma_sq, n):
# Your code here
pass5 test cases · run your code against them in the editor