QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
mcpagodaengine.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
24#ifndef quantlib_mc_pagoda_engine_hpp
25#define quantlib_mc_pagoda_engine_hpp
26
27#include <ql/exercise.hpp>
28#include <ql/experimental/exoticoptions/pagodaoption.hpp>
29#include <ql/pricingengines/mcsimulation.hpp>
30#include <ql/processes/blackscholesprocess.hpp>
31#include <ql/processes/stochasticprocessarray.hpp>
32#include <utility>
33
34namespace QuantLib {
35
37 template <class RNG = PseudoRandom, class S = Statistics>
39 public McSimulation<MultiVariate,RNG,S> {
40 public:
47 // constructor
48 MCPagodaEngine(ext::shared_ptr<StochasticProcessArray>,
49 bool brownianBridge,
50 bool antitheticVariate,
51 Size requiredSamples,
52 Real requiredTolerance,
53 Size maxSamples,
54 BigNatural seed);
55 void calculate() const override {
59 results_.value = this->mcModel_->sampleAccumulator().mean();
60 if (RNG::allowsErrorEstimate)
62 this->mcModel_->sampleAccumulator().errorEstimate();
63 }
64
65 private:
66 // McSimulation implementation
67 TimeGrid timeGrid() const override;
68 ext::shared_ptr<path_generator_type> pathGenerator() const override {
69
70 Size numAssets = processes_->size();
71
72 TimeGrid grid = timeGrid();
73 typename RNG::rsg_type gen =
74 RNG::make_sequence_generator(numAssets*(grid.size()-1),seed_);
75
76 return ext::shared_ptr<path_generator_type>(
78 grid, gen, brownianBridge_));
79 }
80 ext::shared_ptr<path_pricer_type> pathPricer() const override;
81
82 // data members
83 ext::shared_ptr<StochasticProcessArray> processes_;
89 };
90
91
93 template <class RNG = PseudoRandom, class S = Statistics>
95 public:
96 explicit MakeMCPagodaEngine(ext::shared_ptr<StochasticProcessArray>);
97 // named parameters
104 // conversion to pricing engine
105 operator ext::shared_ptr<PricingEngine>() const;
106 private:
107 ext::shared_ptr<StochasticProcessArray> process_;
108 bool brownianBridge_ = false, antithetic_ = false;
112 };
113
114
115 class PagodaMultiPathPricer : public PathPricer<MultiPath> {
116 public:
117 PagodaMultiPathPricer(Real roof, Real fraction,
118 DiscountFactor discount);
119 Real operator()(const MultiPath& multiPath) const override;
120
121 private:
124 };
125
126
127 // template definitions
128
129 template <class RNG, class S>
130 inline MCPagodaEngine<RNG, S>::MCPagodaEngine(ext::shared_ptr<StochasticProcessArray> processes,
131 bool brownianBridge,
132 bool antitheticVariate,
133 Size requiredSamples,
134 Real requiredTolerance,
135 Size maxSamples,
136 BigNatural seed)
137 : McSimulation<MultiVariate, RNG, S>(antitheticVariate, false),
138 processes_(std::move(processes)), requiredSamples_(requiredSamples), maxSamples_(maxSamples),
139 requiredTolerance_(requiredTolerance), brownianBridge_(brownianBridge), seed_(seed) {
141 }
142
143 template <class RNG, class S>
145
146 std::vector<Time> fixingTimes;
147 for (Size i=0; i<arguments_.fixingDates.size(); i++) {
148 Time t = processes_->time(arguments_.fixingDates[i]);
149 QL_REQUIRE(t >= 0.0, "seasoned options are not handled");
150 if (i > 0) {
151 QL_REQUIRE(t > fixingTimes.back(), "fixing dates not sorted");
152 }
153 fixingTimes.push_back(t);
154 }
155
156 return TimeGrid(fixingTimes.begin(), fixingTimes.end());
157 }
158
159
160 template <class RNG, class S>
161 inline
162 ext::shared_ptr<typename MCPagodaEngine<RNG,S>::path_pricer_type>
164
165 ext::shared_ptr<GeneralizedBlackScholesProcess> process =
166 ext::dynamic_pointer_cast<GeneralizedBlackScholesProcess>(
167 processes_->process(0));
168 QL_REQUIRE(process, "Black-Scholes process required");
169
170 return ext::shared_ptr<
172 new PagodaMultiPathPricer(arguments_.roof, arguments_.fraction,
173 process->riskFreeRate()->discount(
174 arguments_.exercise->lastDate())));
175 }
176
177
178 template <class RNG, class S>
180 ext::shared_ptr<StochasticProcessArray> process)
181 : process_(std::move(process)), samples_(Null<Size>()), maxSamples_(Null<Size>()),
182 tolerance_(Null<Real>()) {}
183
184 template <class RNG, class S>
187 brownianBridge_ = brownianBridge;
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>
201 QL_REQUIRE(tolerance_ == Null<Real>(),
202 "tolerance already set");
203 samples_ = samples;
204 return *this;
205 }
206
207 template <class RNG, class S>
210 QL_REQUIRE(samples_ == Null<Size>(),
211 "number of samples already set");
212 QL_REQUIRE(RNG::allowsErrorEstimate,
213 "chosen random generator policy "
214 "does not allow an error estimate");
215 tolerance_ = tolerance;
216 return *this;
217 }
218
219 template <class RNG, class S>
222 maxSamples_ = samples;
223 return *this;
224 }
225
226 template <class RNG, class S>
229 seed_ = seed;
230 return *this;
231 }
232
233 template <class RNG, class S>
234 inline
236 ext::shared_ptr<PricingEngine>() const {
237 return ext::shared_ptr<PricingEngine>(new
238 MCPagodaEngine<RNG,S>(process_,
239 brownianBridge_,
240 antithetic_,
241 samples_, tolerance_,
242 maxSamples_,
243 seed_));
244 }
245
246}
247
248
249
250#endif
Pricing engine for pagoda options using Monte Carlo simulation.
MCPagodaEngine(ext::shared_ptr< StochasticProcessArray >, bool brownianBridge, bool antitheticVariate, Size requiredSamples, Real requiredTolerance, Size maxSamples, BigNatural seed)
ext::shared_ptr< path_generator_type > pathGenerator() const override
void calculate() const override
McSimulation< MultiVariate, RNG, S >::path_pricer_type path_pricer_type
McSimulation< MultiVariate, RNG, S >::stats_type stats_type
McSimulation< MultiVariate, RNG, S >::path_generator_type path_generator_type
ext::shared_ptr< path_pricer_type > pathPricer() const override
ext::shared_ptr< StochasticProcessArray > processes_
TimeGrid timeGrid() const override
Monte Carlo pagoda-option engine factory.
MakeMCPagodaEngine & withMaxSamples(Size samples)
MakeMCPagodaEngine & withAntitheticVariate(bool b=true)
MakeMCPagodaEngine & withBrownianBridge(bool b=true)
MakeMCPagodaEngine & withSamples(Size samples)
MakeMCPagodaEngine & withAbsoluteTolerance(Real tolerance)
MakeMCPagodaEngine(ext::shared_ptr< StochasticProcessArray >)
ext::shared_ptr< StochasticProcessArray > process_
MakeMCPagodaEngine & withSeed(BigNatural seed)
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
Correlated multiple asset paths.
Definition: multipath.hpp:39
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
Real operator()(const MultiPath &multiPath) const override
Pagoda-option engine base class
base class for path pricers
Definition: pathpricer.hpp:40
time grid class
Definition: timegrid.hpp:43
Size size() const
Definition: timegrid.hpp:164
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
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
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