20#ifndef quantlib_mc_longstaff_schwartz_path_engine_hpp
21#define quantlib_mc_longstaff_schwartz_path_engine_hpp
39 template <
class GenericEngine,
template <
class>
class MC,
54 Size timeStepsPerYear,
56 bool antitheticVariate,
59 Real requiredTolerance,
67 virtual ext::shared_ptr<LongstaffSchwartzMultiPathPricer>
84 mutable ext::shared_ptr<LongstaffSchwartzMultiPathPricer>
pathPricer_;
87 template <
class GenericEngine,
template <
class>
class MC,
class RNG,
class S>
89 ext::shared_ptr<StochasticProcess> process,
91 Size timeStepsPerYear,
93 bool antitheticVariate,
96 Real requiredTolerance,
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) {
107 "no time steps provided");
110 "both time steps and time steps per year were provided");
112 "timeSteps must be positive, " << timeSteps <<
115 "timeStepsPerYear must be positive, "
116 << timeStepsPerYear <<
" not allowed");
120 template <
class GenericEngine,
template <
class>
class MC,
123 ext::shared_ptr<
typename
128 QL_REQUIRE(pathPricer_,
"path pricer unknown");
132 template <
class GenericEngine,
template <
class>
class MC,
137 pathPricer_ = this->lsmPathPricer();
138 this->mcModel_ = ext::shared_ptr<MonteCarloModel<MC,RNG,S> >(
140 (pathGenerator(), pathPricer_,
143 this->mcModel_->addSamples(nCalibrationSamples_);
144 this->pathPricer_->calibrate();
149 this->
results_.
value = this->mcModel_->sampleAccumulator().mean();
150 if (RNG::allowsErrorEstimate) {
152 this->mcModel_->sampleAccumulator().errorEstimate();
156 template <
class GenericEngine,
template <
class>
class MC,
161 const std::vector<Date> & fixings = this->arguments_.fixingDates;
162 const Size numberOfFixings = fixings.
size();
164 std::vector<Time> fixingTimes(numberOfFixings);
165 for (
Size i = 0; i < numberOfFixings; ++i) {
167 this->process_->time(fixings[i]);
170 const Size numberOfTimeSteps = timeSteps_ !=
Null<Size>() ? timeSteps_ :
static_cast<Size>(timeStepsPerYear_ * fixingTimes.back());
172 return TimeGrid(fixingTimes.begin(), fixingTimes.end(), numberOfTimeSteps);
175 template <
class GenericEngine,
template <
class>
class MC,
178 ext::shared_ptr<
typename
183 Size dimensions = process_->factors();
185 typename RNG::rsg_type generator =
186 RNG::make_sequence_generator(dimensions*(grid.
size()-1),seed_);
187 return ext::shared_ptr<path_generator_type>(
189 grid, generator, brownianBridge_));
const Instrument::results * results_
template base class for option pricing engines
Longstaff-Schwarz Monte Carlo engine for early exercise options.
ext::shared_ptr< StochasticProcess > process_
const Real requiredTolerance_
McSimulation< MC, RNG, S >::path_generator_type path_generator_type
MC< RNG >::path_type path_type
const bool brownianBridge_
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
const Size requiredSamples_
TimeGrid timeGrid() const
McSimulation< MC, RNG, S >::stats_type stats_type
const Size nCalibrationSamples_
ext::shared_ptr< path_generator_type > pathGenerator() const
ext::shared_ptr< LongstaffSchwartzMultiPathPricer > pathPricer_
const Size timeStepsPerYear_
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.
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
std::size_t Size
size of a container
framework for Monte Carlo engines
RiskStatistics Statistics
default statistics tool
unsigned QL_BIG_INTEGER BigNatural
large positive integer