QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
pathgenerator.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2002, 2003 Ferdinando Ametrano
5 Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
6 Copyright (C) 2003, 2004, 2005, 2006 StatPro Italia srl
7
8 This file is part of QuantLib, a free-software/open-source library
9 for financial quantitative analysts and developers - http://quantlib.org/
10
11 QuantLib is free software: you can redistribute it and/or modify it
12 under the terms of the QuantLib license. You should have received a
13 copy of the license along with this program; if not, please email
14 <quantlib-dev@lists.sf.net>. The license is also available online at
15 <http://quantlib.org/license.shtml>.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the license for more details.
20*/
21
26#ifndef quantlib_montecarlo_path_generator_hpp
27#define quantlib_montecarlo_path_generator_hpp
28
29#include <ql/methods/montecarlo/brownianbridge.hpp>
30#include <ql/stochasticprocess.hpp>
31#include <utility>
32
33namespace QuantLib {
34 class StochasticProcess;
35 class StochasticProcess1D;
37
44 template <class GSG>
46 public:
48 // constructors
49 PathGenerator(const ext::shared_ptr<StochasticProcess>&,
50 Time length,
51 Size timeSteps,
52 GSG generator,
53 bool brownianBridge);
54 PathGenerator(const ext::shared_ptr<StochasticProcess>&,
56 GSG generator,
57 bool brownianBridge);
59
60 const sample_type& next() const;
61 const sample_type& antithetic() const;
62 Size size() const { return dimension_; }
63 const TimeGrid& timeGrid() const { return timeGrid_; }
65 private:
66 const sample_type& next(bool antithetic) const;
71 ext::shared_ptr<StochasticProcess1D> process_;
73 mutable std::vector<Real> temp_;
75 };
76
77
78 // template definitions
79
80 template <class GSG>
81 PathGenerator<GSG>::PathGenerator(const ext::shared_ptr<StochasticProcess>& process,
82 Time length,
83 Size timeSteps,
84 GSG generator,
85 bool brownianBridge)
86 : brownianBridge_(brownianBridge), generator_(std::move(generator)),
87 dimension_(generator_.dimension()), timeGrid_(length, timeSteps),
88 process_(ext::dynamic_pointer_cast<StochasticProcess1D>(process)),
89 next_(Path(timeGrid_), 1.0), temp_(dimension_), bb_(timeGrid_) {
90 QL_REQUIRE(dimension_==timeSteps,
91 "sequence generator dimensionality (" << dimension_
92 << ") != timeSteps (" << timeSteps << ")");
93 }
94
95 template <class GSG>
96 PathGenerator<GSG>::PathGenerator(const ext::shared_ptr<StochasticProcess>& process,
97 TimeGrid timeGrid,
98 GSG generator,
99 bool brownianBridge)
100 : brownianBridge_(brownianBridge), generator_(std::move(generator)),
101 dimension_(generator_.dimension()), timeGrid_(std::move(timeGrid)),
102 process_(ext::dynamic_pointer_cast<StochasticProcess1D>(process)),
103 next_(Path(timeGrid_), 1.0), temp_(dimension_), bb_(timeGrid_) {
104 QL_REQUIRE(dimension_==timeGrid_.size()-1,
105 "sequence generator dimensionality (" << dimension_
106 << ") != timeSteps (" << timeGrid_.size()-1 << ")");
107 }
108
109 template <class GSG>
110 const typename PathGenerator<GSG>::sample_type&
112 return next(false);
113 }
114
115 template <class GSG>
116 const typename PathGenerator<GSG>::sample_type&
118 return next(true);
119 }
120
121 template <class GSG>
122 const typename PathGenerator<GSG>::sample_type&
123 PathGenerator<GSG>::next(bool antithetic) const {
124
125 typedef typename GSG::sample_type sequence_type;
126 const sequence_type& sequence_ =
127 antithetic ? generator_.lastSequence()
128 : generator_.nextSequence();
129
130 if (brownianBridge_) {
131 bb_.transform(sequence_.value.begin(),
132 sequence_.value.end(),
133 temp_.begin());
134 } else {
135 std::copy(sequence_.value.begin(),
136 sequence_.value.end(),
137 temp_.begin());
138 }
139
140 next_.weight = sequence_.weight;
141
142 Path& path = next_.value;
143 path.front() = process_->x0();
144
145 for (Size i=1; i<path.length(); i++) {
146 Time t = timeGrid_[i-1];
147 Time dt = timeGrid_.dt(i-1);
148 path[i] = process_->evolve(t, path[i-1], dt,
149 antithetic ? -temp_[i-1] :
150 temp_[i-1]);
151 }
152
153 return next_;
154 }
155
156}
157
158
159#endif
Builds Wiener process paths using Gaussian variates.
Generates random paths using a sequence generator.
const sample_type & next() const
ext::shared_ptr< StochasticProcess1D > process_
const sample_type & antithetic() const
const TimeGrid & timeGrid() const
Sample< Path > sample_type
std::vector< Real > temp_
PathGenerator(const ext::shared_ptr< StochasticProcess > &, Time length, Size timeSteps, GSG generator, bool brownianBridge)
single-factor random walk
Definition: path.hpp:40
Size length() const
Definition: path.hpp:94
Real value(Size i) const
Definition: path.hpp:114
Real front() const
initial asset value
Definition: path.hpp:122
1-dimensional stochastic process
time grid class
Definition: timegrid.hpp:43
Size size() const
Definition: timegrid.hpp:164
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
std::size_t Size
size of a container
Definition: types.hpp:58
Definition: any.hpp:35
STL namespace.
weighted sample
Definition: sample.hpp:35