QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
ridder.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) 2000, 2001, 2002, 2003 RiskMap srl
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 ridder.hpp
21 \brief Ridder 1-D solver
22*/
23
24#ifndef quantlib_solver1d_ridder_h
25#define quantlib_solver1d_ridder_h
26
27#include <ql/math/solver1d.hpp>
28
29namespace QuantLib {
30
31 //! %Ridder 1-D solver
32 /*! \test the correctness of the returned values is tested by
33 checking them against known good results.
34
35 \ingroup solvers
36 */
37 class Ridder : public Solver1D<Ridder> {
38 public:
39 template <class F>
40 Real solveImpl(const F& f,
41 Real xAcc) const {
42
43 /* The implementation of the algorithm was inspired by
44 Press, Teukolsky, Vetterling, and Flannery,
45 "Numerical Recipes in C", 2nd edition,
46 Cambridge University Press
47 */
48
49 Real fxMid, froot, s, xMid, nextRoot;
50
51 // test on Black-Scholes implied volatility show that
52 // Ridder solver algorithm actually provides an
53 // accuracy 100 times below promised
54 Real xAccuracy = xAcc/100.0;
55
56 // Any highly unlikely value, to simplify logic below
58
60 xMid = 0.5*(xMin_+xMax_);
61 // First of two function evaluations per iteraton
62 fxMid = f(xMid);
64 s = std::sqrt(fxMid*fxMid-fxMin_*fxMax_);
65 if (close(s, 0.0)) {
66 f(root_);
68 return root_;
69 }
70 // Updating formula
71 nextRoot = xMid + (xMid - xMin_) *
72 ((fxMin_ >= fxMax_ ? 1.0 : -1.0) * fxMid / s);
73 if (std::fabs(nextRoot-root_) <= xAccuracy) {
74 f(root_);
76 return root_;
77 }
78
79 root_ = nextRoot;
80 // Second of two function evaluations per iteration
81 froot = f(root_);
83 if (close(froot, 0.0))
84 return root_;
85
86 // Bookkeeping to keep the root bracketed on next iteration
87 if (sign(fxMid,froot) != fxMid) {
88 xMin_ = xMid;
89 fxMin_ = fxMid;
90 xMax_ = root_;
91 fxMax_ = froot;
92 } else if (sign(fxMin_,froot) != fxMin_) {
93 xMax_ = root_;
94 fxMax_ = froot;
95 } else if (sign(fxMax_,froot) != fxMax_) {
96 xMin_ = root_;
97 fxMin_ = froot;
98 } else {
99 QL_FAIL("never get here.");
100 }
101
102 if (std::fabs(xMax_-xMin_) <= xAccuracy) {
103 f(root_);
105 return root_;
106 }
107 }
108
109 QL_FAIL("maximum number of function evaluations ("
110 << maxEvaluations_ << ") exceeded");
111 }
112 private:
113 Real sign(Real a, Real b) const {
114 return b >= 0.0 ? std::fabs(a) : Real(-std::fabs(a));
115 }
116 };
117
118}
119
120#endif
Ridder 1-D solver
Definition: ridder.hpp:37
Real solveImpl(const F &f, Real xAcc) const
Definition: ridder.hpp:40
Real sign(Real a, Real b) const
Definition: ridder.hpp:113
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
ext::function< Real(Real)> b
#define QL_MIN_REAL
Definition: qldefines.hpp:175
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.