QuantLib
: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
Loading...
Searching...
No Matches
ql
math
solvers1d
halley.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) 2024 Klaus Spanderen
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 halley.hpp
21
\brief Halley 1-D solver
22
*/
23
24
#ifndef quantlib_solver1d_halley_hpp
25
#define quantlib_solver1d_halley_hpp
26
27
#include <
ql/math/solvers1d/newtonsafe.hpp
>
28
29
namespace
QuantLib
{
30
31
//! %Halley 1-D solver
32
/*! \note This solver requires that the passed function object
33
implement a method <tt>Real derivative(Real)</tt>
34
and <tt> Real secondDerivative(Real></tt>
35
36
\test the correctness of the returned values is tested by
37
checking them against known good results.
38
39
\ingroup solvers
40
*/
41
class
Halley
:
public
Solver1D
<Halley> {
42
public
:
43
template
<
class
F>
44
Real
solveImpl
(
const
F
&
f
,
45
Real
xAccuracy)
const
{
46
47
while
(++
evaluationNumber_
<=
maxEvaluations_
) {
48
const
Real
fx =
f
(
root_
);
49
const
Real
fPrime =
f
.derivative(
root_
);
50
const
Real
lf = fx*
f
.secondDerivative(
root_
)/(fPrime*fPrime);
51
const
Real
step = 1.0/(1.0 - 0.5*lf)*fx/fPrime;
52
root_
-= step;
53
54
// jumped out of brackets, switch to NewtonSafe
55
if
((
xMin_
-
root_
)*(
root_
-
xMax_
) < 0.0) {
56
NewtonSafe
s
;
57
s
.setMaxEvaluations(
maxEvaluations_
-
evaluationNumber_
);
58
return
s
.solve(
f
, xAccuracy,
root_
+step,
xMin_
,
xMax_
);
59
}
60
61
if
(std::abs(step) < xAccuracy) {
62
f
(
root_
);
63
++
evaluationNumber_
;
64
return
root_
;
65
}
66
67
}
68
69
QL_FAIL
(
"maximum number of function evaluations ("
70
<<
maxEvaluations_
<<
") exceeded"
);
71
}
72
};
73
}
74
75
#endif
QuantLib::Halley
Halley 1-D solver
Definition:
halley.hpp:41
QuantLib::Halley::solveImpl
Real solveImpl(const F &f, Real xAccuracy) const
Definition:
halley.hpp:44
QuantLib::NewtonSafe
safe Newton 1-D solver
Definition:
newtonsafe.hpp:40
QuantLib::Solver1D
Base class for 1-D solvers.
Definition:
solver1d.hpp:67
QuantLib::Solver1D< Halley >::evaluationNumber_
Size evaluationNumber_
Definition:
solver1d.hpp:227
QuantLib::Solver1D< Halley >::xMin_
Real xMin_
Definition:
solver1d.hpp:225
QuantLib::Solver1D< Halley >::root_
Real root_
Definition:
solver1d.hpp:225
QuantLib::Solver1D< Halley >::xMax_
Real xMax_
Definition:
solver1d.hpp:225
QuantLib::Solver1D< Halley >::maxEvaluations_
Size maxEvaluations_
Definition:
solver1d.hpp:226
f
F f
Definition:
defaultdensitystructure.cpp:32
QL_FAIL
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition:
errors.hpp:92
QuantLib::Real
QL_REAL Real
real number
Definition:
types.hpp:50
QuantLib
Definition:
any.hpp:37
newtonsafe.hpp
Safe (bracketed) Newton 1-D solver.
s
Real s
Definition:
perturbativebarrieroptionengine.cpp:1488
F
Real F
Definition:
sabr.cpp:200
Generated by
Doxygen
1.9.5