QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
leastsquare.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) 2001, 2002, 2003 Nicolas Di Césaré
5 Copyright (C) 2005, 2007 StatPro Italia srl
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 leastsquare.hpp
22 \brief Least square cost function
23*/
24
25#ifndef quantlib_least_square_hpp
26#define quantlib_least_square_hpp
27
30#include <ql/math/matrix.hpp>
31
32namespace QuantLib {
33
34 class Constraint;
35 class OptimizationMethod;
36
37 //! Base class for least square problem
39 public:
40 virtual ~LeastSquareProblem() = default;
41 //! size of the problem ie size of target vector
42 virtual Size size() = 0;
43 //! compute the target vector and the values of the function to fit
44 virtual void targetAndValue(const Array& x,
45 Array& target,
46 Array& fct2fit) = 0;
47 /*! compute the target vector, the values of the function to fit
48 and the matrix of derivatives
49 */
50 virtual void targetValueAndGradient(const Array& x,
51 Matrix& grad_fct2fit,
52 Array& target,
53 Array& fct2fit) = 0;
54 };
55
56 //! Cost function for least-square problems
57 /*! Implements a cost function using the interface provided by
58 the LeastSquareProblem class.
59 */
61 public:
62 //! Default constructor
64 //! Destructor
65 ~LeastSquareFunction() override = default;
66
67 //! compute value of the least square function
68 Real value(const Array& x) const override;
69 Array values(const Array&) const override;
70 //! compute vector of derivatives of the least square function
71 void gradient(Array& grad_f, const Array& x) const override;
72 //! compute value and gradient of the least square function
73 Real valueAndGradient(Array& grad_f, const Array& x) const override;
74
75 protected:
76 //! least square problem
78 };
79
80 //! Non-linear least-square method.
81 /*! Using a given optimization algorithm (default is conjugate
82 gradient),
83
84 \f[ min \{ r(x) : x in R^n \} \f]
85
86 where \f$ r(x) = |f(x)|^2 \f$ is the Euclidean norm of \f$
87 f(x) \f$ for some vector-valued function \f$ f \f$ from
88 \f$ R^n \f$ to \f$ R^m \f$,
89 \f[ f = (f_1, ..., f_m) \f]
90 with \f$ f_i(x) = b_i - \phi(x,t_i) \f$ where \f$ b \f$ is the
91 vector of target data and \f$ phi \f$ is a scalar function.
92
93 Assuming the differentiability of \f$ f \f$, the gradient of
94 \f$ r \f$ is defined by
95 \f[ grad r(x) = f'(x)^t.f(x) \f]
96 */
98 public:
99 //! Default constructor
101 Real accuracy = 1e-4,
102 Size maxiter = 100);
103 //! Default constructor
105 Real accuracy,
106 Size maxiter,
107 ext::shared_ptr<OptimizationMethod> om);
108 //! Destructor
110
111 //! Solve least square problem using numerix solver
112 Array& perform(LeastSquareProblem& lsProblem);
113
114 void setInitialValue(const Array& initialValue) {
115 initialValue_ = initialValue;
116 }
117
118 //! return the results
119 Array& results() { return results_; }
120
121 //! return the least square residual norm
122 Real residualNorm() const { return resnorm_; }
123
124 //! return last function value
125 Real lastValue() const { return bestAccuracy_; }
126
127 //! return exit flag
128 Integer exitFlag() const { return exitFlag_; }
129
130 //! return the performed number of iterations
132
133 private:
134 //! solution vector
136 //! least square residual norm
138 //! Exit flag of the optimization process
140 //! required accuracy of the solver
142 //! maximum and real number of iterations
144 //! Optimization method
145 ext::shared_ptr<OptimizationMethod> om_;
146 //constraint
148
149 };
150
151}
152
153#endif
1-D array used in linear algebra.
Definition: array.hpp:52
Base constraint class.
Definition: constraint.hpp:35
Cost function abstract class for optimization problem.
Cost function for least-square problems.
Definition: leastsquare.hpp:60
~LeastSquareFunction() override=default
Destructor.
Real value(const Array &x) const override
compute value of the least square function
Definition: leastsquare.cpp:28
LeastSquareFunction(LeastSquareProblem &lsp)
Default constructor.
Definition: leastsquare.hpp:63
LeastSquareProblem & lsp_
least square problem
Definition: leastsquare.hpp:77
Array values(const Array &) const override
method to overload to compute the cost function values in x
Definition: leastsquare.cpp:39
void gradient(Array &grad_f, const Array &x) const override
compute vector of derivatives of the least square function
Definition: leastsquare.cpp:49
Real valueAndGradient(Array &grad_f, const Array &x) const override
compute value and gradient of the least square function
Definition: leastsquare.cpp:63
Base class for least square problem.
Definition: leastsquare.hpp:38
virtual void targetAndValue(const Array &x, Array &target, Array &fct2fit)=0
compute the target vector and the values of the function to fit
virtual void targetValueAndGradient(const Array &x, Matrix &grad_fct2fit, Array &target, Array &fct2fit)=0
virtual Size size()=0
size of the problem ie size of target vector
virtual ~LeastSquareProblem()=default
Matrix used in linear algebra.
Definition: matrix.hpp:41
Non-linear least-square method.
Definition: leastsquare.hpp:97
Integer exitFlag() const
return exit flag
void setInitialValue(const Array &initialValue)
Real lastValue() const
return last function value
Array & results()
return the results
ext::shared_ptr< OptimizationMethod > om_
Optimization method.
Real accuracy_
required accuracy of the solver
Integer exitFlag_
Exit flag of the optimization process.
Array & perform(LeastSquareProblem &lsProblem)
Solve least square problem using numerix solver.
Definition: leastsquare.cpp:93
Array results_
solution vector
Real residualNorm() const
return the least square residual norm
Real resnorm_
least square residual norm
~NonLinearLeastSquare()=default
Destructor.
Integer iterationsNumber() const
return the performed number of iterations
Size maxIterations_
maximum and real number of iterations
Conjugate gradient optimization method.
QL_REAL Real
real number
Definition: types.hpp:50
QL_INTEGER Integer
integer number
Definition: types.hpp:35
std::size_t Size
size of a container
Definition: types.hpp:58
matrix used in linear algebra.
Definition: any.hpp:35
Abstract optimization problem class.