/usr/include/ql/math/randomnumbers/randomizedlds.hpp is in libquantlib0-dev 1.7.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
Copyright (C) 2004 Ferdinando Ametrano
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<quantlib-dev@lists.sf.net>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/
/*! \file randomizedlds.hpp
\brief Randomized low-discrepancy sequence
*/
#ifndef quantlib_randomized_lds_hpp
#define quantlib_randomized_lds_hpp
#include <ql/math/randomnumbers/randomsequencegenerator.hpp>
#include <ql/math/randomnumbers/mt19937uniformrng.hpp>
namespace QuantLib {
//! Randomized (random shift) low-discrepancy sequence
/*! Random-shifts a uniform low-discrepancy sequence of dimension
\f$ N \f$ by adding (modulo 1 for each coordinate) a pseudo-random
uniform deviate in \f$ (0, 1)^N. \f$
It is used for implementing Randomized Quasi Monte Carlo.
The uniform low discrepancy sequence is supplied by LDS; the
uniform pseudo-random sequence is supplied by PRS.
Both class LDS and PRS must implement the following interface:
\code
LDS::sample_type LDS::nextSequence() const;
Size LDS::dimension() const;
\endcode
\pre LDS and PRS must have the same dimension \f$ N \f$
\warning Inverting LDS and PRS is possible, but it doesn't
make sense.
\todo implement the other randomization algorithms
\test correct initialization is tested.
*/
template <class LDS,
class PRS = RandomSequenceGenerator<MersenneTwisterUniformRng> >
class RandomizedLDS {
public:
typedef Sample<std::vector<Real> > sample_type;
RandomizedLDS(const LDS& ldsg,
const PRS& prsg);
RandomizedLDS(const LDS& ldsg);
RandomizedLDS(Size dimensionality,
BigNatural ldsSeed = 0,
BigNatural prsSeed = 0);
//! returns next sample using a given randomizing vector
const sample_type& nextSequence() const;
const sample_type& lastSequence() const {
return x;
}
/*! update the randomizing vector and re-initialize
the low discrepancy generator */
void nextRandomizer() {
randomizer_ = prsg_.nextSequence();
ldsg_ = pristineldsg_;
}
Size dimension() const {return dimension_;}
private:
mutable LDS ldsg_, pristineldsg_; // mutable because nextSequence is const
PRS prsg_;
Size dimension_;
mutable sample_type x, randomizer_;
};
template <class LDS, class PRS>
RandomizedLDS<LDS, PRS>::RandomizedLDS(const LDS& ldsg, const PRS& prsg)
: ldsg_(ldsg), pristineldsg_(ldsg),
prsg_(prsg), dimension_(ldsg_.dimension()),
x(std::vector<Real> (dimension_), 1.0), randomizer_(std::vector<Real> (dimension_), 1.0) {
QL_REQUIRE(prsg_.dimension()==dimension_,
"generator mismatch: "
<< dimension_ << "-dim low discrepancy "
<< "and " << prsg_.dimension() << "-dim pseudo random")
randomizer_ = prsg_.nextSequence();
}
template <class LDS, class PRS>
RandomizedLDS<LDS, PRS>::RandomizedLDS(const LDS& ldsg)
: ldsg_(ldsg), pristineldsg_(ldsg),
prsg_(ldsg_.dimension()), dimension_(ldsg_.dimension()),
x(std::vector<Real> (dimension_), 1.0), randomizer_(std::vector<Real> (dimension_), 1.0) {
randomizer_ = prsg_.nextSequence();
}
template <class LDS, class PRS>
RandomizedLDS<LDS, PRS>::RandomizedLDS(Size dimensionality,
BigNatural ldsSeed,
BigNatural prsSeed)
: ldsg_(dimensionality, ldsSeed), pristineldsg_(dimensionality, ldsSeed),
prsg_(dimensionality, prsSeed), dimension_(dimensionality),
x(std::vector<Real> (dimensionality), 1.0), randomizer_(std::vector<Real> (dimensionality), 1.0) {
randomizer_ = prsg_.nextSequence();
}
template <class LDS, class PRS>
inline const typename RandomizedLDS<LDS, PRS>::sample_type&
RandomizedLDS<LDS, PRS>::nextSequence() const {
typename LDS::sample_type sample =
ldsg_.nextSequence();
x.weight = randomizer_.weight * sample.weight;
for (Size i = 0; i < dimension_; i++) {
x.value[i] = randomizer_.value[i] + sample.value[i];
if (x.value[i]>1.0)
x.value[i] -= 1.0;
}
return x;
}
}
#endif
|