Skip to content

Instantly share code, notes, and snippets.

@JonathanLoscalzo
Forked from ramhiser/huber.py
Created October 12, 2021 13:31
Show Gist options
  • Save JonathanLoscalzo/218ef1c5fe9f8f87681ca01598a5e7fe to your computer and use it in GitHub Desktop.
Save JonathanLoscalzo/218ef1c5fe9f8f87681ca01598a5e7fe to your computer and use it in GitHub Desktop.
Robust Estimation of Mean and Standard Deviation in Python via the Huber Estimator
import numpy as np
from statsmodels.robust.scale import huber
# Mean and standard deviation to generate normal random variates
mean, std_dev = 0, 2
sample_size = 25
np.random.seed(42)
x = np.random.normal(mean, std_dev, sample_size)
# Appends a couple of outliers
x = np.append(x, (50, 100))
# Notice that the Huber estimators are much closer
# to the *true* mean and standard deviation.
print (np.mean(x), np.std(x)) # (5.253, 20.946)
print huber(x) # (array(-0.033), array(2.367))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment