QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
discreteintegrals.cpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2014 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#include <ql/math/integrals/discreteintegrals.hpp>
21#include <boost/accumulators/accumulators.hpp>
22#include <boost/accumulators/statistics/sum.hpp>
23
24using namespace boost::accumulators;
25
26namespace QuantLib {
27
29 const Array& x, const Array& f) const {
30
31 const Size n = f.size();
32 QL_REQUIRE(n == x.size(), "inconsistent size");
33
34 accumulator_set<Real, features<tag::sum> > acc;
35
36 for (Size i=0; i < n-1; ++i) {
37 acc((x[i+1]-x[i])*(f[i]+f[i+1]));
38 }
39
40 return 0.5*sum(acc);
41 }
42
44 const Array& x, const Array& f) const {
45
46 const Size n = f.size();
47 QL_REQUIRE(n == x.size(), "inconsistent size");
48
49 accumulator_set<Real, features<tag::sum> > acc;
50
51 for (Size j=0; j < n-2; j+=2) {
52 const Real dxj = x[j+1]-x[j];
53 const Real dxjp1 = x[j+2]-x[j+1];
54
55 const Real alpha = -dxjp1*(2*x[j]-3*x[j+1]+x[j+2]);
56 const Real dd = x[j+2]-x[j];
57 const Real k = dd/(6*dxjp1*dxj);
58 const Real beta = dd*dd;
59 const Real gamma = dxj*(x[j]-3*x[j+1]+2*x[j+2]);
60
61 acc(k*alpha*f[j]+k*beta*f[j+1]+k*gamma*f[j+2]);
62 }
63 if ((n & 1) == 0U) {
64 acc(0.5*(x[n-1]-x[n-2])*(f[n-1]+f[n-2]));
65 }
66
67 return sum(acc);
68 }
69
70
72 const ext::function<Real (Real)>& f, Real a, Real b) const {
73 const Array x(maxEvaluations(), a, (b-a)/(maxEvaluations()-1));
74 Array fv(x.size());
75 std::transform(x.begin(), x.end(), fv.begin(), f);
76
78 return DiscreteTrapezoidIntegral()(x, fv);
79 }
80
82 const ext::function<Real (Real)>& f, Real a, Real b) const {
83 const Array x(maxEvaluations(), a, (b-a)/(maxEvaluations()-1));
84 Array fv(x.size());
85 std::transform(x.begin(), x.end(), fv.begin(), f);
86
88 return DiscreteSimpsonIntegral()(x, fv);
89 }
90}
1-D array used in linear algebra.
Definition: array.hpp:52
const_iterator end() const
Definition: array.hpp:511
Size size() const
dimension of the array
Definition: array.hpp:495
const_iterator begin() const
Definition: array.hpp:503
Real operator()(const Array &x, const Array &f) const
Real integrate(const ext::function< Real(Real)> &f, Real a, Real b) const override
Real operator()(const Array &x, const Array &f) const
Real integrate(const ext::function< Real(Real)> &f, Real a, Real b) const override
Size maxEvaluations() const
Definition: integral.cpp:47
void increaseNumberOfEvaluations(Size increase) const
Definition: integral.cpp:67
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35