# Standard Deviation and StandardDeviationFilter

I found this scant description of StandardDeviationFilter in the documentation, implying one could use it to generate a moving standard deviation:

I've got a need for this sort of thing so it looked interesting. I tried the following comparision:

data = RandomReal[{-10, 10}, 1000];
sd1 = StandardDeviation[#] & /@ Partition[data, 50, 1];
sd2 = StandardDeviationFilter[data, 50];

ListLinePlot[{sd1, sd2[[-Length[sd1] ;;]]}, AspectRatio -> 0.25, ImageSize -> 600]
Length[#] & /@ {sd1, sd2}
sd1==sd2


which gives me the following output:

I'd have guessed that the two approaches would give me the same output. Clearly not.

Can anyone explain what StandardDeviationFilter does or doesn't do and why it differs from the expected?

I may have missed something in setting up this comparison (it happens ;-), any ideas appreciated.

-

## 1 Answer

I would say there is something wrong with your partitioning. I think the second argument of StandardDeviationFilter is the number of number of elements to take both to the right and to the left -- i.e., if the second argument is 1, the standard deviation will be computed with 3 elements.

Reducing the data set and simplifying sd1 and sd2 tends to confirm my interpretation.

SeedRandom[42];
data = RandomReal[{-10, 10}, 100];
sd1 = StandardDeviation[#] & /@ Partition[data, 3, 1, {2, 2}];
sd2 = StandardDeviationFilter[data, 1];

ListLinePlot[{sd1, sd2}, AspectRatio -> 0.25, ImageSize -> 400]


-
You seem on the right track. Not certain how one gets to it from the documentation. Curious, with your code: Most[Rest[sd1]] == Most[Rest[sd2]] returns True, sd1==sd2 doesn't. – Jagra Apr 26 '13 at 3:38
@Jagra, I'd tend to interpret "range $r$ neighborhood" to mean "value, and the $r$ values to its left and to its right", resulting in a window of length $2r+1$. BTW, what happens if you give StandardDeviationFilter[] symbolic data (e.g. StandardDeviationFilter[Array[C, 10]])? – J. M. Apr 26 '13 at 4:53
@Jagra. Re: Most[Rest[sd1]] == Most[Rest[sd2]] returns True, sd1 == sd2 doesn't. You can see the difference at the beginning of the plot I posted. I would guess different padding at beginning is responsible. – m_goldberg Apr 26 '13 at 5:10