| 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 <ql/math/optimization/bfgs.hpp> |
| 21 | #include <ql/math/optimization/problem.hpp> |
| 22 | #include <ql/math/optimization/linesearch.hpp> |
| 23 | |
| 24 | namespace QuantLib { |
| 25 | |
| 26 | Array BFGS::getUpdatedDirection(const Problem& P, |
| 27 | Real, |
| 28 | const Array& oldGradient) { |
| 29 | if (inverseHessian_.rows() == 0) |
| 30 | { |
| 31 | // first time in this update, we create needed structures |
| 32 | inverseHessian_ = Matrix(P.currentValue().size(), |
| 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(x: diffGradient[i], y: 2.); |
| 55 | sumxi += std::pow(x: lineSearch_->searchDirection()[i], y: 2.); |
| 56 | } |
| 57 | |
| 58 | if (fac > std::sqrt(x: 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 | } |
| 90 | |