Forecasting Porfolio Cash Flows

Overview


A portfolio is just a collection of individual assets. As such, to forecast the

Implementation


A portfolio is just an array of individual assets. The code below assumes that there a function, forecast, which takes an object representing a loan or bond, and returns an array of cash flows. The code iterates through each bond, obtains its cash flows, and then adds each in turn to the cashFlows array.


let cashFlows = [];
for(let bond of portfolio){
  let newFlows = forecast(bond);
  newFlows.forEach(p=>cashFlows.push(p));
}
					

Grouping


Once the cashflows have been concatenated into a single array, it is often desirable to merge two cashflows that occur on the same date into a single amount.

The group api can be used to group the records on the date and then to sum the amounts. (see also grouping)


let payments1 = [{date:'2000-01-15', payment:100},{date:'2000-02-15', payment:100},{date:'2000-03-15', payment:100},{date:'2000-04-15', payment:100}]
let payments2 = [{date:'2000-01-15', payment:200},{date:'2000-02-15', payment:120},{date:'2000-03-15', payment:100},{date:'2000-04-15', payment:140}];

//aggregate the payments into a single array
let payments = [...payments1, ...payments2];

let group = $group(payments, p=>p.date);

//group the payments by date, then reduce them to a single payment
let portfolioCashFlows = group.map((keys,values)=>{
	let payment = values.reduce((total, p)=>p.payment+total, 0);
	return payment;
})();
					
Try it!

Contents