# interpolating function over dates

I've got a bunch of readings at various dates and times. I'd like to be able to interpolate and then integrate over, say days. I can do this by converting my date/time to AbosluteTime and get the interpolating function that way. Then by integrating and plugging in seconds as my limits of integration I get what I want.

Also, since seconds in really finer than I need, I convert all of the AbsoluteTimes to the number of hours since the first reading.

I was wondering if there might be a way to do this directly using dates. For what it's worth, my readings last for about 2 months and the values are between about 90 and 300.

SAMPLE DATA

 x[[1 ;; 5]]


{{{2014, 8, 4, 10, 36, 0.}, 257.},{{2014, 8, 4, 16, 28, 0.}, 385.},

{{2014, 8, 4, 22, 53, 0.}, 176.}, {{2014, 8, 5, 6, 52, 0.}, 148.}, {{2014, 8, 5, 11, 19, 0.}, 192.}}

-
Sorry for the wrong edit. Anyway, a +1 question :) –  eldo Sep 2 '14 at 18:58
@eldo no problem about the edit. I appreciate your looking over the question. –  Mitchell Kaplan Sep 2 '14 at 20:38

There's a couple ways to do this:

data = {{{2014, 8, 4, 10, 36, 0.}, 257.}, {{2014, 8, 4, 16, 28, 0.}, 385.},
{{2014, 8, 4, 22, 53, 0.}, 176.}, {{2014, 8, 5, 6, 52, 0.}, 148.},
{{2014, 8, 5, 11, 19, 0.}, 192.}};


1) Convert dates to absolute times:

data[[All, 1]] = AbsoluteTime /@ data[[All, 1]];
f1 = Interpolation@data;

f1[AbsoluteTime@{2014,8,4,10,40}]


261.669

2) With TimeSeries (v10)

ts = TimeSeries[data];
ts[{2014, 8, 4, 10, 40}]


258.455

The TimeSeries interpolation uses first order by default but can be changed by changing the ResamplingMethod -> {"Interpolation", opts} option in the TimeSeries function.

ts = TimeSeries[data, ResamplingMethod -> {"Interpolation", InterpolationOrder -> 3}];
ts[{2014, 8, 4, 10, 40}]


261.669

To integrate over:

NIntegrate[ts[t], {t, AbsoluteTime[{2014, 8, 4, 10, 40}], AbsoluteTime[{2014, 8, 4,11}]}]

-
Looks like TimeSeries was a nice addition to version 10 - I like that method. –  Mitchell Kaplan Sep 2 '14 at 20:54
Incidentally, with TimeSeries it isn't necessary to use the "PathFunction". Just evaluate directly on the date stamp. –  Andy Ross Sep 3 '14 at 12:16
@AndyRoss Sweet. Didn't know that. –  kale Sep 3 '14 at 15:47

Some sample data:

r = # - #[[1]] &@(Range[##, 3690] & @@ (AbsoluteTime[{#, {"Day", "Month", "YearShort"}}] & /@
{"05/01/14", "05/03/14"}));
hours = N[r/3600];
data = Transpose[{hours, hours^2}];


Integrate:

f = Interpolation@data;
Integrate[f@x, {x, 0, Last@hours}]

(*9.45434*10^8*)

-