Skip to content

Instantly share code, notes, and snippets.

@ramhiser
Created January 21, 2015 17:39
Show Gist options
  • Save ramhiser/9d0ea69e3b64ed4eabb1 to your computer and use it in GitHub Desktop.
Save ramhiser/9d0ea69e3b64ed4eabb1 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