randomsequencegenerator.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) 2006 Ferdinando Ametrano
5  Copyright (C) 2006 Aurelien Chanudet
6 
7  This file is part of QuantLib, a free-software/open-source library
8  for financial quantitative analysts and developers - http://quantlib.org/
9 
10  QuantLib is free software: you can redistribute it and/or modify it
11  under the terms of the QuantLib license. You should have received a
12  copy of the license along with this program; if not, please email
13  <quantlib-dev@lists.sf.net>. The license is also available online at
14  <http://quantlib.org/license.shtml>.
15 
16  This program is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  FOR A PARTICULAR PURPOSE. See the license for more details.
19 */
20 
21 #ifndef qla_randomsequencegenerator_hpp
22 #define qla_randomsequencegenerator_hpp
23 
24 #include <oh/libraryobject.hpp>
25 
26 #include <ql/math/randomnumbers/rngtraits.hpp>
27 #include <ql/math/randomnumbers/faurersg.hpp>
28 #include <ql/math/randomnumbers/mt19937uniformrng.hpp>
29 #include <ql/math/randomnumbers/sobolrsg.hpp>
30 #include <ql/math/randomnumbers/haltonrsg.hpp>
31 
32 #include <vector>
33 
34 namespace QuantLibAddin {
35 
36 
37  QuantLib::Real rand();
38  void randomize(QuantLib::BigNatural seed);
39 
40  class RandomSequenceGenerator : public ObjectHandler::Object {
41  public:
42  std::vector<std::vector<double> > variates(long samples);
43  virtual std::vector<double> nextSequence() const = 0;
44  protected:
45  OH_OBJ_CTOR(RandomSequenceGenerator, ObjectHandler::Object);
46  };
47 
48  // Pseudo Random Sequences
49 
50  template <class URNG>
52  public:
54  const boost::shared_ptr<ObjectHandler::ValueObject>& properties,
55  long dimension,
56  const URNG& urng,
57  bool permanent)
58  : RandomSequenceGenerator(properties, permanent),
59  ursg_(QuantLib::RandomSequenceGenerator<URNG>(dimension, urng)) {}
60 
61  virtual std::vector<QuantLib::Real> nextSequence() const {
62  return ursg_.nextSequence().value;
63  }
64 
65  private:
66  typename QuantLib::GenericPseudoRandom<URNG, QuantLib::InverseCumulativeNormal>::ursg_type ursg_;
67  };
68 
69  class MersenneTwisterRsg : public PseudoRandomSequenceGenerator<QuantLib::MersenneTwisterUniformRng> {
70  public:
71  typedef QuantLib::MersenneTwisterUniformRng urng_type;
72 
74  const boost::shared_ptr<ObjectHandler::ValueObject>& properties,
75  long dimension,
76  long seed,
77  bool permanent);
78  };
79 
80  // Low Discrepancy Sequences
81 
82  template <class URSG>
84  public:
86  const boost::shared_ptr<ObjectHandler::ValueObject>& properties,
87  const URSG& ursg,
88  bool permanent)
89  : RandomSequenceGenerator(properties, permanent), ursg_(ursg) {}
90 
91  virtual std::vector<double> nextSequence() const {
92  return ursg_.nextSequence().value;
93  }
94 
95  private:
96  typename QuantLib::GenericLowDiscrepancy<URSG, QuantLib::InverseCumulativeNormal>::ursg_type ursg_;
97  };
98 
99  class FaureRsg : public LowDiscrepancySequenceGenerator<QuantLib::FaureRsg> {
100  public:
101  typedef QuantLib::FaureRsg rsg_type;
102  FaureRsg(
103  const boost::shared_ptr<ObjectHandler::ValueObject>& properties,
104  long dimension,
105  bool permanent);
106  };
107 
108  class HaltonRsg : public LowDiscrepancySequenceGenerator<QuantLib::HaltonRsg> {
109  public:
110  typedef QuantLib::HaltonRsg rsg_type;
111  HaltonRsg(
112  const boost::shared_ptr<ObjectHandler::ValueObject>& properties,
113  long dimension,
114  long seed,
115  bool permanent);
116  };
117 
118  class SobolRsg : public LowDiscrepancySequenceGenerator<QuantLib::SobolRsg> {
119  public:
120  typedef QuantLib::SobolRsg rsg_type;
121  SobolRsg(
122  const boost::shared_ptr<ObjectHandler::ValueObject>& properties,
123  long dimension,
124  long seed,
125  bool permanent);
126  };
127 
128 }
129 
130 #endif
131 
Definition: randomsequencegenerator.hpp:51
Definition: randomsequencegenerator.hpp:40
Definition: randomsequencegenerator.hpp:99
virtual std::vector< double > nextSequence() const =0
MersenneTwisterRsg(const boost::shared_ptr< ObjectHandler::ValueObject > &properties, long dimension, long seed, bool permanent)
virtual std::vector< QuantLib::Real > nextSequence() const
Definition: randomsequencegenerator.hpp:61
Definition: randomsequencegenerator.hpp:83
QuantLib::SobolRsg rsg_type
Definition: randomsequencegenerator.hpp:120
QuantLib::MersenneTwisterUniformRng urng_type
Definition: randomsequencegenerator.hpp:71
Definition: randomsequencegenerator.hpp:69
Definition: randomsequencegenerator.hpp:108
void randomize(QuantLib::BigNatural seed)
std::vector< std::vector< double > > variates(long samples)
Definition: randomsequencegenerator.hpp:118
PseudoRandomSequenceGenerator(const boost::shared_ptr< ObjectHandler::ValueObject > &properties, long dimension, const URNG &urng, bool permanent)
Definition: randomsequencegenerator.hpp:53
Definition: abcd.hpp:38
LowDiscrepancySequenceGenerator(const boost::shared_ptr< ObjectHandler::ValueObject > &properties, const URSG &ursg, bool permanent)
Definition: randomsequencegenerator.hpp:85
QuantLib::FaureRsg rsg_type
Definition: randomsequencegenerator.hpp:101
Definition: abcd.hpp:30
OH_OBJ_CTOR(RandomSequenceGenerator, ObjectHandler::Object)
HaltonRsg(const boost::shared_ptr< ObjectHandler::ValueObject > &properties, long dimension, long seed, bool permanent)
SobolRsg(const boost::shared_ptr< ObjectHandler::ValueObject > &properties, long dimension, long seed, bool permanent)
FaureRsg(const boost::shared_ptr< ObjectHandler::ValueObject > &properties, long dimension, bool permanent)
QuantLib::HaltonRsg rsg_type
Definition: randomsequencegenerator.hpp:110
QuantLib::Real rand()
virtual std::vector< double > nextSequence() const
Definition: randomsequencegenerator.hpp:91