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/*! \file mchimalayaengine.hpp
21 \brief Monte Carlo engine for Himalaya options
22*/
23
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>
37 class MCHimalayaEngine : public HimalayaOption::engine,
38 public McSimulation<MultiVariate,RNG,S> {
39 public:
40 typedef typename McSimulation<MultiVariate,RNG,S>::path_generator_type
41 path_generator_type;
42 typedef typename McSimulation<MultiVariate,RNG,S>::path_pricer_type
43 path_pricer_type;
44 typedef typename McSimulation<MultiVariate,RNG,S>::stats_type
45 stats_type;
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 {
55 McSimulation<MultiVariate,RNG,S>::calculate(requiredTolerance_,
56 requiredSamples_,
57 maxSamples_);
58 results_.value = this->mcModel_->sampleAccumulator().mean();
59
60 if (RNG::allowsErrorEstimate)
61 results_.errorEstimate =
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>(
77 new path_generator_type(processes_,
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_;
84 Size requiredSamples_;
85 Size maxSamples_;
86 Real requiredTolerance_;
87 bool brownianBridge_;
88 BigNatural seed_;
89 };
90
91
92 //! Monte Carlo Himalaya-option engine factory
93 template <class RNG = PseudoRandom, class S = Statistics>
94 class MakeMCHimalayaEngine {
95 public:
96 explicit MakeMCHimalayaEngine(ext::shared_ptr<StochasticProcessArray>);
97 // named parameters
98 MakeMCHimalayaEngine& withBrownianBridge(bool b = true);
99 MakeMCHimalayaEngine& withAntitheticVariate(bool b = true);
100 MakeMCHimalayaEngine& withSamples(Size samples);
101 MakeMCHimalayaEngine& withAbsoluteTolerance(Real tolerance);
102 MakeMCHimalayaEngine& withMaxSamples(Size samples);
103 MakeMCHimalayaEngine& withSeed(BigNatural seed);
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;
109 Size samples_, maxSamples_;
110 Real tolerance_;
111 BigNatural seed_ = 0;
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_;
122 DiscountFactor discount_;
123 };
124
125 // template definitions
126
127 template <class RNG, class S>
128 inline MCHimalayaEngine<RNG, S>::MCHimalayaEngine(
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) {
139 registerWith(h: processes_);
140 }
141
142 template <class RNG, class S>
143 inline TimeGrid MCHimalayaEngine<RNG,S>::timeGrid() const {
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(x: 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>
161 MCHimalayaEngine<RNG,S>::pathPricer() const {
162
163 ext::shared_ptr<GeneralizedBlackScholesProcess> process =
164 ext::dynamic_pointer_cast<GeneralizedBlackScholesProcess>(
165 r: processes_->process(i: 0));
166 QL_REQUIRE(process, "Black-Scholes process required");
167
168 return ext::shared_ptr<
169 typename MCHimalayaEngine<RNG,S>::path_pricer_type>(
170 new HimalayaMultiPathPricer(arguments_.payoff,
171 process->riskFreeRate()->discount(
172 d: arguments_.exercise->lastDate())));
173 }
174
175
176 template <class RNG, class S>
177 inline MakeMCHimalayaEngine<RNG, S>::MakeMCHimalayaEngine(
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>
183 inline MakeMCHimalayaEngine<RNG,S>&
184 MakeMCHimalayaEngine<RNG,S>::withBrownianBridge(bool brownianBridge) {
185 brownianBridge_ = brownianBridge;
186 return *this;
187 }
188
189 template <class RNG, class S>
190 inline MakeMCHimalayaEngine<RNG,S>&
191 MakeMCHimalayaEngine<RNG,S>::withAntitheticVariate(bool b) {
192 antithetic_ = b;
193 return *this;
194 }
195
196 template <class RNG, class S>
197 inline MakeMCHimalayaEngine<RNG,S>&
198 MakeMCHimalayaEngine<RNG,S>::withSamples(Size samples) {
199 QL_REQUIRE(tolerance_ == Null<Real>(),
200 "tolerance already set");
201 samples_ = samples;
202 return *this;
203 }
204
205 template <class RNG, class S>
206 inline MakeMCHimalayaEngine<RNG,S>&
207 MakeMCHimalayaEngine<RNG,S>::withAbsoluteTolerance(Real tolerance) {
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>
218 inline MakeMCHimalayaEngine<RNG,S>&
219 MakeMCHimalayaEngine<RNG,S>::withMaxSamples(Size samples) {
220 maxSamples_ = samples;
221 return *this;
222 }
223
224 template <class RNG, class S>
225 inline MakeMCHimalayaEngine<RNG,S>&
226 MakeMCHimalayaEngine<RNG,S>::withSeed(BigNatural seed) {
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
236 MCHimalayaEngine<RNG,S>(process_,
237 brownianBridge_,
238 antithetic_,
239 samples_,
240 tolerance_,
241 maxSamples_,
242 seed_));
243 }
244
245}
246
247#endif
248

source code of quantlib/ql/experimental/exoticoptions/mchimalayaengine.hpp