QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
mclookbackengine.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) 2020 Lew Wei Hao
5
6 This file is part of QuantLib, a free-software/open-source library
7 for financial quantitative analysts and developers - http://quantlib.org/
8
9 QuantLib is free software: you can redistribute it and/or modify it
10 under the terms of the QuantLib license. You should have received a
11 copy of the license along with this program; if not, please email
12 <quantlib-dev@lists.sf.net>. The license is also available online at
13 <http://quantlib.org/license.shtml>.
14
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the license for more details.
18*/
19
20/*! \file mclookbackengine.hpp
21 \brief Monte Carlo lookback fixed engines
22*/
23
24#ifndef quantlib_mc_lookback_engines_hpp
25#define quantlib_mc_lookback_engines_hpp
26
27#include <ql/exercise.hpp>
31#include <utility>
32
33namespace QuantLib {
34
35 //! Monte Carlo lookback-option engine
36 template <class I, class RNG = PseudoRandom, class S = Statistics>
37 class MCLookbackEngine : public I::engine,
38 public McSimulation<SingleVariate,RNG,S> {
39 public:
44 // constructor
45 MCLookbackEngine(ext::shared_ptr<GeneralizedBlackScholesProcess> process,
46 Size timeSteps,
47 Size timeStepsPerYear,
48 bool brownianBridge,
49 bool antithetic,
50 Size requiredSamples,
51 Real requiredTolerance,
52 Size maxSamples,
53 BigNatural seed);
54 void calculate() const override {
55 Real spot = process_->x0();
56 QL_REQUIRE(spot > 0.0, "negative or null underlying given");
60 this->results_.value = this->mcModel_->sampleAccumulator().mean();
61 if (RNG::allowsErrorEstimate)
62 this->results_.errorEstimate =
63 this->mcModel_->sampleAccumulator().errorEstimate();
64 }
65
66 protected:
67 // McSimulation implementation
68 TimeGrid timeGrid() const override;
69 ext::shared_ptr<path_generator_type> pathGenerator() const override {
70 TimeGrid grid = timeGrid();
71 typename RNG::rsg_type gen =
72 RNG::make_sequence_generator(grid.size()-1,seed_);
73 return ext::shared_ptr<path_generator_type>(
75 grid, gen, brownianBridge_));
76 }
77 ext::shared_ptr<path_pricer_type> pathPricer() const override;
78 // data members
79 ext::shared_ptr<GeneralizedBlackScholesProcess> process_;
86 };
87
88
89 //! Monte Carlo lookback-option engine factory
90 template <class I, class RNG = PseudoRandom, class S = Statistics>
92 public:
93 explicit MakeMCLookbackEngine(ext::shared_ptr<GeneralizedBlackScholesProcess>);
94 // named parameters
103 // conversion to pricing engine
104 operator ext::shared_ptr<PricingEngine>() const;
105 private:
106 ext::shared_ptr<GeneralizedBlackScholesProcess> process_;
107 bool brownianBridge_ = false, antithetic_ = false;
111 };
112
113
114 // template definitions
115
116 template <class I, class RNG, class S>
118 ext::shared_ptr<GeneralizedBlackScholesProcess> process,
119 Size timeSteps,
120 Size timeStepsPerYear,
121 bool brownianBridge,
122 bool antitheticVariate,
123 Size requiredSamples,
124 Real requiredTolerance,
125 Size maxSamples,
126 BigNatural seed)
127 : McSimulation<SingleVariate, RNG, S>(antitheticVariate, false), process_(std::move(process)),
128 timeSteps_(timeSteps), timeStepsPerYear_(timeStepsPerYear), requiredSamples_(requiredSamples),
129 maxSamples_(maxSamples), requiredTolerance_(requiredTolerance),
130 brownianBridge_(brownianBridge), seed_(seed) {
131 QL_REQUIRE(timeSteps != Null<Size>() ||
132 timeStepsPerYear != Null<Size>(),
133 "no time steps provided");
134 QL_REQUIRE(timeSteps == Null<Size>() ||
135 timeStepsPerYear == Null<Size>(),
136 "both time steps and time steps per year were provided");
137 QL_REQUIRE(timeSteps != 0,
138 "timeSteps must be positive, " << timeSteps <<
139 " not allowed");
140 QL_REQUIRE(timeStepsPerYear != 0,
141 "timeStepsPerYear must be positive, " << timeStepsPerYear <<
142 " not allowed");
143 this->registerWith(process_);
144 }
145
146
147 template <class I, class RNG, class S>
149
150 Time residualTime = process_->time(this->arguments_.exercise->lastDate());
151 if (timeSteps_ != Null<Size>()) {
152 return TimeGrid(residualTime, timeSteps_);
153 } else if (timeStepsPerYear_ != Null<Size>()) {
154 Size steps = static_cast<Size>(timeStepsPerYear_*residualTime);
155 return TimeGrid(residualTime, std::max<Size>(steps, 1));
156 } else {
157 QL_FAIL("time steps not specified");
158 }
159 }
160
161
162 namespace detail {
163
164 // these functions are specialized for each of the instruments.
165
166 ext::shared_ptr<PathPricer<Path> >
169 const GeneralizedBlackScholesProcess& process,
170 DiscountFactor discount);
171
172 ext::shared_ptr<PathPricer<Path> >
175 const GeneralizedBlackScholesProcess& process,
176 DiscountFactor discount);
177
178 ext::shared_ptr<PathPricer<Path> >
181 const GeneralizedBlackScholesProcess& process,
182 DiscountFactor discount);
183
184 ext::shared_ptr<PathPricer<Path> >
187 const GeneralizedBlackScholesProcess& process,
188 DiscountFactor discount);
189
190 }
191
192
193 template <class I, class RNG, class S>
194 inline ext::shared_ptr<typename MCLookbackEngine<I,RNG,S>::path_pricer_type>
196 TimeGrid grid = this->timeGrid();
197 DiscountFactor discount = this->process_->riskFreeRate()->discount(grid.back());
198
199 return detail::mc_lookback_path_pricer(this->arguments_,
200 *(this->process_),
201 discount);
202 }
203
204
205 template <class I, class RNG, class S>
207 ext::shared_ptr<GeneralizedBlackScholesProcess> process)
208 : process_(std::move(process)), steps_(Null<Size>()), stepsPerYear_(Null<Size>()),
209 samples_(Null<Size>()), maxSamples_(Null<Size>()), tolerance_(Null<Real>()) {}
210
211 template <class I, class RNG, class S>
214 steps_ = steps;
215 return *this;
216 }
217
218 template <class I, class RNG, class S>
221 stepsPerYear_ = steps;
222 return *this;
223 }
224
225 template <class I, class RNG, class S>
228 brownianBridge_ = brownianBridge;
229 return *this;
230 }
231
232 template <class I, class RNG, class S>
235 antithetic_ = b;
236 return *this;
237 }
238
239 template <class I, class RNG, class S>
242 QL_REQUIRE(tolerance_ == Null<Real>(),
243 "tolerance already set");
244 samples_ = samples;
245 return *this;
246 }
247
248 template <class I, class RNG, class S>
251 QL_REQUIRE(samples_ == Null<Size>(),
252 "number of samples already set");
253 QL_REQUIRE(RNG::allowsErrorEstimate,
254 "chosen random generator policy "
255 "does not allow an error estimate");
256 tolerance_ = tolerance;
257 return *this;
258 }
259
260 template <class I, class RNG, class S>
263 maxSamples_ = samples;
264 return *this;
265 }
266
267 template <class I, class RNG, class S>
270 seed_ = seed;
271 return *this;
272 }
273
274 template <class I, class RNG, class S>
275 inline MakeMCLookbackEngine<I,RNG,S>::operator ext::shared_ptr<PricingEngine>() const {
276 QL_REQUIRE(steps_ != Null<Size>() || stepsPerYear_ != Null<Size>(),
277 "number of steps not given");
278 QL_REQUIRE(steps_ == Null<Size>() || stepsPerYear_ == Null<Size>(),
279 "number of steps overspecified");
280
281 return ext::shared_ptr<PricingEngine>(
282 new MCLookbackEngine<I,RNG,S>(process_,
283 steps_,
284 stepsPerYear_,
285 brownianBridge_,
286 antithetic_,
287 samples_,
288 tolerance_,
289 maxSamples_,
290 seed_));
291 }
292
293}
294
295#endif
Black-Scholes processes.
const Instrument::results * results_
Definition: cdsoption.cpp:63
Arguments for continuous fixed lookback option calculation
Arguments for continuous floating lookback option calculation
Arguments for continuous partial fixed lookback option calculation
Arguments for continuous partial floating lookback option calculation
Generalized Black-Scholes stochastic process.
Monte Carlo lookback-option engine.
McSimulation< SingleVariate, RNG, S >::path_generator_type path_generator_type
ext::shared_ptr< path_generator_type > pathGenerator() const override
void calculate() const override
ext::shared_ptr< path_pricer_type > pathPricer() const override
MCLookbackEngine(ext::shared_ptr< GeneralizedBlackScholesProcess > process, Size timeSteps, Size timeStepsPerYear, bool brownianBridge, bool antithetic, Size requiredSamples, Real requiredTolerance, Size maxSamples, BigNatural seed)
McSimulation< SingleVariate, RNG, S >::path_pricer_type path_pricer_type
ext::shared_ptr< GeneralizedBlackScholesProcess > process_
TimeGrid timeGrid() const override
Monte Carlo lookback-option engine factory.
MakeMCLookbackEngine & withSeed(BigNatural seed)
MakeMCLookbackEngine & withMaxSamples(Size samples)
MakeMCLookbackEngine & withAntitheticVariate(bool b=true)
MakeMCLookbackEngine & withStepsPerYear(Size steps)
MakeMCLookbackEngine & withSamples(Size samples)
MakeMCLookbackEngine(ext::shared_ptr< GeneralizedBlackScholesProcess >)
ext::shared_ptr< GeneralizedBlackScholesProcess > process_
MakeMCLookbackEngine & withAbsoluteTolerance(Real tolerance)
MakeMCLookbackEngine & withSteps(Size steps)
MakeMCLookbackEngine & withBrownianBridge(bool b=true)
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
template class providing a null value for a given type.
Definition: null.hpp:76
time grid class
Definition: timegrid.hpp:43
Time back() const
Definition: timegrid.hpp:171
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
Lookback option on a single asset.
framework for Monte Carlo engines
ext::shared_ptr< PathPricer< Path > > mc_lookback_path_pricer(const ContinuousFixedLookbackOption::arguments &args, const GeneralizedBlackScholesProcess &process, DiscountFactor discount)
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