QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
haltonrsg.cpp
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// ===========================================================================
21// NOTE: The following copyright notice applies to the original code,
22//
23// Copyright (C) 2002 Peter Jäckel "Monte Carlo Methods in Finance".
24// All rights reserved.
25//
26// Permission to use, copy, modify, and distribute this software is freely
27// granted, provided that this notice is preserved.
28// ===========================================================================
29
33
34namespace QuantLib {
35
36 HaltonRsg::HaltonRsg(Size dimensionality,
37 unsigned long seed,
38 bool randomStart,
39 bool randomShift)
40 : dimensionality_(dimensionality), sequence_(std::vector<Real>(dimensionality), 1.0),
41 randomStart_(dimensionality, 0UL), randomShift_(dimensionality, 0.0) {
42
43 QL_REQUIRE(dimensionality>0,
44 "dimensionality must be greater than 0");
45
46 if (randomStart || randomShift) {
48 uniformRsg(dimensionality_, seed);
49 if (randomStart)
50 randomStart_ = uniformRsg.nextInt32Sequence();
51 if (randomShift)
52 randomShift_ = uniformRsg.nextSequence().value;
53 }
54 }
55
58 for (Size i=0; i<dimensionality_; ++i) {
59 double h = 0.0;
60 unsigned long b = PrimeNumbers::get(i);
61 double f = 1.0;
62 unsigned long k = sequenceCounter_+randomStart_[i];
63 while (k != 0U) {
64 f /= b;
65 h += (k%b)*f;
66 k /= b;
67 }
69 sequence_.value[i] -= long(sequence_.value[i]);
70 }
71 return sequence_;
72 }
73
74}
75
HaltonRsg(Size dimensionality, unsigned long seed=0, bool randomStart=true, bool randomShift=false)
Definition: haltonrsg.cpp:36
const sample_type & nextSequence() const
Definition: haltonrsg.cpp:56
std::vector< Real > randomShift_
Definition: haltonrsg.hpp:60
sample_type sequence_
Definition: haltonrsg.hpp:58
unsigned long sequenceCounter_
Definition: haltonrsg.hpp:57
std::vector< unsigned long > randomStart_
Definition: haltonrsg.hpp:59
static BigNatural get(Size absoluteIndex)
Get and store one after another.
Random sequence generator based on a pseudo-random number generator.
const sample_type & nextSequence() const
std::vector< BigNatural > nextInt32Sequence() const
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
ext::function< Real(Real)> b
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
Halton low-discrepancy sequence generator.
Definition: any.hpp:35
STL namespace.
Prime numbers calculator.
random-number generation policies