QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
mchestonhullwhiteengine.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2007, 2008 Klaus Spanderen
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
24#ifndef quantlib_mc_heston_hull_white_engine_hpp
25#define quantlib_mc_heston_hull_white_engine_hpp
26
27#include <ql/pricingengines/vanilla/analytichestonhullwhiteengine.hpp>
28#include <ql/pricingengines/vanilla/mcvanillaengine.hpp>
29#include <ql/processes/hestonprocess.hpp>
30#include <ql/processes/hullwhiteprocess.hpp>
31#include <ql/processes/hybridhestonhullwhiteprocess.hpp>
32#include <utility>
33
34namespace QuantLib {
35
36 template <class RNG = PseudoRandom, class S = Statistics>
38 : public MCVanillaEngine<MultiVariate, RNG, S> {
39 public:
45
47 const ext::shared_ptr<HybridHestonHullWhiteProcess>& process,
48 Size timeSteps,
49 Size timeStepsPerYear,
50 bool antitheticVariate,
51 bool controlVariate,
52 Size requiredSamples,
53 Real requiredTolerance,
54 Size maxSamples,
55 BigNatural seed);
56
57 void calculate() const override;
58
59 protected:
60 // just to avoid upcasting
61 ext::shared_ptr<HybridHestonHullWhiteProcess> process_;
62
63 ext::shared_ptr<path_pricer_type> pathPricer() const override;
64
65 ext::shared_ptr<path_pricer_type> controlPathPricer() const override;
66 ext::shared_ptr<PricingEngine> controlPricingEngine() const override;
67 ext::shared_ptr<path_generator_type> controlPathGenerator() const override;
68 };
69
71 template <class RNG = PseudoRandom, class S = Statistics>
73 public:
74 explicit MakeMCHestonHullWhiteEngine(ext::shared_ptr<HybridHestonHullWhiteProcess>);
75 // named parameters
84 // conversion to pricing engine
85 operator ext::shared_ptr<PricingEngine>() const;
86 private:
87 ext::shared_ptr<HybridHestonHullWhiteProcess> process_;
89 bool antithetic_ = false, controlVariate_ = false;
92 };
93
94
95 class HestonHullWhitePathPricer : public PathPricer<MultiPath> {
96 public:
98 ext::shared_ptr<Payoff> payoff,
99 ext::shared_ptr<HybridHestonHullWhiteProcess> process);
100
101 Real operator()(const MultiPath& path) const override;
102
103 private:
105 ext::shared_ptr<Payoff> payoff_;
106 ext::shared_ptr<HybridHestonHullWhiteProcess> process_;
107 };
108
109
110 template<class RNG,class S>
112 const ext::shared_ptr<HybridHestonHullWhiteProcess> & process,
113 Size timeSteps,
114 Size timeStepsPerYear,
115 bool antitheticVariate,
116 bool controlVariate,
117 Size requiredSamples,
118 Real requiredTolerance,
119 Size maxSamples,
120 BigNatural seed)
121 : base_type(process, timeSteps, timeStepsPerYear,
122 false, antitheticVariate,
123 controlVariate, requiredSamples,
124 requiredTolerance, maxSamples, seed),
125 process_(process) {}
126
127 template<class RNG,class S>
130
131 if (this->controlVariate_) {
132 // control variate might lead to small negative
133 // option values for deep OTM options
134 this->results_.value = std::max(0.0, this->results_.value);
135 }
136 }
137
138 template <class RNG,class S> inline
139 ext::shared_ptr<typename MCHestonHullWhiteEngine<RNG,S>::path_pricer_type>
141
142 ext::shared_ptr<Exercise> exercise = this->arguments_.exercise;
143
144 QL_REQUIRE(exercise->type() == Exercise::European,
145 "only european exercise is supported");
146
147 const Time exerciseTime = process_->time(exercise->lastDate());
148
149 return ext::shared_ptr<path_pricer_type>(
150 new HestonHullWhitePathPricer(exerciseTime,
151 this->arguments_.payoff,
152 process_));
153 }
154
155 template <class RNG, class S> inline
156 ext::shared_ptr<
159
160 ext::shared_ptr<HestonProcess> hestonProcess =
161 process_->hestonProcess();
162
163 QL_REQUIRE(hestonProcess, "first constituent of the joint stochastic "
164 "process need to be of type HestonProcess");
165
166 ext::shared_ptr<Exercise> exercise = this->arguments_.exercise;
167
168 QL_REQUIRE(exercise->type() == Exercise::European,
169 "only european exercise is supported");
170
171 const Time exerciseTime = process_->time(exercise->lastDate());
172
173 return ext::shared_ptr<path_pricer_type>(
175 exerciseTime,
176 this->arguments_.payoff,
177 process_) );
178 }
179
180 template <class RNG, class S> inline
181 ext::shared_ptr<PricingEngine>
183
184 ext::shared_ptr<HestonProcess> hestonProcess =
185 process_->hestonProcess();
186
187 ext::shared_ptr<HullWhiteForwardProcess> hullWhiteProcess =
188 process_->hullWhiteProcess();
189
190 ext::shared_ptr<HestonModel> hestonModel(
191 new HestonModel(hestonProcess));
192 ext::shared_ptr<HullWhite> hwModel(
193 new HullWhite(hestonProcess->riskFreeRate(),
194 hullWhiteProcess->a(),
195 hullWhiteProcess->sigma()));
196
197 return ext::shared_ptr<PricingEngine>(
198 new AnalyticHestonHullWhiteEngine(hestonModel, hwModel, 144));
199 }
200
201 template <class RNG, class S> inline
202 ext::shared_ptr<
205
206 Size dimensions = process_->factors();
207 TimeGrid grid = this->timeGrid();
208 typename RNG::rsg_type generator =
209 RNG::make_sequence_generator(dimensions*(grid.size()-1),
210 this->seed_);
211
212 ext::shared_ptr<HybridHestonHullWhiteProcess> cvProcess(
213 new HybridHestonHullWhiteProcess(process_->hestonProcess(),
214 process_->hullWhiteProcess(),
215 0.0,
216 process_->discretization()));
217
218 return ext::shared_ptr<path_generator_type>(
219 new path_generator_type(cvProcess, grid, generator, false));
220 }
221
222
223 template <class RNG, class S>
225 ext::shared_ptr<HybridHestonHullWhiteProcess> process)
226 : process_(std::move(process)), steps_(Null<Size>()), stepsPerYear_(Null<Size>()),
227 samples_(Null<Size>()), maxSamples_(Null<Size>()), tolerance_(Null<Real>()) {}
228
229 template <class RNG, class S>
232 steps_ = steps;
233 return *this;
234 }
235
236 template <class RNG, class S>
239 stepsPerYear_ = steps;
240 return *this;
241 }
242
243 template <class RNG, class S>
246 antithetic_ = b;
247 return *this;
248 }
249
250 template <class RNG, class S>
253 controlVariate_ = b;
254 return *this;
255 }
256
257 template <class RNG, class S>
260 QL_REQUIRE(tolerance_ == Null<Real>(),
261 "tolerance already set");
262 samples_ = samples;
263 return *this;
264 }
265
266 template <class RNG, class S>
269 QL_REQUIRE(samples_ == Null<Size>(),
270 "number of samples already set");
271 QL_REQUIRE(RNG::allowsErrorEstimate,
272 "chosen random generator policy "
273 "does not allow an error estimate");
274 tolerance_ = tolerance;
275 return *this;
276 }
277
278 template <class RNG, class S>
281 maxSamples_ = samples;
282 return *this;
283 }
284
285 template <class RNG, class S>
288 seed_ = seed;
289 return *this;
290 }
291
292 template <class RNG, class S>
293 inline
295 ext::shared_ptr<PricingEngine>() const {
296 QL_REQUIRE(steps_ != Null<Size>() || stepsPerYear_ != Null<Size>(),
297 "number of steps not given");
298 QL_REQUIRE(steps_ == Null<Size>() || stepsPerYear_ == Null<Size>(),
299 "number of steps overspecified");
300 return ext::shared_ptr<PricingEngine>(new
302 steps_,
303 stepsPerYear_,
304 antithetic_,
305 controlVariate_,
306 samples_,
307 tolerance_,
308 maxSamples_,
309 seed_));
310 }
311
312}
313
314#endif
Analytic Heston engine incl. stochastic interest rates.
Real operator()(const MultiPath &path) const override
ext::shared_ptr< HybridHestonHullWhiteProcess > process_
Heston model for the stochastic volatility of an asset.
Definition: hestonmodel.hpp:42
Single-factor Hull-White (extended Vasicek) model class.
Definition: hullwhite.hpp:49
Hybrid Heston Hull-White stochastic process.
MCVanillaEngine< MultiVariate, RNG, S > base_type
base_type::path_generator_type path_generator_type
MCHestonHullWhiteEngine(const ext::shared_ptr< HybridHestonHullWhiteProcess > &process, Size timeSteps, Size timeStepsPerYear, bool antitheticVariate, bool controlVariate, Size requiredSamples, Real requiredTolerance, Size maxSamples, BigNatural seed)
ext::shared_ptr< PricingEngine > controlPricingEngine() const override
base_type::path_pricer_type path_pricer_type
ext::shared_ptr< path_pricer_type > controlPathPricer() const override
ext::shared_ptr< HybridHestonHullWhiteProcess > process_
ext::shared_ptr< path_pricer_type > pathPricer() const override
ext::shared_ptr< path_generator_type > controlPathGenerator() const override
Pricing engine for vanilla options using Monte Carlo simulation.
McSimulation< MC, RNG, S >::result_type result_type
McSimulation< MC, RNG, S >::path_generator_type path_generator_type
void calculate() const override
McSimulation< MC, RNG, S >::path_pricer_type path_pricer_type
Monte Carlo Heston/Hull-White engine factory.
MakeMCHestonHullWhiteEngine & withAbsoluteTolerance(Real tolerance)
MakeMCHestonHullWhiteEngine & withControlVariate(bool b=true)
MakeMCHestonHullWhiteEngine(ext::shared_ptr< HybridHestonHullWhiteProcess >)
MakeMCHestonHullWhiteEngine & withMaxSamples(Size samples)
MakeMCHestonHullWhiteEngine & withStepsPerYear(Size steps)
MakeMCHestonHullWhiteEngine & withSeed(BigNatural seed)
MakeMCHestonHullWhiteEngine & withSteps(Size steps)
MakeMCHestonHullWhiteEngine & withAntitheticVariate(bool b=true)
MakeMCHestonHullWhiteEngine & withSamples(Size samples)
ext::shared_ptr< HybridHestonHullWhiteProcess > process_
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
Size size() const
Definition: timegrid.hpp:164
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
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.