Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When modeling a swap contract a common operation is to sum up two cash flows producing a resulting cashflow with netted amounts in the highest granularity of the two initial cash flows. What is a practical way to do this?

c1 = Cashflow[Annuity[-765.3, 4, 1/12]]; (*-monthly payments_*)

c2 = Cashflow[Annuity[2357.124, 4, 1/4]]; (*_-quarterly payments_*)

c3 = ? (*_-result is the netted cashflow in monthly granularity_*)

A fragment of result modeled with Numbers, with two cash flows in different granularity and the resulting with netted amounts.

share|improve this question
Hello, you don't have to sign inside the post, just edit your profile and we will see :) – Kuba Feb 2 '14 at 22:45
If there isn't anything built in, then after joining those lists this is a duplicate of this topic – Kuba Feb 2 '14 at 22:49
Or just pad the quarterly list with zeroes, e.g. Flatten@Riffle[c2, {{0, 0, 0}}, {2, 2 Length[c2], 2}] – ciao Feb 2 '14 at 23:22
@rasher This tweak generates a list of monthly payments from the quarterly Cashflow using your approach Flatten@Riffle[c2[[1,All,2]],{{0,0,0}},{2,2 Length[c2[[1]],2}] – PlaysDice Feb 2 '14 at 23:44

Kuba (comments) gave a useful hint. I did it using this approach and found it acceptable. Still the code doesn't look very intuitive. I am still tempted to do the aggregation step rather in SQL therefore. The riffle approach (by @rasher) does work, would however require a test of all cash flows to determine highest granularity. This I think requires more overhead. Here is the working example using GatherBy:

c1 = Cashflow[Annuity[-720, 10, 1/12]];  
c2 = Cashflow[Annuity[{3632.81, {3632.81*-1, 0}}, 10, 1]];  

gathered = GatherBy[Union[c1[[1]], c2[[1]], Cashflow[{{1/12, l1}}][[1]]], First];  
aggregated = Map[{#[[1, 1]], Total[#[[;; , 2]]]} &, gathered];  

Grid[c2[[1]][[1 ;; 3]], Frame -> All]  
Grid[c1[[1]][[1 ;; 24]], Frame -> All]  
Grid[gathered[[1 ;; 24]], Frame -> All]  
Grid[aggregated[[1 ;; 24]], Frame -> All] 
share|improve this answer

Your Answer


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.