QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.38
Loading...
Searching...
No Matches
mcperformanceengine.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 Master IMAFA - Polytech'Nice Sophia - Université de Nice Sophia Antipolis
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#ifndef quantlib_mc_performance_engine_hpp
21#define quantlib_mc_performance_engine_hpp
22
23#include <ql/exercise.hpp>
27#include <utility>
28
29namespace QuantLib {
30
31 //! Pricing engine for performance options using Monte Carlo simulation
32 template<class RNG = PseudoRandom, class S = Statistics>
34 public McSimulation<SingleVariate,RNG,S> {
35 public:
36 typedef
43 // constructor
44 MCPerformanceEngine(ext::shared_ptr<GeneralizedBlackScholesProcess> process,
45 bool brownianBridge,
46 bool antitheticVariate,
47 Size requiredSamples,
48 Real requiredTolerance,
49 Size maxSamples,
50 BigNatural seed);
51 void calculate() const override {
55 results_.value = this->mcModel_->sampleAccumulator().mean();
56 if constexpr (RNG::allowsErrorEstimate)
58 this->mcModel_->sampleAccumulator().errorEstimate();
59 }
60
61 protected:
62 // McSimulation implementation
63 TimeGrid timeGrid() const override;
64 ext::shared_ptr<path_generator_type> pathGenerator() const override {
65
66 TimeGrid grid = this->timeGrid();
67 typename RNG::rsg_type gen =
68 RNG::make_sequence_generator(grid.size()-1,seed_);
69 return ext::shared_ptr<path_generator_type>(
71 gen, brownianBridge_));
72 }
73 ext::shared_ptr<path_pricer_type> pathPricer() const override;
74 // data members
75 ext::shared_ptr<GeneralizedBlackScholesProcess> process_;
80 };
81
82
83 //! Monte Carlo performance-option engine factory
84 template <class RNG = PseudoRandom, class S = Statistics>
86 public:
87 MakeMCPerformanceEngine(ext::shared_ptr<GeneralizedBlackScholesProcess>);
88 // named parameters
95 // conversion to pricing engine
96 operator ext::shared_ptr<PricingEngine>() const;
97 private:
98 ext::shared_ptr<GeneralizedBlackScholesProcess> process_;
99 bool brownianBridge_ = false, antithetic_ = false;
103 };
104
105
106
108 public:
110 Real strike,
111 std::vector<DiscountFactor> discounts);
112 Real operator()(const Path& path) const override;
113
114 private:
117 std::vector<DiscountFactor> discounts_;
118 };
119
120
121 // template definitions
122
123 template <class RNG, class S>
125 ext::shared_ptr<GeneralizedBlackScholesProcess> process,
126 bool brownianBridge,
127 bool antitheticVariate,
128 Size requiredSamples,
129 Real requiredTolerance,
130 Size maxSamples,
131 BigNatural seed)
132 : McSimulation<SingleVariate, RNG, S>(antitheticVariate, false), process_(std::move(process)),
133 requiredSamples_(requiredSamples), maxSamples_(maxSamples),
134 requiredTolerance_(requiredTolerance), brownianBridge_(brownianBridge), seed_(seed) {
136 }
137
138
139 template <class RNG, class S>
141
142 std::vector<Time> fixingTimes;
143 fixingTimes.reserve(arguments_.resetDates.size());
144 for (Size i=0; i<arguments_.resetDates.size(); i++)
145 fixingTimes.push_back(process_->time(arguments_.resetDates[i]));
146 fixingTimes.push_back(process_->time(arguments_.exercise->lastDate()));
147
148 return TimeGrid(fixingTimes.begin(), fixingTimes.end());
149 }
150
151
152 template <class RNG, class S>
153 inline
154 ext::shared_ptr<typename MCPerformanceEngine<RNG,S>::path_pricer_type>
156
157 ext::shared_ptr<PercentageStrikePayoff> payoff =
158 ext::dynamic_pointer_cast<PercentageStrikePayoff>(
159 this->arguments_.payoff);
160 QL_REQUIRE(payoff, "non-percentage payoff given");
161
162 ext::shared_ptr<EuropeanExercise> exercise =
163 ext::dynamic_pointer_cast<EuropeanExercise>(
164 this->arguments_.exercise);
165 QL_REQUIRE(exercise, "wrong exercise given");
166
167 std::vector<DiscountFactor> discounts;
168
169 discounts.reserve(arguments_.resetDates.size());
170 for (Size k=0;k<arguments_.resetDates.size();k++) {
171 discounts.push_back(this->process_->riskFreeRate()->discount(
172 arguments_.resetDates[k]));
173 }
174 discounts.push_back(this->process_->riskFreeRate()->discount(
175 arguments_.exercise->lastDate()));
176
177 return ext::shared_ptr<
179 new PerformanceOptionPathPricer(payoff->optionType(),
180 payoff->strike(),
181 discounts));
182 }
183
184
185 template <class RNG, class S>
187 ext::shared_ptr<GeneralizedBlackScholesProcess> process)
188 : process_(std::move(process)), samples_(Null<Size>()), maxSamples_(Null<Size>()),
189 tolerance_(Null<Real>()) {}
190
191 template <class RNG, class S>
194 brownianBridge_ = brownianBridge;
195 return *this;
196 }
197
198 template <class RNG, class S>
201 antithetic_ = b;
202 return *this;
203 }
204
205 template <class RNG, class S>
208 QL_REQUIRE(tolerance_ == Null<Real>(),
209 "tolerance already set");
210 samples_ = samples;
211 return *this;
212 }
213
214 template <class RNG, class S>
217 QL_REQUIRE(samples_ == Null<Size>(),
218 "number of samples already set");
219 QL_REQUIRE(RNG::allowsErrorEstimate,
220 "chosen random generator policy "
221 "does not allow an error estimate");
222 tolerance_ = tolerance;
223 return *this;
224 }
225
226 template <class RNG, class S>
229 maxSamples_ = samples;
230 return *this;
231 }
232
233 template <class RNG, class S>
236 seed_ = seed;
237 return *this;
238 }
239
240 template <class RNG, class S>
241 inline
242 MakeMCPerformanceEngine<RNG,S>::operator ext::shared_ptr<PricingEngine>()
243 const {
244 return ext::shared_ptr<PricingEngine>(new
246 brownianBridge_,
247 antithetic_,
248 samples_,
249 tolerance_,
250 maxSamples_,
251 seed_));
252 }
253
254}
255
256
257#endif
Black-Scholes processes.
Cliquet engine base class.
Pricing engine for performance options using Monte Carlo simulation.
McSimulation< SingleVariate, RNG, S >::path_generator_type path_generator_type
ext::shared_ptr< path_generator_type > pathGenerator() const override
McSimulation< SingleVariate, RNG, S >::stats_type stats_type
MCPerformanceEngine(ext::shared_ptr< GeneralizedBlackScholesProcess > process, bool brownianBridge, bool antitheticVariate, Size requiredSamples, Real requiredTolerance, Size maxSamples, BigNatural seed)
McSimulation< SingleVariate, RNG, S >::path_pricer_type path_pricer_type
ext::shared_ptr< GeneralizedBlackScholesProcess > process_
ext::shared_ptr< path_pricer_type > pathPricer() const override
TimeGrid timeGrid() const override
Monte Carlo performance-option engine factory.
MakeMCPerformanceEngine & withAntitheticVariate(bool b=true)
MakeMCPerformanceEngine & withBrownianBridge(bool b=true)
MakeMCPerformanceEngine(ext::shared_ptr< GeneralizedBlackScholesProcess >)
MakeMCPerformanceEngine & withAbsoluteTolerance(Real tolerance)
MakeMCPerformanceEngine & withSeed(BigNatural seed)
ext::shared_ptr< GeneralizedBlackScholesProcess > process_
MakeMCPerformanceEngine & withMaxSamples(Size samples)
MakeMCPerformanceEngine & withSamples(Size samples)
base class for Monte Carlo engines
MonteCarloModel< MC, RNG, S >::path_generator_type path_generator_type
ext::shared_ptr< MonteCarloModel< MC, RNG, S > > mcModel_
void calculate(Real requiredTolerance, Size requiredSamples, Size maxSamples) const
basic calculate method provided to inherited pricing engines
MonteCarloModel< MC, RNG, S >::path_pricer_type path_pricer_type
template class providing a null value for a given type.
Definition: null.hpp:59
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:226
single-factor random walk
Definition: path.hpp:40
base class for path pricers
Definition: pathpricer.hpp:40
std::vector< DiscountFactor > discounts_
Real operator()(const Path &path) const override
time grid class
Definition: timegrid.hpp:43
Size size() const
Definition: timegrid.hpp:169
Cliquet option.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Option exercise classes and payoff function.
std::function< Real(Real)> b
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
ext::shared_ptr< QuantLib::Payoff > payoff
framework for Monte Carlo engines
Definition: any.hpp:37
unsigned QL_BIG_INTEGER BigNatural
large positive integer
Definition: types.hpp:46
STL namespace.
default Monte Carlo traits for single-variate models
Definition: mctraits.hpp:39