QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
mceuropeanhestonengine.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2005 Klaus Spanderen
5 Copyright (C) 2007 StatPro Italia srl
6
7 This file is part of QuantLib, a free-software/open-source library
8 for financial quantitative analysts and developers - http://quantlib.org/
9
10 QuantLib is free software: you can redistribute it and/or modify it
11 under the terms of the QuantLib license. You should have received a
12 copy of the license along with this program; if not, please email
13 <quantlib-dev@lists.sf.net>. The license is also available online at
14 <http://quantlib.org/license.shtml>.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the license for more details.
19*/
20
25#ifndef quantlib_mc_european_heston_engine_hpp
26#define quantlib_mc_european_heston_engine_hpp
27
28#include <ql/pricingengines/vanilla/mcvanillaengine.hpp>
29#include <ql/processes/hestonprocess.hpp>
30#include <utility>
31
32namespace QuantLib {
33
35
40 template <class RNG = PseudoRandom,
41 class S = Statistics, class P = HestonProcess>
43 : public MCVanillaEngine<MultiVariate,RNG,S> {
44 public:
47 MCEuropeanHestonEngine(const ext::shared_ptr<P>&,
48 Size timeSteps,
49 Size timeStepsPerYear,
50 bool antitheticVariate,
51 Size requiredSamples,
52 Real requiredTolerance,
53 Size maxSamples,
54 BigNatural seed);
55 protected:
56 ext::shared_ptr<path_pricer_type> pathPricer() const override;
57 };
58
60 template <class RNG = PseudoRandom,
61 class S = Statistics, class P = HestonProcess>
63 public:
64 explicit MakeMCEuropeanHestonEngine(ext::shared_ptr<P>);
65 // named parameters
73 // conversion to pricing engine
74 operator ext::shared_ptr<PricingEngine>() const;
75 private:
76 ext::shared_ptr<P> process_;
77 bool antithetic_ = false;
81 };
82
83
84 class EuropeanHestonPathPricer : public PathPricer<MultiPath> {
85 public:
87 Real strike,
88 DiscountFactor discount);
89 Real operator()(const MultiPath& Multipath) const override;
90
91 private:
94 };
95
96
97 // template definitions
98
99 template <class RNG, class S, class P>
101 const ext::shared_ptr<P>& process,
102 Size timeSteps, Size timeStepsPerYear, bool antitheticVariate,
103 Size requiredSamples, Real requiredTolerance,
104 Size maxSamples, BigNatural seed)
105 : MCVanillaEngine<MultiVariate,RNG,S>(process, timeSteps, timeStepsPerYear,
106 false, antitheticVariate, false,
107 requiredSamples, requiredTolerance,
108 maxSamples, seed) {}
109
110
111 template <class RNG, class S, class P>
112 ext::shared_ptr<
115
116 ext::shared_ptr<PlainVanillaPayoff> payoff(
117 ext::dynamic_pointer_cast<PlainVanillaPayoff>(
118 this->arguments_.payoff));
119 QL_REQUIRE(payoff, "non-plain payoff given");
120
121 ext::shared_ptr<P> process =
122 ext::dynamic_pointer_cast<P>(this->process_);
123 QL_REQUIRE(process, "Heston like process required");
124
125 return ext::shared_ptr<
128 payoff->optionType(),
129 payoff->strike(),
130 process->riskFreeRate()->discount(
131 this->timeGrid().back())));
132 }
133
134
135 template <class RNG, class S, class P>
137 ext::shared_ptr<P> process)
138 : process_(std::move(process)), steps_(Null<Size>()), stepsPerYear_(Null<Size>()),
139 samples_(Null<Size>()), maxSamples_(Null<Size>()), tolerance_(Null<Real>()) {}
140
141 template <class RNG, class S,class P>
144 QL_REQUIRE(stepsPerYear_ == Null<Size>(),
145 "number of steps per year already set");
146 steps_ = steps;
147 return *this;
148 }
149
150 template <class RNG, class S, class P>
153 QL_REQUIRE(steps_ == Null<Size>(),
154 "number of steps already set");
155 stepsPerYear_ = steps;
156 return *this;
157 }
158
159 template <class RNG, class S,class P>
162 QL_REQUIRE(tolerance_ == Null<Real>(),
163 "tolerance already set");
164 samples_ = samples;
165 return *this;
166 }
167
168 template <class RNG, class S, class P>
171 QL_REQUIRE(samples_ == Null<Size>(),
172 "number of samples already set");
173 QL_REQUIRE(RNG::allowsErrorEstimate,
174 "chosen random generator policy "
175 "does not allow an error estimate");
176 tolerance_ = tolerance;
177 return *this;
178 }
179
180 template <class RNG, class S, class P>
183 maxSamples_ = samples;
184 return *this;
185 }
186
187 template <class RNG, class S, class P>
190 seed_ = seed;
191 return *this;
192 }
193
194 template <class RNG, class S, class P>
197 antithetic_ = b;
198 return *this;
199 }
200
201 template <class RNG, class S, class P>
202 inline
204 operator ext::shared_ptr<PricingEngine>() const {
205 QL_REQUIRE(steps_ != Null<Size>() || stepsPerYear_ != Null<Size>(),
206 "number of steps not given");
207 return ext::shared_ptr<PricingEngine>(
209 steps_,
210 stepsPerYear_,
211 antithetic_,
212 samples_, tolerance_,
213 maxSamples_,
214 seed_));
215 }
216
217
218
220 Option::Type type,
221 Real strike,
222 DiscountFactor discount)
223 : payoff_(type, strike), discount_(discount) {
224 QL_REQUIRE(strike>=0.0,
225 "strike less than zero not allowed");
226 }
227
229 const MultiPath& multiPath) const {
230 const Path& path = multiPath[0];
231 const Size n = multiPath.pathSize();
232 QL_REQUIRE(n>0, "the path cannot be empty");
233
234 return payoff_(path.back()) * discount_;
235 }
236
237}
238
239
240#endif
Real operator()(const MultiPath &Multipath) const override
EuropeanHestonPathPricer(Option::Type type, Real strike, DiscountFactor discount)
Square-root stochastic-volatility Heston process.
Monte Carlo Heston-model engine for European options.
MCVanillaEngine< MultiVariate, RNG, S >::path_pricer_type path_pricer_type
MCEuropeanHestonEngine(const ext::shared_ptr< P > &, Size timeSteps, Size timeStepsPerYear, bool antitheticVariate, Size requiredSamples, Real requiredTolerance, Size maxSamples, BigNatural seed)
ext::shared_ptr< path_pricer_type > pathPricer() const override
Pricing engine for vanilla options using Monte Carlo simulation.
McSimulation< MC, RNG, S >::path_pricer_type path_pricer_type
Monte Carlo Heston European engine factory.
MakeMCEuropeanHestonEngine & withSteps(Size steps)
MakeMCEuropeanHestonEngine & withAntitheticVariate(bool b=true)
MakeMCEuropeanHestonEngine & withMaxSamples(Size samples)
MakeMCEuropeanHestonEngine & withStepsPerYear(Size steps)
MakeMCEuropeanHestonEngine & withSamples(Size samples)
MakeMCEuropeanHestonEngine & withAbsoluteTolerance(Real tolerance)
MakeMCEuropeanHestonEngine & withSeed(BigNatural seed)
Correlated multiple asset paths.
Definition: multipath.hpp:39
Size pathSize() const
Definition: multipath.hpp:48
template class providing a null value for a given type.
Definition: null.hpp:76
single-factor random walk
Definition: path.hpp:40
Real back() const
final asset value
Definition: path.hpp:130
base class for path pricers
Definition: pathpricer.hpp:40
Plain-vanilla payoff.
Definition: payoffs.hpp:105
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
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