statsmodels has a class that makes calculating weighted statistics easier: statsmodels.stats.weightstats.DescrStatsW .
Assuming this dataset and weight:
import numpy as np from statsmodels.stats.weightstats import DescrStatsW array = np.array([1,2,1,2,1,2,1,3]) weights = np.ones_like(array) weights[3] = 100
You initialize the class (note that you must pass the correction factor, delta degrees of freedom at this point):
weighted_stats = DescrStatsW(array, weights=weights, ddof=0)
Then you can calculate:
.mean weighted average :
>>> weighted_stats.mean 1.97196261682243
.std weighted standard deviation :
>>> weighted_stats.std 0.21434289609681711
.var weighted variance :
>>> weighted_stats.var 0.045942877107170932
.std_mean standard weighted average error :
>>> weighted_stats.std_mean 0.020818822467555047
Just in case, if you are interested in the relationship between standard error and standard deviation: standard error (for ddof == 0 ) is calculated as the weighted standard deviation divided by the square root of the sum of the weights minus 1 (the corresponding source for statsmodels version 0.9 on GitHub ):
standard_error = standard_deviation / sqrt(sum(weights) - 1)
MSeifert Apr 7 '16 at 0:57 2016-04-07 00:57
source share