Coverage Report

Created: 2025-11-04 06:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quantlib/ql/experimental/math/zigguratrng.hpp
Line
Count
Source
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
 <https://www.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
27
#include <ql/math/randomnumbers/mt19937uniformrng.hpp>
28
#include <ql/math/randomnumbers/randomsequencegenerator.hpp>
29
30
namespace 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
    */
52
    class ZigguratRng {
53
      public:
54
        typedef Sample<Real> sample_type;
55
        explicit ZigguratRng(unsigned long seed = 0);
56
0
        sample_type next() const { return {nextGaussian(), 1.0}; }
57
58
      private:
59
        mutable MersenneTwisterUniformRng mt32_;
60
        Real nextGaussian() const;
61
    };
62
63
    // RNG traits for Ziggurat generator
64
    struct Ziggurat {
65
        // typedefs
66
        typedef ZigguratRng rng_type;
67
        typedef RandomSequenceGenerator<rng_type> rsg_type;
68
        // more traits
69
        enum { allowsErrorEstimate = 1 };
70
        // factory
71
        static rsg_type make_sequence_generator(Size dimension,
72
0
                                                BigNatural seed) {
73
0
            return rsg_type(dimension, seed);
74
0
        }
75
    };
76
77
}
78
79
#endif