Coverage Report

Created: 2026-10-31 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/math/richardsonextrapolation.cpp
Line
Count
Source
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
 Copyright (C) 2012 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
 <https://www.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 richardsonextrapolation.cpp
21
*/
22
23
#include <ql/errors.hpp>
24
#include <ql/math/solvers1d/brent.hpp>
25
#include <ql/math/richardsonextrapolation.hpp>
26
27
#include <cmath>
28
29
namespace QuantLib {
30
    namespace {
31
        class RichardsonEqn {
32
          public:
33
            RichardsonEqn(Real fh, Real ft, Real fs, Real t, Real s)
34
0
            : fdelta_h_(fh), ft_(ft), fs_(fs), t_(t), s_(s) { }
35
36
0
            Real operator()(Real k) const {
37
0
                return      ft_ + (ft_-fdelta_h_)/(std::pow(t_, k)-1.0)
38
0
                        - ( fs_ + (fs_-fdelta_h_)/(std::pow(s_, k)-1.0));
39
0
            }
40
          private:
41
            const Real fdelta_h_, ft_, fs_, t_, s_;
42
        };
43
44
    }
45
46
    RichardsonExtrapolation::RichardsonExtrapolation(
47
        const std::function<Real (Real)>& f, Real delta_h, Real n)
48
0
    : delta_h_(delta_h),
49
0
      fdelta_h_(f(delta_h)),
50
0
      n_(n),
51
0
      f_(f) {
52
0
    }
53
54
55
0
    Real RichardsonExtrapolation::operator()(Real t) const {
56
57
0
        QL_REQUIRE(t > 1, "scaling factor must be greater than 1");
58
0
        QL_REQUIRE(n_ != Null<Real>(), "order of convergence must be known");
59
60
0
        const Real tk = std::pow(t, n_);
61
62
0
        return (tk*f_(delta_h_/t)-fdelta_h_)/(tk-1.0);
63
0
    }
64
65
    Real RichardsonExtrapolation::operator()(Real t, Real s)
66
0
    const {
67
0
        QL_REQUIRE(t > 1 && s > 1, "scaling factors must be greater than 1");
68
0
        QL_REQUIRE(t > s, "t must be greater than s");
69
70
0
        const Real ft = f_(delta_h_/t);
71
0
        const Real fs = f_(delta_h_/s);
72
73
0
        const RichardsonEqn eqn(fdelta_h_, ft, fs, t, s);
74
75
0
        const Real step = 0.1;
76
0
        Real left = 0.05;
77
0
        Real fr = eqn(left + step), fl = eqn(left);
78
0
        while (fr*fl > 0.0 && left < 15.1) {
79
0
            left+=step;
80
0
            fl = fr;
81
0
            fr = eqn(left + step);
82
0
        }
83
84
0
        QL_REQUIRE(left < 15.1,"could not estimate the order of convergence");
85
86
0
        const Real k = Brent().solve(eqn, 1e-8, left+0.5*step, left, left+step);
87
88
0
        const Real ts = std::pow(s, k);
89
90
0
        return (ts*fs-fdelta_h_)/(ts-1.0);
91
0
    }
92
}