1 /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 3 /* 4 Copyright (C) 2004 Ferdinando Ametrano 5 Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl 6 Copyright (C) 2004 Walter Penschke 7 8 This file is part of QuantLib, a free-software/open-source library 9 for financial quantitative analysts and developers - http://quantlib.org/ 10 11 QuantLib is free software: you can redistribute it and/or modify it 12 under the terms of the QuantLib license. You should have received a 13 copy of the license along with this program; if not, please email 14 <quantlib-dev@lists.sf.net>. The license is also available online at 15 <http://quantlib.org/license.shtml>. 16 17 This program is distributed in the hope that it will be useful, but WITHOUT 18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 19 FOR A PARTICULAR PURPOSE. See the license for more details. 20 */ 21 22 /*! \file rngtraits.hpp 23 \brief random-number generation policies 24 */ 25 26 #ifndef quantlib_rng_traits_hpp 27 #define quantlib_rng_traits_hpp 28 29 #include <ql/methods/montecarlo/pathgenerator.hpp> 30 #include <ql/math/randomnumbers/mt19937uniformrng.hpp> 31 #include <ql/math/randomnumbers/inversecumulativerng.hpp> 32 #include <ql/math/randomnumbers/randomsequencegenerator.hpp> 33 #include <ql/math/randomnumbers/sobolrsg.hpp> 34 #include <ql/math/randomnumbers/inversecumulativersg.hpp> 35 #include <ql/math/distributions/normaldistribution.hpp> 36 #include <ql/math/distributions/poissondistribution.hpp> 37 38 namespace QuantLib { 39 40 // random number traits 41 42 template <class URNG, class IC> 43 struct GenericPseudoRandom { 44 // typedefs 45 typedef URNG urng_type; 46 typedef InverseCumulativeRng<urng_type,IC> rng_type; 47 typedef RandomSequenceGenerator<urng_type> ursg_type; 48 typedef InverseCumulativeRsg<ursg_type,IC> rsg_type; 49 // more traits 50 enum { allowsErrorEstimate = 1 }; 51 // factory make_sequence_generatorQuantLib::GenericPseudoRandom52 static rsg_type make_sequence_generator(Size dimension, 53 BigNatural seed) { 54 ursg_type g(dimension, seed); 55 return (icInstance ? rsg_type(g, *icInstance) : rsg_type(g)); 56 } 57 // data 58 static ext::shared_ptr<IC> icInstance; 59 }; 60 61 // static member initialization 62 template<class URNG, class IC> 63 ext::shared_ptr<IC> GenericPseudoRandom<URNG, IC>::icInstance; 64 65 66 //! default traits for pseudo-random number generation 67 /*! \test a sequence generator is generated and tested by comparing 68 samples against known good values. 69 */ 70 typedef GenericPseudoRandom<MersenneTwisterUniformRng, 71 InverseCumulativeNormal> PseudoRandom; 72 73 //! traits for Poisson-distributed pseudo-random number generation 74 /*! \test sequence generators are generated and tested by comparing 75 samples against known good values. 76 */ 77 typedef GenericPseudoRandom<MersenneTwisterUniformRng, 78 InverseCumulativePoisson> PoissonPseudoRandom; 79 80 81 template <class URSG, class IC> 82 struct GenericLowDiscrepancy { 83 // typedefs 84 typedef URSG ursg_type; 85 typedef InverseCumulativeRsg<ursg_type,IC> rsg_type; 86 // more traits 87 enum { allowsErrorEstimate = 0 }; 88 // factory make_sequence_generatorQuantLib::GenericLowDiscrepancy89 static rsg_type make_sequence_generator(Size dimension, 90 BigNatural seed) { 91 ursg_type g(dimension, seed); 92 return (icInstance ? rsg_type(g, *icInstance) : rsg_type(g)); 93 } 94 // data 95 static ext::shared_ptr<IC> icInstance; 96 }; 97 98 // static member initialization 99 template<class URSG, class IC> 100 ext::shared_ptr<IC> GenericLowDiscrepancy<URSG, IC>::icInstance; 101 102 103 //! default traits for low-discrepancy sequence generation 104 typedef GenericLowDiscrepancy<SobolRsg, 105 InverseCumulativeNormal> LowDiscrepancy; 106 107 } 108 109 110 #endif 111