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

enter image description here enter image description here

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}

which gives me the following output:

enter image description here

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 1


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.

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]

enter image description here

  • $\begingroup$ 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. $\endgroup$
    – Jagra
    Apr 26, 2013 at 3:38
  • $\begingroup$ @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]])? $\endgroup$ Apr 26, 2013 at 4:53
  • $\begingroup$ @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. $\endgroup$
    – m_goldberg
    Apr 26, 2013 at 5:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.