QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
bfgs.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) 2009 Frédéric Degraeve
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
23
24namespace QuantLib {
25
27 Real,
28 const Array& oldGradient) {
29 if (inverseHessian_.rows() == 0)
30 {
31 // first time in this update, we create needed structures
33 P.currentValue().size(), 0.);
34 for (Size i = 0; i < P.currentValue().size(); ++i)
35 inverseHessian_[i][i] = 1.;
36 }
37
38 Array diffGradient;
39 Array diffGradientWithHessianApplied(P.currentValue().size(), 0.);
40
41 diffGradient = lineSearch_->lastGradient() - oldGradient;
42 for (Size i = 0; i < P.currentValue().size(); ++i)
43 for (Size j = 0; j < P.currentValue().size(); ++j)
44 diffGradientWithHessianApplied[i] += inverseHessian_[i][j] * diffGradient[j];
45
46 Real fac, fae, fad;
47 Real sumdg, sumxi;
48
49 fac = fae = sumdg = sumxi = 0.;
50 for (Size i = 0; i < P.currentValue().size(); ++i)
51 {
52 fac += diffGradient[i] * lineSearch_->searchDirection()[i];
53 fae += diffGradient[i] * diffGradientWithHessianApplied[i];
54 sumdg += std::pow(diffGradient[i], 2.);
55 sumxi += std::pow(lineSearch_->searchDirection()[i], 2.);
56 }
57
58 if (fac > std::sqrt(1e-8 * sumdg * sumxi)) // skip update if fac not sufficiently positive
59 {
60 fac = 1.0 / fac;
61 fad = 1.0 / fae;
62
63 for (Size i = 0; i < P.currentValue().size(); ++i)
64 diffGradient[i] = fac * lineSearch_->searchDirection()[i] - fad * diffGradientWithHessianApplied[i];
65
66 for (Size i = 0; i < P.currentValue().size(); ++i)
67 for (Size j = 0; j < P.currentValue().size(); ++j)
68 {
69 inverseHessian_[i][j] += fac * lineSearch_->searchDirection()[i] * lineSearch_->searchDirection()[j];
70 inverseHessian_[i][j] -= fad * diffGradientWithHessianApplied[i] * diffGradientWithHessianApplied[j];
71 inverseHessian_[i][j] += fae * diffGradient[i] * diffGradient[j];
72 }
73 }
74 //else
75 // throw "BFGS: FAC not sufficiently positive";
76
77
78 Array direction(P.currentValue().size());
79 for (Size i = 0; i < P.currentValue().size(); ++i)
80 {
81 direction[i] = 0.0;
82 for (Size j = 0; j < P.currentValue().size(); ++j)
83 direction[i] -= inverseHessian_[i][j] * lineSearch_->lastGradient()[j];
84 }
85
86 return direction;
87 }
88
89}
Broyden-Fletcher-Goldfarb-Shanno optimization method.
1-D array used in linear algebra.
Definition: array.hpp:52
Size size() const
dimension of the array
Definition: array.hpp:495
Matrix inverseHessian_
inverse of hessian matrix
Definition: bfgs.hpp:50
Array getUpdatedDirection(const Problem &P, Real gold2, const Array &oldGradient) override
computes the new search direction
Definition: bfgs.cpp:26
ext::shared_ptr< LineSearch > lineSearch_
line search
Matrix used in linear algebra.
Definition: matrix.hpp:41
Size rows() const
Definition: matrix.hpp:504
Constrained optimization problem.
Definition: problem.hpp:42
const Array & currentValue() const
current value of the local minimum
Definition: problem.hpp:81
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Line search abstract class.
Definition: any.hpp:35
Abstract optimization problem class.