/src/quantlib/ql/math/randomnumbers/faurersg.hpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2004 Ferdinando Ametrano |
5 | | Copyright (C) 2004 Gianni Piolanti |
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 | | <https://www.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 faurersg.hpp |
22 | | \brief Faure low-discrepancy sequence generator |
23 | | */ |
24 | | |
25 | | #ifndef quantlib_faure_ld_rsg_h |
26 | | #define quantlib_faure_ld_rsg_h |
27 | | |
28 | | #include <ql/math/matrix.hpp> |
29 | | #include <ql/methods/montecarlo/sample.hpp> |
30 | | #include <vector> |
31 | | |
32 | | |
33 | | namespace QuantLib { |
34 | | |
35 | | //! Faure low-discrepancy sequence generator |
36 | | /*! It is based on existing Fortran and C algorithms to calculate pascal |
37 | | matrix and gray transforms. |
38 | | -# E. Thiemard Economic generation of low-discrepancy sequences with |
39 | | a b-ary gray code. |
40 | | -# Algorithms 659, 647. http://www.netlib.org/toms/647, |
41 | | http://www.netlib.org/toms/659 |
42 | | |
43 | | \test the correctness of the returned values is tested by |
44 | | reproducing known good values. |
45 | | */ |
46 | | class FaureRsg { |
47 | | public: |
48 | | typedef Sample<std::vector<Real> > sample_type; |
49 | | FaureRsg(Size dimensionality); |
50 | 0 | const std::vector<long int>& nextIntSequence() const { |
51 | 0 | generateNextIntSequence(); |
52 | 0 | return integerSequence_; |
53 | 0 | } |
54 | 0 | const std::vector<long int>& lastIntSequence() const { |
55 | 0 | return integerSequence_; |
56 | 0 | } |
57 | 0 | const sample_type& nextSequence() const { |
58 | 0 | generateNextIntSequence(); |
59 | 0 | for (Size i=0; i<dimensionality_; i++) |
60 | 0 | sequence_.value[i] = integerSequence_[i]/normalizationFactor_; |
61 | 0 | return sequence_; |
62 | 0 | } |
63 | 0 | const sample_type& lastSequence() const { return sequence_; } |
64 | 0 | Size dimension() const { return dimensionality_; } |
65 | | private: |
66 | | void generateNextIntSequence() const; |
67 | | Size dimensionality_; |
68 | | // mutable unsigned long sequenceCounter_; |
69 | | mutable sample_type sequence_; |
70 | | mutable std::vector<long int> integerSequence_; |
71 | | mutable std::vector<long int> bary_; |
72 | | mutable std::vector<std::vector<long int> > gray_; |
73 | | Size base_, mbit_; |
74 | | std::vector<std::vector<long int> > powBase_; |
75 | | std::vector<long int> addOne_; |
76 | | std::vector<std::vector<std::vector<long int> > > pascal3D; |
77 | | double normalizationFactor_; |
78 | | }; |
79 | | |
80 | | } |
81 | | |
82 | | #endif |
83 | | |
84 | | |