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
|
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2007 Allen Kuo
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/termstructures/yield/fittedbonddiscountcurve.hpp>
#include <ql/math/optimization/simplex.hpp>
#include <ql/math/optimization/costfunction.hpp>
#include <ql/math/optimization/constraint.hpp>
#include <ql/cashflows/cashflows.hpp>
namespace QuantLib {
class FittedBondDiscountCurve::FittingMethod::FittingCost
: public CostFunction {
friend class FittedBondDiscountCurve::FittingMethod;
public:
FittingCost(FittedBondDiscountCurve::FittingMethod* fittingMethod);
Real value(const Array& x) const;
Disposable<Array> values(const Array& x) const;
private:
FittedBondDiscountCurve::FittingMethod* fittingMethod_;
mutable Date refDate_;
mutable std::vector<Integer> startingCashFlowIndex_;
};
FittedBondDiscountCurve::FittedBondDiscountCurve (
Natural settlementDays,
const Calendar& calendar,
const std::vector<boost::shared_ptr<FixedRateBondHelper> >&
instruments,
const DayCounter& dayCounter,
const FittingMethod& fittingMethod,
Real accuracy,
Size maxEvaluations,
const Array& guess,
Real simplexLambda)
: YieldTermStructure(settlementDays, calendar, dayCounter),
accuracy_(accuracy),
maxEvaluations_(maxEvaluations),
simplexLambda_(simplexLambda),
guessSolution_(guess),
instruments_(instruments),
fittingMethod_(fittingMethod) {
fittingMethod_->curve_ = this;
setup();
}
FittedBondDiscountCurve::FittedBondDiscountCurve (
const Date& referenceDate,
const std::vector<boost::shared_ptr<FixedRateBondHelper> >&
instruments,
const DayCounter& dayCounter,
const FittingMethod& fittingMethod,
Real accuracy,
Size maxEvaluations,
const Array& guess,
Real simplexLambda)
: YieldTermStructure(referenceDate, Calendar(), dayCounter),
accuracy_(accuracy),
maxEvaluations_(maxEvaluations),
simplexLambda_(simplexLambda),
guessSolution_(guess),
instruments_(instruments),
fittingMethod_(fittingMethod) {
fittingMethod_->curve_ = this;
setup();
}
Size FittedBondDiscountCurve::numberOfBonds() const {
return instruments_.size();
}
Date FittedBondDiscountCurve::maxDate() const {
calculate();
return maxDate_;
}
const FittedBondDiscountCurve::FittingMethod&
FittedBondDiscountCurve::fitResults() const {
calculate();
return *fittingMethod_;
}
void FittedBondDiscountCurve::update() {
TermStructure::update();
LazyObject::update();
}
void FittedBondDiscountCurve::setup() {
for (Size i=0; i<instruments_.size(); ++i)
registerWith(instruments_[i]);
}
void FittedBondDiscountCurve::performCalculations() const {
QL_REQUIRE(!instruments_.empty(), "no instruments given");
// double check bond quotes still valid and/or instruments not expired
for (Size i=0; i<instruments_.size(); ++i) {
QL_REQUIRE(instruments_[i]->quoteIsValid(),
"instrument with invalid quote");
instruments_[i]->setTermStructure(
const_cast<FittedBondDiscountCurve*>(this));
boost::shared_ptr<Bond> bond = instruments_[i]->bond();
QL_REQUIRE(!bond->isExpired(), "expired bond instrument");
}
maxDate_ = Date::minDate();
for (Size i=0; i<instruments_.size(); ++i)
maxDate_ = std::max(maxDate_, instruments_[i]->latestDate());
fittingMethod_->init();
fittingMethod_->calculate();
}
DiscountFactor FittedBondDiscountCurve::discountImpl(Time t) const {
calculate();
return fittingMethod_->discountFunction(fittingMethod_->solution_,t);
}
FittedBondDiscountCurve::FittingMethod::FittingMethod(bool constrainAtZero)
: constrainAtZero_(constrainAtZero) {}
Integer FittedBondDiscountCurve::FittingMethod::numberOfIterations() const {
return numberOfIterations_;
}
Real FittedBondDiscountCurve::FittingMethod::minimumCostValue() const {
return costValue_;
}
Array FittedBondDiscountCurve::FittingMethod::solution() const {
return solution_;
}
void FittedBondDiscountCurve::FittingMethod::init() {
Array tempWeights(curve_->instruments_.size(), 0.0);
Date today = curve_->referenceDate();
Real squaredSum = 0.0;
for (Size k=0; k<curve_->instruments_.size(); ++k) {
boost::shared_ptr<Bond> bond = curve_->instruments_[k]->bond();
Leg leg = bond->cashflows();
Real cleanPrice = curve_->instruments_[k]->quoteValue();
Rate ytm = bond->yield(cleanPrice,
curve_->instruments_[k]->dayCounter(),
Compounded,
curve_->instruments_[k]->frequency(),
today);
InterestRate r(ytm,
curve_->instruments_[k]->dayCounter(),
Compounded,
curve_->instruments_[k]->frequency());
Date settlement = bond->settlementDate(today);
Time duration =
CashFlows::duration(leg, r, Duration::Modified, settlement);
tempWeights[k] = 1.0/duration;
squaredSum += tempWeights[k]*tempWeights[k];
}
weights_ = tempWeights/std::sqrt(squaredSum);
// set cost function related below
costFunction_ = boost::shared_ptr<FittingCost>(new FittingCost(this));
costFunction_->refDate_ = curve_->referenceDate();
costFunction_->startingCashFlowIndex_.clear();
for (Size i=0; i<curve_->instruments_.size(); ++i) {
boost::shared_ptr<Bond> bond = curve_->instruments_[i]->bond();
Date settlementDate = bond->settlementDate(today);
Leg cf = bond->cashflows();
for (Size k=0; k<cf.size(); ++k) {
if (!cf[k]->hasOccurred(settlementDate)) {
costFunction_->startingCashFlowIndex_.push_back(k);
break;
}
}
}
}
void FittedBondDiscountCurve::FittingMethod::calculate() {
FittingCost& costFunction = *costFunction_;
Constraint constraint = NoConstraint();
// start with the guess solution, if it exists
Array x(size(), 0.0);
if (!curve_->guessSolution_.empty()) {
x = curve_->guessSolution_;
}
Simplex simplex(curve_->simplexLambda_);
Problem problem(costFunction, constraint, x);
Natural maxStationaryStateIterations = 100;
Real rootEpsilon = curve_->accuracy_;
Real functionEpsilon = curve_->accuracy_;
Real gradientNormEpsilon = curve_->accuracy_;
EndCriteria endCriteria(curve_->maxEvaluations_,
maxStationaryStateIterations,
rootEpsilon,
functionEpsilon,
gradientNormEpsilon);
simplex.minimize(problem,endCriteria);
solution_ = problem.currentValue();
numberOfIterations_ = problem.functionEvaluation();
costValue_ = problem.functionValue();
// save the results as the guess solution, in case of recalculation
curve_->guessSolution_ = solution_;
}
FittedBondDiscountCurve::FittingMethod::FittingCost::FittingCost(
FittedBondDiscountCurve::FittingMethod* fittingMethod)
: fittingMethod_(fittingMethod) {}
Real FittedBondDiscountCurve::FittingMethod::FittingCost::value(
const Array &x) const {
// speed optimization by setting some of the below in the constructor
// rather than here
Size numberOfBonds = fittingMethod_->curve_->instruments_.size();
Date today = fittingMethod_->curve_->referenceDate();
Array trialDirtyPrice(numberOfBonds,0.);
Real squaredError = 0.0;
for (Size i=0; i<numberOfBonds;++i) {
boost::shared_ptr<Bond> bond =
fittingMethod_->curve_->instruments_[i]->bond();
Real quotedPrice =
fittingMethod_->curve_->instruments_[i]->quoteValue();
Date settlement = bond->settlementDate(today);
Real dirtyPrice = quotedPrice + bond->accruedAmount(settlement);
DayCounter bondDayCount =
fittingMethod_->curve_->instruments_[i]->dayCounter();
Leg cf = bond->cashflows();
// loop over cashFlows: P_j = sum( cf_i * d(t_i))
for (Size k=startingCashFlowIndex_[i]; k<cf.size(); ++k) {
Time tenor = bondDayCount.yearFraction(today, cf[k]->date());
trialDirtyPrice[i] += cf[k]->amount() *
fittingMethod_->discountFunction(x,tenor);
}
// adjust dirty price (NPV) for a forward settlement
if (settlement != today ) {
Time tenor = bondDayCount.yearFraction(today, settlement);
trialDirtyPrice[i] = trialDirtyPrice[i]/
fittingMethod_->discountFunction(x,tenor);
}
squaredError = squaredError +
std::pow(fittingMethod_->weights_[i]*
(trialDirtyPrice[i] - dirtyPrice),2);
}
return squaredError;
}
Disposable<Array>
FittedBondDiscountCurve::FittingMethod::FittingCost::values(
const Array &x) const {
Array y(1);
y[0] = value(x);
return y;
}
}
|