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
21/*! \file mceuropeanhestonengine.hpp
22 \brief Monte Carlo Heston-model engine for European options
23*/
24
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
34 //! Monte Carlo Heston-model engine for European options
35 /*! \ingroup vanillaengines
36
37 \test the correctness of the returned value is tested by
38 reproducing results available in web/literature
39 */
40 template <class RNG = PseudoRandom,
41 class S = Statistics, class P = HestonProcess>
42 class MCEuropeanHestonEngine
43 : public MCVanillaEngine<MultiVariate,RNG,S> {
44 public:
45 typedef typename MCVanillaEngine<MultiVariate,RNG,S>::path_pricer_type
46 path_pricer_type;
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
59 //! Monte Carlo Heston European engine factory
60 template <class RNG = PseudoRandom,
61 class S = Statistics, class P = HestonProcess>
62 class MakeMCEuropeanHestonEngine {
63 public:
64 explicit MakeMCEuropeanHestonEngine(ext::shared_ptr<P>);
65 // named parameters
66 MakeMCEuropeanHestonEngine& withSteps(Size steps);
67 MakeMCEuropeanHestonEngine& withStepsPerYear(Size steps);
68 MakeMCEuropeanHestonEngine& withSamples(Size samples);
69 MakeMCEuropeanHestonEngine& withAbsoluteTolerance(Real tolerance);
70 MakeMCEuropeanHestonEngine& withMaxSamples(Size samples);
71 MakeMCEuropeanHestonEngine& withSeed(BigNatural seed);
72 MakeMCEuropeanHestonEngine& withAntitheticVariate(bool b = true);
73 // conversion to pricing engine
74 operator ext::shared_ptr<PricingEngine>() const;
75 private:
76 ext::shared_ptr<P> process_;
77 bool antithetic_ = false;
78 Size steps_, stepsPerYear_, samples_, maxSamples_;
79 Real tolerance_;
80 BigNatural seed_ = 0;
81 };
82
83
84 class EuropeanHestonPathPricer : public PathPricer<MultiPath> {
85 public:
86 EuropeanHestonPathPricer(Option::Type type,
87 Real strike,
88 DiscountFactor discount);
89 Real operator()(const MultiPath& Multipath) const override;
90
91 private:
92 PlainVanillaPayoff payoff_;
93 DiscountFactor discount_;
94 };
95
96
97 // template definitions
98
99 template <class RNG, class S, class P>
100 MCEuropeanHestonEngine<RNG, S, P>::MCEuropeanHestonEngine(
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<
113 typename MCEuropeanHestonEngine<RNG,S,P>::path_pricer_type>
114 MCEuropeanHestonEngine<RNG,S,P>::pathPricer() const {
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<
126 typename MCEuropeanHestonEngine<RNG,S,P>::path_pricer_type>(
127 new EuropeanHestonPathPricer(
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>
136 inline MakeMCEuropeanHestonEngine<RNG, S, P>::MakeMCEuropeanHestonEngine(
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>
142 inline MakeMCEuropeanHestonEngine<RNG,S,P>&
143 MakeMCEuropeanHestonEngine<RNG,S,P>::withSteps(Size steps) {
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>
151 inline MakeMCEuropeanHestonEngine<RNG,S,P>&
152 MakeMCEuropeanHestonEngine<RNG,S,P>::withStepsPerYear(Size steps) {
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>
160 inline MakeMCEuropeanHestonEngine<RNG,S,P>&
161 MakeMCEuropeanHestonEngine<RNG,S,P>::withSamples(Size samples) {
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>
169 inline MakeMCEuropeanHestonEngine<RNG,S,P>&
170 MakeMCEuropeanHestonEngine<RNG,S,P>::withAbsoluteTolerance(Real tolerance) {
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>
181 inline MakeMCEuropeanHestonEngine<RNG,S,P>&
182 MakeMCEuropeanHestonEngine<RNG,S,P>::withMaxSamples(Size samples) {
183 maxSamples_ = samples;
184 return *this;
185 }
186
187 template <class RNG, class S, class P>
188 inline MakeMCEuropeanHestonEngine<RNG,S,P>&
189 MakeMCEuropeanHestonEngine<RNG,S,P>::withSeed(BigNatural seed) {
190 seed_ = seed;
191 return *this;
192 }
193
194 template <class RNG, class S, class P>
195 inline MakeMCEuropeanHestonEngine<RNG,S,P>&
196 MakeMCEuropeanHestonEngine<RNG,S,P>::withAntitheticVariate(bool b) {
197 antithetic_ = b;
198 return *this;
199 }
200
201 template <class RNG, class S, class P>
202 inline
203 MakeMCEuropeanHestonEngine<RNG,S,P>::
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>(
208 new MCEuropeanHestonEngine<RNG,S,P>(process_,
209 steps_,
210 stepsPerYear_,
211 antithetic_,
212 samples_, tolerance_,
213 maxSamples_,
214 seed_));
215 }
216
217
218
219 inline EuropeanHestonPathPricer::EuropeanHestonPathPricer(
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
228 inline Real EuropeanHestonPathPricer::operator()(
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
241

source code of quantlib/ql/pricingengines/vanilla/mceuropeanhestonengine.hpp