11
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ You can convert the dates to numbers using AbsoluteTime; DateListPlot will still work and you can also do the fit and other manipulations. $\endgroup$ Commented Oct 8, 2012 at 9:29
  • $\begingroup$ DateListPlot will actually render faster if you convert to absolute time beforehand (incl. timing for the conversion). $\endgroup$ Commented Oct 8, 2012 at 10:38

3 Answers 3

13
$\begingroup$

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]

DateListPlot with fit and moving average

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]

DateListPlot with fit and moving average

$\endgroup$
8
  • 1
    $\begingroup$ 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 $\endgroup$
    – Ajasja
    Commented Oct 8, 2012 at 10:08
  • 1
    $\begingroup$ @Ajasja: ...unless you have to do something after plotting/fitting that needs the date lists as opposed to the absolute times. $\endgroup$ Commented Oct 8, 2012 at 10:10
  • $\begingroup$ I'd interpret moving average in this context as meaning just of the data -- not of the dates (times) as well. $\endgroup$ Commented Oct 8, 2012 at 10:36
  • $\begingroup$ Thx, exactly what I've been looking for. $\endgroup$
    – RMMA
    Commented Oct 8, 2012 at 10:53
  • 1
    $\begingroup$ @VLC trim the dates (times) and transpose with the averaged data. $\endgroup$ Commented Oct 8, 2012 at 11:45
5
$\begingroup$

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

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

chart

$\endgroup$
0
$\begingroup$

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]

enter image description here

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

newts = MovingAverage[ts, 5]

enter image description here

DateListPlot[ newts]

enter image description here

$\endgroup$

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.