| 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> |
| 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 | : 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(x: t_, y: k)-1.0) |
| 38 | - ( fs_ + (fs_-fdelta_h_)/(std::pow(x: s_, y: k)-1.0)); |
| 39 | } |
| 40 | private: |
| 41 | const Real fdelta_h_, ft_, fs_, t_, s_; |
| 42 | }; |
| 43 | |
| 44 | } |
| 45 | |
| 46 | RichardsonExtrapolation::( |
| 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 | |
| 55 | Real RichardsonExtrapolation::(Real t) const { |
| 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(x: t, y: n_); |
| 61 | |
| 62 | return (tk*f_(delta_h_/t)-fdelta_h_)/(tk-1.0); |
| 63 | } |
| 64 | |
| 65 | Real RichardsonExtrapolation::(Real t, Real s) |
| 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(f: eqn, accuracy: 1e-8, guess: left+0.5*step, xMin: left, xMax: left+step); |
| 87 | |
| 88 | const Real ts = std::pow(x: s, y: k); |
| 89 | |
| 90 | return (ts*fs-fdelta_h_)/(ts-1.0); |
| 91 | } |
| 92 | } |
| 93 | |