| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2011 Ferdinando Ametrano |
| 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 | /*! \file finitedifferencenewtonsafe.hpp |
| 21 | \brief Safe (bracketed) Newton 1-D solver with finite difference derivatives |
| 22 | */ |
| 23 | |
| 24 | #ifndef quantlib_solver1d_finitedifferencenewtonsafe_h |
| 25 | #define quantlib_solver1d_finitedifferencenewtonsafe_h |
| 26 | |
| 27 | #include <ql/math/solver1d.hpp> |
| 28 | |
| 29 | namespace QuantLib { |
| 30 | |
| 31 | //! safe %Newton 1-D solver with finite difference derivatives |
| 32 | /*! |
| 33 | \test the correctness of the returned values is tested by |
| 34 | checking them against known good results. |
| 35 | |
| 36 | \ingroup solvers |
| 37 | */ |
| 38 | class FiniteDifferenceNewtonSafe : public Solver1D<FiniteDifferenceNewtonSafe> { |
| 39 | public: |
| 40 | template <class F> |
| 41 | Real solveImpl(const F& f, |
| 42 | Real xAccuracy) const { |
| 43 | |
| 44 | // Orient the search so that f(xl) < 0 |
| 45 | Real xh, xl; |
| 46 | if (fxMin_ < 0.0) { |
| 47 | xl = xMin_; |
| 48 | xh = xMax_; |
| 49 | } else { |
| 50 | xh = xMin_; |
| 51 | xl = xMax_; |
| 52 | } |
| 53 | |
| 54 | Real froot = f(root_); |
| 55 | ++evaluationNumber_; |
| 56 | // first order finite difference derivative |
| 57 | Real dfroot = xMax_-root_ < root_-xMin_ ? |
| 58 | (fxMax_-froot)/(xMax_-root_) : |
| 59 | (fxMin_-froot)/(xMin_-root_) ; |
| 60 | |
| 61 | // xMax_-xMin_>0 is verified in the constructor |
| 62 | Real dx = xMax_-xMin_; |
| 63 | while (evaluationNumber_<=maxEvaluations_) { |
| 64 | Real frootold = froot; |
| 65 | Real rootold = root_; |
| 66 | Real dxold = dx; |
| 67 | // Bisect if (out of range || not decreasing fast enough) |
| 68 | if ((((root_-xh)*dfroot-froot)* |
| 69 | ((root_-xl)*dfroot-froot) > 0.0) |
| 70 | || (std::fabs(x: 2.0*froot) > std::fabs(x: dxold*dfroot))) { |
| 71 | dx = (xh-xl)/2.0; |
| 72 | root_ = xl+dx; |
| 73 | // if the root estimate just computed is close to the |
| 74 | // previous one, we should calculate dfroot at root and |
| 75 | // xh rather than root and rootold (xl instead of xh would |
| 76 | // be just as good) |
| 77 | if (close(x: root_, y: rootold, n: 2500)) { |
| 78 | rootold = xh; |
| 79 | frootold = f(xh); |
| 80 | } |
| 81 | } else { // Newton |
| 82 | dx = froot/dfroot; |
| 83 | root_ -= dx; |
| 84 | } |
| 85 | |
| 86 | // Convergence criterion |
| 87 | if (std::fabs(x: dx) < xAccuracy) |
| 88 | return root_; |
| 89 | |
| 90 | froot = f(root_); |
| 91 | ++evaluationNumber_; |
| 92 | dfroot = (frootold-froot)/(rootold-root_); |
| 93 | |
| 94 | if (froot < 0.0) |
| 95 | xl=root_; |
| 96 | else |
| 97 | xh=root_; |
| 98 | } |
| 99 | |
| 100 | QL_FAIL("maximum number of function evaluations (" |
| 101 | << maxEvaluations_ << ") exceeded" ); |
| 102 | } |
| 103 | }; |
| 104 | |
| 105 | } |
| 106 | |
| 107 | #endif |
| 108 | |