Message Boards Message Boards

3
|
6065 Views
|
2 Replies
|
8 Total Likes
View groups...
Share
Share this post:

Joining Date-Lists Together

Posted 11 years ago
I often have temporal data for multiple series where the dates don't quite line up, and I want to perform various functions on only the data for the common dates. To do this with TemporalData seems to require extracting all the dates for each object, finding the intersection of these lists, then constructing new TemporalObjects for each one representing the data only on the intersection date list. This seems very slow. Is there a better way to accomplish this?


Thanks.
POSTED BY: Michael Stern
2 Replies
This is a common issue in programming with time series data and esp. in finance. Given two lists with dates, how can you join them together so that events from the same date are matched -up?

Assuming the lists are sorted, you can always recursively generate a paired list with a recursive function. An example:
 zipper[e_, {}, acc_] := acc
 zipper[{}, e_, acc_] := acc
 
 zipper[{{date1_, val1_}, rest1___}, {{date2_, val2_}, rest2___}, acc_] :=
   Switch[Sign@DateDifference[date1, date2],
     0, zipper[{rest1}, {rest2}, Append[acc, {date1, {val1, val2}}]],
     1, zipper[{rest1}, Join[{{date2, val2}}, {rest2}], acc] ,
     -1, zipper[Join[{{date1, val1}}, {rest1}], {rest2}, acc]
   ]

This joins the list like so:
msft = {{{2010, 1, 5}, 28.1}, {{2010, 1, 6}, 27.93}, {{2010, 1, 7}, 27.64}, {{2010, 1, 8}, 27.83}};
goog = {{{2010, 1, 4}, 626.75}, {{2010, 1, 5}, 623.99}, {{2010, 1, 6}, 608.26}};
zipper[msft, goog, {}]
{{{2010, 1, 5}, {28.1, 623.99}}, {{2010, 1, 6}, {27.93, 608.26}}}
Only the dates which show up in both lists are kept. For each date we hav a list of pairs.
POSTED BY: Sean Clarke
Here is another way:
groupSeries[data__List]:={#[[1,1]],#[[All,2]]}&/@Select[GatherBy[Join@data,First],Length@#==2&]
groupSeries[msft, goog]
{{{2010, 1, 5}, {28.1, 623.99}}, {{2010, 1, 6}, {27.93, 608.26}}}
POSTED BY: Rodrigo Murta
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract