Why does numpy std() give a different result to matlab std()?

The NumPy function np.std takes an optional parameter ddof: “Delta Degrees of Freedom”. By default, this is 0. Set it to 1 to get the MATLAB result: >>> np.std([1,3,4,6], ddof=1) 2.0816659994661326 To add a little more context, in the calculation of the variance (of which the standard deviation is the square root) we typically divide … Read more

Standard Deviation in LINQ

You can make your own extension calculating it public static class Extensions { public static double StdDev(this IEnumerable<double> values) { double ret = 0; int count = values.Count(); if (count > 1) { //Compute the Average double avg = values.Average(); //Perform the Sum of (value-avg)^2 double sum = values.Sum(d => (d – avg) * (d … Read more

Weighted standard deviation in NumPy

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))

tech