Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 2 down vote accepted

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

share|improve this answer
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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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