QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
leastsquare.cpp
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 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
24#include <utility>
25
26namespace QuantLib {
27
29 // size of target and function to fit vectors
30 Array target(lsp_.size()), fct2fit(lsp_.size());
31 // compute its values
32 lsp_.targetAndValue(x, target, fct2fit);
33 // do the difference
34 Array diff = target - fct2fit;
35 // and compute the scalar product (square of the norm)
36 return DotProduct(diff, diff);
37 }
38
40 // size of target and function to fit vectors
41 Array target(lsp_.size()), fct2fit(lsp_.size());
42 // compute its values
43 lsp_.targetAndValue(x, target, fct2fit);
44 // do the difference
45 Array diff = target - fct2fit;
46 return diff*diff;
47 }
48
50 const Array& x) const {
51 // size of target and function to fit vectors
52 Array target (lsp_.size ()), fct2fit (lsp_.size ());
53 // size of gradient matrix
54 Matrix grad_fct2fit (lsp_.size (), x.size ());
55 // compute its values
56 lsp_.targetValueAndGradient(x, grad_fct2fit, target, fct2fit);
57 // do the difference
58 Array diff = target - fct2fit;
59 // compute derivative
60 grad_f = -2.0*(transpose(grad_fct2fit)*diff);
61 }
62
64 const Array& x) const {
65 // size of target and function to fit vectors
66 Array target(lsp_.size()), fct2fit(lsp_.size());
67 // size of gradient matrix
68 Matrix grad_fct2fit(lsp_.size(), x.size());
69 // compute its values
70 lsp_.targetValueAndGradient(x, grad_fct2fit, target, fct2fit);
71 // do the difference
72 Array diff = target - fct2fit;
73 // compute derivative
74 grad_f = -2.0*(transpose(grad_fct2fit)*diff);
75 // and compute the scalar product (square of the norm)
76 return DotProduct(diff, diff);
77 }
78
80 Real accuracy,
81 Size maxiter)
82 : exitFlag_(-1), accuracy_ (accuracy), maxIterations_ (maxiter),
83 om_ (ext::shared_ptr<OptimizationMethod>(new ConjugateGradient())),
84 c_(c)
85 {}
86
88 Real accuracy,
89 Size maxiter,
90 ext::shared_ptr<OptimizationMethod> om)
91 : exitFlag_(-1), accuracy_(accuracy), maxIterations_(maxiter), om_(std::move(om)), c_(c) {}
92
94 Real eps = accuracy_;
95
96 // wrap the least square problem in an optimization function
97 LeastSquareFunction lsf(lsProblem);
98
99 // define optimization problem
100 Problem P(lsf, c_, initialValue_);
101
102 // minimize
104 std::min(static_cast<Size>(maxIterations_/2), static_cast<Size>(100)),
105 eps, eps, eps);
106 exitFlag_ = om_->minimize(P, ec);
107
108 // summarize results of minimization
109 // nbIterations_ = om_->iterationNumber();
110
114
115 return results_;
116 }
117
118}
1-D array used in linear algebra.
Definition: array.hpp:52
Size size() const
dimension of the array
Definition: array.hpp:495
Multi-dimensional Conjugate Gradient class.
Base constraint class.
Definition: constraint.hpp:35
Criteria to end optimization process:
Definition: endcriteria.hpp:40
Cost function for least-square problems.
Definition: leastsquare.hpp:60
Real value(const Array &x) const override
compute value of the least square function
Definition: leastsquare.cpp:28
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
Matrix used in linear algebra.
Definition: matrix.hpp:41
ext::shared_ptr< OptimizationMethod > om_
Optimization method.
NonLinearLeastSquare(Constraint &c, Real accuracy=1e-4, Size maxiter=100)
Default constructor.
Definition: leastsquare.cpp:79
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 resnorm_
least square residual norm
Size maxIterations_
maximum and real number of iterations
Abstract class for constrained optimization method.
Definition: method.hpp:36
Constrained optimization problem.
Definition: problem.hpp:42
const Array & currentValue() const
current value of the local minimum
Definition: problem.hpp:81
Real functionValue() const
value of cost function
Definition: problem.hpp:88
Conjugate gradient optimization method.
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Least square cost function.
Definition: any.hpp:35
Matrix transpose(const Matrix &m)
Definition: matrix.hpp:700
Real DotProduct(const Array &v1, const Array &v2)
Definition: array.hpp:553
STL namespace.
Abstract optimization problem class.