-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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