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
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2009 Chris Kenyon
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/cashflows/inflationcoupon.hpp>
#include <ql/cashflows/cashflowvectors.hpp>
#include <ql/time/daycounters/thirty360.hpp>
#include <ql/cashflows/cpicoupon.hpp>
#include <ql/cashflows/cpicouponpricer.hpp>
namespace QuantLib {
CPICoupon::
CPICoupon(
Real baseCPI, // user provided
const Date& paymentDate,
Real nominal,
const Date& startDate,
const Date& endDate,
Natural fixingDays,
const boost::shared_ptr<ZeroInflationIndex>& zeroIndex,
const Period& observationLag,
CPI::InterpolationType observationInterpolation,
const DayCounter& dayCounter,
Real fixedRate, // aka gearing
Spread spread,
const Date& refPeriodStart,
const Date& refPeriodEnd)
: InflationCoupon(paymentDate, nominal, startDate, endDate,
fixingDays, zeroIndex, observationLag,
dayCounter, refPeriodStart, refPeriodEnd),
baseCPI_(baseCPI), fixedRate_(fixedRate), spread_(spread),
observationInterpolation_(observationInterpolation) {
QL_REQUIRE(std::fabs(baseCPI_) > 1e-16,
"|baseCPI_| < 1e-16, future divide-by-zero problem");
}
void CPICoupon::accept(AcyclicVisitor& v) {
Visitor<CPICoupon>* v1 =
dynamic_cast<Visitor<CPICoupon>*>(&v);
if (v1 != 0)
v1->visit(*this);
else
InflationCoupon::accept(v);
}
bool CPICoupon::checkPricerImpl(
const boost::shared_ptr<InflationCouponPricer>&pricer) const {
return static_cast<bool>(
boost::dynamic_pointer_cast<CPICouponPricer>(pricer));
}
Rate CPICoupon::indexFixing(const Date &d) const {
// you may want to modify the interpolation of the index
// this gives you the chance
Rate I1;
// what interpolation do we use? Index / flat / linear
if (observationInterpolation() == CPI::AsIndex) {
I1 = cpiIndex()->fixing(d);
} else {
// work out what it should be
std::pair<Date,Date> dd = inflationPeriod(d, cpiIndex()->frequency());
Real indexStart = cpiIndex()->fixing(dd.first);
if (observationInterpolation() == CPI::Linear) {
Real indexEnd = cpiIndex()->fixing(dd.second+Period(1,Days));
// linear interpolation
I1 = indexStart + (indexEnd - indexStart) * (d - dd.first)
/ (Real)( (dd.second+Period(1,Days)) - dd.first); // can't get to next period's value within current period
} else {
// no interpolation, i.e. flat = constant, so use start-of-period value
I1 = indexStart;
}
}
return I1;
}
Date CPICashFlow::baseDate() const {
// you may not have a valid date
QL_FAIL("no base date specified");
}
Real CPICashFlow::baseFixing() const {
return baseFixing_;
}
Real CPICashFlow::amount() const {
Real I0 = baseFixing();
Real I1;
// what interpolation do we use? Index / flat / linear
if (interpolation() == CPI::AsIndex ) {
I1 = index()->fixing(fixingDate());
} else {
// work out what it should be
//std::cout << fixingDate() << " and " << frequency() << std::endl;
//std::pair<Date,Date> dd = inflationPeriod(fixingDate(), frequency());
//std::cout << fixingDate() << " and " << dd.first << " " << dd.second << std::endl;
// work out what it should be
std::pair<Date,Date> dd = inflationPeriod(fixingDate(), frequency());
Real indexStart = index()->fixing(dd.first);
if (interpolation() == CPI::Linear) {
Real indexEnd = index()->fixing(dd.second+Period(1,Days));
// linear interpolation
//std::cout << indexStart << " and " << indexEnd << std::endl;
I1 = indexStart + (indexEnd - indexStart) * (fixingDate() - dd.first)
/ ( (dd.second+Period(1,Days)) - dd.first); // can't get to next period's value within current period
} else {
// no interpolation, i.e. flat = constant, so use start-of-period value
I1 = indexStart;
}
}
if (growthOnly())
return notional() * (I1 / I0 - 1.0);
else
return notional() * (I1 / I0);
}
CPILeg::CPILeg(const Schedule& schedule, const boost::shared_ptr<ZeroInflationIndex>& index,
const Real baseCPI, const Period& observationLag) :
schedule_(schedule), index_(index),
baseCPI_(baseCPI), observationLag_(observationLag),
paymentDayCounter_(Thirty360()),
paymentAdjustment_(ModifiedFollowing),
fixingDays_(std::vector<Natural>(1,0)),
observationInterpolation_(CPI::AsIndex),
subtractInflationNominal_(true),
spreads_(std::vector<Real>(1,0))
{}
CPILeg& CPILeg::withObservationInterpolation(CPI::InterpolationType interp) {
observationInterpolation_ = interp;
return *this;
}
CPILeg& CPILeg::withFixedRates(Real fixedRate) {
fixedRates_ = std::vector<Real>(1,fixedRate);
return *this;
}
CPILeg& CPILeg::withFixedRates(const std::vector<Real>& fixedRates) {
fixedRates_ = fixedRates;
return *this;
}
CPILeg& CPILeg::withNotionals(Real notional) {
notionals_ = std::vector<Real>(1,notional);
return *this;
}
CPILeg& CPILeg::withNotionals(const std::vector<Real>& notionals) {
notionals_ = notionals;
return *this;
}
CPILeg& CPILeg::withSubtractInflationNominal(bool growthOnly) {
subtractInflationNominal_ = growthOnly;
return *this;
}
CPILeg& CPILeg::withPaymentDayCounter(const DayCounter& dayCounter) {
paymentDayCounter_ = dayCounter;
return *this;
}
CPILeg& CPILeg::withPaymentAdjustment(BusinessDayConvention convention) {
paymentAdjustment_ = convention;
return *this;
}
CPILeg& CPILeg::withFixingDays(Natural fixingDays) {
fixingDays_ = std::vector<Natural>(1,fixingDays);
return *this;
}
CPILeg& CPILeg::withFixingDays(const std::vector<Natural>& fixingDays) {
fixingDays_ = fixingDays;
return *this;
}
CPILeg& CPILeg::withSpreads(Spread spread) {
spreads_ = std::vector<Spread>(1,spread);
return *this;
}
CPILeg& CPILeg::withSpreads(const std::vector<Spread>& spreads) {
spreads_ = spreads;
return *this;
}
CPILeg& CPILeg::withCaps(Rate cap) {
caps_ = std::vector<Rate>(1,cap);
return *this;
}
CPILeg& CPILeg::withCaps(const std::vector<Rate>& caps) {
caps_ = caps;
return *this;
}
CPILeg& CPILeg::withFloors(Rate floor) {
floors_ = std::vector<Rate>(1,floor);
return *this;
}
CPILeg& CPILeg::withFloors(const std::vector<Rate>& floors) {
floors_ = floors;
return *this;
}
CPILeg::operator Leg() const {
QL_REQUIRE(!notionals_.empty(), "no notional given");
Size n = schedule_.size()-1;
Calendar calendar = schedule_.calendar();
Leg leg;
leg.reserve(n+1); // +1 for notional, we always have some sort ...
if (n>0) {
QL_REQUIRE(!fixedRates_.empty() || !spreads_.empty(),
"no fixedRates or spreads given");
Date refStart, start, refEnd, end;
for (Size i=0; i<n; ++i) {
refStart = start = schedule_.date(i);
refEnd = end = schedule_.date(i+1);
Date paymentDate = calendar.adjust(end, paymentAdjustment_);
if (i==0 && !schedule_.isRegular(i+1)) {
BusinessDayConvention bdc = schedule_.businessDayConvention();
refStart = schedule_.calendar().adjust(end - schedule_.tenor(), bdc);
}
if (i==n-1 && !schedule_.isRegular(i+1)) {
BusinessDayConvention bdc = schedule_.businessDayConvention();
refEnd = schedule_.calendar().adjust(start + schedule_.tenor(), bdc);
}
if (detail::get(fixedRates_, i, 1.0) == 0.0) { // fixed coupon
leg.push_back(boost::shared_ptr<CashFlow>
(new FixedRateCoupon
(paymentDate, detail::get(notionals_, i, 0.0),
detail::effectiveFixedRate(spreads_,caps_,floors_,i),
paymentDayCounter_, start, end, refStart, refEnd)));
} else { // zero inflation coupon
if (detail::noOption(caps_, floors_, i)) { // just swaplet
boost::shared_ptr<CPICoupon> coup;
coup = boost::shared_ptr<CPICoupon>
(new CPICoupon(baseCPI_, // all have same base for ratio
paymentDate,
detail::get(notionals_, i, 0.0),
start, end,
detail::get(fixingDays_, i, 0.0),
index_, observationLag_,
observationInterpolation_,
paymentDayCounter_,
detail::get(fixedRates_, i, 0.0),
detail::get(spreads_, i, 0.0),
refStart, refEnd));
// in this case you can set a pricer
// straight away because it only provides computation - not data
boost::shared_ptr<CPICouponPricer> pricer
(new CPICouponPricer);
coup->setPricer(pricer);
leg.push_back(boost::dynamic_pointer_cast<CashFlow>(coup));
} else { // cap/floorlet
QL_FAIL("caps/floors on CPI coupons not implemented.");
}
}
}
}
// in CPI legs you always have a notional flow of some sort
Date paymentDate = calendar.adjust(schedule_.date(n), paymentAdjustment_);
Date fixingDate = paymentDate - observationLag_;
boost::shared_ptr<CashFlow> xnl(new CPICashFlow
(detail::get(notionals_, n, 0.0), index_,
Date(), // is fake, i.e. you do not have one
baseCPI_, fixingDate, paymentDate,
subtractInflationNominal_, observationInterpolation_,
index_->frequency())
);
leg.push_back(xnl);
return leg;
}
} // namespace RiskLib
|