QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
mcdigitalengine.hpp
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
27#ifndef quantlib_digital_mc_engine_hpp
28#define quantlib_digital_mc_engine_hpp
29
30#include <ql/exercise.hpp>
31#include <ql/methods/montecarlo/mctraits.hpp>
32#include <ql/pricingengines/vanilla/mcvanillaengine.hpp>
33#include <ql/processes/blackscholesprocess.hpp>
34#include <ql/termstructures/volatility/equityfx/blackvoltermstructure.hpp>
35#include <ql/termstructures/yieldtermstructure.hpp>
36#include <utility>
37
38namespace QuantLib {
39
41
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
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<
181 new DigitalPathPricer(payoff,
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
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
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 single-variate models
Definition: mctraits.hpp:39