QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
randomizedlds.hpp
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
24#ifndef quantlib_randomized_lds_hpp
25#define quantlib_randomized_lds_hpp
26
27#include <ql/math/randomnumbers/mt19937uniformrng.hpp>
28#include <ql/math/randomnumbers/randomsequencegenerator.hpp>
29#include <utility>
30
31namespace QuantLib {
32
34
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);
68 const sample_type& nextSequence() const;
69 const sample_type& lastSequence() const {
70 return x;
71 }
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
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
unsigned QL_BIG_INTEGER BigNatural
large positive integer
Definition: types.hpp:46
STL namespace.
weighted sample
Definition: sample.hpp:35