lballabio/quantlib — pull request #28 by BGC-nglass (2013-08-20 09:58:32+00:00)
Title: Fix simpleDuration, modifiedDuration and convexity with Act/Act(ISMA)

http://sourceforge.net/p/quantlib/bugs/162/

This commit fixes simpleduration, duration and convexity with Act/Act(ISMA)

Tested with following code:

```
Calendar calendar = TARGET();

Integer fixingDays = 1;
Natural settlementDays = 1;

Date settlementDate(21, August, 2013);
settlementDate = calendar.adjust(settlementDate);

Date issueDate(15, August, 2013);
Date interestAccrue(15, August, 2013);
Date firstCouponDate(15, February, 2014);
Date maturityDate(15, August, 2023);

Rate coupon = 2.500;

Period period = 6*QuantLib::Months;

Compounding comp = Compounded;
Frequency freq = Semiannual;
DayCounter dc = ActualActual(ActualActual::ISMA);

FixedRateBond bond (settlementDays, 100.0,
Schedule(
interestAccrue,
maturityDate,
period,
NullCalendar(),
Unadjusted,
Unadjusted,
DateGeneration::Forward,
false,
firstCouponDate),
std::vector(1, coupon/100.0),
dc,
Unadjusted,
100.0,
issueDate,
calendar);

std::cout << std::setprecision(10);

std::cout << "ISIN: US912828VS66" << std::endl;
std::cout << "CUSIP: 912828VS6" << std::endl;
std::cout << "Settlement date: " << settlementDate << std::endl;
std::cout << "Issue Date: " << issueDate << std::endl;
std::cout << "Interest Accrue: " << interestAccrue << std::endl;
std::cout << "First Coupon: " << firstCouponDate << std::endl;
std::cout << "Maturity: " << maturityDate << std::endl;
std::cout << "coupon: " << coupon << std::endl;
std::cout << "period: " << period << std::endl;

Real price = 102.25;
Real accrued = bond.accruedAmount(settlementDate);
Real npv = price + accrued;

const Leg& leg = bond.cashflows();

Rate yield = CashFlows::yield(leg, npv, dc, comp, freq, false, settlementDate);
Time duration = CashFlows::duration(leg, yield, dc, comp, freq, Duration::Modified, false, settlementDate);
Real convexity = CashFlows::convexity(leg, yield, dc, comp, freq, false, settlementDate);

std::cout << "Price : " << price << std::endl;
std::cout << "Accrued : " << accrued * 10000.0 << std::endl;
std::cout << "NPV : " << npv << std::endl;
std::cout << "Yield : " << yield * 100.0 << std::endl;
std::cout << "Mod duration : " << duration << std::endl;
std::cout << "Convexity : " << convexity / 100.0 << std::endl;

std::cout << "PV 0.01 : " << npv * duration / 10000.0 << std::endl;

std::cout << "CashFlow: " << std::endl;

Integer n = leg.size();
for (Integer i = 0; i < n; ++i)
{
std::cout << leg[i]->date() << " : " << leg[i]->amount() << std::endl;
}
```

and verified to produce following:
ISIN: US912828VS66
CUSIP: 912828VS6
Settlement date: August 21st, 2013
Issue Date: August 15th, 2013
Interest Accrue: August 15th, 2013
First Coupon: February 15th, 2014
Maturity: August 15th, 2023
coupon: 2.5
period: 6M
Price : 102.25
Accrued : 407.6086957
NPV : 102.2907609
Yield : 2.247124772
Mod duration : 8.809360184
Convexity : 0.8778663256
PV 0.01 : 0.0901116156
CashFlow:
February 15th, 2014 : 1.25
August 15th, 2014 : 1.25
February 15th, 2015 : 1.25
August 15th, 2015 : 1.25
February 15th, 2016 : 1.25
August 15th, 2016 : 1.25
February 15th, 2017 : 1.25
August 15th, 2017 : 1.25
February 15th, 2018 : 1.25
August 15th, 2018 : 1.25
February 15th, 2019 : 1.25
August 15th, 2019 : 1.25
February 15th, 2020 : 1.25
August 15th, 2020 : 1.25
February 15th, 2021 : 1.25
August 15th, 2021 : 1.25
February 15th, 2022 : 1.25
August 15th, 2022 : 1.25
February 15th, 2023 : 1.25
August 15th, 2023 : 1.25
August 15th, 2023 : 100