QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
multipathgenerator.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) 2000, 2001, 2002, 2003 RiskMap srl
5 Copyright (C) 2003 Ferdinando Ametrano
6 Copyright (C) 2003, 2004, 2005 StatPro Italia srl
7 Copyright (C) 2005 Klaus Spanderen
8
9 This file is part of QuantLib, a free-software/open-source library
10 for financial quantitative analysts and developers - http://quantlib.org/
11
12 QuantLib is free software: you can redistribute it and/or modify it
13 under the terms of the QuantLib license. You should have received a
14 copy of the license along with this program; if not, please email
15 <quantlib-dev@lists.sf.net>. The license is also available online at
16 <http://quantlib.org/license.shtml>.
17
18 This program is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 FOR A PARTICULAR PURPOSE. See the license for more details.
21*/
22
23/*! \file multipathgenerator.hpp
24 \brief Generates a multi path from a random-array generator
25*/
26
27#ifndef quantlib_multi_path_generator_hpp
28#define quantlib_multi_path_generator_hpp
29
33#include <utility>
34
35namespace QuantLib {
36
37 //! Generates a multipath from a random number generator.
38 /*! RSG is a sample generator which returns a random sequence.
39 It must have the minimal interface:
40 \code
41 RSG {
42 Sample<Array> next();
43 };
44 \endcode
45
46 \ingroup mcarlo
47
48 \test the generated paths are checked against cached results
49 */
50 template <class GSG>
52 public:
54 MultiPathGenerator(const ext::shared_ptr<StochasticProcess>&,
55 const TimeGrid&,
56 GSG generator,
57 bool brownianBridge = false);
58 const sample_type& next() const;
59 const sample_type& antithetic() const;
60 private:
61 const sample_type& next(bool antithetic) const;
63 ext::shared_ptr<StochasticProcess> process_;
66 };
67
68
69 // template definitions
70
71 template <class GSG>
72 MultiPathGenerator<GSG>::MultiPathGenerator(const ext::shared_ptr<StochasticProcess>& process,
73 const TimeGrid& times,
74 GSG generator,
75 bool brownianBridge)
76 : brownianBridge_(brownianBridge), process_(process), generator_(std::move(generator)),
77 next_(MultiPath(process->size(), times), 1.0) {
78
79 QL_REQUIRE(generator_.dimension() ==
80 process->factors()*(times.size()-1),
81 "dimension (" << generator_.dimension()
82 << ") is not equal to ("
83 << process->factors() << " * " << times.size()-1
84 << ") the number of factors "
85 << "times the number of time steps");
86 QL_REQUIRE(times.size() > 1,
87 "no times given");
88 }
89
90 template <class GSG>
91 inline const typename MultiPathGenerator<GSG>::sample_type&
93 return next(false);
94 }
95
96 template <class GSG>
97 inline const typename MultiPathGenerator<GSG>::sample_type&
99 return next(true);
100 }
101
102 template <class GSG>
104 MultiPathGenerator<GSG>::next(bool antithetic) const {
105
106 if (brownianBridge_) {
107
108 QL_FAIL("Brownian bridge not supported");
109
110 } else {
111
112 typedef typename GSG::sample_type sequence_type;
113 const sequence_type& sequence_ =
114 antithetic ? generator_.lastSequence()
115 : generator_.nextSequence();
116
117 Size m = process_->size();
118 Size n = process_->factors();
119
120 MultiPath& path = next_.value;
121
122 Array asset = process_->initialValues();
123 for (Size j=0; j<m; j++)
124 path[j].front() = asset[j];
125
126 Array temp(n);
127 next_.weight = sequence_.weight;
128
129 const TimeGrid& timeGrid = path[0].timeGrid();
130 Time t, dt;
131 for (Size i = 1; i < path.pathSize(); i++) {
132 Size offset = (i-1)*n;
133 t = timeGrid[i-1];
134 dt = timeGrid.dt(i-1);
135 if (antithetic)
136 std::transform(sequence_.value.begin()+offset,
137 sequence_.value.begin()+offset+n,
138 temp.begin(),
139 std::negate<>());
140 else
141 std::copy(sequence_.value.begin()+offset,
142 sequence_.value.begin()+offset+n,
143 temp.begin());
144
145 asset = process_->evolve(t, asset, dt, temp);
146 for (Size j=0; j<m; j++)
147 path[j][i] = asset[j];
148 }
149 return next_;
150 }
151 }
152
153}
154
155#endif
1-D array used in linear algebra.
Definition: array.hpp:52
const_iterator begin() const
Definition: array.hpp:503
Generates a multipath from a random number generator.
const sample_type & antithetic() const
ext::shared_ptr< StochasticProcess > process_
MultiPathGenerator(const ext::shared_ptr< StochasticProcess > &, const TimeGrid &, GSG generator, bool brownianBridge=false)
const sample_type & next() const
Correlated multiple asset paths.
Definition: multipath.hpp:39
Size pathSize() const
Definition: multipath.hpp:48
time grid class
Definition: timegrid.hpp:43
Time dt(Size i) const
Definition: timegrid.hpp:154
Size size() const
Definition: timegrid.hpp:164
const DefaultType & t
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition: errors.hpp:92
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
std::size_t Size
size of a container
Definition: types.hpp:58
Correlated multiple asset paths.
Definition: any.hpp:35
STL namespace.
weighted sample
stochastic processes
weighted sample
Definition: sample.hpp:35