QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
mcamericanpathengine.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) 2009 Andrea Odetti
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#ifndef quantlib_american_path_montecarlo_engine_hpp
21#define quantlib_american_path_montecarlo_engine_hpp
22
29#include <utility>
30
31namespace QuantLib {
32
33 //! least-square Monte Carlo engine
34 /*! \warning This method is intrinsically weak for out-of-the-money
35 options.
36
37 \ingroup basketengines
38 */
39 template <class RNG = PseudoRandom>
42 PathMultiAssetOption::engine,MultiVariate,RNG> {
43 public:
44 MCAmericanPathEngine(const ext::shared_ptr<StochasticProcessArray>&,
45 Size timeSteps,
46 Size timeStepsPerYear,
47 bool brownianBridge,
48 bool antitheticVariate,
49 bool controlVariate,
50 Size requiredSamples,
51 Real requiredTolerance,
52 Size maxSamples,
53 BigNatural seed,
54 Size nCalibrationSamples = Null<Size>());
55 protected:
56 ext::shared_ptr<LongstaffSchwartzMultiPathPricer>
57 lsmPathPricer() const;
58 };
59
60
61 //! Monte Carlo American basket-option engine factory
62 template <class RNG = PseudoRandom>
64 public:
65 explicit MakeMCAmericanPathEngine(ext::shared_ptr<StochasticProcessArray>);
66 // named parameters
77 // conversion to pricing engine
78 operator ext::shared_ptr<PricingEngine>() const;
79 private:
80 ext::shared_ptr<StochasticProcessArray> process_;
85 };
86
87
88 template <class RNG> inline
90 const ext::shared_ptr<StochasticProcessArray>& processes,
91 Size timeSteps,
92 Size timeStepsPerYear,
93 bool brownianBridge,
94 bool antitheticVariate,
95 bool controlVariate,
96 Size requiredSamples,
97 Real requiredTolerance,
98 Size maxSamples,
99 BigNatural seed,
100 Size nCalibrationSamples)
102 MultiVariate,RNG>(processes,
103 timeSteps,
104 timeStepsPerYear,
105 brownianBridge,
106 antitheticVariate,
107 controlVariate,
108 requiredSamples,
109 requiredTolerance,
110 maxSamples,
111 seed,
112 nCalibrationSamples) {}
113
114 template <class RNG>
115 inline ext::shared_ptr<LongstaffSchwartzMultiPathPricer>
117
118 ext::shared_ptr<StochasticProcessArray> processArray =
119 ext::dynamic_pointer_cast<StochasticProcessArray>(this->process_);
120 QL_REQUIRE(processArray && processArray->size()>0,
121 "Stochastic process array required");
122
123 ext::shared_ptr<GeneralizedBlackScholesProcess> process =
124 ext::dynamic_pointer_cast<GeneralizedBlackScholesProcess>(
125 processArray->process(0));
126 QL_REQUIRE(process, "generalized Black-Scholes process required");
127
128 const TimeGrid theTimeGrid = this->timeGrid();
129
130 const std::vector<Time> & times = theTimeGrid.mandatoryTimes();
131 const Size numberOfTimes = times.size();
132
133 const std::vector<Date> & fixings = this->arguments_.fixingDates;
134
135 QL_REQUIRE(fixings.size() == numberOfTimes, "Invalid dates/times");
136
137 std::vector<Size> timePositions(numberOfTimes);
138 Array discountFactors(numberOfTimes);
139 std::vector<Handle<YieldTermStructure> > forwardTermStructures(numberOfTimes);
140
141 const Handle<YieldTermStructure> & riskFreeRate = process->riskFreeRate();
142
143 for (Size i = 0; i < numberOfTimes; ++i) {
144 timePositions[i] = theTimeGrid.index(times[i]);
145 discountFactors[i] = riskFreeRate->discount(times[i]);
146 forwardTermStructures[i] = Handle<YieldTermStructure>(
147 ext::make_shared<ImpliedTermStructure>(riskFreeRate,
148 fixings[i]));
149 }
150
151 const Size polynomialOrder = 2;
153
154 return ext::make_shared<LongstaffSchwartzMultiPathPricer> (
155 this->arguments_.payoff,
156 timePositions,
157 forwardTermStructures,
158 discountFactors,
159 polynomialOrder,
160 polynomialType);
161 }
162
163
164 template <class RNG>
166 ext::shared_ptr<StochasticProcessArray> process)
167 : process_(std::move(process)), brownianBridge_(false), antithetic_(false),
168 controlVariate_(false), steps_(Null<Size>()), stepsPerYear_(Null<Size>()),
169 samples_(Null<Size>()), maxSamples_(Null<Size>()), calibrationSamples_(Null<Size>()),
170 tolerance_(Null<Real>()), seed_(0) {}
171
172 template <class RNG>
175 steps_ = steps;
176 return *this;
177 }
178
179 template <class RNG>
182 stepsPerYear_ = steps;
183 return *this;
184 }
185
186 template <class RNG>
189 brownianBridge_ = brownianBridge;
190 return *this;
191 }
192
193 template <class RNG>
196 antithetic_ = b;
197 return *this;
198 }
199
200 template <class RNG>
203 controlVariate_ = b;
204 return *this;
205 }
206
207 template <class RNG>
210 QL_REQUIRE(tolerance_ == Null<Real>(),
211 "tolerance already set");
212 samples_ = samples;
213 return *this;
214 }
215
216 template <class RNG>
219 QL_REQUIRE(samples_ == Null<Size>(),
220 "number of samples already set");
221 QL_REQUIRE(RNG::allowsErrorEstimate,
222 "chosen random generator policy "
223 "does not allow an error estimate");
224 tolerance_ = tolerance;
225 return *this;
226 }
227
228 template <class RNG>
231 maxSamples_ = samples;
232 return *this;
233 }
234
235 template <class RNG>
238 seed_ = seed;
239 return *this;
240 }
241
242 template <class RNG>
245 calibrationSamples_ = samples;
246 return *this;
247 }
248
249 template <class RNG>
250 inline
252 ext::shared_ptr<PricingEngine>() const {
253 QL_REQUIRE(steps_ != Null<Size>() || stepsPerYear_ != Null<Size>(),
254 "number of steps not given");
255 QL_REQUIRE(steps_ == Null<Size>() || stepsPerYear_ == Null<Size>(),
256 "number of steps overspecified");
257 return ext::shared_ptr<PricingEngine>(new
259 steps_,
260 stepsPerYear_,
261 brownianBridge_,
262 antithetic_,
263 controlVariate_,
264 samples_,
265 tolerance_,
266 maxSamples_,
267 seed_,
268 calibrationSamples_));
269 }
270
271}
272
273#endif
Black-Scholes processes.
1-D array used in linear algebra.
Definition: array.hpp:52
Shared handle to an observable.
Definition: handle.hpp:41
least-square Monte Carlo engine
MCAmericanPathEngine(const ext::shared_ptr< StochasticProcessArray > &, Size timeSteps, Size timeStepsPerYear, bool brownianBridge, bool antitheticVariate, bool controlVariate, Size requiredSamples, Real requiredTolerance, Size maxSamples, BigNatural seed, Size nCalibrationSamples=Null< Size >())
ext::shared_ptr< LongstaffSchwartzMultiPathPricer > lsmPathPricer() const
Longstaff-Schwarz Monte Carlo engine for early exercise options.
Monte Carlo American basket-option engine factory.
MakeMCAmericanPathEngine & withSteps(Size steps)
MakeMCAmericanPathEngine & withControlVariate(bool b=true)
MakeMCAmericanPathEngine & withSeed(BigNatural seed)
MakeMCAmericanPathEngine & withBrownianBridge(bool b=true)
MakeMCAmericanPathEngine & withAntitheticVariate(bool b=true)
MakeMCAmericanPathEngine & withCalibrationSamples(Size samples)
MakeMCAmericanPathEngine & withAbsoluteTolerance(Real tolerance)
ext::shared_ptr< StochasticProcessArray > process_
MakeMCAmericanPathEngine & withMaxSamples(Size samples)
MakeMCAmericanPathEngine & withSamples(Size samples)
MakeMCAmericanPathEngine(ext::shared_ptr< StochasticProcessArray >)
MakeMCAmericanPathEngine & withStepsPerYear(Size steps)
template class providing a null value for a given type.
Definition: null.hpp:76
Base class for path-dependent options on multiple assets.
time grid class
Definition: timegrid.hpp:43
Size index(Time t) const
returns the index i such that grid[i] = t
Definition: timegrid.cpp:43
const std::vector< Time > & mandatoryTimes() const
Definition: timegrid.hpp:151
#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
Implied term structure.
Definition: any.hpp:35
unsigned QL_BIG_INTEGER BigNatural
large positive integer
Definition: types.hpp:46
STL namespace.
Option on multiple assets.
Array of correlated 1-D stochastic processes.
default Monte Carlo traits for multi-variate models
Definition: mctraits.hpp:50