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

dataSet = 
  Import["http://www.analyticsvidhya.com/wp-content/uploads/2016/02/AirPassengers.csv", "HeaderLines" -> 1 , "DateStringFormat" -> {"Year", "-", "Month"}]; 
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]]];

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

enter image description here

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


Dear Alexey Golyshev, m_goldberg, Yves Klett, Mr.Wizard Thanks for your reply.

  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"

  • $\begingroup$ 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. $\endgroup$
    – m_goldberg
    Commented Feb 12, 2016 at 8:48
  • $\begingroup$ 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? $\endgroup$
    – Mr.Wizard
    Commented Feb 12, 2016 at 12:41
  • $\begingroup$ 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? $\endgroup$
    – Mr.Wizard
    Commented Feb 12, 2016 at 13:41

1 Answer 1



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

 {ts, ma, ma + sd , ma - sd}, 
 PlotLegends -> {"Original", "Moving Mean", "+ Moving STD", "- Moving STD"}

enter image description here

  • $\begingroup$ Dear Alexey Golyshev, Thank you. $\endgroup$ Commented Feb 13, 2016 at 2:30

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.