QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
mceuropeanengine.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) 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
22/*! \file mceuropeanengine.hpp
23 \brief Monte Carlo European option engine
24*/
25
26#ifndef quantlib_montecarlo_european_engine_hpp
27#define quantlib_montecarlo_european_engine_hpp
28
33
34namespace QuantLib {
35
36 //! European option pricing engine using Monte Carlo simulation
37 /*! \ingroup vanillaengines
38
39 \test the correctness of the returned value is tested by
40 checking it against analytic results.
41 */
42 template <class RNG = PseudoRandom, class S = Statistics>
43 class MCEuropeanEngine : public MCVanillaEngine<SingleVariate,RNG,S> {
44 public:
45 typedef
48 typedef
53 // constructor
55 const ext::shared_ptr<GeneralizedBlackScholesProcess>& process,
56 Size timeSteps,
57 Size timeStepsPerYear,
58 bool brownianBridge,
59 bool antitheticVariate,
60 Size requiredSamples,
61 Real requiredTolerance,
62 Size maxSamples,
63 BigNatural seed);
64 protected:
65 ext::shared_ptr<path_pricer_type> pathPricer() const override;
66 };
67
68 //! Monte Carlo European engine factory
69 template <class RNG = PseudoRandom, class S = Statistics>
71 public:
72 MakeMCEuropeanEngine(ext::shared_ptr<GeneralizedBlackScholesProcess>);
73 // named parameters
82 // conversion to pricing engine
83 operator ext::shared_ptr<PricingEngine>() const;
84 private:
85 ext::shared_ptr<GeneralizedBlackScholesProcess> process_;
86 bool antithetic_ = false;
89 bool brownianBridge_ = false;
91 };
92
93 class EuropeanPathPricer : public PathPricer<Path> {
94 public:
96 Real strike,
97 DiscountFactor discount);
98 Real operator()(const Path& path) const override;
99
100 private:
103 };
104
105
106 // inline definitions
107
108 template <class RNG, class S>
109 inline
111 const ext::shared_ptr<GeneralizedBlackScholesProcess>& process,
112 Size timeSteps,
113 Size timeStepsPerYear,
114 bool brownianBridge,
115 bool antitheticVariate,
116 Size requiredSamples,
117 Real requiredTolerance,
118 Size maxSamples,
119 BigNatural seed)
120 : MCVanillaEngine<SingleVariate,RNG,S>(process,
121 timeSteps,
122 timeStepsPerYear,
123 brownianBridge,
124 antitheticVariate,
125 false,
126 requiredSamples,
127 requiredTolerance,
128 maxSamples,
129 seed) {}
130
131
132 template <class RNG, class S>
133 inline
134 ext::shared_ptr<typename MCEuropeanEngine<RNG,S>::path_pricer_type>
136
137 ext::shared_ptr<PlainVanillaPayoff> payoff =
138 ext::dynamic_pointer_cast<PlainVanillaPayoff>(
139 this->arguments_.payoff);
140 QL_REQUIRE(payoff, "non-plain payoff given");
141
142 ext::shared_ptr<GeneralizedBlackScholesProcess> process =
143 ext::dynamic_pointer_cast<GeneralizedBlackScholesProcess>(
144 this->process_);
145 QL_REQUIRE(process, "Black-Scholes process required");
146
147 return ext::shared_ptr<
150 payoff->optionType(),
151 payoff->strike(),
152 process->riskFreeRate()->discount(this->timeGrid().back())));
153 }
154
155
156 template <class RNG, class S>
158 ext::shared_ptr<GeneralizedBlackScholesProcess> process)
159 : process_(std::move(process)), steps_(Null<Size>()), stepsPerYear_(Null<Size>()),
160 samples_(Null<Size>()), maxSamples_(Null<Size>()), tolerance_(Null<Real>()) {}
161
162 template <class RNG, class S>
165 steps_ = steps;
166 return *this;
167 }
168
169 template <class RNG, class S>
172 stepsPerYear_ = steps;
173 return *this;
174 }
175
176 template <class RNG, class S>
179 QL_REQUIRE(tolerance_ == Null<Real>(),
180 "tolerance already set");
181 samples_ = samples;
182 return *this;
183 }
184
185 template <class RNG, class S>
188 QL_REQUIRE(samples_ == Null<Size>(),
189 "number of samples already set");
190 QL_REQUIRE(RNG::allowsErrorEstimate,
191 "chosen random generator policy "
192 "does not allow an error estimate");
193 tolerance_ = tolerance;
194 return *this;
195 }
196
197 template <class RNG, class S>
200 maxSamples_ = samples;
201 return *this;
202 }
203
204 template <class RNG, class S>
207 seed_ = seed;
208 return *this;
209 }
210
211 template <class RNG, class S>
214 brownianBridge_ = brownianBridge;
215 return *this;
216 }
217
218 template <class RNG, class S>
221 antithetic_ = b;
222 return *this;
223 }
224
225 template <class RNG, class S>
226 inline
227 MakeMCEuropeanEngine<RNG,S>::operator ext::shared_ptr<PricingEngine>()
228 const {
229 QL_REQUIRE(steps_ != Null<Size>() || stepsPerYear_ != Null<Size>(),
230 "number of steps not given");
231 QL_REQUIRE(steps_ == Null<Size>() || stepsPerYear_ == Null<Size>(),
232 "number of steps overspecified");
233 return ext::shared_ptr<PricingEngine>(new
235 steps_,
236 stepsPerYear_,
237 brownianBridge_,
238 antithetic_,
239 samples_, tolerance_,
240 maxSamples_,
241 seed_));
242 }
243
244
245
247 Real strike,
248 DiscountFactor discount)
249 : payoff_(type, strike), discount_(discount) {
250 QL_REQUIRE(strike>=0.0,
251 "strike less than zero not allowed");
252 }
253
254 inline Real EuropeanPathPricer::operator()(const Path& path) const {
255 QL_REQUIRE(path.length() > 0, "the path cannot be empty");
256 return payoff_(path.back()) * discount_;
257 }
258
259}
260
261
262#endif
Black constant volatility, no time dependence, no strike dependence.
Black-Scholes processes.
Black volatility curve modelled as variance curve.
EuropeanPathPricer(Option::Type type, Real strike, DiscountFactor discount)
Real operator()(const Path &path) const override
European option pricing engine using Monte Carlo simulation.
MCEuropeanEngine(const ext::shared_ptr< GeneralizedBlackScholesProcess > &process, Size timeSteps, Size timeStepsPerYear, bool brownianBridge, bool antitheticVariate, Size requiredSamples, Real requiredTolerance, Size maxSamples, BigNatural seed)
ext::shared_ptr< path_pricer_type > pathPricer() const override
MCVanillaEngine< SingleVariate, RNG, S >::stats_type stats_type
MCVanillaEngine< SingleVariate, RNG, S >::path_generator_type path_generator_type
MCVanillaEngine< SingleVariate, RNG, S >::path_pricer_type path_pricer_type
Pricing engine for vanilla options using Monte Carlo simulation.
McSimulation< MC, RNG, S >::path_generator_type path_generator_type
McSimulation< MC, RNG, S >::path_pricer_type path_pricer_type
Monte Carlo European engine factory.
MakeMCEuropeanEngine & withSeed(BigNatural seed)
MakeMCEuropeanEngine & withMaxSamples(Size samples)
MakeMCEuropeanEngine(ext::shared_ptr< GeneralizedBlackScholesProcess >)
MakeMCEuropeanEngine & withSteps(Size steps)
MakeMCEuropeanEngine & withAbsoluteTolerance(Real tolerance)
MakeMCEuropeanEngine & withAntitheticVariate(bool b=true)
MakeMCEuropeanEngine & withStepsPerYear(Size steps)
ext::shared_ptr< GeneralizedBlackScholesProcess > process_
MakeMCEuropeanEngine & withSamples(Size samples)
MakeMCEuropeanEngine & withBrownianBridge(bool b=true)
template class providing a null value for a given type.
Definition: null.hpp:76
single-factor random walk
Definition: path.hpp:40
Size length() const
Definition: path.hpp:94
Real back() const
final asset value
Definition: path.hpp:130
base class for path pricers
Definition: pathpricer.hpp:40
Plain-vanilla payoff.
Definition: payoffs.hpp:105
#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
const ext::shared_ptr< Payoff > payoff_
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
Monte Carlo vanilla option engine.
Definition: any.hpp:35
unsigned QL_BIG_INTEGER BigNatural
large positive integer
Definition: types.hpp:46
STL namespace.
default Monte Carlo traits for single-variate models
Definition: mctraits.hpp:39