Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
multipathvariategenerator.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2020 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
20
21#include <boost/make_shared.hpp>
22
23using namespace QuantLib;
24
25namespace QuantExt {
26
27MultiPathVariateGeneratorBase::MultiPathVariateGeneratorBase(const Size dimension, const Size timeSteps)
28 : dimension_(dimension), timeSteps_(timeSteps) {}
29
30Sample<std::vector<Array>> MultiPathVariateGeneratorBase::next() const {
31
32 Sample<std::vector<Real>> sequence = nextSequence();
33 Sample<std::vector<Array>> result(std::vector<Array>(timeSteps_, Array(dimension_)), sequence.weight);
34
35 for (Size i = 0; i < timeSteps_; ++i) {
36 Size offset = i * dimension_;
37 std::copy(sequence.value.begin() + offset, sequence.value.begin() + offset + dimension_,
38 result.value[i].begin());
39 }
40
41 return result;
42}
43
45 const Size timeSteps,
46 BigNatural seed,
47 bool antitheticSampling)
48 : MultiPathVariateGeneratorBase(dimension, timeSteps), seed_(seed), antitheticSampling_(antitheticSampling),
49 antitheticVariate_(true) {
51}
52
54 rsg_ = QuantLib::ext::make_shared<
55 InverseCumulativeRsg<RandomSequenceGenerator<MersenneTwisterUniformRng>, InverseCumulativeNormal>>(
56 RandomSequenceGenerator<MersenneTwisterUniformRng>(dimension_ * timeSteps_, MersenneTwisterUniformRng(seed_)),
57 InverseCumulativeNormal());
58 antitheticVariate_ = true;
59}
60
65 auto tmp = rsg_->lastSequence();
66 std::transform(tmp.value.begin(), tmp.value.end(), tmp.value.begin(), std::negate<Real>());
67 return tmp;
68 } else {
69 return rsg_->nextSequence();
70 }
71 }
72 return rsg_->nextSequence();
73}
74
75MultiPathVariateGeneratorSobol::MultiPathVariateGeneratorSobol(const Size dimension, const Size timeSteps,
76 BigNatural seed,
77 SobolRsg::DirectionIntegers directionIntegers)
78 : MultiPathVariateGeneratorBase(dimension, timeSteps), seed_(seed), directionIntegers_(directionIntegers) {
80}
81
83 rsg_ = QuantLib::ext::make_shared<InverseCumulativeRsg<SobolRsg, InverseCumulativeNormal>>(
84 SobolRsg(dimension_ * timeSteps_, seed_, directionIntegers_), InverseCumulativeNormal());
85}
86
87Sample<std::vector<Real>> MultiPathVariateGeneratorSobol::nextSequence() const { return rsg_->nextSequence(); }
88
90 const Size dimension, const Size timeSteps, BigNatural seed, SobolRsg::DirectionIntegers directionIntegers,
91 BigNatural scrambleSeed)
92 : MultiPathVariateGeneratorBase(dimension, timeSteps), seed_(seed), directionIntegers_(directionIntegers),
93 scrambleSeed_(scrambleSeed) {
95}
96
98 rsg_ = QuantLib::ext::make_shared<InverseCumulativeRsg<Burley2020SobolRsg, InverseCumulativeNormal>>(
99 Burley2020SobolRsg(dimension_ * timeSteps_, seed_, directionIntegers_, scrambleSeed_), InverseCumulativeNormal());
100}
101
103 return rsg_->nextSequence();
104}
105
107 const Size dimension, const Size timeSteps, SobolBrownianGenerator::Ordering ordering, BigNatural seed,
108 SobolRsg::DirectionIntegers directionIntegers)
109 : MultiPathVariateGeneratorBase(dimension, timeSteps), ordering_(ordering), seed_(seed),
110 directionIntegers_(directionIntegers) {}
111
113 // not needed, we directly overwrite next()
114 return Sample<std::vector<Real>>(std::vector<Real>(), 0.0);
115}
116
118 Real weight = gen_->nextPath();
119 std::vector<Array> output(timeSteps_, Array(dimension_));
120 std::vector<Real> tmp(dimension_);
121 for (Size i = 0; i < timeSteps_; ++i) {
122 gen_->nextStep(tmp);
123 std::copy(tmp.begin(), tmp.end(), output[i].begin());
124 }
125 return Sample<std::vector<Array>>(output, weight);
126}
127
129 const Size dimension, const Size timeSteps, SobolBrownianGenerator::Ordering ordering, BigNatural seed,
130 SobolRsg::DirectionIntegers directionIntegers)
131 : MultiPathVariateGeneratorSobolBrownianBridgeBase(dimension, timeSteps, ordering, seed, directionIntegers) {
133}
134
136 gen_ = QuantLib::ext::make_shared<SobolBrownianGenerator>(dimension_, timeSteps_, ordering_, seed_, directionIntegers_);
137}
138
140 const Size dimension, const Size timeSteps, SobolBrownianGenerator::Ordering ordering, BigNatural seed,
141 SobolRsg::DirectionIntegers directionIntegers, BigNatural scrambleSeed)
142 : MultiPathVariateGeneratorSobolBrownianBridgeBase(dimension, timeSteps, ordering, seed, directionIntegers),
143 scrambleSeed_(scrambleSeed) {
145}
146
148 gen_ = QuantLib::ext::make_shared<Burley2020SobolBrownianGenerator>(dimension_, timeSteps_, ordering_, seed_,
150}
151
152QuantLib::ext::shared_ptr<MultiPathVariateGeneratorBase>
153makeMultiPathVariateGenerator(const SequenceType s, const Size dimension, const Size timeSteps, const BigNatural seed,
154 const SobolBrownianGenerator::Ordering ordering,
155 const SobolRsg::DirectionIntegers directionIntegers) {
156 switch (s) {
157 case MersenneTwister:
158 return QuantLib::ext::make_shared<QuantExt::MultiPathVariateGeneratorMersenneTwister>(dimension, timeSteps, seed,
159 false);
161 return QuantLib::ext::make_shared<QuantExt::MultiPathVariateGeneratorMersenneTwister>(dimension, timeSteps, seed, true);
162 case Sobol:
163 return QuantLib::ext::make_shared<QuantExt::MultiPathVariateGeneratorSobol>(dimension, timeSteps, seed,
164 directionIntegers);
165 case Burley2020Sobol:
166 return QuantLib::ext::make_shared<QuantExt::MultiPathVariateGeneratorBurley2020Sobol>(dimension, timeSteps, seed,
167 directionIntegers, seed + 1);
169 return QuantLib::ext::make_shared<QuantExt::MultiPathVariateGeneratorSobolBrownianBridge>(
170 dimension, timeSteps, ordering, seed, directionIntegers);
172 return QuantLib::ext::make_shared<QuantExt::MultiPathVariateGeneratorBurley2020SobolBrownianBridge>(
173 dimension, timeSteps, ordering, seed, directionIntegers, seed + 1);
174 default:
175 QL_FAIL("Unknown sequence type");
176 }
177}
178
179} // namespace QuantExt
virtual Sample< std::vector< Real > > nextSequence() const =0
virtual Sample< std::vector< Array > > next() const
MultiPathVariateGeneratorBase(const Size dimension, const Size timeSteps)
MultiPathVariateGeneratorBurley2020SobolBrownianBridge(const Size dimension, const Size timeSteps, SobolBrownianGenerator::Ordering ordering=SobolBrownianGenerator::Steps, BigNatural seed=42, SobolRsg::DirectionIntegers directionIntegers=SobolRsg::JoeKuoD7, BigNatural scrambleSeed=43)
QuantLib::ext::shared_ptr< InverseCumulativeRsg< Burley2020SobolRsg, InverseCumulativeNormal > > rsg_
Sample< std::vector< Real > > nextSequence() const override
MultiPathVariateGeneratorBurley2020Sobol(const Size dimension, const Size timeSteps, BigNatural seed=42, SobolRsg::DirectionIntegers directionIntegers=SobolRsg::JoeKuoD7, BigNatural scrambleSeed=43)
MultiPathVariateGeneratorMersenneTwister(const Size dimension, const Size timeSteps, BigNatural seed=0, bool antitheticSampling=false)
Sample< std::vector< Real > > nextSequence() const override
QuantLib::ext::shared_ptr< PseudoRandom::rsg_type > rsg_
MultiPathVariateGeneratorSobolBrownianBridgeBase(const Size dimension, const Size timeSteps, SobolBrownianGenerator::Ordering ordering=SobolBrownianGenerator::Steps, BigNatural seed=0, SobolRsg::DirectionIntegers directionIntegers=SobolRsg::JoeKuoD7)
Sample< std::vector< Real > > nextSequence() const override
QuantLib::ext::shared_ptr< SobolBrownianGeneratorBase > gen_
MultiPathVariateGeneratorSobolBrownianBridge(const Size dimension, const Size timeSteps, SobolBrownianGenerator::Ordering ordering=SobolBrownianGenerator::Steps, BigNatural seed=42, SobolRsg::DirectionIntegers directionIntegers=SobolRsg::JoeKuoD7)
MultiPathVariateGeneratorSobol(const Size dimension, const Size timeSteps, BigNatural seed=0, SobolRsg::DirectionIntegers directionIntegers=SobolRsg::JoeKuoD7)
Sample< std::vector< Real > > nextSequence() const override
QuantLib::ext::shared_ptr< LowDiscrepancy::rsg_type > rsg_
multi path generators returning the generating N(0,1) variates, this is very much in parallel to the ...
QuantLib::ext::shared_ptr< MultiPathVariateGeneratorBase > makeMultiPathVariateGenerator(const SequenceType s, const Size dimension, const Size timeSteps, const BigNatural seed, const SobolBrownianGenerator::Ordering ordering, const SobolRsg::DirectionIntegers directionIntegers)