QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
momentbasedgaussianpolynomial.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) 2020 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 momentbasedgaussianpolynomial.hpp
21 \brief Gaussian quadrature defined by the moments of the distribution
22*/
23
24#ifndef quantlib_moment_based_gaussian_polynomial_hpp
25#define quantlib_moment_based_gaussian_polynomial_hpp
26
29#include <ql/errors.hpp>
30#include <cmath>
31#include <vector>
32
33namespace QuantLib {
34 /*! References:
35 Gauss quadratures and orthogonal polynomials
36
37 G.H. Gloub and J.H. Welsch: Calculation of Gauss quadrature rule.
38 Math. Comput. 23 (1986), 221-230,
39 http://web.stanford.edu/class/cme335/spr11/S0025-5718-69-99647-1.pdf
40
41 M. Morandi Cecchi and M. Redivo Zaglia, Computing the coefficients
42 of a recurrence formula for numerical integration by moments and
43 modified moments.
44 http://ac.els-cdn.com/0377042793901522/1-s2.0-0377042793901522-main.pdf?_tid=643d5dca-a05d-11e6-9a56-00000aab0f27&acdnat=1478023545_cf7c87cba4cc9e37a136e68a2564d411
45 */
46
47 template <class mp_real>
50 public:
52
53 Real mu_0() const override;
54 Real alpha(Size i) const override;
55 Real beta(Size i) const override;
56
57 virtual mp_real moment(Size i) const = 0;
58
59 private:
60 mp_real alpha_(Size i) const;
61 mp_real beta_(Size i) const;
62
63 mp_real z(Integer k, Integer i) const;
64
65 mutable std::vector<mp_real> b_, c_;
66 mutable std::vector<std::vector<mp_real> > z_;
67 };
68
69 template <class mp_real> inline
71 : z_(1, std::vector<mp_real>()) {}
72
73 template <class mp_real> inline
75 if (k == -1) return mp_real(0.0);
76
77 const Integer rows = z_.size();
78 const Integer cols = z_[0].size();
79
80 if (cols <= i) {
81 for (Integer l=0; l<rows; ++l)
82 z_[l].resize(i+1, std::numeric_limits<mp_real>::quiet_NaN());
83 }
84 if (rows <= k) {
85 z_.resize(k+1, std::vector<mp_real>(
86 z_[0].size(), std::numeric_limits<mp_real>::quiet_NaN()));
87 }
88
89 if (std::isnan(z_[k][i])) {
90 if (k == 0)
91 z_[k][i] = moment(i);
92 else {
93 const mp_real tmp = z(k-1, i+1)
94 - alpha_(k-1)*z(k-1, i) - beta_(k-1)*z(k-2, i);
95 z_[k][i] = tmp;
96 }
97 }
98
99 return z_[k][i];
100 };
101
102 template <class mp_real> inline
104
105 if (b_.size() <= u)
106 b_.resize(u+1, std::numeric_limits<mp_real>::quiet_NaN());
107
108 if (std::isnan(b_[u])) {
109 if (u == 0)
110 b_[u] = moment(1);
111 else {
112 const Integer iu(u);
113 const mp_real tmp =
114 -z(iu-1, iu)/z(iu-1, iu-1) + z(iu, iu+1)/z(iu, iu);
115 b_[u] = tmp;
116 }
117 }
118 return b_[u];
119 }
120
121 template <class mp_real> inline
123 if (u == 0)
124 return mp_real(1.0);
125
126 if (c_.size() <= u)
127 c_.resize(u+1, std::numeric_limits<mp_real>::quiet_NaN());
128
129 if (std::isnan(c_[u])) {
130 const Integer iu(u);
131 const mp_real tmp = z(iu, iu) / z(iu-1, iu-1);
132 c_[u] = tmp;
133 }
134 return c_[u];
135 }
136
137 template <> inline
139 return alpha_(u);
140 }
141
142 template <class mp_real> inline
144 return alpha_(u).template convert_to<Real>();
145 }
146
147 template <> inline
149 return beta_(u);
150 }
151
152 template <class mp_real> inline
154 mp_real b = beta_(u);
155 return b.template convert_to<Real>();
156 }
157
158 template <> inline
160 const Real m0 = moment(0);
161 QL_REQUIRE(close_enough(m0, 1.0), "zero moment must by one.");
162
163 return moment(0);
164 }
165
166 template <class mp_real> inline
168 return moment(0).template convert_to<Real>();
169 }
170}
171
172#endif
orthogonal polynomial for Gaussian quadratures
virtual mp_real moment(Size i) const =0
floating-point comparisons
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
ext::function< Real(Real)> b
orthogonal polynomials for gaussian quadratures
QL_REAL Real
real number
Definition: types.hpp:50
QL_INTEGER Integer
integer number
Definition: types.hpp:35
std::size_t Size
size of a container
Definition: types.hpp:58
const VF_R b_
Definition: any.hpp:35
bool close_enough(const Quantity &m1, const Quantity &m2, Size n)
Definition: quantity.cpp:182
STL namespace.