QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
finitedifferencenewtonsafe.hpp
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) 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
29namespace 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_);
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_;
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(2.0*froot) > std::fabs(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(root_, rootold, 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(dx) < xAccuracy)
88 return root_;
89
90 froot = f(root_);
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
safe Newton 1-D solver with finite difference derivatives
Real solveImpl(const F &f, Real xAccuracy) const
Base class for 1-D solvers.
Definition: solver1d.hpp:67
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition: errors.hpp:92
QL_REAL Real
real number
Definition: types.hpp:50
Definition: any.hpp:35
bool close(const Quantity &m1, const Quantity &m2, Size n)
Definition: quantity.cpp:163
Real F
Definition: sabr.cpp:200
Abstract 1-D solver class.