QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
mchimalayaengine.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_himalaya_engine_hpp
25#define quantlib_mc_himalaya_engine_hpp
26
27#include <ql/exercise.hpp>
28#include <ql/experimental/exoticoptions/himalayaoption.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
36 template <class RNG = PseudoRandom, class S = Statistics>
38 public McSimulation<MultiVariate,RNG,S> {
39 public:
46 MCHimalayaEngine(ext::shared_ptr<StochasticProcessArray>,
47 bool brownianBridge,
48 bool antitheticVariate,
49 Size requiredSamples,
50 Real requiredTolerance,
51 Size maxSamples,
52 BigNatural seed);
53
54 void calculate() const override {
58 results_.value = this->mcModel_->sampleAccumulator().mean();
59
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 MakeMCHimalayaEngine(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 HimalayaMultiPathPricer : public PathPricer<MultiPath> {
116 public:
117 HimalayaMultiPathPricer(ext::shared_ptr<Payoff> payoff, DiscountFactor discount);
118 Real operator()(const MultiPath& multiPath) const override;
119
120 private:
121 ext::shared_ptr<Payoff> payoff_;
123 };
124
125 // template definitions
126
127 template <class RNG, class S>
129 ext::shared_ptr<StochasticProcessArray> processes,
130 bool brownianBridge,
131 bool antitheticVariate,
132 Size requiredSamples,
133 Real requiredTolerance,
134 Size maxSamples,
135 BigNatural seed)
136 : McSimulation<MultiVariate, RNG, S>(antitheticVariate, false),
137 processes_(std::move(processes)), requiredSamples_(requiredSamples), maxSamples_(maxSamples),
138 requiredTolerance_(requiredTolerance), brownianBridge_(brownianBridge), seed_(seed) {
140 }
141
142 template <class RNG, class S>
144
145 std::vector<Time> fixingTimes;
146 for (Size i=0; i<arguments_.fixingDates.size(); i++) {
147 Time t = processes_->time(arguments_.fixingDates[i]);
148 QL_REQUIRE(t >= 0.0, "seasoned options are not handled");
149 if (i > 0) {
150 QL_REQUIRE(t > fixingTimes.back(), "fixing dates not sorted");
151 }
152 fixingTimes.push_back(t);
153 }
154
155 return TimeGrid(fixingTimes.begin(), fixingTimes.end());
156 }
157
158 template <class RNG, class S>
159 inline
160 ext::shared_ptr<typename MCHimalayaEngine<RNG,S>::path_pricer_type>
162
163 ext::shared_ptr<GeneralizedBlackScholesProcess> process =
164 ext::dynamic_pointer_cast<GeneralizedBlackScholesProcess>(
165 processes_->process(0));
166 QL_REQUIRE(process, "Black-Scholes process required");
167
168 return ext::shared_ptr<
170 new HimalayaMultiPathPricer(arguments_.payoff,
171 process->riskFreeRate()->discount(
172 arguments_.exercise->lastDate())));
173 }
174
175
176 template <class RNG, class S>
178 ext::shared_ptr<StochasticProcessArray> process)
179 : process_(std::move(process)), samples_(Null<Size>()), maxSamples_(Null<Size>()),
180 tolerance_(Null<Real>()) {}
181
182 template <class RNG, class S>
185 brownianBridge_ = brownianBridge;
186 return *this;
187 }
188
189 template <class RNG, class S>
192 antithetic_ = b;
193 return *this;
194 }
195
196 template <class RNG, class S>
199 QL_REQUIRE(tolerance_ == Null<Real>(),
200 "tolerance already set");
201 samples_ = samples;
202 return *this;
203 }
204
205 template <class RNG, class S>
208 QL_REQUIRE(samples_ == Null<Size>(),
209 "number of samples already set");
210 QL_REQUIRE(RNG::allowsErrorEstimate,
211 "chosen random generator policy "
212 "does not allow an error estimate");
213 tolerance_ = tolerance;
214 return *this;
215 }
216
217 template <class RNG, class S>
220 maxSamples_ = samples;
221 return *this;
222 }
223
224 template <class RNG, class S>
227 seed_ = seed;
228 return *this;
229 }
230
231 template <class RNG, class S>
232 inline
233 MakeMCHimalayaEngine<RNG,S>::operator ext::shared_ptr<PricingEngine>()
234 const {
235 return ext::shared_ptr<PricingEngine>(new
237 brownianBridge_,
238 antithetic_,
239 samples_,
240 tolerance_,
241 maxSamples_,
242 seed_));
243 }
244
245}
246
247#endif
ext::shared_ptr< Payoff > payoff_
Real operator()(const MultiPath &multiPath) const override
ext::shared_ptr< path_pricer_type > pathPricer() const override
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< StochasticProcessArray > processes_
MCHimalayaEngine(ext::shared_ptr< StochasticProcessArray >, bool brownianBridge, bool antitheticVariate, Size requiredSamples, Real requiredTolerance, Size maxSamples, BigNatural seed)
TimeGrid timeGrid() const override
Monte Carlo Himalaya-option engine factory.
MakeMCHimalayaEngine & withMaxSamples(Size samples)
MakeMCHimalayaEngine & withAntitheticVariate(bool b=true)
MakeMCHimalayaEngine & withSamples(Size samples)
MakeMCHimalayaEngine & withAbsoluteTolerance(Real tolerance)
MakeMCHimalayaEngine(ext::shared_ptr< StochasticProcessArray >)
MakeMCHimalayaEngine & withSeed(BigNatural seed)
ext::shared_ptr< StochasticProcessArray > process_
MakeMCHimalayaEngine & withBrownianBridge(bool b=true)
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
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