Recently, I was porting some code from Matlab to python when I came across an interesting bit of information. The default standard deviation in Matlab and python do not return the same value. I found this out after messing with python’s implementation of a standard deviation filter for half an hour. I thought maybe python’s implementation was incorrect. Turn’s out they are both correct.

Matlab defaults to the population standard deviation:

While numpy defaults to the sample standard deviation:

Lesson Learned: Always make sure to read to documentation thoroughly.

## Filters

Anyway, my goal was to implement a 2D standard deviation filter that was the same as the matlab version. For example the std filter in Matlab returns the following:

Note that on line 2 I transpose the matrix. This way the reshape function will act the same in Matlab as it does in python.

Python does not have a built in std filter, but they do have a generic filter that is capable of implementing a standard deviation filter.

Notice that x_filt*np.sqrt(9./8) produces the same output as the Matlab function. More formally,

While experimenting with the python function, however, I noticed it was quite slow. I should say brutally slow. Searching around I found a stackoverflow post asking about performance. A user, nneonneo, suggests a much quicker implementation that you can see on the linked stackoverflow post. After fiddling with his code a little bit, I was able to perfectly reproduce the results from scipy’s generic_filter. The code that replicates scipy’s function is:

As you can see, it returns the same values as the python filter. If you want to return the same values as the Matlab function, all you have to do is multiply the returned value by $\frac{window\_size^2}{window\_size^2-1}$ which is what was done above since the window size was 3.

Just to prove how much faster this implementation is than the generic filter, here are some benchmarks on different size arrays.

Generic Uniform
20 x 20 0.0157 0.0004
500 x 500 7.5842 0.0114
1,000 x 1,000 30.0421 0.0581

Finally, as a sanity check to make sure they both output the same results on randomly sized matrices:

And there we are. A quick implementation of a standard deviation filter in python that produces the same results as the Matlab version. A big thank you to nneonneo for the original implementation.

## Caveats

While the fast implementation is fantastic, it does return nans when a part of the array has a standard deviation of zero. I haven’t fully tested it, but I am assuming it is a numerical issue. For example:

As is seen above, there are nans present in returned function. Let’s debug the function line by line.

So, as is shown above, the result is a really small negative number which will turn into a nan when we take the square root of it. Interestingly, it doesn’t occur for all distributions of random numbers. If we change the random seed, nans can occur in different places or even not occur at all. This leads me to believe that it has something to do with the underlying memory. If you know what is causing this small problem let me know!

So finally, maybe a better representation of the function might be:

The small random numbers stop the memory problem and ensures the correct value is returned.