QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
mcdigitalengine.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) 2002, 2003 Ferdinando Ametrano
5 Copyright (C) 2002, 2003 Sadruddin Rejeb
6 Copyright (C) 2003 Neil Firth
7 Copyright (C) 2007 StatPro Italia srl
8
9 This file is part of QuantLib, a free-software/open-source library
10 for financial quantitative analysts and developers - http://quantlib.org/
11
12 QuantLib is free software: you can redistribute it and/or modify it
13 under the terms of the QuantLib license. You should have received a
14 copy of the license along with this program; if not, please email
15 <quantlib-dev@lists.sf.net>. The license is also available online at
16 <http://quantlib.org/license.shtml>.
17
18 This program is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 FOR A PARTICULAR PURPOSE. See the license for more details.
21*/
22
23/*! \file mcdigitalengine.hpp
24 \brief digital option Monte Carlo engine
25*/
26
27#ifndef quantlib_digital_mc_engine_hpp
28#define quantlib_digital_mc_engine_hpp
29
30#include <ql/exercise.hpp>
36#include <utility>
37
38namespace QuantLib {
39
40 //! Pricing engine for digital options using Monte Carlo simulation
41 /*! Uses the Brownian Bridge correction for the barrier found in
42 <i>
43 Going to Extremes: Correcting Simulation Bias in Exotic
44 Option Valuation - D.R. Beaglehole, P.H. Dybvig and G. Zhou
45 Financial Analysts Journal; Jan/Feb 1997; 53, 1. pg. 62-68
46 </i>
47 and
48 <i>
49 Simulating path-dependent options: A new approach -
50 M. El Babsiri and G. Noel
51 Journal of Derivatives; Winter 1998; 6, 2; pg. 65-83
52 </i>
53
54 \ingroup vanillaengines
55
56 \test the correctness of the returned value in case of
57 cash-or-nothing at-hit digital payoff is tested by
58 reproducing known good results.
59 */
60 template<class RNG = PseudoRandom, class S = Statistics>
61 class MCDigitalEngine : public MCVanillaEngine<SingleVariate,RNG,S> {
62 public:
63 typedef
66 typedef
71 // constructor
73 const ext::shared_ptr<GeneralizedBlackScholesProcess>&,
74 Size timeSteps,
75 Size timeStepsPerYear,
76 bool brownianBridge,
77 bool antitheticVariate,
78 Size requiredSamples,
79 Real requiredTolerance,
80 Size maxSamples,
81 BigNatural seed);
82 protected:
83 // McSimulation implementation
84 ext::shared_ptr<path_pricer_type> pathPricer() const override;
85 };
86
87 //! Monte Carlo digital engine factory
88 template <class RNG = PseudoRandom, class S = Statistics>
90 public:
91 MakeMCDigitalEngine(ext::shared_ptr<GeneralizedBlackScholesProcess>);
92 // named parameters
101 // conversion to pricing engine
102 operator ext::shared_ptr<PricingEngine>() const;
103 private:
104 ext::shared_ptr<GeneralizedBlackScholesProcess> process_;
105 bool antithetic_ = false;
108 bool brownianBridge_ = false;
110 };
111
112 class DigitalPathPricer : public PathPricer<Path> {
113 public:
114 DigitalPathPricer(ext::shared_ptr<CashOrNothingPayoff> payoff,
115 ext::shared_ptr<AmericanExercise> exercise,
117 ext::shared_ptr<StochasticProcess1D> diffProcess,
118 PseudoRandom::ursg_type sequenceGen);
119 Real operator()(const Path& path) const override;
120
121 private:
122 ext::shared_ptr<CashOrNothingPayoff> payoff_;
123 ext::shared_ptr<AmericanExercise> exercise_;
124 ext::shared_ptr<StochasticProcess1D> diffProcess_;
127 };
128
129
130
131 // template definitions
132
133 template<class RNG, class S>
135 const ext::shared_ptr<GeneralizedBlackScholesProcess>& process,
136 Size timeSteps,
137 Size timeStepsPerYear,
138 bool brownianBridge,
139 bool antitheticVariate,
140 Size requiredSamples,
141 Real requiredTolerance,
142 Size maxSamples,
143 BigNatural seed)
144 : MCVanillaEngine<SingleVariate,RNG,S>(process,
145 timeSteps,
146 timeStepsPerYear,
147 brownianBridge,
148 antitheticVariate,
149 false,
150 requiredSamples,
151 requiredTolerance,
152 maxSamples,
153 seed) {}
154
155 template <class RNG, class S>
156 inline
157 ext::shared_ptr<typename MCDigitalEngine<RNG,S>::path_pricer_type>
159
160 ext::shared_ptr<CashOrNothingPayoff> payoff =
161 ext::dynamic_pointer_cast<CashOrNothingPayoff>(
162 this->arguments_.payoff);
163 QL_REQUIRE(payoff, "wrong payoff given");
164
165 ext::shared_ptr<AmericanExercise> exercise =
166 ext::dynamic_pointer_cast<AmericanExercise>(
167 this->arguments_.exercise);
168 QL_REQUIRE(exercise, "wrong exercise given");
169
170 ext::shared_ptr<GeneralizedBlackScholesProcess> process =
171 ext::dynamic_pointer_cast<GeneralizedBlackScholesProcess>(
172 this->process_);
173 QL_REQUIRE(process, "Black-Scholes process required");
174
175 TimeGrid grid = this->timeGrid();
176 PseudoRandom::ursg_type sequenceGen(grid.size()-1,
178
179 return ext::shared_ptr<
182 exercise,
183 process->riskFreeRate(),
184 process,
185 sequenceGen));
186 }
187
188
189 template <class RNG, class S>
191 ext::shared_ptr<GeneralizedBlackScholesProcess> process)
192 : process_(std::move(process)), steps_(Null<Size>()), stepsPerYear_(Null<Size>()),
193 samples_(Null<Size>()), maxSamples_(Null<Size>()), tolerance_(Null<Real>()) {}
194
195 template <class RNG, class S>
198 steps_ = steps;
199 return *this;
200 }
201
202 template <class RNG, class S>
205 stepsPerYear_ = steps;
206 return *this;
207 }
208
209 template <class RNG, class S>
212 QL_REQUIRE(tolerance_ == Null<Real>(),
213 "tolerance already set");
214 samples_ = samples;
215 return *this;
216 }
217
218 template <class RNG, class S>
221 QL_REQUIRE(samples_ == Null<Size>(),
222 "number of samples already set");
223 QL_REQUIRE(RNG::allowsErrorEstimate,
224 "chosen random generator policy "
225 "does not allow an error estimate");
226 tolerance_ = tolerance;
227 return *this;
228 }
229
230 template <class RNG, class S>
233 maxSamples_ = samples;
234 return *this;
235 }
236
237 template <class RNG, class S>
240 seed_ = seed;
241 return *this;
242 }
243
244 template <class RNG, class S>
247 brownianBridge_ = brownianBridge;
248 return *this;
249 }
250
251 template <class RNG, class S>
254 antithetic_ = b;
255 return *this;
256 }
257
258 template <class RNG, class S>
259 inline
260 MakeMCDigitalEngine<RNG,S>::operator ext::shared_ptr<PricingEngine>()
261 const {
262 QL_REQUIRE(steps_ != Null<Size>() || stepsPerYear_ != Null<Size>(),
263 "number of steps not given");
264 QL_REQUIRE(steps_ == Null<Size>() || stepsPerYear_ == Null<Size>(),
265 "number of steps overspecified");
266 return ext::shared_ptr<PricingEngine>(new
267 MCDigitalEngine<RNG,S>(process_,
268 steps_,
269 stepsPerYear_,
270 brownianBridge_,
271 antithetic_,
272 samples_, tolerance_,
273 maxSamples_,
274 seed_));
275 }
276
277}
278
279
280#endif
Black-Scholes processes.
Black volatility term structure base classes.
ext::shared_ptr< AmericanExercise > exercise_
ext::shared_ptr< CashOrNothingPayoff > payoff_
PseudoRandom::ursg_type sequenceGen_
Handle< YieldTermStructure > discountTS_
Real operator()(const Path &path) const override
ext::shared_ptr< StochasticProcess1D > diffProcess_
Shared handle to an observable.
Definition: handle.hpp:41
Pricing engine for digital options using Monte Carlo simulation.
ext::shared_ptr< path_pricer_type > pathPricer() const override
MCDigitalEngine(const ext::shared_ptr< GeneralizedBlackScholesProcess > &, Size timeSteps, Size timeStepsPerYear, bool brownianBridge, bool antitheticVariate, Size requiredSamples, Real requiredTolerance, Size maxSamples, BigNatural seed)
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 digital engine factory.
MakeMCDigitalEngine & withAntitheticVariate(bool b=true)
MakeMCDigitalEngine & withSteps(Size steps)
MakeMCDigitalEngine(ext::shared_ptr< GeneralizedBlackScholesProcess >)
MakeMCDigitalEngine & withStepsPerYear(Size steps)
MakeMCDigitalEngine & withBrownianBridge(bool b=true)
MakeMCDigitalEngine & withMaxSamples(Size samples)
MakeMCDigitalEngine & withSamples(Size samples)
MakeMCDigitalEngine & withSeed(BigNatural seed)
ext::shared_ptr< GeneralizedBlackScholesProcess > process_
MakeMCDigitalEngine & withAbsoluteTolerance(Real tolerance)
template class providing a null value for a given type.
Definition: null.hpp:76
single-factor random walk
Definition: path.hpp:40
base class for path pricers
Definition: pathpricer.hpp:40
Random sequence generator based on a pseudo-random number generator.
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
Option exercise classes and payoff function.
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
ext::shared_ptr< QuantLib::Payoff > payoff
Monte Carlo policies.
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
Interest-rate term structure.