Skip to content
DeepForgeby svx
All problems
MediumStatisticsst-128

Sandwich Standard Error

Problem

Return the HC0 sandwich standard error of a simple regression slope: sqrt(sum(xc_i^2 e_i^2)) / Sxx with xc = x - mean(x). Return 0.0 when n < 3, lengths differ, or x has zero variance.

Hint

The squared residuals make the variance robust to heteroscedasticity.

Test cases

case 1
in: [[1,2,3,4],[1,-1,1,-1]]
expected: 0.447213595499958
case 2
in: [[1,2,3,4,5],[1,1,1,1,1]]
expected: 0.31622776601683794
case 3
in: [[1,2,3],[0,0,0]]
expected: 0
case 4
in: [[1,1,2],[1,2,3]]
expected: 3.2015621187164247
case 5
in: [[1,2],[1,1]]
expected: 0
Starter codePython
def sandwich_se(x, residuals):
    # Your code here
    pass

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