QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
trapezoidintegral.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) 2003 Roman Gitlin
5 Copyright (C) 2003 StatPro Italia srl
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
21/*! \file trapezoidintegral.hpp
22 \brief integral of a one-dimensional function using the trapezoid formula
23*/
24
25#ifndef quantlib_trapezoid_integral_hpp
26#define quantlib_trapezoid_integral_hpp
27
29#include <ql/utilities/null.hpp>
30#include <ql/errors.hpp>
31
32namespace QuantLib {
33
34 //! Integral of a one-dimensional function
35 /*! Given a target accuracy \f$ \epsilon \f$, the integral of
36 a function \f$ f \f$ between \f$ a \f$ and \f$ b \f$ is
37 calculated by means of the trapezoid formula
38 \f[
39 \int_{a}^{b} f \mathrm{d}x =
40 \frac{1}{2} f(x_{0}) + f(x_{1}) + f(x_{2}) + \dots
41 + f(x_{N-1}) + \frac{1}{2} f(x_{N})
42 \f]
43 where \f$ x_0 = a \f$, \f$ x_N = b \f$, and
44 \f$ x_i = a+i \Delta x \f$ with
45 \f$ \Delta x = (b-a)/N \f$. The number \f$ N \f$ of intervals
46 is repeatedly increased until the target accuracy is reached.
47
48 \test the correctness of the result is tested by checking it
49 against known good values.
50 */
51 template <class IntegrationPolicy>
53 public:
55 Size maxIterations)
56 : Integrator(accuracy, maxIterations){}
57
58 protected:
59 Real integrate(const ext::function<Real(Real)>& f, Real a, Real b) const override {
60
61 // start from the coarsest trapezoid...
62 Size N = 1;
63 Real I = (f(a)+f(b))*(b-a)/2.0, newI;
65 // ...and refine it
66 Size i = 1;
67 do {
68 newI = IntegrationPolicy::integrate(f,a,b,I,N);
69 increaseNumberOfEvaluations(N*(IntegrationPolicy::nbEvalutions()-1));
70 N *= IntegrationPolicy::nbEvalutions();
71 // good enough? Also, don't run away immediately
72 if (std::fabs(I-newI) <= absoluteAccuracy() && i > 5)
73 // ok, exit
74 return newI;
75 // oh well. Another step.
76 I = newI;
77 i++;
78 } while (i < maxEvaluations());
79 QL_FAIL("max number of iterations reached");
80 }
81 };
82
83 // Integration policies
84 struct Default {
85 inline static Real integrate(const ext::function<Real (Real)>& f,
86 Real a,
87 Real b,
88 Real I,
89 Size N)
90 {
91 Real sum = 0.0;
92 Real dx = (b-a)/N;
93 Real x = a + dx/2.0;
94 for (Size i=0; i<N; x += dx, ++i)
95 sum += f(x);
96 return (I + dx*sum)/2.0;
97 }
98 inline static Size nbEvalutions(){ return 2;}
99 };
100
101 struct MidPoint {
102 inline static Real integrate(const ext::function<Real (Real)>& f,
103 Real a,
104 Real b,
105 Real I,
106 Size N)
107 {
108 Real sum = 0.0;
109 Real dx = (b-a)/N;
110 Real x = a + dx/6.0;
111 Real D = 2.0*dx/3.0;
112 for (Size i=0; i<N; x += dx, ++i)
113 sum += f(x) + f(x+D);
114 return (I + dx*sum)/3.0;
115 }
116 inline static Size nbEvalutions(){ return 3;}
117 };
118
119}
120
121#endif
Real absoluteAccuracy() const
Definition: integral.cpp:43
Size maxEvaluations() const
Definition: integral.cpp:47
void increaseNumberOfEvaluations(Size increase) const
Definition: integral.cpp:67
Integral of a one-dimensional function.
TrapezoidIntegral(Real accuracy, Size maxIterations)
Real integrate(const ext::function< Real(Real)> &f, Real a, Real b) const override
Classes and functions for error handling.
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition: errors.hpp:92
ext::function< Real(Real)> b
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Integrators base class definition.
Definition: any.hpp:35
null values
static Real integrate(const ext::function< Real(Real)> &f, Real a, Real b, Real I, Size N)
static Size nbEvalutions()
static Real integrate(const ext::function< Real(Real)> &f, Real a, Real b, Real I, Size N)
static Size nbEvalutions()