QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
mcforwardeuropeanhestonengine.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 This file is part of QuantLib, a free-software/open-source library
5 for financial quantitative analysts and developers - http://quantlib.org/
6 QuantLib is free software: you can redistribute it and/or modify it
7 under the terms of the QuantLib license. You should have received a
8 copy of the license along with this program; if not, please email
9 <quantlib-dev@lists.sf.net>. The license is also available online at
10 <http://quantlib.org/license.shtml>.
11 This program is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 FOR A PARTICULAR PURPOSE. See the license for more details.
14*/
15
16/*! \file mcforwardeuropeanhestonengine.hpp
17 \brief Monte Carlo engine for forward-starting strike-reset European options using Heston-like process
18*/
19
20#ifndef quantlib_mc_forward_european_heston_engine_hpp
21#define quantlib_mc_forward_european_heston_engine_hpp
22
27#include <utility>
28
29namespace QuantLib {
30
31 /*! References:
32
33 Control Variate trade-off considerations discussed in pull request:
34 https://github.com/lballabio/QuantLib/pull/948
35
36 \ingroup forwardengines
37
38 \test
39 - Heston MC prices for a flat Heston process are
40 compared to analytical BS prices with the same
41 volatility for a range of moneynesses
42 - Heston MC prices for a forward-starting option
43 resetting at t=0 are compared to semi-analytical
44 Heston prices for a range of moneynesses
45 */
46 template <class RNG = PseudoRandom,
47 class S = Statistics, class P = HestonProcess>
49 : public MCForwardVanillaEngine<MultiVariate,RNG,S> {
50 public:
51 typedef
54 typedef
59 // constructor
61 const ext::shared_ptr<P>& process,
62 Size timeSteps,
63 Size timeStepsPerYear,
64 bool antitheticVariate,
65 Size requiredSamples,
66 Real requiredTolerance,
67 Size maxSamples,
68 BigNatural seed,
69 bool controlVariate = false);
70 protected:
71 ext::shared_ptr<path_pricer_type> pathPricer() const override;
72
73 // Use the vanilla option running from t=0 to t=expiryTime with an analytic Heston pricer
74 // as a control variate. Works well if resetTime small.
75 ext::shared_ptr<path_pricer_type> controlPathPricer() const override;
76 ext::shared_ptr<PricingEngine> controlPricingEngine() const override {
77 ext::shared_ptr<P> process = ext::dynamic_pointer_cast<P>(this->process_);
78 QL_REQUIRE(process, "Heston-like process required");
79
80 ext::shared_ptr<HestonModel> hestonModel(new HestonModel(process));
81 return ext::shared_ptr<PricingEngine>(new
82 AnalyticHestonEngine(hestonModel));
83 }
84 };
85
86
87 template <class RNG = PseudoRandom,
88 class S = Statistics, class P = HestonProcess>
90 public:
91 explicit MakeMCForwardEuropeanHestonEngine(ext::shared_ptr<P> process);
92 // named parameters
101 // conversion to pricing engine
102 operator ext::shared_ptr<PricingEngine>() const;
103 private:
104 ext::shared_ptr<P> process_;
105 bool antithetic_ = false, controlVariate_ = false;
109 };
110
111
112 class ForwardEuropeanHestonPathPricer : public PathPricer<MultiPath> {
113 public:
115 Real moneyness,
116 Size resetIndex,
117 DiscountFactor discount);
118 Real operator()(const MultiPath& multiPath) const override;
119
120 private:
125 };
126
127
128 // inline definitions
129
130 template <class RNG, class S, class P>
132 const ext::shared_ptr<P>& process,
133 Size timeSteps,
134 Size timeStepsPerYear,
135 bool antitheticVariate,
136 Size requiredSamples,
137 Real requiredTolerance,
138 Size maxSamples,
139 BigNatural seed,
140 bool controlVariate)
142 timeSteps,
143 timeStepsPerYear,
144 false,
145 antitheticVariate,
146 requiredSamples,
147 requiredTolerance,
148 maxSamples,
149 seed,
150 controlVariate) {}
151
152
153 template <class RNG, class S, class P>
154 inline ext::shared_ptr<typename MCForwardEuropeanHestonEngine<RNG,S,P>::path_pricer_type>
156
157 TimeGrid timeGrid = this->timeGrid();
158
159 Time resetTime = this->process_->time(this->arguments_.resetDate);
160 Size resetIndex = timeGrid.closestIndex(resetTime);
161
162 ext::shared_ptr<PlainVanillaPayoff> payoff =
163 ext::dynamic_pointer_cast<PlainVanillaPayoff>(
164 this->arguments_.payoff);
165 QL_REQUIRE(payoff, "non-plain payoff given");
166
167 ext::shared_ptr<EuropeanExercise> exercise =
168 ext::dynamic_pointer_cast<EuropeanExercise>(
169 this->arguments_.exercise);
170 QL_REQUIRE(exercise, "wrong exercise given");
171
172 ext::shared_ptr<P> process =
173 ext::dynamic_pointer_cast<P>(this->process_);
174 QL_REQUIRE(process, "Heston like process required");
175
176 return ext::shared_ptr<typename
179 payoff->optionType(),
180 this->arguments_.moneyness,
181 resetIndex,
182 process->riskFreeRate()->discount(
183 timeGrid.back())));
184 }
185
186 template <class RNG, class S, class P>
187 inline ext::shared_ptr<typename MCForwardEuropeanHestonEngine<RNG,S,P>::path_pricer_type>
189
190 // Control variate prices a vanilla option on the path, and compares to analytical Heston
191 // vanilla price. First entry in TimeGrid is 0, so use the existing path pricer reset at 0
192 Size resetIndex = 0;
193 TimeGrid timeGrid = this->timeGrid();
194
195 ext::shared_ptr<PlainVanillaPayoff> payoff =
196 ext::dynamic_pointer_cast<PlainVanillaPayoff>(
197 this->arguments_.payoff);
198 QL_REQUIRE(payoff, "non-plain payoff given");
199
200 ext::shared_ptr<EuropeanExercise> exercise =
201 ext::dynamic_pointer_cast<EuropeanExercise>(
202 this->arguments_.exercise);
203 QL_REQUIRE(exercise, "wrong exercise given");
204
205 ext::shared_ptr<P> process =
206 ext::dynamic_pointer_cast<P>(this->process_);
207 QL_REQUIRE(process, "Heston like process required");
208
209 return ext::shared_ptr<typename
212 payoff->optionType(),
213 this->arguments_.moneyness,
214 resetIndex,
215 process->riskFreeRate()->discount(
216 timeGrid.back())));
217 }
218
219 template <class RNG, class S, class P>
221 ext::shared_ptr<P> process)
222 : process_(std::move(process)), steps_(Null<Size>()), stepsPerYear_(Null<Size>()),
223 samples_(Null<Size>()), maxSamples_(Null<Size>()), tolerance_(Null<Real>()) {}
224
225 template <class RNG, class S, class P>
228 steps_ = steps;
229 return *this;
230 }
231
232 template <class RNG, class S, class P>
235 stepsPerYear_ = steps;
236 return *this;
237 }
238
239 template <class RNG, class S, class P>
242 QL_REQUIRE(tolerance_ == Null<Real>(),
243 "tolerance already set");
244 samples_ = samples;
245 return *this;
246 }
247
248 template <class RNG, class S, class P>
251 Real tolerance) {
252 QL_REQUIRE(samples_ == Null<Size>(),
253 "number of samples already set");
254 QL_REQUIRE(RNG::allowsErrorEstimate,
255 "chosen random generator policy "
256 "does not allow an error estimate");
257 tolerance_ = tolerance;
258 return *this;
259 }
260
261 template <class RNG, class S, class P>
264 maxSamples_ = samples;
265 return *this;
266 }
267
268 template <class RNG, class S, class P>
271 seed_ = seed;
272 return *this;
273 }
274
275 template <class RNG, class S, class P>
278 antithetic_ = b;
279 return *this;
280 }
281
282 template <class RNG, class S, class P>
285 controlVariate_ = b;
286 return *this;
287 }
288
289 template <class RNG, class S, class P>
290 inline MakeMCForwardEuropeanHestonEngine<RNG,S,P>::operator ext::shared_ptr<PricingEngine>()
291 const {
292 QL_REQUIRE(steps_ != Null<Size>() || stepsPerYear_ != Null<Size>(),
293 "number of steps not given");
294 QL_REQUIRE(steps_ == Null<Size>() || stepsPerYear_ == Null<Size>(),
295 "number of steps overspecified - set EITHER steps OR stepsPerYear");
296 return ext::shared_ptr<PricingEngine>(new
298 steps_,
299 stepsPerYear_,
300 antithetic_,
301 samples_,
302 tolerance_,
303 maxSamples_,
304 seed_,
305 controlVariate_));
306 }
307}
308
309
310#endif
analytic Heston-model engine
analytic Heston-model engine based on Fourier transform
Real operator()(const MultiPath &multiPath) const override
Heston model for the stochastic volatility of an asset.
Definition: hestonmodel.hpp:42
MCForwardEuropeanHestonEngine(const ext::shared_ptr< P > &process, Size timeSteps, Size timeStepsPerYear, bool antitheticVariate, Size requiredSamples, Real requiredTolerance, Size maxSamples, BigNatural seed, bool controlVariate=false)
ext::shared_ptr< path_pricer_type > pathPricer() const override
MCForwardVanillaEngine< MultiVariate, RNG, S >::path_pricer_type path_pricer_type
MCForwardVanillaEngine< MultiVariate, RNG, S >::path_generator_type path_generator_type
ext::shared_ptr< PricingEngine > controlPricingEngine() const override
ext::shared_ptr< path_pricer_type > controlPathPricer() const override
MCForwardVanillaEngine< MultiVariate, RNG, S >::stats_type stats_type
Monte Carlo engine for forward-starting vanilla options.
ext::shared_ptr< StochasticProcess > process_
McSimulation< MC, RNG, S >::path_generator_type path_generator_type
McSimulation< MC, RNG, S >::path_pricer_type path_pricer_type
MakeMCForwardEuropeanHestonEngine & withSeed(BigNatural seed)
MakeMCForwardEuropeanHestonEngine & withAntitheticVariate(bool b=true)
MakeMCForwardEuropeanHestonEngine & withControlVariate(bool b=false)
MakeMCForwardEuropeanHestonEngine & withMaxSamples(Size samples)
MakeMCForwardEuropeanHestonEngine & withStepsPerYear(Size steps)
MakeMCForwardEuropeanHestonEngine & withSamples(Size samples)
MakeMCForwardEuropeanHestonEngine & withSteps(Size steps)
MakeMCForwardEuropeanHestonEngine & withAbsoluteTolerance(Real tolerance)
Correlated multiple asset paths.
Definition: multipath.hpp:39
template class providing a null value for a given type.
Definition: null.hpp:76
base class for path pricers
Definition: pathpricer.hpp:40
time grid class
Definition: timegrid.hpp:43
Time back() const
Definition: timegrid.hpp:171
Size closestIndex(Time t) const
returns the index i such that grid[i] is closest to t
Definition: timegrid.cpp:80
#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
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
Heston model for the stochastic volatility of an asset.
Heston stochastic process.
ext::shared_ptr< QuantLib::Payoff > payoff
Monte Carlo engine for forward-starting strike-reset vanilla options.
Definition: any.hpp:35
RiskStatistics Statistics
default statistics tool
Definition: statistics.hpp:35
unsigned QL_BIG_INTEGER BigNatural
large positive integer
Definition: types.hpp:46
GenericPseudoRandom< MersenneTwisterUniformRng, InverseCumulativeNormal > PseudoRandom
default traits for pseudo-random number generation
Definition: rngtraits.hpp:71
STL namespace.
default Monte Carlo traits for multi-variate models
Definition: mctraits.hpp:50