QuantLib: a free/open-source library for quantitative finance
Fully annotated sources - version 1.32
Loading...
Searching...
No Matches
mclongstaffschwartzpathengine.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3/*
4 Copyright (C) 2009 Andrea Odetti
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#ifndef quantlib_mc_longstaff_schwartz_path_engine_hpp
21#define quantlib_mc_longstaff_schwartz_path_engine_hpp
22
23#include <ql/experimental/mcbasket/longstaffschwartzmultipathpricer.hpp>
24#include <ql/pricingengines/mcsimulation.hpp>
25#include <utility>
26
27namespace QuantLib {
28
30
39 template <class GenericEngine, template <class> class MC,
40 class RNG, class S = Statistics>
42 public McSimulation<MC,RNG,S> {
43 public:
44 typedef typename MC<RNG>::path_type path_type;
51
52 MCLongstaffSchwartzPathEngine(ext::shared_ptr<StochasticProcess> process,
53 Size timeSteps,
54 Size timeStepsPerYear,
55 bool brownianBridge,
56 bool antitheticVariate,
57 bool controlVariate,
58 Size requiredSamples,
59 Real requiredTolerance,
60 Size maxSamples,
61 BigNatural seed,
62 Size nCalibrationSamples = Null<Size>());
63
64 void calculate() const;
65
66 protected:
67 virtual ext::shared_ptr<LongstaffSchwartzMultiPathPricer>
68 lsmPathPricer() const = 0;
69
71 ext::shared_ptr<path_pricer_type> pathPricer() const;
72 ext::shared_ptr<path_generator_type> pathGenerator() const;
73
74 ext::shared_ptr<StochasticProcess> process_;
77 const bool brownianBridge_;
81 const Size seed_;
83
84 mutable ext::shared_ptr<LongstaffSchwartzMultiPathPricer> pathPricer_;
85 };
86
87 template <class GenericEngine, template <class> class MC, class RNG, class S>
89 ext::shared_ptr<StochasticProcess> process,
90 Size timeSteps,
91 Size timeStepsPerYear,
92 bool brownianBridge,
93 bool antitheticVariate,
94 bool controlVariate,
95 Size requiredSamples,
96 Real requiredTolerance,
97 Size maxSamples,
98 BigNatural seed,
99 Size nCalibrationSamples)
100 : McSimulation<MC, RNG, S>(antitheticVariate, controlVariate), process_(std::move(process)),
101 timeSteps_(timeSteps), timeStepsPerYear_(timeStepsPerYear), brownianBridge_(brownianBridge),
102 requiredSamples_(requiredSamples), requiredTolerance_(requiredTolerance),
103 maxSamples_(maxSamples), seed_(seed),
104 nCalibrationSamples_((nCalibrationSamples == Null<Size>()) ? 2048 : nCalibrationSamples) {
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, "
116 << timeStepsPerYear << " not allowed");
117 this->registerWith(process_);
118 }
119
120 template <class GenericEngine, template <class> class MC,
121 class RNG, class S>
122 inline
123 ext::shared_ptr<typename
126 const {
127
128 QL_REQUIRE(pathPricer_, "path pricer unknown");
129 return pathPricer_;
130 }
131
132 template <class GenericEngine, template <class> class MC,
133 class RNG, class S>
134 inline
136 const {
137 pathPricer_ = this->lsmPathPricer();
138 this->mcModel_ = ext::shared_ptr<MonteCarloModel<MC,RNG,S> >(
140 (pathGenerator(), pathPricer_,
141 stats_type(), this->antitheticVariate_));
142
143 this->mcModel_->addSamples(nCalibrationSamples_);
144 this->pathPricer_->calibrate();
145
146 McSimulation<MC,RNG,S>::calculate(requiredTolerance_,
147 requiredSamples_,
148 maxSamples_);
149 this->results_.value = this->mcModel_->sampleAccumulator().mean();
150 if (RNG::allowsErrorEstimate) {
151 this->results_.errorEstimate =
152 this->mcModel_->sampleAccumulator().errorEstimate();
153 }
154 }
155
156 template <class GenericEngine, template <class> class MC,
157 class RNG, class S>
158 inline
160 const {
161 const std::vector<Date> & fixings = this->arguments_.fixingDates;
162 const Size numberOfFixings = fixings.size();
163
164 std::vector<Time> fixingTimes(numberOfFixings);
165 for (Size i = 0; i < numberOfFixings; ++i) {
166 fixingTimes[i] =
167 this->process_->time(fixings[i]);
168 }
169
170 const Size numberOfTimeSteps = timeSteps_ != Null<Size>() ? timeSteps_ : static_cast<Size>(timeStepsPerYear_ * fixingTimes.back());
171
172 return TimeGrid(fixingTimes.begin(), fixingTimes.end(), numberOfTimeSteps);
173 }
174
175 template <class GenericEngine, template <class> class MC,
176 class RNG, class S>
177 inline
178 ext::shared_ptr<typename
181 const {
182
183 Size dimensions = process_->factors();
184 TimeGrid grid = this->timeGrid();
185 typename RNG::rsg_type generator =
186 RNG::make_sequence_generator(dimensions*(grid.size()-1),seed_);
187 return ext::shared_ptr<path_generator_type>(
188 new path_generator_type(process_,
189 grid, generator, brownianBridge_));
190 }
191}
192
193
194#endif
template base class for option pricing engines
Longstaff-Schwarz Monte Carlo engine for early exercise options.
ext::shared_ptr< StochasticProcess > process_
McSimulation< MC, RNG, S >::path_generator_type path_generator_type
MCLongstaffSchwartzPathEngine(ext::shared_ptr< StochasticProcess > process, Size timeSteps, Size timeStepsPerYear, bool brownianBridge, bool antitheticVariate, bool controlVariate, Size requiredSamples, Real requiredTolerance, Size maxSamples, BigNatural seed, Size nCalibrationSamples=Null< Size >())
McSimulation< MC, RNG, S >::path_pricer_type path_pricer_type
McSimulation< MC, RNG, S >::stats_type stats_type
ext::shared_ptr< path_generator_type > pathGenerator() const
ext::shared_ptr< LongstaffSchwartzMultiPathPricer > pathPricer_
virtual ext::shared_ptr< LongstaffSchwartzMultiPathPricer > lsmPathPricer() const =0
ext::shared_ptr< path_pricer_type > pathPricer() const
base class for Monte Carlo engines
MonteCarloModel< MC, RNG, S >::path_generator_type path_generator_type
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
General-purpose Monte Carlo model for path samples.
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
QL_REAL Real
real number
Definition: types.hpp:50
std::size_t Size
size of a container
Definition: types.hpp:58
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
STL namespace.