public static double simpleDuration(List <CashFlow> cashflows, InterestRate y, Date settlementDate)
{
if (cashflows.Count == 0)
{
return(0.0);
}
double P = 0, dPdy = 0;
DayCounter dc = y.dayCounter();
foreach (CashFlow cf in cashflows.Where(cf => !cf.hasOccurred(settlementDate)))
{
double t = dc.yearFraction(settlementDate, cf.date());
double c = cf.amount();
double B = y.discountFactor(t);
P += c * B;
dPdy += t * c * B;
}
// no cashflows
if (P == 0.0)
{
return(0.0);
}
return(dPdy / P);
}