QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
leastsquare.hpp
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
25#ifndef quantlib_least_square_hpp
26#define quantlib_least_square_hpp
27
28#include <ql/math/optimization/problem.hpp>
29#include <ql/math/optimization/conjugategradient.hpp>
30#include <ql/math/matrix.hpp>
31
32namespace QuantLib {
33
34 class Constraint;
35 class OptimizationMethod;
36
39 public:
40 virtual ~LeastSquareProblem() = default;
42 virtual Size size() = 0;
44 virtual void targetAndValue(const Array& x,
45 Array& target,
46 Array& fct2fit) = 0;
50 virtual void targetValueAndGradient(const Array& x,
51 Matrix& grad_fct2fit,
52 Array& target,
53 Array& fct2fit) = 0;
54 };
55
57
61 public:
65 ~LeastSquareFunction() override = default;
66
68 Real value(const Array& x) const override;
69 Array values(const Array&) const override;
71 void gradient(Array& grad_f, const Array& x) const override;
73 Real valueAndGradient(Array& grad_f, const Array& x) const override;
74
75 protected:
78 };
79
81
98 public:
101 Real accuracy = 1e-4,
102 Size maxiter = 100);
105 Real accuracy,
106 Size maxiter,
107 ext::shared_ptr<OptimizationMethod> om);
110
112 Array& perform(LeastSquareProblem& lsProblem);
113
114 void setInitialValue(const Array& initialValue) {
115 initialValue_ = initialValue;
116 }
117
119 Array& results() { return results_; }
120
122 Real residualNorm() const { return resnorm_; }
123
125 Real lastValue() const { return bestAccuracy_; }
126
128 Integer exitFlag() const { return exitFlag_; }
129
132
133 private:
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
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
Definition: any.hpp:35