QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
fittedbonddiscountcurve.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2007 Allen Kuo
5 Copyright (C) 2009 Ferdinando Ametrano
6 Copyright (C) 2015 Andres Hernandez
7
8 This file is part of QuantLib, a free-software/open-source library
9 for financial quantitative analysts and developers - http://quantlib.org/
10
11 QuantLib is free software: you can redistribute it and/or modify it
12 under the terms of the QuantLib license. You should have received a
13 copy of the license along with this program; if not, please email
14 <quantlib-dev@lists.sf.net>. The license is also available online at
15 <http://quantlib.org/license.shtml>.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the license for more details.
20*/
21
26#ifndef quantlib_fitted_bond_discount_curve_hpp
27#define quantlib_fitted_bond_discount_curve_hpp
28
29#include <ql/termstructures/yield/bondhelpers.hpp>
30#include <ql/math/optimization/method.hpp>
31#include <ql/patterns/lazyobject.hpp>
32#include <ql/math/array.hpp>
33#include <ql/utilities/clone.hpp>
34
35namespace QuantLib {
36
38
81 public LazyObject {
82 public:
83 class FittingMethod;
84
86
87
89 const Calendar& calendar,
90 std::vector<ext::shared_ptr<BondHelper> > bonds,
92 const FittingMethod& fittingMethod,
93 Real accuracy = 1.0e-10,
94 Size maxEvaluations = 10000,
95 Array guess = Array(),
96 Real simplexLambda = 1.0,
97 Size maxStationaryStateIterations = 100);
100 std::vector<ext::shared_ptr<BondHelper> > bonds,
101 const DayCounter& dayCounter,
102 const FittingMethod& fittingMethod,
103 Real accuracy = 1.0e-10,
104 Size maxEvaluations = 10000,
105 Array guess = Array(),
106 Real simplexLambda = 1.0,
107 Size maxStationaryStateIterations = 100);
109
111
112
113 Size numberOfBonds() const;
115 Date maxDate() const override;
117 const FittingMethod& fitResults() const;
119
121
122 void update() override;
124
125 private:
126 void setup();
127 void performCalculations() const override;
128 DiscountFactor discountImpl(Time) const override;
129 // target accuracy level to be used in the optimization routine
131 // max number of evaluations to be used in the optimization routine
133 // sets the scale in the (Simplex) optimization routine
135 // max number of evaluations where no improvement to solution is made
137 // a guess solution may be passed into the constructor to speed calcs
139 mutable Date maxDate_;
140 std::vector<ext::shared_ptr<BondHelper> > bondHelpers_;
142 };
143
144
146
182 // internal class
183 class FittingCost;
184 public:
185 virtual ~FittingMethod() = default;
187 virtual Size size() const = 0;
189 Array solution() const;
193 Real minimumCostValue() const;
197 virtual std::unique_ptr<FittingMethod> clone() const = 0;
199 bool constrainAtZero() const;
201 Array weights() const;
203 Array l2() const;
205 ext::shared_ptr<OptimizationMethod> optimizationMethod() const;
207 DiscountFactor discount(const Array& x, Time t) const;
208 protected:
210 FittingMethod(bool constrainAtZero = true,
211 const Array& weights = Array(),
212 ext::shared_ptr<OptimizationMethod> optimizationMethod =
213 ext::shared_ptr<OptimizationMethod>(),
214 Array l2 = Array(),
215 Real minCutoffTime = 0.0,
216 Real maxCutoffTime = QL_MAX_REAL);
218 virtual void init();
221 Time t) const = 0;
222
230
235 ext::shared_ptr<FittingCost> costFunction_;
236 private:
237 // curve optimization called here- adjust optimization parameters here
238 void calculate();
239 // array of normalized (duration) weights, one for each bond helper
241 // array of l2 penalties one for each parameter
243 // whether or not the weights should be calculated internally
245 // total number of iterations used in the optimization routine
246 // (possibly including gradient evaluations)
248 // final value for the minimized cost function
250 // error code returned by OptimizationMethod::minimize()
252 // optimization method to be used, if none provided use Simplex
253 ext::shared_ptr<OptimizationMethod> optimizationMethod_;
254 // flat extrapolation of instantaneous forward before / after cutoff
256 };
257
258 // inline
259
261 return bondHelpers_.size();
262 }
263
265 calculate();
266 return maxDate_;
267 }
268
271 calculate();
272 return *fittingMethod_;
273 }
274
278 }
279
281 for (auto& bondHelper : bondHelpers_)
282 registerWith(bondHelper);
283 }
284
286 calculate();
287 return fittingMethod_->discount(fittingMethod_->solution_, t);
288 }
289
290 inline Integer
292 return numberOfIterations_;
293 }
294
295 inline
297 return costValue_;
298 }
299
300 inline
302 return errorCode_;
303 }
304
306 return solution_;
307 }
308
310 return constrainAtZero_;
311 }
312
314 return weights_;
315 }
316
318 return l2_;
319 }
320
321 inline ext::shared_ptr<OptimizationMethod>
323 return optimizationMethod_;
324 }
325
327 if (t < minCutoffTime_) {
328 // flat fwd extrapolation before min cutoff time
329 return std::exp(std::log(discountFunction(x, minCutoffTime_)) / minCutoffTime_ * t);
330 } else if (t > maxCutoffTime_) {
331 // flat fwd extrapolation after max cutoff time
332 return discountFunction(x, maxCutoffTime_) *
333 std::exp((std::log(discountFunction(x, maxCutoffTime_ + 1E-4)) -
334 std::log(discountFunction(x, maxCutoffTime_))) *
335 1E4 * (t - maxCutoffTime_));
336 } else {
337 return discountFunction(x, t);
338 }
339 }
340}
341
342#endif
1-D array used in linear algebra.
Definition: array.hpp:52
calendar class
Definition: calendar.hpp:61
cloning proxy to an underlying object
Definition: clone.hpp:40
Concrete date class.
Definition: date.hpp:125
day counter class
Definition: daycounter.hpp:44
Base fitting method used to construct a fitted bond discount curve.
FittedBondDiscountCurve * curve_
internal reference to the FittedBondDiscountCurve instance
virtual void init()
rerun every time instruments/referenceDate changes
virtual Size size() const =0
total number of coefficients to fit/solve for
Array guessSolution_
optional guess solution to be passed into constructor.
ext::shared_ptr< OptimizationMethod > optimizationMethod_
bool constrainAtZero() const
return whether there is a constraint at zero
Real minimumCostValue() const
final value of cost function after optimization
Integer numberOfIterations() const
final number of iterations used in the optimization problem
Array solution_
solution array found from optimization, set in calculate()
bool constrainAtZero_
constrains discount function to unity at , if true
virtual std::unique_ptr< FittingMethod > clone() const =0
clone of the current object
virtual DiscountFactor discountFunction(const Array &x, Time t) const =0
discount function called by FittedBondDiscountCurve
EndCriteria::Type errorCode() const
error code of the optimization
Array l2() const
return l2 penalties being used
ext::shared_ptr< OptimizationMethod > optimizationMethod() const
return optimization method being used
ext::shared_ptr< FittingCost > costFunction_
base class sets this cost function used in the optimization routine
DiscountFactor discount(const Array &x, Time t) const
open discountFunction to public
Array solution() const
output array of results of optimization problem
Discount curve fitted to a set of fixed-coupon bonds.
const FittingMethod & fitResults() const
class holding the results of the fit
std::vector< ext::shared_ptr< BondHelper > > bondHelpers_
DiscountFactor discountImpl(Time) const override
discount factor calculation
Date maxDate() const override
the latest date for which the curve can return values
Size numberOfBonds() const
total number of bonds used to fit the yield curve
Framework for calculation on demand and result caching.
Definition: lazyobject.hpp:35
virtual void calculate() const
Definition: lazyobject.hpp:253
void update() override
Definition: lazyobject.hpp:188
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
virtual Natural settlementDays() const
the settlementDays used for reference date calculation
virtual const Date & referenceDate() const
the date at which discount = 1.0 and/or variance = 0.0
virtual Calendar calendar() const
the calendar used for reference and/or option date calculation
virtual DayCounter dayCounter() const
the day counter used for date/time conversion
Interest-rate term structure.
#define QL_MAX_REAL
Definition: qldefines.hpp:176
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real DiscountFactor
discount factor between dates
Definition: types.hpp:66
unsigned QL_INTEGER Natural
positive integer
Definition: types.hpp:43
QL_INTEGER Integer
integer number
Definition: types.hpp:35
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35