The NumPy np.std function accepts the optional ddof parameter: Delta Degrees of Freedom. The default is 0 . Set it to 1 to get MATLAB result:
>>> np.std([1,3,4,6], ddof=1) 2.0816659994661326
To add a little more context, when calculating the variance (of which the standard deviation is the square root), we usually divide by the number of values that we have.
But if we select a random sample of N elements from a larger distribution and calculate the variance, dividing by N may underestimate the actual variance. To fix this, we can reduce the number we divide by ( degrees of freedom ) by a number less than N (usually N-1 ). The ddof parameter allows us to change the divisor by the specified amount.
Unless otherwise indicated, NumPy will calculate the biased estimate for the variance ( ddof=0 , dividing by N ). This is what you want if you are working with the entire distribution (and not a subset of values that were accidentally selected from a larger distribution). If the ddof parameter is ddof , NumPy instead divides by N - ddof .
The default behavior of MATLAB std is to correct bias for sample variance by dividing by N-1 . This eliminates some (but probably not all) biases in the standard deviation. This will probably be what you want if you use the function on a larger random sample.
The good answer @hbaderts gives further mathematical details.
Alex Riley Dec 22 '14 at 9:54 2014-12-22 09:54
source share