QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
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) 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
28#include <ql/errors.hpp>
29#include <vector>
30
31namespace 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>
51 public:
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:
89 RNG rng_;
91 mutable std::vector<BigNatural> int32Sequence_;
92 };
93
94}
95
96
97#endif
Random sequence generator based on a pseudo-random number generator.
const sample_type & nextSequence() const
RandomSequenceGenerator(Size dimensionality, const RNG &rng)
const sample_type & lastSequence() const
std::vector< BigNatural > nextInt32Sequence() const
Sample< std::vector< Real > > sample_type
RandomSequenceGenerator(Size dimensionality, BigNatural seed=0)
Classes and functions for error handling.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
unsigned QL_BIG_INTEGER BigNatural
large positive integer
Definition: types.hpp:46
STL namespace.
weighted sample
weighted sample
Definition: sample.hpp:35