Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Weighted standard deviation in NumPy

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

like image 695
10 revs, 7 users 53% Avatar asked Mar 09 '10 23:03

10 revs, 7 users 53%


People also ask

What is a weighted standard deviation?

A weighted standard deviation allows you to apply a weight, or relative significance to each value in a set of values. Values with a higher value for their weight are considered as more significant to a sample as compared to the other values in a sample.

How do you find the standard deviation in Numpy?

The standard deviation is the square root of the average of the squared deviations from the mean, i.e., std = sqrt(mean(x)) , where x = abs(a - a. mean())**2 . The average squared deviation is typically calculated as x. sum() / N , where N = len(x) .

What is the STD in Numpy in Python?

std() in Python. numpy. std(arr, axis = None) : Compute the standard deviation of the given data (array elements) along the specified axis(if any).. Standard Deviation (SD) is measured as the spread of data distribution in the given data set.


1 Answers

How about the following short "manual calculation"?

def weighted_avg_and_std(values, weights):     """     Return the weighted average and standard deviation.      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)) 
like image 90
Eric O Lebigot Avatar answered Sep 20 '22 19:09

Eric O Lebigot