QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
mcdiscreteasianenginebase.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) 2000, 2001, 2002, 2003 RiskMap srl
5 Copyright (C) 2003, 2004 Ferdinando Ametrano
6 Copyright (C) 2007, 2008 StatPro Italia srl
7
8 This file is part of QuantLib, a free-software/open-source library
9 for financial quantitative analysts and developers - http://quantlib.org/
10
11 QuantLib is free software: you can redistribute it and/or modify it
12 under the terms of the QuantLib license. You should have received a
13 copy of the license along with this program; if not, please email
14 <quantlib-dev@lists.sf.net>. The license is also available online at
15 <http://quantlib.org/license.shtml>.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the license for more details.
20*/
21
22/*! \file mcdiscreteasianenginebase.hpp
23 \brief Monte Carlo pricing engine for discrete average Asians
24*/
25
26#ifndef quantlib_mcdiscreteasian_engine_base_hpp
27#define quantlib_mcdiscreteasian_engine_base_hpp
28
29#include <ql/exercise.hpp>
32#include <utility>
33
34namespace QuantLib {
35
36
37 namespace detail {
38
39 class PastFixingsOnly : public Error {
40 public:
42 : Error("n/a", 0, "n/a",
43 "all fixings are in the past") {}
44 };
45
46 }
47
48 //! Pricing engine for discrete average Asians using Monte Carlo simulation
49 /*! \warning control-variate calculation is disabled under VC++6.
50 \ingroup asianengines
51 */
52
53 template<template <class> class MC,
54 class RNG = PseudoRandom, class S = Statistics>
57 public McSimulation<MC,RNG,S> {
58 public:
59 typedef
66 // constructor
67 MCDiscreteAveragingAsianEngineBase(ext::shared_ptr<StochasticProcess> process,
68 bool brownianBridge,
69 bool antitheticVariate,
70 bool controlVariate,
71 Size requiredSamples,
72 Real requiredTolerance,
73 Size maxSamples,
74 BigNatural seed,
75 Size timeSteps = Null<Size>(),
76 Size timeStepsPerYear = Null<Size>());
77 void calculate() const override {
78 try {
82 } catch (detail::PastFixingsOnly&) {
83 // Ideally, here we could calculate the payoff (which
84 // is fully determine) and write it into the results.
85 // This would probably need a new virtual method that
86 // derived engines should implement.
87 throw;
88 }
89
90 results_.value = this->mcModel_->sampleAccumulator().mean();
91
92 if (this->controlVariate_) {
93 // control variate might lead to small negative
94 // option values for deep OTM options
95 this->results_.value = std::max(0.0, this->results_.value);
96 }
97
98 if (RNG::allowsErrorEstimate)
100 this->mcModel_->sampleAccumulator().errorEstimate();
101
102 // Allow inspection of the timeGrid via additional results
103 this->results_.additionalResults["TimeGrid"] = this->timeGrid();
104 }
105
106 protected:
107 // McSimulation implementation
108 TimeGrid timeGrid() const override;
109 ext::shared_ptr<path_generator_type> pathGenerator() const override {
110
111 Size dimensions = process_->factors();
112 TimeGrid grid = this->timeGrid();
113 typename RNG::rsg_type gen =
114 RNG::make_sequence_generator(dimensions*(grid.size()-1),seed_);
115 return ext::shared_ptr<path_generator_type>(
117 gen, brownianBridge_));
118 }
119 Real controlVariateValue() const override;
120 // data members
121 ext::shared_ptr<StochasticProcess> process_;
126 };
127
128
129 // template definitions
130
131 template <template <class> class MC, class RNG, class S>
133 ext::shared_ptr<StochasticProcess> process,
134 bool brownianBridge,
135 bool antitheticVariate,
136 bool controlVariate,
137 Size requiredSamples,
138 Real requiredTolerance,
139 Size maxSamples,
140 BigNatural seed,
141 Size timeSteps,
142 Size timeStepsPerYear)
143 : McSimulation<MC, RNG, S>(antitheticVariate, controlVariate), process_(std::move(process)),
144 requiredSamples_(requiredSamples), maxSamples_(maxSamples), timeSteps_(timeSteps),
145 timeStepsPerYear_(timeStepsPerYear), requiredTolerance_(requiredTolerance),
146 brownianBridge_(brownianBridge), seed_(seed) {
148 }
149
150 template <template <class> class MC, class RNG, class S>
152
153 std::vector<Time> fixingTimes;
154 Size i;
155 for (i=0; i<arguments_.fixingDates.size(); i++) {
156 Time t = process_->time(arguments_.fixingDates[i]);
157 if (t>=0) {
158 fixingTimes.push_back(t);
159 }
160 }
161
162 if (fixingTimes.empty() ||
163 (fixingTimes.size() == 1 && fixingTimes.front() == 0.0))
165
166 // Some models (eg. Heston) might request additional points in
167 // the time grid to improve the accuracy of the discretization
168 Date lastExerciseDate = this->arguments_.exercise->lastDate();
169 Time t = process_->time(lastExerciseDate);
170
171 if (this->timeSteps_ != Null<Size>()) {
172 return TimeGrid(fixingTimes.begin(), fixingTimes.end(), timeSteps_);
173 } else if (this->timeStepsPerYear_ != Null<Size>()) {
174 return TimeGrid(fixingTimes.begin(), fixingTimes.end(),
175 static_cast<Size>(this->timeStepsPerYear_*t));
176 }
177
178 return TimeGrid(fixingTimes.begin(), fixingTimes.end());
179 }
180
181 template<template <class> class MC, class RNG, class S>
182 inline
184
185 ext::shared_ptr<PricingEngine> controlPE =
186 this->controlPricingEngine();
187 QL_REQUIRE(controlPE,
188 "engine does not provide "
189 "control variation pricing engine");
190
191 auto* controlArguments =
192 dynamic_cast<DiscreteAveragingAsianOption::arguments*>(controlPE->getArguments());
193 *controlArguments = arguments_;
194 controlPE->calculate();
195
196 const auto* controlResults =
197 dynamic_cast<const DiscreteAveragingAsianOption::results*>(controlPE->getResults());
198
199 return controlResults->value;
200 }
201
202}
203
204
205#endif
Asian option on a single asset.
Concrete date class.
Definition: date.hpp:125
Extra arguments for single-asset discrete-average Asian option.
Discrete-averaging Asian engine base class.
Base error class.
Definition: errors.hpp:39
std::map< std::string, ext::any > additionalResults
Definition: instrument.hpp:123
Pricing engine for discrete average Asians using Monte Carlo simulation.
McSimulation< MC, RNG, S >::path_generator_type path_generator_type
MCDiscreteAveragingAsianEngineBase(ext::shared_ptr< StochasticProcess > process, bool brownianBridge, bool antitheticVariate, bool controlVariate, Size requiredSamples, Real requiredTolerance, Size maxSamples, BigNatural seed, Size timeSteps=Null< Size >(), Size timeStepsPerYear=Null< Size >())
ext::shared_ptr< path_generator_type > pathGenerator() const override
McSimulation< MC, RNG, S >::path_pricer_type path_pricer_type
McSimulation< MC, RNG, S >::stats_type stats_type
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
time grid class
Definition: timegrid.hpp:43
Size size() const
Definition: timegrid.hpp:164
const DefaultType & t
#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.
Real Time
continuous quantity with 1-year units
Definition: types.hpp:62
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
framework for Monte Carlo engines
Definition: any.hpp:35
RiskStatistics Statistics
default statistics tool
Definition: statistics.hpp:35
unsigned QL_BIG_INTEGER BigNatural
large positive integer
Definition: types.hpp:46
GenericPseudoRandom< MersenneTwisterUniformRng, InverseCumulativeNormal > PseudoRandom
default traits for pseudo-random number generation
Definition: rngtraits.hpp:71
STL namespace.