QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
mcperformanceengine.hpp
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>
24#include <ql/instruments/cliquetoption.hpp>
25#include <ql/pricingengines/mcsimulation.hpp>
26#include <ql/processes/blackscholesprocess.hpp>
27#include <utility>
28
29namespace QuantLib {
30
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 (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
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 for (Size i=0; i<arguments_.resetDates.size(); i++)
144 fixingTimes.push_back(process_->time(arguments_.resetDates[i]));
145 fixingTimes.push_back(process_->time(arguments_.exercise->lastDate()));
146
147 return TimeGrid(fixingTimes.begin(), fixingTimes.end());
148 }
149
150
151 template <class RNG, class S>
152 inline
153 ext::shared_ptr<typename MCPerformanceEngine<RNG,S>::path_pricer_type>
155
156 ext::shared_ptr<PercentageStrikePayoff> payoff =
157 ext::dynamic_pointer_cast<PercentageStrikePayoff>(
158 this->arguments_.payoff);
159 QL_REQUIRE(payoff, "non-percentage payoff given");
160
161 ext::shared_ptr<EuropeanExercise> exercise =
162 ext::dynamic_pointer_cast<EuropeanExercise>(
163 this->arguments_.exercise);
164 QL_REQUIRE(exercise, "wrong exercise given");
165
166 std::vector<DiscountFactor> discounts;
167
168 for (Size k=0;k<arguments_.resetDates.size();k++) {
169 discounts.push_back(this->process_->riskFreeRate()->discount(
170 arguments_.resetDates[k]));
171 }
172 discounts.push_back(this->process_->riskFreeRate()->discount(
173 arguments_.exercise->lastDate()));
174
175 return ext::shared_ptr<
177 new PerformanceOptionPathPricer(payoff->optionType(),
178 payoff->strike(),
179 discounts));
180 }
181
182
183 template <class RNG, class S>
185 ext::shared_ptr<GeneralizedBlackScholesProcess> process)
186 : process_(std::move(process)), samples_(Null<Size>()), maxSamples_(Null<Size>()),
187 tolerance_(Null<Real>()) {}
188
189 template <class RNG, class S>
192 brownianBridge_ = brownianBridge;
193 return *this;
194 }
195
196 template <class RNG, class S>
199 antithetic_ = b;
200 return *this;
201 }
202
203 template <class RNG, class S>
206 QL_REQUIRE(tolerance_ == Null<Real>(),
207 "tolerance already set");
208 samples_ = samples;
209 return *this;
210 }
211
212 template <class RNG, class S>
215 QL_REQUIRE(samples_ == Null<Size>(),
216 "number of samples already set");
217 QL_REQUIRE(RNG::allowsErrorEstimate,
218 "chosen random generator policy "
219 "does not allow an error estimate");
220 tolerance_ = tolerance;
221 return *this;
222 }
223
224 template <class RNG, class S>
227 maxSamples_ = samples;
228 return *this;
229 }
230
231 template <class RNG, class S>
234 seed_ = seed;
235 return *this;
236 }
237
238 template <class RNG, class S>
239 inline
240 MakeMCPerformanceEngine<RNG,S>::operator ext::shared_ptr<PricingEngine>()
241 const {
242 return ext::shared_ptr<PricingEngine>(new
244 brownianBridge_,
245 antithetic_,
246 samples_,
247 tolerance_,
248 maxSamples_,
249 seed_));
250 }
251
252}
253
254
255#endif
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:76
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
Definition: observable.hpp:228
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:164
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.
default Monte Carlo traits for single-variate models
Definition: mctraits.hpp:39