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 |
|
20 |
#include "optimization/BFGS.hpp" |
21 |
#include "optimization/Problem.hpp" |
22 |
#include "optimization/LineSearch.hpp" |
23 |
|
24 |
namespace QuantLib { |
25 |
|
26 |
DynamicVector<RealType> BFGS::getUpdatedDirection(const Problem& P, |
27 |
RealType, |
28 |
const DynamicVector<RealType>& oldGradient) { |
29 |
if (inverseHessian_.getNRow() == 0) |
30 |
{ |
31 |
// first time in this update, we create needed structures |
32 |
inverseHessian_ = DynamicRectMatrix<RealType>(P.currentValue().size(), |
33 |
P.currentValue().size(), 0.); |
34 |
for (size_t i = 0; i < P.currentValue().size(); ++i) |
35 |
inverseHessian_(i,i) = 1.; |
36 |
} |
37 |
|
38 |
DynamicVector<RealType> diffGradient; |
39 |
DynamicVector<RealType> diffGradientWithHessianApplied(P.currentValue().size(), 0.); |
40 |
|
41 |
diffGradient = lineSearch_->lastGradient() - oldGradient; |
42 |
for (size_t i = 0; i < P.currentValue().size(); ++i) |
43 |
for (size_t j = 0; j < P.currentValue().size(); ++j) |
44 |
diffGradientWithHessianApplied[i] += inverseHessian_(i,j) * diffGradient[j]; |
45 |
|
46 |
double fac, fae, fad; |
47 |
double sumdg, sumxi; |
48 |
|
49 |
fac = fae = sumdg = sumxi = 0.; |
50 |
for (size_t 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_t i = 0; i < P.currentValue().size(); ++i) |
64 |
diffGradient[i] = fac * lineSearch_->searchDirection()[i] - fad * diffGradientWithHessianApplied[i]; |
65 |
|
66 |
for (size_t i = 0; i < P.currentValue().size(); ++i) |
67 |
for (size_t 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 |
DynamicVector<RealType> direction(P.currentValue().size()); |
79 |
for (size_t i = 0; i < P.currentValue().size(); ++i) |
80 |
{ |
81 |
direction[i] = 0.0; |
82 |
for (size_t j = 0; j < P.currentValue().size(); ++j) |
83 |
direction[i] -= inverseHessian_(i,j) * lineSearch_->lastGradient()[j]; |
84 |
} |
85 |
|
86 |
return direction; |
87 |
} |
88 |
|
89 |
} |