Classical standard errors assume equal noise everywhere
The classical standard errors rest on NIST's assumptions about the random errors: their means are zero and their standard deviation is constant. Spending isn't so tidy: customers with unusual discounts can be far more unpredictable. Halbert White's 1980 paper gave a covariance estimator that stays consistent even when the noise varies, and the two estimators are about equal when it doesn't and diverge when it does. statsmodels lists it as HC0, White's robust standard errors, with variants up to HC3. The code below uses HC1: HC0 scaled by n / (n − k), where k counts the coefficients with the intercept, the same small-sample fix the classical formula makes when it divides by n − k.
def standard_errors(columns, y):
"""Classical and HC1 robust standard errors for each coefficient."""
X = np.column_stack([np.ones(len(y))] + list(columns))
beta = np.linalg.lstsq(X, y, rcond=None)[0]
resid = y - X @ beta
k = X.shape[1]
inv = np.linalg.inv(X.T @ X)
classical = np.sqrt(np.diag(inv) * (resid @ resid) / (len(y) - k))
meat = X.T @ (X * resid[:, None] ** 2)
robust = np.sqrt(np.diag(inv @ meat @ inv) * len(y) / (len(y) - k))
return classical, robust
gap = np.abs(discount - discount.mean())
# unusual discounts, noisy spend
noisy = 60 + 8 * tenure + 1.5 * discount + rng.normal(0, 1, n) * 5 * gap
classical, robust = standard_errors([discount, tenure], noisy)
print(f"standard error of the discount coefficient: classical {classical[1]:.3f}, robust {robust[1]:.3f}")
The noise is biggest for customers with unusual discounts. Which standard error is too small?