# MovingAverage[] and Fit[] with data containing DateLists

I have data which has the following appearance: data = {{DateList, Real}, ..., {DateList, Real}} It's easy to visualize the data by using DateListPlot. In addition to DateListPlot, I would also like to show a function which fits the data and the moving average of the data.

I hope I described the problem clear enough.

-
You can convert the dates to numbers using AbsoluteTime; DateListPlot will still work and you can also do the fit and other manipulations. – b.gatessucks Oct 8 '12 at 9:29
DateListPlot will actually render faster if you convert to absolute time beforehand (incl. timing for the conversion). – Mike Honeychurch Oct 8 '12 at 10:38

To fit a function and to calculate the moving average you need to convert your dates in absolute time using AbsoluteTime[].

data = FinancialData["IBM", "Jan. 1, 2004"];
newdata =
Table[{AbsoluteTime[data[[i, 1]]], data[[i, 2]]}, {i, Length[data]}];
lm = LinearModelFit[newdata, x, x];
movAvg = MovingAverage[newdata, 200];
Show[DateListPlot[newdata],DateListPlot[movAvg, PlotStyle -> Red],
Plot[lm[x], {x, Min[newdata[[All, 1]]], Max[newdata[[All, 1]]]}],
Frame -> True]


## Update

This update implements the comment by Mike Honeychurch. Note that the moving average can be computed by averaging runs of only odd r elements.

movAvgDoneRight = MovingAverage[newdata[[All, 2]], 201];
elementsToDrop = (Length[newdata] - Length[movAvgDoneRight]);
movAvgData = Transpose[{Drop[
Drop[newdata[[All, 1]], elementsToDrop/2], -elementsToDrop/2],
movAvgDoneRight}];
Show[DateListPlot[newdata], DateListPlot[movAvgData, PlotStyle -> Red],
Plot[lm[x], {x, Min[newdata[[All, 1]]], Max[newdata[[All, 1]]]}],
Frame -> True]


-
Just a minor comment: You don't really need a table to get newdata. I find modifiying data in place using data[[All, 1]] = AbsoluteTime /@ data[[All, 1]]; more elegant :) Otherwise +1 – Ajasja Oct 8 '12 at 10:08
@Ajasja: ...unless you have to do something after plotting/fitting that needs the date lists as opposed to the absolute times. – J. M. Oct 8 '12 at 10:10
I'd interpret moving average in this context as meaning just of the data -- not of the dates (times) as well. – Mike Honeychurch Oct 8 '12 at 10:36
Thx, exactly what I've been looking for. – RMMA Oct 8 '12 at 10:53
@VLC trim the dates (times) and transpose with the averaged data. – Mike Honeychurch Oct 8 '12 at 11:45

For data in the form { ..., {date_i, value_i}, ... } you should consider using TimeSeries.

For example:

data = FinancialData["IBM", "Jan. 1, 2015"];


Then:

ts = TimeSeries[data]


And then you can operate on the time series directly, with functions like MovingAverage, and DateListPlot:

newts = MovingAverage[ts, 5]


DateListPlot[ newts]


-

Another possibility is to use TradingChart or InteractiveTradingChart, which have a long list of statistical indicators :

InteractiveTradingChart[{"IBM", {"Jan. 1, 2004"}}]


-