File: randomsequencegenerator.hpp

package info (click to toggle)
quantlib 1.21-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 45,532 kB
  • sloc: cpp: 388,042; makefile: 6,661; sh: 4,381; lisp: 86
file content (97 lines) | stat: -rw-r--r-- 3,388 bytes parent folder | download | duplicates (3)
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
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*
 Copyright (C) 2003 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 randomsequencegenerator.hpp
    \brief Random sequence generator based on a pseudo-random number generator
*/

#ifndef quantlib_random_sequence_generator_h
#define quantlib_random_sequence_generator_h

#include <ql/methods/montecarlo/sample.hpp>
#include <ql/errors.hpp>
#include <vector>

namespace QuantLib {

    //! Random sequence generator based on a pseudo-random number generator
    /*! Random sequence generator based on a pseudo-random number
        generator RNG.

        Class RNG must implement the following interface:
        \code
            RNG::sample_type RNG::next() const;
        \endcode
        If a client of this class wants to use the nextInt32Sequence method,
        class RNG must also implement
        \code
            unsigned long RNG::nextInt32() const;
        \endcode

        \warning do not use with low-discrepancy sequence generator.
    */
    template<class RNG>
    class RandomSequenceGenerator {
      public:
        typedef Sample<std::vector<Real> > sample_type;
        RandomSequenceGenerator(Size dimensionality,
                                const RNG& rng)
        : dimensionality_(dimensionality), rng_(rng),
          sequence_(std::vector<Real> (dimensionality), 1.0),
          int32Sequence_(dimensionality) {
          QL_REQUIRE(dimensionality>0, 
                     "dimensionality must be greater than 0");
        }

        explicit RandomSequenceGenerator(Size dimensionality,
                                         BigNatural seed = 0)
        : dimensionality_(dimensionality), rng_(seed),
          sequence_(std::vector<Real> (dimensionality), 1.0),
          int32Sequence_(dimensionality) {}

        const sample_type& nextSequence() const {
            sequence_.weight = 1.0;
            for (Size i=0; i<dimensionality_; i++) {
                typename RNG::sample_type x(rng_.next());
                sequence_.value[i] = x.value;
                sequence_.weight  *= x.weight;
            }
            return sequence_;
        }
        std::vector<BigNatural> nextInt32Sequence() const {
            for (Size i=0; i<dimensionality_; i++) {
                int32Sequence_[i] = rng_.nextInt32();
            }
            return int32Sequence_;
        }
        const sample_type& lastSequence() const {
            return sequence_;
        }
        Size dimension() const {return dimensionality_;}
      private:
        Size dimensionality_;
        RNG rng_;
        mutable sample_type sequence_;
        mutable std::vector<BigNatural> int32Sequence_;
    };

}


#endif