QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
randomizedlds.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) 2004 Ferdinando Ametrano
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 randomizedlds.hpp
21 \brief Randomized low-discrepancy sequence
22*/
23
24#ifndef quantlib_randomized_lds_hpp
25#define quantlib_randomized_lds_hpp
26
29#include <utility>
30
31namespace QuantLib {
32
33 //! Randomized (random shift) low-discrepancy sequence
34 /*! Random-shifts a uniform low-discrepancy sequence of dimension
35 \f$ N \f$ by adding (modulo 1 for each coordinate) a pseudo-random
36 uniform deviate in \f$ (0, 1)^N. \f$
37 It is used for implementing Randomized Quasi Monte Carlo.
38
39 The uniform low discrepancy sequence is supplied by LDS; the
40 uniform pseudo-random sequence is supplied by PRS.
41
42 Both class LDS and PRS must implement the following interface:
43 \code
44 LDS::sample_type LDS::nextSequence() const;
45 Size LDS::dimension() const;
46 \endcode
47
48 \pre LDS and PRS must have the same dimension \f$ N \f$
49
50 \warning Inverting LDS and PRS is possible, but it doesn't
51 make sense.
52
53 \todo implement the other randomization algorithms
54
55 \test correct initialization is tested.
56 */
57 template <class LDS,
58 class PRS = RandomSequenceGenerator<MersenneTwisterUniformRng> >
60 public:
62 RandomizedLDS(const LDS& ldsg, PRS prsg);
63 RandomizedLDS(const LDS& ldsg);
64 RandomizedLDS(Size dimensionality,
65 BigNatural ldsSeed = 0,
66 BigNatural prsSeed = 0);
67 //! returns next sample using a given randomizing vector
68 const sample_type& nextSequence() const;
69 const sample_type& lastSequence() const {
70 return x;
71 }
72 /*! update the randomizing vector and re-initialize
73 the low discrepancy generator */
75 randomizer_ = prsg_.nextSequence();
77 }
78 Size dimension() const {return dimension_;}
79 private:
80 mutable LDS ldsg_, pristineldsg_; // mutable because nextSequence is const
81 PRS prsg_;
84 };
85
86 template <class LDS, class PRS>
87 RandomizedLDS<LDS, PRS>::RandomizedLDS(const LDS& ldsg, PRS prsg)
88 : ldsg_(ldsg), pristineldsg_(ldsg), prsg_(std::move(prsg)), dimension_(ldsg_.dimension()),
89 x(std::vector<Real>(dimension_), 1.0), randomizer_(std::vector<Real>(dimension_), 1.0) {
90
91 QL_REQUIRE(prsg_.dimension()==dimension_,
92 "generator mismatch: "
93 << dimension_ << "-dim low discrepancy "
94 << "and " << prsg_.dimension() << "-dim pseudo random");
95
96 randomizer_ = prsg_.nextSequence();
97 }
98
99 template <class LDS, class PRS>
101 : ldsg_(ldsg), pristineldsg_(ldsg),
102 prsg_(ldsg_.dimension()), dimension_(ldsg_.dimension()),
103 x(std::vector<Real> (dimension_), 1.0), randomizer_(std::vector<Real> (dimension_), 1.0) {
104
105 randomizer_ = prsg_.nextSequence();
106
107 }
108
109 template <class LDS, class PRS>
111 BigNatural ldsSeed,
112 BigNatural prsSeed)
113 : ldsg_(dimensionality, ldsSeed), pristineldsg_(dimensionality, ldsSeed),
114 prsg_(dimensionality, prsSeed), dimension_(dimensionality),
115 x(std::vector<Real> (dimensionality), 1.0), randomizer_(std::vector<Real> (dimensionality), 1.0) {
116
117 randomizer_ = prsg_.nextSequence();
118 }
119
120 template <class LDS, class PRS>
121 inline const typename RandomizedLDS<LDS, PRS>::sample_type&
123 typename LDS::sample_type sample =
124 ldsg_.nextSequence();
125 x.weight = randomizer_.weight * sample.weight;
126 for (Size i = 0; i < dimension_; i++) {
127 x.value[i] = randomizer_.value[i] + sample.value[i];
128 if (x.value[i]>1.0)
129 x.value[i] -= 1.0;
130 }
131 return x;
132 }
133
134}
135
136
137#endif
Randomized (random shift) low-discrepancy sequence.
RandomizedLDS(const LDS &ldsg, PRS prsg)
const sample_type & nextSequence() const
returns next sample using a given randomizing vector
const sample_type & lastSequence() const
Sample< std::vector< Real > > sample_type
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Mersenne Twister uniform random number generator.
Definition: any.hpp:35
unsigned QL_BIG_INTEGER BigNatural
large positive integer
Definition: types.hpp:46
STL namespace.
Random sequence generator based on a pseudo-random number generator.
weighted sample
Definition: sample.hpp:35