QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
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 \exp^{-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 ext::shared_ptr<OptimizationMethod>(),
56 const Array& l2 = Array(),
57 Real minCutoffTime = 0.0,
58 Real maxCutoffTime = QL_MAX_REAL,
59 Size numCoeffs = 9,
60 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>());
69 Size numCoeffs,
70 Real fixedKappa,
71 const Array& weights = Array() );
72
73
74 std::unique_ptr<FittedBondDiscountCurve::FittingMethod> clone() const override;
75 private:
78 Size size() const override;
79 DiscountFactor discountFunction(const Array& x, Time t) const override;
80 };
81
82
83 //! Nelson-Siegel fitting method
84 /*! Fits a discount function to the form
85 \f$ d(t) = \exp^{-r t}, \f$ where the zero rate \f$r\f$ is defined as
86 \f[
87 r \equiv c_0 + (c_1 + c_2)*(1 - exp^{-\kappa*t})/(\kappa t) -
88 c_2 exp^{ - \kappa t}.
89 \f]
90 See: Nelson, C. and A. Siegel (1985): "Parsimonious modeling of yield
91 curves for US Treasury bills." NBER Working Paper Series, no 1594.
92 */
95 public:
97 const ext::shared_ptr<OptimizationMethod>& optimizationMethod =
98 ext::shared_ptr<OptimizationMethod>(),
99 const Array& l2 = Array(),
100 Real minCutoffTime = 0.0,
101 Real maxCutoffTime = QL_MAX_REAL);
103 const Array& l2,
104 Real minCutoffTime = 0.0,
105 Real maxCutoffTime = QL_MAX_REAL);
106 std::unique_ptr<FittedBondDiscountCurve::FittingMethod> clone() const override;
107 private:
108 Size size() const override;
109 DiscountFactor discountFunction(const Array& x, Time t) const override;
110 };
111
112
113 //! Svensson Fitting method
114 /*! Fits a discount function to the form
115 \f$ d(t) = \exp^{-r t}, \f$ where the zero rate \f$r\f$ is defined as
116 \f[
117 r \equiv c_0 + (c_0 + c_1)(\frac {1 - exp^{-\kappa t}}{\kappa t})
118 - c_2exp^{ - \kappa t}
119 + c_3{(\frac{1 - exp^{-\kappa_1 t}}{\kappa_1 t} -exp^{-\kappa_1 t})}.
120 \f]
121 See: Svensson, L. (1994). Estimating and interpreting forward
122 interest rates: Sweden 1992-4.
123 Discussion paper, Centre for Economic Policy Research(1051).
124 */
127 public:
129 const ext::shared_ptr<OptimizationMethod>& optimizationMethod =
130 ext::shared_ptr<OptimizationMethod>(),
131 const Array& l2 = Array(),
132 Real minCutoffTime = 0.0,
133 Real maxCutoffTime = QL_MAX_REAL);
135 const Array& l2,
136 Real minCutoffTime = 0.0,
137 Real maxCutoffTime = QL_MAX_REAL);
138 std::unique_ptr<FittedBondDiscountCurve::FittingMethod> clone() const override;
139 private:
140 Size size() const override;
141 DiscountFactor discountFunction(const Array& x, Time t) const override;
142 };
143
144
145 //! CubicSpline B-splines fitting method
146 /*! Fits a discount function to a set of cubic B-splines
147 \f$ N_{i,3}(t) \f$, i.e.,
148 \f[
149 d(t) = \sum_{i=0}^{n} c_i * N_{i,3}(t)
150 \f]
151
152 See: McCulloch, J. 1971, "Measuring the Term Structure of
153 Interest Rates." Journal of Business, 44: 19-31
154
155 McCulloch, J. 1975, "The tax adjusted yield curve."
156 Journal of Finance, XXX811-30
157
158 \warning "The results are extremely sensitive to the number
159 and location of the knot points, and there is no
160 optimal way of selecting them." James, J. and
161 N. Webber, "Interest Rate Modelling" John Wiley,
162 2000, pp. 440.
163 */
166 public:
167 CubicBSplinesFitting(const std::vector<Time>& knotVector,
168 bool constrainAtZero = true,
169 const Array& weights = Array(),
170 const ext::shared_ptr<OptimizationMethod>& optimizationMethod =
171 ext::shared_ptr<OptimizationMethod>(),
172 const Array& l2 = Array(),
173 Real minCutoffTime = 0.0,
174 Real maxCutoffTime = QL_MAX_REAL);
175 CubicBSplinesFitting(const std::vector<Time>& knotVector,
176 bool constrainAtZero,
177 const Array& weights,
178 const Array& l2,
179 Real minCutoffTime = 0.0,
180 Real maxCutoffTime = QL_MAX_REAL);
181 //! cubic B-spline basis functions
182 Real basisFunction(Integer i, Time t) const;
183 std::unique_ptr<FittedBondDiscountCurve::FittingMethod> clone() const override;
184 private:
185 Size size() const override;
186 DiscountFactor discountFunction(const Array& x, Time t) const override;
189 //! N_th basis function coefficient to solve for when d(0)=1
191 };
192
193
194 //! Simple polynomial fitting method
195 /* Fits a discount function to the simple polynomial form:
196 \f[
197 d(t) = \sum_{i=0}^{degree} c_i * t^{i}
198 \f]
199 where the constants \f$ c_i \f$ are to be determined.
200
201 This is a simple/crude, but fast and robust, means of fitting
202 a yield curve.
203 */
206 public:
208 bool constrainAtZero = true,
209 const Array& weights = Array(),
210 const ext::shared_ptr<OptimizationMethod>& optimizationMethod =
211 ext::shared_ptr<OptimizationMethod>(),
212 const Array& l2 = Array(),
213 Real minCutoffTime = 0.0,
214 Real maxCutoffTime = QL_MAX_REAL);
216 bool constrainAtZero,
217 const Array& weights,
218 const Array& l2,
219 Real minCutoffTime = 0.0,
220 Real maxCutoffTime = QL_MAX_REAL);
221 std::unique_ptr<FittedBondDiscountCurve::FittingMethod> clone() const override;
222 private:
223 Size size() const override;
224 DiscountFactor discountFunction(const Array& x, Time t) const override;
226 };
227
228
229 //! Spread fitting method helper
230 /* Fits a spread curve on top of a discount function according to given parametric method
231 */
234 public:
235 SpreadFittingMethod(const ext::shared_ptr<FittingMethod>& method,
236 Handle<YieldTermStructure> discountCurve,
237 Real minCutoffTime = 0.0,
238 Real maxCutoffTime = QL_MAX_REAL);
239 std::unique_ptr<FittedBondDiscountCurve::FittingMethod> clone() const override;
240 protected:
241 void init() override;
242
243 private:
244 Size size() const override;
245 DiscountFactor discountFunction(const Array& x, Time t) const override;
246 // underlying parametric method
247 ext::shared_ptr<FittingMethod> method_;
248 // adjustment in case underlying discount curve has different reference date
250 // discount curve from on top of which the spread will be calculated
252 };
253}
254
255
256#endif
B-spline basis functions.
1-D array used in linear algebra.
Definition: array.hpp:52
B-spline basis functions.
Definition: bspline.hpp:58
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.
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
template class providing a null value for a given type.
Definition: null.hpp:76
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:35
Maps shared_ptr to either the boost or std implementation.