The examples for using MovingAverage mostly refer to data evenly spaced in time,such as stock values. In typical physics data, events arrive random in time (e.g. radio active decay events, but also if acting in day trading on the stock market). I do not see how I can use the apparatus of MovingAverage and associated evaluations in this case. Do I not understand the function, or should I go ahead and invent my own functions?

  • $\begingroup$ Make a TimeSeries object from your data, then use TimeSeriesResample to get an evenly sampled version from it, then use MovingAverage. $\endgroup$
    – MarcoB
    Feb 12, 2021 at 13:31
  • $\begingroup$ No, that is not good: never add or remove data points. This is like "fake data", may look a handy tool but can lead to trouble in later analysis. I think I have the solution, in my second remark. $\endgroup$
    – Hans W
    Feb 12, 2021 at 14:56
  • $\begingroup$ Sure. I'd expect the result to be the same, since I expect that TimeSeriesResample would do a linear interpolation over the missing range, and the MovingMap with a Quantity[1, "Hours"] window that you proposed in comments below would probably do the same for you, but behind the scenes. I don't see how else it could possible average over data that is not there. $\endgroup$
    – MarcoB
    Feb 12, 2021 at 15:47

1 Answer 1


By default the MovingAverage could be applied for 1-D lists but regarding to your case, you need make the TemporalData from your {t,y} list:

y= Table[RandomReal[{-1, 1}] + 5 Sin[i/(6 Pi)], {i, 1, 100}];
t = Table[i + RandomReal[{-0.3, 0.3}], {i, 1, 100}]; (*As you see, the timestamps contain random shifts*)
td = TemporalData[y, {t}];

ListPlot[{td, MovingAverage[td, 5]},
 PlotMarkers -> {Automatic, None},
 Joined -> {False, True},
 ImageSize -> 800,
 PlotStyle -> {Directive[Lighter[Blue, 0.5]], 
   Directive[Red, Thick]}]

enter image description here

  • $\begingroup$ Thank you, but still I am not quite happy: The moving average is now averaged over 5 neighboring points and not over time. To get the average event rate I need the number of events within the averaging time window and divide by the window width. $\endgroup$
    – Hans W
    Feb 12, 2021 at 12:37
  • $\begingroup$ Have to change my comment: I can indeed average over time using for the time in the temporal data datelists and averaging with Quantity[1,"Hours"] . Would not have guessed from the documentation. Thanks again. $\endgroup$
    – Hans W
    Feb 12, 2021 at 13:45
  • 1
    $\begingroup$ @HansW, the fundamental sense of the moving average is namely about moving the aver-window from data point to data point but not from timestamp to timestamp. $\endgroup$
    – Rom38
    Feb 12, 2021 at 15:08
  • $\begingroup$ What you calculate is an event average, I need a time average. Another aspect is that in comparing moving averages for different averaging windows, the results are out of phase by half a window. This is because it averages data all prior to the data point for which the average is made. This is also visible in the plot you made. The red curve needs to be moved by 2.5 to straddle the data. Is there a simple fix? $\endgroup$
    – Hans W
    Feb 12, 2021 at 17:29
  • $\begingroup$ @HansW, the time-averaging requires another filters. The MoAv deals namely with your sample points by the definition of MoAv. The shift of the red curve occurres due to the principle of MoAv - it takes an array of N-points and returns the N-r+1 points when av-window size is r. In my example r=5 and the averaged array is shorter than initial for 4 points (the new timestamps of TemporalData take it in account). $\endgroup$
    – Rom38
    Feb 13, 2021 at 13:06

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.