QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
mcamericanbasketengine.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2004 Neil Firth
5 Copyright (C) 2006 Klaus Spanderen
6 Copyright (C) 2007, 2008 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_american_basket_montecarlo_engine_hpp
27#define quantlib_american_basket_montecarlo_engine_hpp
28
29#include <ql/exercise.hpp>
30#include <ql/functional.hpp>
31#include <ql/instruments/basketoption.hpp>
32#include <ql/methods/montecarlo/lsmbasissystem.hpp>
33#include <ql/pricingengines/mclongstaffschwartzengine.hpp>
34#include <ql/processes/blackscholesprocess.hpp>
35#include <ql/processes/stochasticprocessarray.hpp>
36#include <ql/qldefines.hpp>
37#include <utility>
38
39namespace QuantLib {
40
42
47 template <class RNG = PseudoRandom>
49 : public MCLongstaffSchwartzEngine<BasketOption::engine,
50 MultiVariate,RNG> {
51 public:
52 MCAmericanBasketEngine(const ext::shared_ptr<StochasticProcessArray>&,
53 Size timeSteps,
54 Size timeStepsPerYear,
55 bool brownianBridge,
56 bool antitheticVariate,
57 Size requiredSamples,
58 Real requiredTolerance,
59 Size maxSamples,
60 BigNatural seed,
61 Size nCalibrationSamples = Null<Size>(),
62 Size polynomialOrder = 2,
64 protected:
65 ext::shared_ptr<LongstaffSchwartzPathPricer<MultiPath> > lsmPathPricer() const override;
66
67 private:
70 };
71
72
74 template <class RNG = PseudoRandom>
76 public:
77 MakeMCAmericanBasketEngine(ext::shared_ptr<StochasticProcessArray>);
78 // named parameters
90
91 // conversion to pricing engine
92 operator ext::shared_ptr<PricingEngine>() const;
93 private:
94 ext::shared_ptr<StochasticProcessArray> process_;
95 bool brownianBridge_ = false, antithetic_ = false;
101 };
102
103
105 : public EarlyExercisePathPricer<MultiPath> {
106 public:
108 Size assetNumber,
109 ext::shared_ptr<Payoff> payoff,
110 Size polynomialOrder = 2,
112
113 Array state(const MultiPath& path, Size t) const override;
114 Real operator()(const MultiPath& path, Size t) const override;
115
116 std::vector<ext::function<Real(Array)> > basisSystem() const override;
117
118 protected:
119 Real payoff(const Array& state) const;
120
122 const ext::shared_ptr<Payoff> payoff_;
123
125 std::vector<ext::function<Real(Array)> > v_;
126 };
127
128 template <class RNG> inline
130 const ext::shared_ptr<StochasticProcessArray>& processes,
131 Size timeSteps,
132 Size timeStepsPerYear,
133 bool brownianBridge,
134 bool antitheticVariate,
135 Size requiredSamples,
136 Real requiredTolerance,
137 Size maxSamples,
138 BigNatural seed,
139 Size nCalibrationSamples,
140 Size polynomialOrder,
141 LsmBasisSystem::PolynomialType polynomialType)
143 MultiVariate,RNG>(processes,
144 timeSteps,
145 timeStepsPerYear,
146 brownianBridge,
147 antitheticVariate,
148 false,
149 requiredSamples,
150 requiredTolerance,
151 maxSamples,
152 seed,
153 nCalibrationSamples),
154 polynomialOrder_(polynomialOrder), polynomialType_(polynomialType) {}
155
156 template <class RNG>
157 inline ext::shared_ptr<LongstaffSchwartzPathPricer<MultiPath> >
159
160 ext::shared_ptr<StochasticProcessArray> processArray =
161 ext::dynamic_pointer_cast<StochasticProcessArray>(
162 this->process_);
163 QL_REQUIRE(processArray && processArray->size()>0,
164 "Stochastic process array required");
165
166 ext::shared_ptr<GeneralizedBlackScholesProcess> process =
167 ext::dynamic_pointer_cast<GeneralizedBlackScholesProcess>(
168 processArray->process(0));
169 QL_REQUIRE(process, "generalized Black-Scholes process required");
170
171 ext::shared_ptr<EarlyExercise> exercise =
172 ext::dynamic_pointer_cast<EarlyExercise>(
173 this->arguments_.exercise);
174 QL_REQUIRE(exercise, "wrong exercise given");
175 QL_REQUIRE(!exercise->payoffAtExpiry(),
176 "payoff at expiry not handled");
177
178 ext::shared_ptr<AmericanBasketPathPricer> earlyExercisePathPricer(
179 new AmericanBasketPathPricer(processArray->size(),
180 this->arguments_.payoff,
181 polynomialOrder_,
182 polynomialType_));
183
184 return ext::make_shared<LongstaffSchwartzPathPricer<MultiPath> > (
185
186 this->timeGrid(),
187 earlyExercisePathPricer,
188 *(process->riskFreeRate()));
189 }
190
191
192 template <class RNG>
194 ext::shared_ptr<StochasticProcessArray> process)
195 : process_(std::move(process)), steps_(Null<Size>()), stepsPerYear_(Null<Size>()),
196 samples_(Null<Size>()), maxSamples_(Null<Size>()), calibrationSamples_(Null<Size>()),
197 tolerance_(Null<Real>()) {}
198
199 template <class RNG>
202 steps_ = steps;
203 return *this;
204 }
205
206 template <class RNG>
209 stepsPerYear_ = steps;
210 return *this;
211 }
212
213 template <class RNG>
216 brownianBridge_ = brownianBridge;
217 return *this;
218 }
219
220 template <class RNG>
223 antithetic_ = b;
224 return *this;
225 }
226
227 template <class RNG>
230 QL_REQUIRE(tolerance_ == Null<Real>(),
231 "tolerance already set");
232 samples_ = samples;
233 return *this;
234 }
235
236 template <class RNG>
239 QL_REQUIRE(samples_ == Null<Size>(),
240 "number of samples already set");
241 QL_REQUIRE(RNG::allowsErrorEstimate,
242 "chosen random generator policy "
243 "does not allow an error estimate");
244 tolerance_ = tolerance;
245 return *this;
246 }
247
248 template <class RNG>
251 maxSamples_ = samples;
252 return *this;
253 }
254
255 template <class RNG>
258 seed_ = seed;
259 return *this;
260 }
261
262 template <class RNG>
265 calibrationSamples_ = samples;
266 return *this;
267 }
268
269 template <class RNG>
272 polynomialOrder_ = polynomialOrder;
273 return *this;
274 }
275
276 template <class RNG>
279 polynomialType_ = polynomialType;
280 return *this;
281 }
282
283 template <class RNG>
284 inline
286 ext::shared_ptr<PricingEngine>() const {
287 QL_REQUIRE(steps_ != Null<Size>() || stepsPerYear_ != Null<Size>(),
288 "number of steps not given");
289 QL_REQUIRE(steps_ == Null<Size>() || stepsPerYear_ == Null<Size>(),
290 "number of steps overspecified");
291 return ext::shared_ptr<PricingEngine>(new
293 steps_,
294 stepsPerYear_,
295 brownianBridge_,
296 antithetic_,
297 samples_,
298 tolerance_,
299 maxSamples_,
300 seed_,
301 calibrationSamples_,
302 polynomialOrder_,
303 polynomialType_));
304 }
305
306}
307
308#endif
Real payoff(const Array &state) const
Real operator()(const MultiPath &path, Size t) const override
Array state(const MultiPath &path, Size t) const override
std::vector< ext::function< Real(Array)> > basisSystem() const override
std::vector< ext::function< Real(Array)> > v_
const ext::shared_ptr< Payoff > payoff_
1-D array used in linear algebra.
Definition: array.hpp:52
Basket option on a number of assets.
base class for early exercise path pricers
least-square Monte Carlo engine
const LsmBasisSystem::PolynomialType polynomialType_
ext::shared_ptr< LongstaffSchwartzPathPricer< MultiPath > > lsmPathPricer() const override
MCAmericanBasketEngine(const ext::shared_ptr< StochasticProcessArray > &, Size timeSteps, Size timeStepsPerYear, bool brownianBridge, bool antitheticVariate, Size requiredSamples, Real requiredTolerance, Size maxSamples, BigNatural seed, Size nCalibrationSamples=Null< Size >(), Size polynomialOrder=2, LsmBasisSystem::PolynomialType polynomialType=LsmBasisSystem::Monomial)
Longstaff-Schwarz Monte Carlo engine for early exercise options.
Monte Carlo American basket-option engine factory.
MakeMCAmericanBasketEngine & withSamples(Size samples)
MakeMCAmericanBasketEngine & withCalibrationSamples(Size samples)
MakeMCAmericanBasketEngine & withBrownianBridge(bool b=true)
MakeMCAmericanBasketEngine & withBasisSystem(LsmBasisSystem::PolynomialType polynomialType)
LsmBasisSystem::PolynomialType polynomialType_
MakeMCAmericanBasketEngine & withMaxSamples(Size samples)
MakeMCAmericanBasketEngine & withAntitheticVariate(bool b=true)
MakeMCAmericanBasketEngine(ext::shared_ptr< StochasticProcessArray >)
MakeMCAmericanBasketEngine & withPolynomialOrder(Size polynmOrder)
MakeMCAmericanBasketEngine & withSeed(BigNatural seed)
ext::shared_ptr< StochasticProcessArray > process_
MakeMCAmericanBasketEngine & withStepsPerYear(Size steps)
MakeMCAmericanBasketEngine & withAbsoluteTolerance(Real tolerance)
MakeMCAmericanBasketEngine & withSteps(Size steps)
Correlated multiple asset paths.
Definition: multipath.hpp:39
template class providing a null value for a given type.
Definition: null.hpp:76
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
unsigned QL_BIG_INTEGER BigNatural
large positive integer
Definition: types.hpp:46
STL namespace.
default Monte Carlo traits for multi-variate models
Definition: mctraits.hpp:50