QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
mceuropeangjrgarchengine.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) 2008 Yee Man Chan
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 mceuropeangjrgarchengine.hpp
21 \brief Monte Carlo GJR-GARCH-model engine for European options
22*/
23
24#ifndef quantlib_mc_european_gjrgarch_engine_hpp
25#define quantlib_mc_european_gjrgarch_engine_hpp
26
29#include <utility>
30
31namespace QuantLib {
32
33 //! Monte Carlo GJR-GARCH-model engine for European options
34 /*! \ingroup vanillaengines
35
36 \test the correctness of the returned value is tested by
37 reproducing results available in web/literature
38 */
39 template <class RNG = PseudoRandom, class S = Statistics>
41 : public MCVanillaEngine<MultiVariate,RNG,S> {
42 public:
45 MCEuropeanGJRGARCHEngine(const ext::shared_ptr<GJRGARCHProcess>&,
46 Size timeSteps,
47 Size timeStepsPerYear,
48 bool antitheticVariate,
49 Size requiredSamples,
50 Real requiredTolerance,
51 Size maxSamples,
52 BigNatural seed);
53 protected:
54 ext::shared_ptr<path_pricer_type> pathPricer() const override;
55 };
56
57 //! Monte Carlo GJR-GARCH European engine factory
58 template <class RNG = PseudoRandom, class S = Statistics>
60 public:
61 MakeMCEuropeanGJRGARCHEngine(ext::shared_ptr<GJRGARCHProcess>);
62 // named parameters
70 // conversion to pricing engine
71 operator ext::shared_ptr<PricingEngine>() const;
72 private:
73 ext::shared_ptr<GJRGARCHProcess> process_;
74 bool antithetic_ = false;
78 };
79
80
81 class EuropeanGJRGARCHPathPricer : public PathPricer<MultiPath> {
82 public:
84 Real strike,
85 DiscountFactor discount);
86 Real operator()(const MultiPath& Multipath) const override;
87
88 private:
91 };
92
93
94 // template definitions
95
96 template <class RNG, class S>
98 const ext::shared_ptr<GJRGARCHProcess>& process,
99 Size timeSteps, Size timeStepsPerYear, bool antitheticVariate,
100 Size requiredSamples, Real requiredTolerance,
101 Size maxSamples, BigNatural seed)
102 : MCVanillaEngine<MultiVariate,RNG,S>(process, timeSteps, timeStepsPerYear,
103 false, antitheticVariate, false,
104 requiredSamples, requiredTolerance,
105 maxSamples, seed) {}
106
107
108 template <class RNG, class S>
109 ext::shared_ptr<
112
113 ext::shared_ptr<PlainVanillaPayoff> payoff(
114 ext::dynamic_pointer_cast<PlainVanillaPayoff>(
115 this->arguments_.payoff));
116 QL_REQUIRE(payoff, "non-plain payoff given");
117
118 ext::shared_ptr<GJRGARCHProcess> process =
119 ext::dynamic_pointer_cast<GJRGARCHProcess>(this->process_);
120 QL_REQUIRE(process, "GJRGARCH process required");
121
122 return ext::shared_ptr<
125 payoff->optionType(),
126 payoff->strike(),
127 process->riskFreeRate()->discount(
128 this->timeGrid().back())));
129 }
130
131
132 template <class RNG, class S>
134 ext::shared_ptr<GJRGARCHProcess> process)
135 : process_(std::move(process)), steps_(Null<Size>()), stepsPerYear_(Null<Size>()),
136 samples_(Null<Size>()), maxSamples_(Null<Size>()), tolerance_(Null<Real>()) {}
137
138 template <class RNG, class S>
141 QL_REQUIRE(stepsPerYear_ == Null<Size>(),
142 "number of steps per year already set");
143 steps_ = steps;
144 return *this;
145 }
146
147 template <class RNG, class S>
150 QL_REQUIRE(steps_ == Null<Size>(),
151 "number of steps already set");
152 stepsPerYear_ = steps;
153 return *this;
154 }
155
156 template <class RNG, class S>
159 QL_REQUIRE(tolerance_ == Null<Real>(),
160 "tolerance already set");
161 samples_ = samples;
162 return *this;
163 }
164
165 template <class RNG, class S>
168 QL_REQUIRE(samples_ == Null<Size>(),
169 "number of samples already set");
170 QL_REQUIRE(RNG::allowsErrorEstimate,
171 "chosen random generator policy "
172 "does not allow an error estimate");
173 tolerance_ = tolerance;
174 return *this;
175 }
176
177 template <class RNG, class S>
180 maxSamples_ = samples;
181 return *this;
182 }
183
184 template <class RNG, class S>
187 seed_ = seed;
188 return *this;
189 }
190
191 template <class RNG, class S>
194 antithetic_ = b;
195 return *this;
196 }
197
198 template <class RNG, class S>
199 inline
201 operator ext::shared_ptr<PricingEngine>() const {
202 QL_REQUIRE(steps_ != Null<Size>() || stepsPerYear_ != Null<Size>(),
203 "number of steps not given");
204 return ext::shared_ptr<PricingEngine>(
206 steps_,
207 stepsPerYear_,
208 antithetic_,
209 samples_, tolerance_,
210 maxSamples_,
211 seed_));
212 }
213
214
215
217 Option::Type type,
218 Real strike,
219 DiscountFactor discount)
220 : payoff_(type, strike), discount_(discount) {
221 QL_REQUIRE(strike>=0.0,
222 "strike less than zero not allowed");
223 }
224
226 const MultiPath& multiPath) const {
227 const Path& path = multiPath[0];
228 const Size n = multiPath.pathSize();
229 QL_REQUIRE(n>0, "the path cannot be empty");
230
231 return payoff_(path.back()) * discount_;
232 }
233
234}
235
236
237#endif
EuropeanGJRGARCHPathPricer(Option::Type type, Real strike, DiscountFactor discount)
Real operator()(const MultiPath &Multipath) const override
Monte Carlo GJR-GARCH-model engine for European options.
MCVanillaEngine< MultiVariate, RNG, S >::path_pricer_type path_pricer_type
ext::shared_ptr< path_pricer_type > pathPricer() const override
MCEuropeanGJRGARCHEngine(const ext::shared_ptr< GJRGARCHProcess > &, Size timeSteps, Size timeStepsPerYear, bool antitheticVariate, Size requiredSamples, Real requiredTolerance, Size maxSamples, BigNatural seed)
Pricing engine for vanilla options using Monte Carlo simulation.
McSimulation< MC, RNG, S >::path_pricer_type path_pricer_type
Monte Carlo GJR-GARCH European engine factory.
MakeMCEuropeanGJRGARCHEngine & withAntitheticVariate(bool b=true)
MakeMCEuropeanGJRGARCHEngine & withSamples(Size samples)
MakeMCEuropeanGJRGARCHEngine & withStepsPerYear(Size steps)
ext::shared_ptr< GJRGARCHProcess > process_
MakeMCEuropeanGJRGARCHEngine & withAbsoluteTolerance(Real tolerance)
MakeMCEuropeanGJRGARCHEngine(ext::shared_ptr< GJRGARCHProcess >)
MakeMCEuropeanGJRGARCHEngine & withSteps(Size steps)
MakeMCEuropeanGJRGARCHEngine & withMaxSamples(Size samples)
MakeMCEuropeanGJRGARCHEngine & 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
#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
const ext::shared_ptr< Payoff > payoff_
GJR-GARCH(1,1) stochastic process.
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
ext::shared_ptr< QuantLib::Payoff > payoff
Monte Carlo vanilla option engine.
Definition: any.hpp:35
unsigned QL_BIG_INTEGER BigNatural
large positive integer
Definition: types.hpp:46
STL namespace.
default Monte Carlo traits for multi-variate models
Definition: mctraits.hpp:50