QuantLib: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
mcforwardvanillaengine.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 This file is part of QuantLib, a free-software/open-source library
5 for financial quantitative analysts and developers - http://quantlib.org/
6 QuantLib is free software: you can redistribute it and/or modify it
7 under the terms of the QuantLib license. You should have received a
8 copy of the license along with this program; if not, please email
9 <quantlib-dev@lists.sf.net>. The license is also available online at
10 <http://quantlib.org/license.shtml>.
11 This program is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 FOR A PARTICULAR PURPOSE. See the license for more details.
14*/
15
16/*! \file mcforwardvanillaengine.hpp
17 \brief Monte Carlo engine for forward-starting strike-reset vanilla options
18*/
19
20#ifndef quantlib_mcforwardvanilla_engine_hpp
21#define quantlib_mcforwardvanilla_engine_hpp
22
26#include <utility>
27
28namespace QuantLib {
29
30 //! Monte Carlo engine for forward-starting vanilla options
31 /*! \ingroup forwardengines
32 */
33 template<template <class> class MC,
34 class RNG = PseudoRandom, class S = Statistics>
35 class MCForwardVanillaEngine : public GenericEngine<ForwardOptionArguments<VanillaOption::arguments>,
36 VanillaOption::results>,
37 public McSimulation<MC,RNG,S>
38 {
39 public:
46 // constructor
47 MCForwardVanillaEngine(ext::shared_ptr<StochasticProcess> process,
48 Size timeSteps,
49 Size timeStepsPerYear,
50 bool brownianBridge,
51 bool antitheticVariate,
52 Size requiredSamples,
53 Real requiredTolerance,
54 Size maxSamples,
55 BigNatural seed,
56 bool controlVariate = false);
57 void calculate() const override {
61 this->results_.value = this->mcModel_->sampleAccumulator().mean();
62 if (RNG::allowsErrorEstimate)
64 this->mcModel_->sampleAccumulator().errorEstimate();
65 }
66
67 protected:
68 // McSimulation implementation
69 TimeGrid timeGrid() const override;
70 Real controlVariateValue() const override;
71 ext::shared_ptr<path_generator_type> pathGenerator() const override {
72
73 Size dimensions = process_->factors();
74 TimeGrid grid = this->timeGrid();
75 typename RNG::rsg_type gen =
76 RNG::make_sequence_generator(dimensions*(grid.size()-1),seed_);
77 return ext::shared_ptr<path_generator_type>(
79 gen, brownianBridge_));
80 }
81 // data members
82 ext::shared_ptr<StochasticProcess> process_;
87 };
88
89 template <template <class> class MC, class RNG, class S>
91 ext::shared_ptr<StochasticProcess> process,
92 Size timeSteps,
93 Size timeStepsPerYear,
94 bool brownianBridge,
95 bool antitheticVariate,
96 Size requiredSamples,
97 Real requiredTolerance,
98 Size maxSamples,
99 BigNatural seed,
100 bool controlVariate)
101 : McSimulation<MC, RNG, S>(antitheticVariate, controlVariate), process_(std::move(process)),
102 timeSteps_(timeSteps), timeStepsPerYear_(timeStepsPerYear), requiredSamples_(requiredSamples),
103 maxSamples_(maxSamples), requiredTolerance_(requiredTolerance),
104 brownianBridge_(brownianBridge), seed_(seed) {
105 QL_REQUIRE(timeSteps != Null<Size>() ||
106 timeStepsPerYear != Null<Size>(),
107 "no time steps provided");
108 QL_REQUIRE(timeSteps == Null<Size>() ||
109 timeStepsPerYear == Null<Size>(),
110 "both time steps and time steps per year were provided");
111 QL_REQUIRE(timeSteps != 0,
112 "timeSteps must be positive, " << timeSteps <<
113 " not allowed");
114 QL_REQUIRE(timeStepsPerYear != 0,
115 "timeStepsPerYear must be positive, " << timeStepsPerYear <<
116 " not allowed");
118 }
119
120 template <template <class> class MC, class RNG, class S>
122
123 Date resetDate = arguments_.resetDate;
124 Date lastExerciseDate = arguments_.exercise->lastDate();
125
126 Time t1 = process_->time(resetDate);
127 Time t2 = process_->time(lastExerciseDate);
128
129 Size totalSteps = Null<Size>();
130 if (this->timeSteps_ != Null<Size>()) {
131 totalSteps = timeSteps_;
132 } else if (this->timeStepsPerYear_ != Null<Size>()) {
133 totalSteps = static_cast<Size>(this->timeStepsPerYear_*t2);
134 }
135
136 std::vector<Time> fixingTimes;
137 fixingTimes.push_back(t1);
138 fixingTimes.push_back(t2);
139
140 return TimeGrid(fixingTimes.begin(), fixingTimes.end(), totalSteps);
141 }
142
143 template <template <class> class MC, class RNG, class S>
145
146 ext::shared_ptr<PricingEngine> controlPE =
147 this->controlPricingEngine();
148 QL_REQUIRE(controlPE, "engine does not provide "
149 "control variation pricing engine");
150
151 // Create vanilla option arguments with the same payoff and expiry, but with
152 // strike-reset equal to initial spot*moneyness, price analytically
153 ext::shared_ptr<StrikedTypePayoff> payoff =
154 ext::dynamic_pointer_cast<StrikedTypePayoff>(
155 this->arguments_.payoff);
156 QL_REQUIRE(payoff, "non-plain payoff given");
157
158 Real spot = this->process_->initialValues()[0];
159 Real moneyness = this->arguments_.moneyness;
160 Real strike = moneyness * spot;
161
162 ext::shared_ptr<StrikedTypePayoff> newPayoff(new
163 PlainVanillaPayoff(payoff->optionType(), strike));
164
165 auto* controlArguments = dynamic_cast<VanillaOption::arguments*>(controlPE->getArguments());
166
167 controlArguments->payoff = newPayoff;
168 controlArguments->exercise = this->arguments_.exercise;
169 controlPE->calculate();
170
171 const auto* controlResults =
172 dynamic_cast<const VanillaOption::results*>(controlPE->getResults());
173
174 return controlResults->value;
175 }
176}
177
178
179#endif
Concrete date class.
Definition: date.hpp:125
template base class for option pricing engines
Monte Carlo engine for forward-starting vanilla options.
ext::shared_ptr< StochasticProcess > process_
McSimulation< MC, RNG, S >::path_generator_type path_generator_type
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
MCForwardVanillaEngine(ext::shared_ptr< StochasticProcess > process, Size timeSteps, Size timeStepsPerYear, bool brownianBridge, bool antitheticVariate, Size requiredSamples, Real requiredTolerance, Size maxSamples, BigNatural seed, bool controlVariate=false)
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
basic option arguments
Definition: option.hpp:57
ext::shared_ptr< Payoff > payoff
Definition: option.hpp:64
Plain-vanilla payoff.
Definition: payoffs.hpp:105
time grid class
Definition: timegrid.hpp:43
Size size() const
Definition: timegrid.hpp:164
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Definition: errors.hpp:117
Forward version of a vanilla option.
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
ext::shared_ptr< QuantLib::Payoff > payoff
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.
Vanilla option on a single asset.