27#ifndef quantlib_mc_american_engine_hpp
28#define quantlib_mc_american_engine_hpp
51 class RNG_Calibration = RNG>
54 SingleVariate,RNG,S,RNG_Calibration> {
56 MCAmericanEngine(
const ext::shared_ptr<GeneralizedBlackScholesProcess>& process,
58 Size timeStepsPerYear,
59 bool antitheticVariate,
62 Real requiredTolerance,
68 const ext::optional<bool>& antitheticVariateCalibration =
ext::nullopt,
74 ext::shared_ptr<LongstaffSchwartzPathPricer<Path> >
lsmPathPricer()
const override;
107 class RNG_Calibration = RNG>
127 operator ext::shared_ptr<PricingEngine>()
const;
129 ext::shared_ptr<GeneralizedBlackScholesProcess>
process_;
141 template <
class RNG,
class S,
class RNG_Calibration>
143 const ext::shared_ptr<GeneralizedBlackScholesProcess>& process,
145 Size timeStepsPerYear,
146 bool antitheticVariate,
148 Size requiredSamples,
149 Real requiredTolerance,
152 Size polynomialOrder,
154 Size nCalibrationSamples,
155 const ext::optional<bool>& antitheticVariateCalibration,
170 antitheticVariateCalibration,
172 polynomialOrder_(polynomialOrder), polynomialType_(polynomialType) {}
174 template <
class RNG,
class S,
class RNG_Calibration>
177 RNG_Calibration>::calculate();
178 if (this->controlVariate_) {
185 template <
class RNG,
class S,
class RNG_Calibration>
186 inline ext::shared_ptr<LongstaffSchwartzPathPricer<Path> >
188 ext::shared_ptr<GeneralizedBlackScholesProcess> process =
189 ext::dynamic_pointer_cast<GeneralizedBlackScholesProcess>(
191 QL_REQUIRE(process,
"generalized Black-Scholes process required");
193 ext::shared_ptr<EarlyExercise> exercise =
194 ext::dynamic_pointer_cast<EarlyExercise>(
195 this->arguments_.exercise);
198 "payoff at expiry not handled");
200 ext::shared_ptr<AmericanPathPricer> earlyExercisePathPricer(
202 polynomialOrder_, polynomialType_));
204 return ext::make_shared<LongstaffSchwartzPathPricer<Path> > (
207 earlyExercisePathPricer,
208 *(process->riskFreeRate()));
211 template <
class RNG,
class S,
class RNG_Calibration>
212 inline ext::shared_ptr<PathPricer<Path> >
214 ext::shared_ptr<StrikedTypePayoff>
payoff =
215 ext::dynamic_pointer_cast<StrikedTypePayoff>(
216 this->arguments_.payoff);
219 ext::shared_ptr<GeneralizedBlackScholesProcess> process =
220 ext::dynamic_pointer_cast<GeneralizedBlackScholesProcess>(
222 QL_REQUIRE(process,
"generalized Black-Scholes process required");
224 return ext::shared_ptr<PathPricer<Path> >(
228 process->riskFreeRate()->discount(this->timeGrid().back()))
232 template <
class RNG,
class S,
class RNG_Calibration>
233 inline ext::shared_ptr<PricingEngine>
235 ext::shared_ptr<GeneralizedBlackScholesProcess> process =
236 ext::dynamic_pointer_cast<GeneralizedBlackScholesProcess>(
238 QL_REQUIRE(process,
"generalized Black-Scholes process required");
240 return ext::shared_ptr<PricingEngine>(
244 template <
class RNG,
class S,
class RNG_Calibration>
247 ext::shared_ptr<PricingEngine> controlPE =
248 this->controlPricingEngine();
251 "engine does not provide "
252 "control variation pricing engine");
255 *controlArguments = this->arguments_;
256 controlArguments->
exercise = ext::shared_ptr<Exercise>(
259 controlPE->calculate();
261 const auto* controlResults =
264 return controlResults->
value;
267 template <
class RNG,
class S,
class RNG_Calibration>
269 ext::shared_ptr<GeneralizedBlackScholesProcess> process)
272 antitheticCalibration_(ext::nullopt), seedCalibration_(
Null<
Size>()) {}
274 template <
class RNG,
class S,
class RNG_Calibration>
277 polynomialOrder_ = polynomialOrder;
281 template <
class RNG,
class S,
class RNG_Calibration>
284 polynomialType_ = polynomialType;
288 template <
class RNG,
class S,
class RNG_Calibration>
295 template <
class RNG,
class S,
class RNG_Calibration>
299 stepsPerYear_ = steps;
303 template <
class RNG,
class S,
class RNG_Calibration>
307 "tolerance already set");
312 template <
class RNG,
class S,
class RNG_Calibration>
317 "number of samples already set");
319 "chosen random generator policy "
320 "does not allow an error estimate");
321 tolerance_ = tolerance;
325 template <
class RNG,
class S,
class RNG_Calibration>
329 maxSamples_ = samples;
333 template <
class RNG,
class S,
class RNG_Calibration>
337 calibrationSamples_ = samples;
341 template <
class RNG,
class S,
class RNG_Calibration>
348 template <
class RNG,
class S,
class RNG_Calibration>
356 template <
class RNG,
class S,
class RNG_Calibration>
363 template <
class RNG,
class S,
class RNG_Calibration>
365 RNG,
S, RNG_Calibration>::withAntitheticVariateCalibration(
bool b) {
366 antitheticCalibration_ =
b;
370 template <
class RNG,
class S,
class RNG_Calibration>
374 seedCalibration_ = seed;
378 template <
class RNG,
class S,
class RNG_Calibration>
380 operator ext::shared_ptr<PricingEngine>()
const {
382 "number of steps not given");
384 "number of steps overspecified");
385 return ext::shared_ptr<PricingEngine>(
new
391 samples_, tolerance_,
397 antitheticCalibration_,
Analytic European engine.
const Instrument::results * results_
std::vector< ext::function< Real(Real)> > basisSystem() const override
Real state(const Path &path, Size t) const override
Real operator()(const Path &path, Size t) const override
std::vector< ext::function< Real(Real)> > v_
const ext::shared_ptr< Payoff > payoff_
Pricing engine for European vanilla options using analytical formulae.
base class for early exercise path pricers
American Monte Carlo engine.
const LsmBasisSystem::PolynomialType polynomialType_
ext::shared_ptr< LongstaffSchwartzPathPricer< Path > > lsmPathPricer() const override
const Size polynomialOrder_
void calculate() const override
MCAmericanEngine(const ext::shared_ptr< GeneralizedBlackScholesProcess > &process, Size timeSteps, Size timeStepsPerYear, bool antitheticVariate, bool controlVariate, Size requiredSamples, Real requiredTolerance, Size maxSamples, BigNatural seed, Size polynomialOrder, LsmBasisSystem::PolynomialType polynomialType, Size nCalibrationSamples=Null< Size >(), const ext::optional< bool > &antitheticVariateCalibration=ext::nullopt, BigNatural seedCalibration=Null< Size >())
ext::shared_ptr< PricingEngine > controlPricingEngine() const override
Real controlVariateValue() const override
ext::shared_ptr< PathPricer< Path > > controlPathPricer() const override
Longstaff-Schwarz Monte Carlo engine for early exercise options.
Monte Carlo American engine factory.
MakeMCAmericanEngine & withBasisSystem(LsmBasisSystem::PolynomialType)
MakeMCAmericanEngine & withSeedCalibration(BigNatural seed)
MakeMCAmericanEngine & withAntitheticVariateCalibration(bool b=true)
MakeMCAmericanEngine & withStepsPerYear(Size steps)
MakeMCAmericanEngine & withSamples(Size samples)
ext::optional< bool > antitheticCalibration_
MakeMCAmericanEngine & withPolynomialOrder(Size polynomialOrder)
MakeMCAmericanEngine & withCalibrationSamples(Size calibrationSamples)
MakeMCAmericanEngine & withControlVariate(bool b=true)
LsmBasisSystem::PolynomialType polynomialType_
MakeMCAmericanEngine & withAbsoluteTolerance(Real tolerance)
MakeMCAmericanEngine & withMaxSamples(Size samples)
MakeMCAmericanEngine & withSeed(BigNatural seed)
BigNatural seedCalibration_
ext::shared_ptr< GeneralizedBlackScholesProcess > process_
MakeMCAmericanEngine & withAntitheticVariate(bool b=true)
MakeMCAmericanEngine & withSteps(Size steps)
MakeMCAmericanEngine(ext::shared_ptr< GeneralizedBlackScholesProcess >)
template class providing a null value for a given type.
ext::shared_ptr< Exercise > exercise
single-factor random walk
Vanilla option (no discrete dividends, no barriers) on a single asset.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
Option exercise classes and payoff function.
ext::function< Real(Real)> b
std::size_t Size
size of a container
ext::shared_ptr< QuantLib::Payoff > payoff
utility classes for Longstaff-Schwartz early-exercise Monte Carlo
Monte Carlo European option engine.
Longstaff Schwartz Monte Carlo engine for early exercise options.
const boost::none_t & nullopt
RiskStatistics Statistics
default statistics tool
unsigned QL_BIG_INTEGER BigNatural
large positive integer
GenericPseudoRandom< MersenneTwisterUniformRng, InverseCumulativeNormal > PseudoRandom
default traits for pseudo-random number generation
Maps optional to either the boost or std implementation.
Global definitions and compiler switches.
default Monte Carlo traits for single-variate models