QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
Loading...
Searching...
No Matches
nonlinearfittingmethods.hpp
Go to the documentation of this file.
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) 2010 Alessandro Roveda
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
21/*! \file nonlinearfittingmethods.hpp
22 \brief nonlinear methods to fit a bond discount function
23*/
24
25#ifndef quantlib_nonlinear_fitting_methods_hpp
26#define quantlib_nonlinear_fitting_methods_hpp
27
29#include <ql/math/bspline.hpp>
30#include <ql/shared_ptr.hpp>
31
32namespace QuantLib {
33
34 //! Exponential-splines fitting method
35 /*! Fits a discount function to the exponential form
36 \f[
37 d(t) = \sum_{i=1}^9 c_i e^{-\kappa_i t}
38 \f]
39 where the constants \f$ c_i \f$ and \f$ \kappa \f$ are to be
40 determined. See:Li, B., E. DeWetering, G. Lucas, R. Brenner
41 and A. Shapiro (2001): "Merrill Lynch Exponential Spline
42 Model." Merrill Lynch Working Paper
43
44 \f$ \kappa \f$ can be passed a fixed value, in which case it
45 is excluded from optimization.
46
47 \warning convergence may be slow
48 */
51 public:
53 const Array& weights = Array(),
54 const ext::shared_ptr<OptimizationMethod>& optimizationMethod = {},
55 const Array& l2 = Array(),
56 Real minCutoffTime = 0.0,
57 Real maxCutoffTime = QL_MAX_REAL,
58 Size numCoeffs = 9,
59 Real fixedKappa = Null<Real>(),
62 const Array& weights,
63 const Array& l2,
64 Real minCutoffTime = 0.0,
65 Real maxCutoffTime = QL_MAX_REAL,
66 Size numCoeffs = 9,
67 Real fixedKappa = Null<Real>(),
70 Size numCoeffs,
71 Real fixedKappa,
72 const Array& weights = Array(),
74
75
76 std::unique_ptr<FittedBondDiscountCurve::FittingMethod> clone() const override;
77 private:
80 Size size() const override;
81 DiscountFactor discountFunction(const Array& x, Time t) const override;
82 };
83
84
85 //! Nelson-Siegel fitting method
86 /*! Fits a discount function to the form
87 \f$ d(t) = e^{-r t}, \f$ where the zero rate \f$r\f$ is defined as
88 \f[
89 r \equiv c_0 + (c_1 + c_2) \left( \frac{1 - e^{-\kappa t}}{\kappa t} \right) -
90 c_2 e^{ - \kappa t}.
91 \f]
92 See: Nelson, C. and A. Siegel (1985): "Parsimonious modeling of yield
93 curves for US Treasury bills." NBER Working Paper Series, no 1594.
94 */
97 public:
99 const ext::shared_ptr<OptimizationMethod>& optimizationMethod = {},
100 const Array& l2 = Array(),
101 Real minCutoffTime = 0.0,
102 Real maxCutoffTime = QL_MAX_REAL,
105 const Array& l2,
106 Real minCutoffTime = 0.0,
107 Real maxCutoffTime = QL_MAX_REAL,
109 std::unique_ptr<FittedBondDiscountCurve::FittingMethod> clone() const override;
110 private:
111 Size size() const override;
112 DiscountFactor discountFunction(const Array& x, Time t) const override;
113 };
114
115
116 //! Svensson Fitting method
117 /*! Fits a discount function to the form
118 \f$ d(t) = e^{-r t}, \f$ where the zero rate \f$r\f$ is defined as
119 \f[
120 r \equiv c_0 + (c_1 + c_2) \left( \frac {1 - e^{-\kappa t}}{\kappa t} \right)
121 - c_2 e^{ - \kappa t}
122 + c_3 \left( \frac{1 - e^{-\kappa_1 t}}{\kappa_1 t} -e^{-\kappa_1 t} \right).
123 \f]
124 See: Svensson, L. (1994). Estimating and interpreting forward
125 interest rates: Sweden 1992-4.
126 Discussion paper, Centre for Economic Policy Research(1051).
127 */
130 public:
132 const ext::shared_ptr<OptimizationMethod>& optimizationMethod = {},
133 const Array& l2 = Array(),
134 Real minCutoffTime = 0.0,
135 Real maxCutoffTime = QL_MAX_REAL,
138 const Array& l2,
139 Real minCutoffTime = 0.0,
140 Real maxCutoffTime = QL_MAX_REAL,
142 std::unique_ptr<FittedBondDiscountCurve::FittingMethod> clone() const override;
143 private:
144 Size size() const override;
145 DiscountFactor discountFunction(const Array& x, Time t) const override;
146 };
147
148
149 //! CubicSpline B-splines fitting method
150 /*! Fits a discount function to a set of cubic B-splines
151 \f$ N_{i,3}(t) \f$, i.e.,
152 \f[
153 d(t) = \sum_{i=0}^{n} c_i \times N_{i,3}(t)
154 \f]
155
156 See: McCulloch, J. 1971, "Measuring the Term Structure of
157 Interest Rates." Journal of Business, 44: 19-31
158
159 McCulloch, J. 1975, "The tax adjusted yield curve."
160 Journal of Finance, XXX811-30
161
162 \warning "The results are extremely sensitive to the number
163 and location of the knot points, and there is no
164 optimal way of selecting them." James, J. and
165 N. Webber, "Interest Rate Modelling" John Wiley,
166 2000, pp. 440.
167 */
170 public:
171 CubicBSplinesFitting(const std::vector<Time>& knotVector,
172 bool constrainAtZero = true,
173 const Array& weights = Array(),
174 const ext::shared_ptr<OptimizationMethod>& optimizationMethod = {},
175 const Array& l2 = Array(),
176 Real minCutoffTime = 0.0,
177 Real maxCutoffTime = QL_MAX_REAL,
179 CubicBSplinesFitting(const std::vector<Time>& knotVector,
180 bool constrainAtZero,
181 const Array& weights,
182 const Array& l2,
183 Real minCutoffTime = 0.0,
184 Real maxCutoffTime = QL_MAX_REAL,
186 //! cubic B-spline basis functions
187 Real basisFunction(Integer i, Time t) const;
188 std::unique_ptr<FittedBondDiscountCurve::FittingMethod> clone() const override;
189 private:
190 Size size() const override;
191 DiscountFactor discountFunction(const Array& x, Time t) const override;
194 //! N_th basis function coefficient to solve for when d(0)=1
196 };
197
198
199 //! Simple polynomial fitting method
200 /*! Fits a discount function to the simple polynomial form:
201 \f[
202 d(t) = \sum_{i=0}^{degree} c_i t^{i}
203 \f]
204 where the constants \f$ c_i \f$ are to be determined.
205
206 This is a simple/crude, but fast and robust, means of fitting
207 a yield curve.
208 */
211 public:
213 bool constrainAtZero = true,
214 const Array& weights = Array(),
215 const ext::shared_ptr<OptimizationMethod>& optimizationMethod = {},
216 const Array& l2 = Array(),
217 Real minCutoffTime = 0.0,
218 Real maxCutoffTime = QL_MAX_REAL,
221 bool constrainAtZero,
222 const Array& weights,
223 const Array& l2,
224 Real minCutoffTime = 0.0,
225 Real maxCutoffTime = QL_MAX_REAL,
227 std::unique_ptr<FittedBondDiscountCurve::FittingMethod> clone() const override;
228 private:
229 Size size() const override;
230 DiscountFactor discountFunction(const Array& x, Time t) const override;
232 };
233
234
235 //! Spread fitting method helper
236 /*! Fits a spread curve on top of a discount function according
237 to the given parametric method
238 */
241 public:
242 SpreadFittingMethod(const ext::shared_ptr<FittingMethod>& method,
243 Handle<YieldTermStructure> discountCurve,
244 Real minCutoffTime = 0.0,
245 Real maxCutoffTime = QL_MAX_REAL);
246 std::unique_ptr<FittedBondDiscountCurve::FittingMethod> clone() const override;
247 protected:
248 void init() override;
249
250 private:
251 Size size() const override;
252 DiscountFactor discountFunction(const Array& x, Time t) const override;
253 // underlying parametric method
254 ext::shared_ptr<FittingMethod> method_;
255 // adjustment in case underlying discount curve has different reference date
257 // discount curve from on top of which the spread will be calculated
259 };
260}
261
262
263#endif
B-spline basis functions.
1-D array used in linear algebra.
Definition: array.hpp:52
B-spline basis functions.
Definition: bspline.hpp:58
Base constraint class.
Definition: constraint.hpp:35
CubicSpline B-splines fitting method.
std::unique_ptr< FittedBondDiscountCurve::FittingMethod > clone() const override
clone of the current object
Size size() const override
total number of coefficients to fit/solve for
Real basisFunction(Integer i, Time t) const
cubic B-spline basis functions
Natural N_
N_th basis function coefficient to solve for when d(0)=1.
DiscountFactor discountFunction(const Array &x, Time t) const override
discount function called by FittedBondDiscountCurve
Exponential-splines fitting method.
std::unique_ptr< FittedBondDiscountCurve::FittingMethod > clone() const override
clone of the current object
Size size() const override
total number of coefficients to fit/solve for
DiscountFactor discountFunction(const Array &x, Time t) const override
discount function called by FittedBondDiscountCurve
Base fitting method used to construct a fitted bond discount curve.
const Constraint & constraint() const
return optimization contraint
bool constrainAtZero() const
return whether there is a constraint at zero
Array l2() const
return l2 penalties being used
ext::shared_ptr< OptimizationMethod > optimizationMethod() const
return optimization method being used
Shared handle to an observable.
Definition: handle.hpp:41
Nelson-Siegel fitting method.
std::unique_ptr< FittedBondDiscountCurve::FittingMethod > clone() const override
clone of the current object
Size size() const override
total number of coefficients to fit/solve for
DiscountFactor discountFunction(const Array &x, Time t) const override
discount function called by FittedBondDiscountCurve
No constraint.
Definition: constraint.hpp:79
template class providing a null value for a given type.
Definition: null.hpp:59
Simple polynomial fitting method.
std::unique_ptr< FittedBondDiscountCurve::FittingMethod > clone() const override
clone of the current object
Size size() const override
total number of coefficients to fit/solve for
DiscountFactor discountFunction(const Array &x, Time t) const override
discount function called by FittedBondDiscountCurve
Spread fitting method helper.
std::unique_ptr< FittedBondDiscountCurve::FittingMethod > clone() const override
clone of the current object
Size size() const override
total number of coefficients to fit/solve for
Handle< YieldTermStructure > discountingCurve_
ext::shared_ptr< FittingMethod > method_
void init() override
rerun every time instruments/referenceDate changes
DiscountFactor discountFunction(const Array &x, Time t) const override
discount function called by FittedBondDiscountCurve
std::unique_ptr< FittedBondDiscountCurve::FittingMethod > clone() const override
clone of the current object
Size size() const override
total number of coefficients to fit/solve for
DiscountFactor discountFunction(const Array &x, Time t) const override
discount function called by FittedBondDiscountCurve
const DefaultType & t
discount curve fitted to a set of bonds
#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:37
Maps shared_ptr to either the boost or std implementation.