1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<https://www.quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
#include <ql/time/daycounters/actualactual.hpp>
#include <algorithm>
#include <cmath>
namespace QuantLib {
namespace {
// the template argument works around passing a protected type
template <class T>
Integer findCouponsPerYear(const T& impl,
Date refStart, Date refEnd) {
// This will only work for day counts longer than 15 days.
auto months = (Integer)std::lround(12 * Real(impl.dayCount(refStart, refEnd)) / 365.0);
return (Integer)std::lround(12.0 / Real(months));
}
/* An ISMA day counter either needs a schedule or to have
been explicitly passed a reference period. This usage
leads to inaccurate year fractions.
*/
template <class T>
Time yearFractionGuess(const T& impl,
const Date& start, const Date& end) {
// asymptotically correct.
return Real(impl.dayCount(start, end)) / 365.0;
}
std::vector<Date> getListOfPeriodDatesIncludingQuasiPayments(
const Schedule& schedule) {
// Process the schedule into an array of dates.
Date issueDate = schedule.date(0);
std::vector<Date> newDates = schedule.dates();
if (!schedule.hasIsRegular() || !schedule.isRegular(1))
{
Date firstCoupon = schedule.date(1);
Date notionalFirstCoupon =
schedule.calendar().advance(firstCoupon,
-schedule.tenor(),
schedule.businessDayConvention(),
schedule.endOfMonth());
newDates[0] = notionalFirstCoupon;
//long first coupon
if (notionalFirstCoupon > issueDate) {
Date priorNotionalCoupon =
schedule.calendar().advance(notionalFirstCoupon,
-schedule.tenor(),
schedule.businessDayConvention(),
schedule.endOfMonth());
newDates.insert(newDates.begin(),
priorNotionalCoupon); //insert as the first element?
}
}
if (!schedule.hasIsRegular() || !schedule.isRegular(schedule.size() - 1))
{
Date notionalLastCoupon =
schedule.calendar().advance(schedule.date(schedule.size() - 2),
schedule.tenor(),
schedule.businessDayConvention(),
schedule.endOfMonth());
newDates[schedule.size() - 1] = notionalLastCoupon;
if (notionalLastCoupon < schedule.endDate())
{
Date nextNotionalCoupon =
schedule.calendar().advance(notionalLastCoupon,
schedule.tenor(),
schedule.businessDayConvention(),
schedule.endOfMonth());
newDates.push_back(nextNotionalCoupon);
}
}
return newDates;
}
template <class T>
Time yearFractionWithReferenceDates(const T& impl,
const Date& d1, const Date& d2,
const Date& d3, const Date& d4) {
QL_REQUIRE(d1 <= d2,
"This function is only correct if d1 <= d2\n"
"d1: " << d1 << " d2: " << d2);
Real referenceDayCount = Real(impl.dayCount(d3, d4));
//guess how many coupon periods per year:
Integer couponsPerYear;
if (referenceDayCount < 16) {
couponsPerYear = 1;
referenceDayCount = impl.dayCount(d1, d1 + 1 * Years);
}
else {
couponsPerYear = findCouponsPerYear(impl, d3, d4);
}
return Real(impl.dayCount(d1, d2)) / (referenceDayCount*couponsPerYear);
}
}
ext::shared_ptr<DayCounter::Impl>
ActualActual::implementation(ActualActual::Convention c, Schedule schedule) {
switch (c) {
case ISMA:
case Bond:
if (!schedule.empty())
return ext::shared_ptr<DayCounter::Impl>(new ISMA_Impl(std::move(schedule)));
else
return ext::shared_ptr<DayCounter::Impl>(new Old_ISMA_Impl);
case ISDA:
case Historical:
case Actual365:
return ext::shared_ptr<DayCounter::Impl>(new ISDA_Impl);
case AFB:
case Euro:
return ext::shared_ptr<DayCounter::Impl>(new AFB_Impl);
default:
QL_FAIL("unknown act/act convention");
}
}
Time ActualActual::ISMA_Impl::yearFraction(const Date& d1,
const Date& d2,
const Date& d3,
const Date& d4) const {
if (d1 == d2) {
return 0.0;
} else if (d2 < d1) {
return -yearFraction(d2, d1, d3, d4);
}
std::vector<Date> couponDates =
getListOfPeriodDatesIncludingQuasiPayments(schedule_);
Date firstDate = *std::min_element(couponDates.begin(), couponDates.end());
Date lastDate = *std::max_element(couponDates.begin(), couponDates.end());
QL_REQUIRE(d1 >= firstDate && d2 <= lastDate, "Dates out of range of schedule: "
<< "date 1: " << d1 << ", date 2: " << d2 << ", first date: "
<< firstDate << ", last date: " << lastDate);
Real yearFractionSum = 0.0;
for (Size i = 0; i < couponDates.size() - 1; i++) {
Date startReferencePeriod = couponDates[i];
Date endReferencePeriod = couponDates[i + 1];
if (d1 < endReferencePeriod && d2 > startReferencePeriod) {
yearFractionSum +=
yearFractionWithReferenceDates(*this,
std::max(d1, startReferencePeriod),
std::min(d2, endReferencePeriod),
startReferencePeriod,
endReferencePeriod);
}
}
return yearFractionSum;
}
Time ActualActual::Old_ISMA_Impl::yearFraction(const Date& d1,
const Date& d2,
const Date& d3,
const Date& d4) const {
if (d1 == d2)
return 0.0;
if (d1 > d2)
return -yearFraction(d2,d1,d3,d4);
// when the reference period is not specified, try taking
// it equal to (d1,d2)
Date refPeriodStart = (d3 != Date() ? d3 : d1);
Date refPeriodEnd = (d4 != Date() ? d4 : d2);
QL_REQUIRE(refPeriodEnd > refPeriodStart && refPeriodEnd > d1,
"invalid reference period: "
<< "date 1: " << d1
<< ", date 2: " << d2
<< ", reference period start: " << refPeriodStart
<< ", reference period end: " << refPeriodEnd);
// estimate roughly the length in months of a period
auto months = (Integer)std::lround(12 * Real(refPeriodEnd - refPeriodStart) / 365);
// for short periods...
if (months == 0) {
// ...take the reference period as 1 year from d1
refPeriodStart = d1;
refPeriodEnd = d1 + 1*Years;
months = 12;
}
Time period = Real(months)/12.0;
if (d2 <= refPeriodEnd) {
// here refPeriodEnd is a future (notional?) payment date
if (d1 >= refPeriodStart) {
// here refPeriodStart is the last (maybe notional)
// payment date.
// refPeriodStart <= d1 <= d2 <= refPeriodEnd
// [maybe the equality should be enforced, since
// refPeriodStart < d1 <= d2 < refPeriodEnd
// could give wrong results] ???
return period*Real(daysBetween(d1,d2)) /
daysBetween(refPeriodStart,refPeriodEnd);
} else {
// here refPeriodStart is the next (maybe notional)
// payment date and refPeriodEnd is the second next
// (maybe notional) payment date.
// d1 < refPeriodStart < refPeriodEnd
// AND d2 <= refPeriodEnd
// this case is long first coupon
// the last notional payment date
Date previousRef = refPeriodStart - months*Months;
if (d2 > refPeriodStart)
return yearFraction(d1, refPeriodStart, previousRef,
refPeriodStart) +
yearFraction(refPeriodStart, d2, refPeriodStart,
refPeriodEnd);
else
return yearFraction(d1,d2,previousRef,refPeriodStart);
}
} else {
// here refPeriodEnd is the last (notional?) payment date
// d1 < refPeriodEnd < d2 AND refPeriodStart < refPeriodEnd
QL_REQUIRE(refPeriodStart<=d1,
"invalid dates: "
"d1 < refPeriodStart < refPeriodEnd < d2");
// now it is: refPeriodStart <= d1 < refPeriodEnd < d2
// the part from d1 to refPeriodEnd
Time sum = yearFraction(d1, refPeriodEnd,
refPeriodStart, refPeriodEnd);
// the part from refPeriodEnd to d2
// count how many regular periods are in [refPeriodEnd, d2],
// then add the remaining time
Integer i=0;
Date newRefStart, newRefEnd;
for (;;) {
newRefStart = refPeriodEnd + (months*i)*Months;
newRefEnd = refPeriodEnd + (months*(i+1))*Months;
if (d2 < newRefEnd) {
break;
} else {
sum += period;
i++;
}
}
sum += yearFraction(newRefStart,d2,newRefStart,newRefEnd);
return sum;
}
}
Time ActualActual::ISDA_Impl::yearFraction(const Date& d1,
const Date& d2,
const Date&,
const Date&) const {
if (d1 == d2)
return 0.0;
if (d1 > d2)
return -yearFraction(d2,d1,Date(),Date());
Integer y1 = d1.year(), y2 = d2.year();
Real dib1 = (Date::isLeap(y1) ? 366.0 : 365.0),
dib2 = (Date::isLeap(y2) ? 366.0 : 365.0);
Time sum = y2 - y1 - 1;
sum += daysBetween(d1, Date(1,January,y1+1))/dib1;
sum += daysBetween(Date(1,January,y2),d2)/dib2;
return sum;
}
Time ActualActual::AFB_Impl::yearFraction(const Date& d1,
const Date& d2,
const Date&,
const Date&) const {
if (d1 == d2)
return 0.0;
if (d1 > d2)
return -yearFraction(d2,d1,Date(),Date());
Date newD2=d2, temp=d2;
Time sum = 0.0;
while (temp > d1) {
temp = newD2 - 1*Years;
if (temp.dayOfMonth()==28 && temp.month()==2
&& Date::isLeap(temp.year())) {
temp += 1;
}
if (temp>=d1) {
sum += 1.0;
newD2 = temp;
}
}
Real den = 365.0;
if (Date::isLeap(newD2.year())) {
temp = Date(29, February, newD2.year());
if (newD2>temp && d1<=temp)
den += 1.0;
} else if (Date::isLeap(d1.year())) {
temp = Date(29, February, d1.year());
if (newD2>temp && d1<=temp)
den += 1.0;
}
return sum+daysBetween(d1, newD2)/den;
}
}
|