QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
mtbrowniangenerator.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) 2006 StatPro Italia srl
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
21#include <algorithm>
22
23namespace QuantLib {
24
25 MTBrownianGenerator::MTBrownianGenerator(Size factors, Size steps, unsigned long seed)
26 : factors_(factors), steps_(steps),
27 generator_(factors * steps, MersenneTwisterUniformRng(seed)) {}
28
29 Real MTBrownianGenerator::nextStep(std::vector<Real>& output) {
30 #if defined(QL_EXTRA_SAFETY_CHECKS)
31 QL_REQUIRE(output.size() == factors_, "size mismatch");
32 QL_REQUIRE(lastStep_<steps_, "uniform sequence exhausted");
33 #endif
34 // no copying, just fetching a reference
35 const std::vector<Real>& currentSequence = generator_.lastSequence().value;
36 Size start = lastStep_*factors_, end = (lastStep_+1)*factors_;
37 std::transform(currentSequence.begin()+start,
38 currentSequence.begin()+end,
39 output.begin(),
41 ++lastStep_;
42 return 1.0;
43 }
44
47 sample_type;
48
49 const sample_type& sample = generator_.nextSequence();
50 lastStep_ = 0;
51 return sample.weight;
52 }
53
55
57
58
60 : seed_(seed) {}
61
62 ext::shared_ptr<BrownianGenerator>
64 return ext::shared_ptr<BrownianGenerator>(
65 new MTBrownianGenerator(factors, steps, seed_));
66 }
67
68}
69
MTBrownianGeneratorFactory(unsigned long seed=0)
ext::shared_ptr< BrownianGenerator > create(Size factors, Size steps) const override
Mersenne-twister Brownian generator for market-model simulations.
RandomSequenceGenerator< MersenneTwisterUniformRng > generator_
Real nextStep(std::vector< Real > &) override
Size numberOfFactors() const override
Size numberOfSteps() const override
MTBrownianGenerator(Size factors, Size steps, unsigned long seed=0)
InverseCumulativeNormal inverseCumulative_
Uniform random number generator.
const sample_type & nextSequence() const
const sample_type & lastSequence() const
#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