Robust and weighted fits in Python
RLM downweights outliers; WLS uses weights you supply.
import numpy as np
import statsmodels.api as sm
rng = np.random.default_rng(7)
x = np.linspace(0, 20, 60)
y = 5 + 2 * x + rng.normal(0, 1, size=60)
y[:3] += 60 # three wild outliers
X = sm.add_constant(x)
ols = sm.OLS(y, X).fit()
robust = sm.RLM(y, X, M=sm.robust.norms.HuberT()).fit()
weights = np.where(np.arange(60) < 3, 0.05, 1.0)
weighted = sm.WLS(y, X, weights=weights).fit()
print("ols ", ols.params.round(3))
print("robust ", robust.params.round(3))
print("weighted", weighted.params.round(3))
How it works
RLMwith Huber loss resists a few bad points.WLSweights each observation, usually by inverse variance.- Compare the slopes to see how much the outliers moved OLS.
Keywords and builtins used here
asprint
The run, in numbers
- Lines
- 17
- Characters to type
- 510
- Tokens
- 191
- Three-star pace
- 110 tpm
At the three-star pace of 110 tokens a minute, this run takes about 104 seconds.
Step 2 of 2 in Diagnostics & robustness, step 8 of 19 in Statistics with statsmodels.