| 1 | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 | |
| 3 | /* |
| 4 | Copyright (C) 2003 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 | |
| 20 | /*! \file randomsequencegenerator.hpp |
| 21 | \brief Random sequence generator based on a pseudo-random number generator |
| 22 | */ |
| 23 | |
| 24 | #ifndef quantlib_random_sequence_generator_h |
| 25 | #define quantlib_random_sequence_generator_h |
| 26 | |
| 27 | #include <ql/methods/montecarlo/sample.hpp> |
| 28 | #include <ql/errors.hpp> |
| 29 | #include <vector> |
| 30 | |
| 31 | namespace QuantLib { |
| 32 | |
| 33 | //! Random sequence generator based on a pseudo-random number generator |
| 34 | /*! Random sequence generator based on a pseudo-random number |
| 35 | generator RNG. |
| 36 | |
| 37 | Class RNG must implement the following interface: |
| 38 | \code |
| 39 | RNG::sample_type RNG::next() const; |
| 40 | \endcode |
| 41 | If a client of this class wants to use the nextInt32Sequence method, |
| 42 | class RNG must also implement |
| 43 | \code |
| 44 | unsigned long RNG::nextInt32() const; |
| 45 | \endcode |
| 46 | |
| 47 | \warning do not use with low-discrepancy sequence generator. |
| 48 | */ |
| 49 | template<class RNG> |
| 50 | class RandomSequenceGenerator { |
| 51 | public: |
| 52 | typedef Sample<std::vector<Real> > sample_type; |
| 53 | RandomSequenceGenerator(Size dimensionality, |
| 54 | const RNG& rng) |
| 55 | : dimensionality_(dimensionality), rng_(rng), |
| 56 | sequence_(std::vector<Real> (dimensionality), 1.0), |
| 57 | int32Sequence_(dimensionality) { |
| 58 | QL_REQUIRE(dimensionality>0, |
| 59 | "dimensionality must be greater than 0" ); |
| 60 | } |
| 61 | |
| 62 | explicit RandomSequenceGenerator(Size dimensionality, |
| 63 | BigNatural seed = 0) |
| 64 | : dimensionality_(dimensionality), rng_(seed), |
| 65 | sequence_(std::vector<Real> (dimensionality), 1.0), |
| 66 | int32Sequence_(dimensionality) {} |
| 67 | |
| 68 | const sample_type& nextSequence() const { |
| 69 | sequence_.weight = 1.0; |
| 70 | for (Size i=0; i<dimensionality_; i++) { |
| 71 | typename RNG::sample_type x(rng_.next()); |
| 72 | sequence_.value[i] = x.value; |
| 73 | sequence_.weight *= x.weight; |
| 74 | } |
| 75 | return sequence_; |
| 76 | } |
| 77 | std::vector<BigNatural> nextInt32Sequence() const { |
| 78 | for (Size i=0; i<dimensionality_; i++) { |
| 79 | int32Sequence_[i] = rng_.nextInt32(); |
| 80 | } |
| 81 | return int32Sequence_; |
| 82 | } |
| 83 | const sample_type& lastSequence() const { |
| 84 | return sequence_; |
| 85 | } |
| 86 | Size dimension() const {return dimensionality_;} |
| 87 | private: |
| 88 | Size dimensionality_; |
| 89 | RNG rng_; |
| 90 | mutable sample_type sequence_; |
| 91 | mutable std::vector<BigNatural> int32Sequence_; |
| 92 | }; |
| 93 | |
| 94 | } |
| 95 | |
| 96 | |
| 97 | #endif |
| 98 | |