QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
richardsonextrapolation.cpp
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) 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 <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 richardsonextrapolation.cpp
21*/
22
23#include <ql/errors.hpp>
26
27#include <cmath>
28
29namespace QuantLib {
30 namespace {
31 class RichardsonEqn {
32 public:
33 RichardsonEqn(Real fh, Real ft, Real fs, Real t, Real s)
34 : fdelta_h_(fh), ft_(ft), fs_(fs), t_(t), s_(s) { }
35
36 Real operator()(Real k) const {
37 return ft_ + (ft_-fdelta_h_)/(std::pow(t_, k)-1.0)
38 - ( fs_ + (fs_-fdelta_h_)/(std::pow(s_, k)-1.0));
39 }
40 private:
42 };
43
44 }
45
47 const ext::function<Real (Real)>& f, Real delta_h, Real n)
48 : delta_h_(delta_h),
49 fdelta_h_(f(delta_h)),
50 n_(n),
51 f_(f) {
52 }
53
54
56
57 QL_REQUIRE(t > 1, "scaling factor must be greater than 1");
58 QL_REQUIRE(n_ != Null<Real>(), "order of convergence must be known");
59
60 const Real tk = std::pow(t, n_);
61
62 return (tk*f_(delta_h_/t)-fdelta_h_)/(tk-1.0);
63 }
64
66 const {
67 QL_REQUIRE(t > 1 && s > 1, "scaling factors must be greater than 1");
68 QL_REQUIRE(t > s, "t must be greater than s");
69
70 const Real ft = f_(delta_h_/t);
71 const Real fs = f_(delta_h_/s);
72
73 const RichardsonEqn eqn(fdelta_h_, ft, fs, t, s);
74
75 const Real step = 0.1;
76 Real left = 0.05;
77 Real fr = eqn(left + step), fl = eqn(left);
78 while (fr*fl > 0.0 && left < 15.1) {
79 left+=step;
80 fl = fr;
81 fr = eqn(left + step);
82 }
83
84 QL_REQUIRE(left < 15.1,"could not estimate the order of convergence");
85
86 const Real k = Brent().solve(eqn, 1e-8, left+0.5*step, left, left+step);
87
88 const Real ts = std::pow(s, k);
89
90 return (ts*fs-fdelta_h_)/(ts-1.0);
91 }
92}
Brent 1-D solver.
Brent 1-D solver
Definition: brent.hpp:37
template class providing a null value for a given type.
Definition: null.hpp:76
const ext::function< Real(Real)> f_
RichardsonExtrapolation(const ext::function< Real(Real)> &f, Real delta_h, Real n=Null< Real >())
Real solve(const F &f, Real accuracy, Real guess, Real step) const
Definition: solver1d.hpp:84
ext::function< Real(Real)> f_
const DefaultType & t
Classes and functions for error handling.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
QL_REAL Real
real number
Definition: types.hpp:50
Definition: any.hpp:35
const Real s_
const Real ft_
const Real fdelta_h_
const Real fs_