QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
zigguratrng.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) 2010 Kakhkhor Abdijalilov
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 zigguratrng.hpp
21 \brief Ziggurat random-number generator
22*/
23
24#ifndef quantlib_ziggurat_generator_hpp
25#define quantlib_ziggurat_generator_hpp
26
29
30namespace QuantLib {
31
32 //! Ziggurat random-number generator
33 /*! This generator returns standard normal variates using the
34 Ziggurat method. The underlying RNG is mt19937 (32 bit
35 version). The algorithm is described in Marsaglia and Tsang
36 (2000). "The Ziggurat Method for Generating Random
37 Variables". Journal of Statistical Software 5 (8). Note that
38 step 2 from the above paper reuses the rightmost 8 bits of the
39 random integer, which creates correlation between steps 1 and
40 2. This implementation was written from scratch, following
41 Marsaglia and Tsang. It avoids the correlation by using only
42 the leftmost 24 bits of mt19937's output.
43
44 Note that the GNU GSL implementation uses a different value
45 for the right-most step. The GSL value is somewhat different
46 from the one reported by Marsaglia and Tsang because GSL uses
47 a different tail. This implementation uses the same right-most
48 step as reported by Marsaglia and Tsang. The generator was
49 put through Marsaglia's Diehard battery of tests and didn't
50 exibit any abnormal behavior.
51 */
53 public:
55 explicit ZigguratRng(unsigned long seed = 0);
56 sample_type next() const { return {nextGaussian(), 1.0}; }
57
58 private:
60 Real nextGaussian() const;
61 };
62
63 // RNG traits for Ziggurat generator
64 struct Ziggurat {
65 // typedefs
68 // more traits
69 enum { allowsErrorEstimate = 1 };
70 // factory
72 BigNatural seed) {
73 return rsg_type(dimension, seed);
74 }
75 };
76
77}
78
79#endif
Uniform random number generator.
Random sequence generator based on a pseudo-random number generator.
Ziggurat random-number generator.
Definition: zigguratrng.hpp:52
sample_type next() const
Definition: zigguratrng.hpp:56
MersenneTwisterUniformRng mt32_
Definition: zigguratrng.hpp:59
Real nextGaussian() const
Sample< Real > sample_type
Definition: zigguratrng.hpp:54
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
Random sequence generator based on a pseudo-random number generator.
weighted sample
Definition: sample.hpp:35
static rsg_type make_sequence_generator(Size dimension, BigNatural seed)
Definition: zigguratrng.hpp:71
ZigguratRng rng_type
Definition: zigguratrng.hpp:66
RandomSequenceGenerator< rng_type > rsg_type
Definition: zigguratrng.hpp:67