# How can I implement a moving standard deviation for a time series?

I imported the following dataset and converted it into a time series.

dataSet =
ts = TimeSeries[dataSet];


Because I didn't find a built-in function function for a moving standard deviation, I implemented one with the following code.

A = Table[dataSet[[i ;; i + 11, 2]], {i, 1, Length[dataSet] - 11}];
B = TimeSeries[N[StandardDeviation @ Transpose[A]]];

Show[
DateListPlot[ts, ImageSize -> Medium, PlotLegends -> {"Original"}],
ListLinePlot[MovingAverage[ts, 12],
PlotLegends -> {"Rolling Mean"}, PlotStyle -> {{Red, Dashing[Tiny]}}],
ListLinePlot[B,
PlotLegends -> {"Rolling STD"}, PlotStyle -> {{Green, Dashing[Large]}}]]


Please feel free to comment about implementing a moving standard deviation for a time series.

Updated

1. I applied the function "MovingMap", it works with list or vector, however, it doesn't accept timeseries(ts) as an argument.

MovingMap[StandardDeviation, ts, 12]

2. The "Moving STD" cannot be shown correctly; it is because I cannot find a function "MovingStandardDeviation" which is accepted the same arguments as MovingAverage.

3. Source of data has been updated."http://www.analyticsvidhya.com/wp-content/uploads/2016/02/AirPassengers.csv"

• Since you don't give us data that allows to experiment with your code, alll I can do is give you some general advice: before trying to plot B, validate that it actually contains the numbers you want to plot. – m_goldberg Feb 12 '16 at 8:48
• What exactly is the question here? Is this a request for a code-review? If so it should be tagged accordingly. Questions of the form “I use ______ for ______, what do you use?" are specifically off-topic. See: What types of questions should I avoid asking? – Mr.Wizard Feb 12 '16 at 12:41
• I think I misread your question. So you do not have a working solution, and you do not have code that you wish to debug? If this is the case then I suggest you rewrite the question removing things that are irrelevant, e.g. MovingAverage, and instead focus I a clear description of what you need. Example data would be useful as well; for example is it uniformly sampled or irregular? – Mr.Wizard Feb 12 '16 at 13:41

ma = MovingMap[Mean, ts, Quantity[12, "Months"]];