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?
1 Answer
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]}]
-
$\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 WFeb 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 WFeb 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$– Rom38Feb 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 WFeb 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 theN-r+1
points when av-window size isr
. In my exampler=5
and the averaged array is shorter than initial for 4 points (the new timestamps ofTemporalData
take it in account). $\endgroup$– Rom38Feb 13, 2021 at 13:06
TimeSeries
object from your data, then useTimeSeriesResample
to get an evenly sampled version from it, then useMovingAverage
. $\endgroup$TimeSeriesResample
would do a linear interpolation over the missing range, and theMovingMap
with aQuantity[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$