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
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2002, 2003 Decillion Pty(Ltd)
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
<http://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/legacy/termstructures/extendeddiscountcurve.hpp>
#include <ql/legacy/termstructures/compoundforward.hpp>
namespace QuantLib {
ExtendedDiscountCurve::ExtendedDiscountCurve(
const std::vector<Date>& dates,
const std::vector<DiscountFactor>& discounts,
const Calendar& calendar,
const BusinessDayConvention conv,
const DayCounter& dayCounter)
: DiscountCurve(dates, discounts, dayCounter, calendar),
conv_(conv) {
calibrateNodes();
}
void ExtendedDiscountCurve::calibrateNodes() const {
Size i;
Integer ci;
std::vector<Date> dates = dates_;
std::vector<Time> times = times_;
std::vector<Rate> discounts = data_;
for (i = 0, ci = 1; i < dates.size(); i++) {
Date rateDate = dates[i];
Date tmpDate = calendar().advance(referenceDate(),
ci, Months, conv_);
while (rateDate > tmpDate) {
dates.insert(dates.begin() + i, tmpDate);
Time t = dayCounter().yearFraction(referenceDate(),tmpDate);
times.insert(times.begin() + i, t);
discounts.insert(discounts.begin() + i,
interpolation_(t,true));
i++;
tmpDate = calendar().advance(referenceDate(),
++ci, Months, conv_);
}
if (tmpDate == rateDate)
ci++;
}
dates_ = dates;
times_ = times;
data_ = discounts;
interpolation_ = LogLinearInterpolation(times_.begin(), times_.end(),
data_.begin());
interpolation_.update();
}
boost::shared_ptr<CompoundForward>
ExtendedDiscountCurve::reversebootstrap(Integer compounding) const {
std::vector<Rate> forwards;
Date compoundDate = calendar().advance(referenceDate(),
12/compounding,
Months, conv_);
Time compoundTime = dayCounter().yearFraction(referenceDate(),
compoundDate);
Real qFactor = 0.0;
Size i;
Integer ci;
// Ignore first entry (SPOT with df=1.0)
for (i = 1, ci = 1; i < dates_.size(); i++) {
Rate fwd;
Date rateDate = dates_[i];
Time t = dayCounter().yearFraction(referenceDate(),rateDate);
DiscountFactor df = discount(t);
if (t <= compoundTime) {
fwd = ((1.0/df)-1.0)/t;
qFactor = df*t;
} else {
Date tmpDate = calendar().advance(referenceDate(),
(12/compounding) * (ci+1),
Months, conv_);
Time tt = dayCounter().yearFraction(compoundDate, rateDate);
fwd = (1.0-df)/(qFactor+df*tt);
// Rates on non-compounding boundaries?
if (rateDate >= tmpDate) {
ci++;
qFactor += df*tt;
compoundDate = tmpDate;
}
}
forwards.push_back(fwd);
}
forwards.insert(forwards.begin(),forwards[0]);
return boost::shared_ptr<CompoundForward>(new
CompoundForward(referenceDate(),
dates_, forwards, calendar(), conv_,
compounding,
dayCounter()));
}
Rate ExtendedDiscountCurve::compoundForwardImpl(Time t,
Integer f) const {
if (f == 0) {
Rate zy = zeroYieldImpl(t);
if (f == 0)
return zy;
if (t <= 1.0/f)
return (std::exp(zy*t)-1.0)/t;
return (std::exp(zy*(1.0/f))-1.0)*f;
}
return forwardCurve(f)->compoundForward(t,f);
}
Rate ExtendedDiscountCurve::zeroYieldImpl(Time t) const {
DiscountFactor df;
if (t==0.0) {
Time dt = 0.001;
df = discountImpl(dt);
return Rate(-std::log(df)/dt);
} else {
df = discountImpl(t);
return Rate(-std::log(df)/t);
}
}
boost::shared_ptr<CompoundForward>
ExtendedDiscountCurve::forwardCurve(Integer compounding) const {
if (forwardCurveMap_.find(compounding) == forwardCurveMap_.end())
forwardCurveMap_[compounding] = reversebootstrap(compounding);
return forwardCurveMap_[compounding];
}
}
|