Search code examples
pythonnumpystatsmodelsstandard-deviationweighted

Weighted standard deviation in NumPy


numpy.average() has a weights option, but numpy.std() does not. Does anyone have suggestions for a workaround?


Solution

  • How about the following short "manual calculation"?

    def weighted_avg_and_std(values, weights):
        """
        Return the weighted average and standard deviation.
    
        They weights are in effect first normalized so that they 
        sum to 1 (and so they must not all be 0).
    
        values, weights -- NumPy ndarrays with the same shape.
        """
        average = numpy.average(values, weights=weights)
        # Fast and numerically precise:
        variance = numpy.average((values-average)**2, weights=weights)
        return (average, math.sqrt(variance))