Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
    
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
add comment

2 Answers 2

up vote 11 down vote accepted

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

share|improve this answer
1  
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
1  
@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
1  
@VLC trim the dates (times) and transpose with the averaged data. –  Mike Honeychurch Oct 8 '12 at 11:45
show 3 more comments

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

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

chart

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.