QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
momentbasedgaussianpolynomial.hpp
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
24#ifndef quantlib_moment_based_gaussian_polynomial_hpp
25#define quantlib_moment_based_gaussian_polynomial_hpp
26
27#include <ql/math/comparison.hpp>
28#include <ql/math/integrals/gaussianorthogonalpolynomial.hpp>
29#include <ql/errors.hpp>
30#include <cmath>
31#include <vector>
32
33namespace QuantLib {
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
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
Definition: any.hpp:35
bool close_enough(const Quantity &m1, const Quantity &m2, Size n)
Definition: quantity.cpp:182
STL namespace.