QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
mceuropeanbasketengine.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) 2004 Neil Firth
5 Copyright (C) 2007, 2008 StatPro Italia srl
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
21/*! \file mceuropeanbasketengine.hpp
22 \brief European basket MC Engine
23*/
24
25#ifndef quantlib_mc_european_basket_engine_hpp
26#define quantlib_mc_european_basket_engine_hpp
27
28#include <ql/exercise.hpp>
33#include <utility>
34
35namespace QuantLib {
36
37 //! Pricing engine for European basket options using Monte Carlo simulation
38 /*! \ingroup basketengines
39
40 \test the correctness of the returned value is tested by
41 reproducing results available in literature.
42 */
43 template <class RNG = PseudoRandom, class S = Statistics>
45 public McSimulation<MultiVariate,RNG,S> {
46 public:
53 // constructor
54 MCEuropeanBasketEngine(ext::shared_ptr<StochasticProcessArray>,
55 Size timeSteps,
56 Size timeStepsPerYear,
57 bool brownianBridge,
58 bool antitheticVariate,
59 Size requiredSamples,
60 Real requiredTolerance,
61 Size maxSamples,
62 BigNatural seed);
63 void calculate() const override {
67 results_.value = this->mcModel_->sampleAccumulator().mean();
68 if (RNG::allowsErrorEstimate)
70 this->mcModel_->sampleAccumulator().errorEstimate();
71 }
72
73 protected:
74 // McSimulation implementation
75 TimeGrid timeGrid() const override;
76 ext::shared_ptr<path_generator_type> pathGenerator() const override {
77
78 ext::shared_ptr<BasketPayoff> payoff =
79 ext::dynamic_pointer_cast<BasketPayoff>(
81 QL_REQUIRE(payoff, "non-basket payoff given");
82
83 Size numAssets = processes_->size();
84
85 TimeGrid grid = timeGrid();
86 typename RNG::rsg_type gen =
87 RNG::make_sequence_generator(numAssets*(grid.size()-1),seed_);
88
89 return ext::shared_ptr<path_generator_type>(
91 grid, gen, brownianBridge_));
92 }
93 ext::shared_ptr<path_pricer_type> pathPricer() const override;
94 // data members
95 ext::shared_ptr<StochasticProcessArray> processes_;
102 };
103
104
105 //! Monte Carlo basket-option engine factory
106 template <class RNG = PseudoRandom, class S = Statistics>
108 public:
109 MakeMCEuropeanBasketEngine(ext::shared_ptr<StochasticProcessArray>);
110 // named parameters
119 // conversion to pricing engine
120 operator ext::shared_ptr<PricingEngine>() const;
121 private:
122 ext::shared_ptr<StochasticProcessArray> process_;
123 bool brownianBridge_ = false, antithetic_ = false;
127 };
128
129
130 class EuropeanMultiPathPricer : public PathPricer<MultiPath> {
131 public:
132 EuropeanMultiPathPricer(ext::shared_ptr<BasketPayoff> payoff, DiscountFactor discount);
133 Real operator()(const MultiPath& multiPath) const override;
134
135 private:
136 ext::shared_ptr<BasketPayoff> payoff_;
138 };
139
140
141 // template definitions
142
143 template <class RNG, class S>
145 ext::shared_ptr<StochasticProcessArray> processes,
146 Size timeSteps,
147 Size timeStepsPerYear,
148 bool brownianBridge,
149 bool antitheticVariate,
150 Size requiredSamples,
151 Real requiredTolerance,
152 Size maxSamples,
153 BigNatural seed)
154 : McSimulation<MultiVariate, RNG, S>(antitheticVariate, false),
155 processes_(std::move(processes)), timeSteps_(timeSteps), timeStepsPerYear_(timeStepsPerYear),
156 requiredSamples_(requiredSamples), maxSamples_(maxSamples),
157 requiredTolerance_(requiredTolerance), brownianBridge_(brownianBridge), seed_(seed) {
158 QL_REQUIRE(timeSteps != Null<Size>() ||
159 timeStepsPerYear != Null<Size>(),
160 "no time steps provided");
161 QL_REQUIRE(timeSteps == Null<Size>() ||
162 timeStepsPerYear == Null<Size>(),
163 "both time steps and time steps per year were provided");
164 QL_REQUIRE(timeSteps != 0,
165 "timeSteps must be positive, " << timeSteps <<
166 " not allowed");
167 QL_REQUIRE(timeStepsPerYear != 0,
168 "timeStepsPerYear must be positive, " << timeStepsPerYear <<
169 " not allowed");
171 }
172
173 template <class RNG, class S>
175
176 Time residualTime = processes_->time(
177 this->arguments_.exercise->lastDate());
178 if (timeSteps_ != Null<Size>()) {
179 return TimeGrid(residualTime, timeSteps_);
180 } else if (timeStepsPerYear_ != Null<Size>()) {
181 Size steps = static_cast<Size>(timeStepsPerYear_*residualTime);
182 return TimeGrid(residualTime, std::max<Size>(steps, 1));
183 } else {
184 QL_FAIL("time steps not specified");
185 }
186 }
187
188 template <class RNG, class S>
189 inline
190 ext::shared_ptr<typename MCEuropeanBasketEngine<RNG,S>::path_pricer_type>
192
193 ext::shared_ptr<BasketPayoff> payoff =
194 ext::dynamic_pointer_cast<BasketPayoff>(arguments_.payoff);
195 QL_REQUIRE(payoff, "non-basket payoff given");
196
197 ext::shared_ptr<GeneralizedBlackScholesProcess> process =
198 ext::dynamic_pointer_cast<GeneralizedBlackScholesProcess>(
199 processes_->process(0));
200 QL_REQUIRE(process, "Black-Scholes process required");
201
202 return ext::shared_ptr<
205 process->riskFreeRate()->discount(
206 arguments_.exercise->lastDate())));
207 }
208
209
210 template <class RNG, class S>
212 ext::shared_ptr<StochasticProcessArray> process)
213 : process_(std::move(process)), steps_(Null<Size>()), stepsPerYear_(Null<Size>()),
214 samples_(Null<Size>()), maxSamples_(Null<Size>()), tolerance_(Null<Real>()) {}
215
216 template <class RNG, class S>
219 steps_ = steps;
220 return *this;
221 }
222
223 template <class RNG, class S>
226 stepsPerYear_ = steps;
227 return *this;
228 }
229
230 template <class RNG, class S>
233 brownianBridge_ = brownianBridge;
234 return *this;
235 }
236
237 template <class RNG, class S>
240 antithetic_ = b;
241 return *this;
242 }
243
244 template <class RNG, class S>
247 QL_REQUIRE(tolerance_ == Null<Real>(),
248 "tolerance already set");
249 samples_ = samples;
250 return *this;
251 }
252
253 template <class RNG, class S>
256 QL_REQUIRE(samples_ == Null<Size>(),
257 "number of samples already set");
258 QL_REQUIRE(RNG::allowsErrorEstimate,
259 "chosen random generator policy "
260 "does not allow an error estimate");
261 tolerance_ = tolerance;
262 return *this;
263 }
264
265 template <class RNG, class S>
268 maxSamples_ = samples;
269 return *this;
270 }
271
272 template <class RNG, class S>
275 seed_ = seed;
276 return *this;
277 }
278
279 template <class RNG, class S>
280 inline
282 ext::shared_ptr<PricingEngine>() const {
283 QL_REQUIRE(steps_ != Null<Size>() || stepsPerYear_ != Null<Size>(),
284 "number of steps not given");
285 QL_REQUIRE(steps_ == Null<Size>() || stepsPerYear_ == Null<Size>(),
286 "number of steps overspecified");
287 return ext::shared_ptr<PricingEngine>(new
289 steps_,
290 stepsPerYear_,
291 brownianBridge_,
292 antithetic_,
293 samples_, tolerance_,
294 maxSamples_,
295 seed_));
296 }
297
298}
299
300
301#endif
Basket option on a number of assets.
Black-Scholes processes.
Basket-option engine base class
ext::shared_ptr< BasketPayoff > payoff_
Real operator()(const MultiPath &multiPath) const override
Pricing engine for European basket options using Monte Carlo simulation.
MCEuropeanBasketEngine(ext::shared_ptr< StochasticProcessArray >, Size timeSteps, Size timeStepsPerYear, bool brownianBridge, bool antitheticVariate, Size requiredSamples, Real requiredTolerance, Size maxSamples, BigNatural seed)
ext::shared_ptr< path_generator_type > pathGenerator() const override
McSimulation< MultiVariate, RNG, S >::path_pricer_type path_pricer_type
ext::shared_ptr< path_pricer_type > pathPricer() const override
McSimulation< MultiVariate, RNG, S >::stats_type stats_type
McSimulation< MultiVariate, RNG, S >::path_generator_type path_generator_type
ext::shared_ptr< StochasticProcessArray > processes_
Monte Carlo basket-option engine factory.
MakeMCEuropeanBasketEngine & withAbsoluteTolerance(Real tolerance)
MakeMCEuropeanBasketEngine & withSeed(BigNatural seed)
MakeMCEuropeanBasketEngine & withSamples(Size samples)
MakeMCEuropeanBasketEngine & withStepsPerYear(Size steps)
MakeMCEuropeanBasketEngine & withAntitheticVariate(bool b=true)
MakeMCEuropeanBasketEngine & withBrownianBridge(bool b=true)
MakeMCEuropeanBasketEngine & withMaxSamples(Size samples)
ext::shared_ptr< StochasticProcessArray > process_
MakeMCEuropeanBasketEngine & withSteps(Size steps)
MakeMCEuropeanBasketEngine(ext::shared_ptr< StochasticProcessArray >)
base class for Monte Carlo engines
MonteCarloModel< MC, RNG, S >::path_generator_type path_generator_type
ext::shared_ptr< MonteCarloModel< MC, RNG, S > > mcModel_
void calculate(Real requiredTolerance, Size requiredSamples, Size maxSamples) const
basic calculate method provided to inherited pricing engines
MonteCarloModel< MC, RNG, S >::path_pricer_type path_pricer_type
Correlated multiple asset paths.
Definition: multipath.hpp:39
template class providing a null value for a given type.
Definition: null.hpp:76
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
ext::shared_ptr< Payoff > payoff
Definition: option.hpp:64
base class for path pricers
Definition: pathpricer.hpp:40
time grid class
Definition: timegrid.hpp:43
Size size() const
Definition: timegrid.hpp:164
#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
Option exercise classes and payoff function.
ext::function< Real(Real)> b
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
Real DiscountFactor
discount factor between dates
Definition: types.hpp:66
std::size_t Size
size of a container
Definition: types.hpp:58
ext::shared_ptr< QuantLib::Payoff > payoff
framework for Monte Carlo engines
Definition: any.hpp:35
unsigned QL_BIG_INTEGER BigNatural
large positive integer
Definition: types.hpp:46
STL namespace.
Array of correlated 1-D stochastic processes.
default Monte Carlo traits for multi-variate models
Definition: mctraits.hpp:50