26#ifndef quantlib_mc_barrier_engines_hpp
27#define quantlib_mc_barrier_engines_hpp
56 template <
class RNG = PseudoRandom,
class S = Statistics>
68 MCBarrierEngine(ext::shared_ptr<GeneralizedBlackScholesProcess> process,
70 Size timeStepsPerYear,
72 bool antitheticVariate,
74 Real requiredTolerance,
80 QL_REQUIRE(spot > 0.0,
"negative or null underlying given");
86 if (RNG::allowsErrorEstimate)
88 this->
mcModel_->sampleAccumulator().errorEstimate();
96 typename RNG::rsg_type gen =
97 RNG::make_sequence_generator(grid.
size()-1,
seed_);
98 return ext::shared_ptr<path_generator_type>(
102 ext::shared_ptr<path_pricer_type>
pathPricer()
const override;
104 ext::shared_ptr<GeneralizedBlackScholesProcess>
process_;
115 template <
class RNG = PseudoRandom,
class S = Statistics>
130 operator ext::shared_ptr<PricingEngine>()
const;
132 ext::shared_ptr<GeneralizedBlackScholesProcess>
process_;
147 std::vector<DiscountFactor> discounts,
148 ext::shared_ptr<StochasticProcess1D> diffProcess,
170 std::vector<DiscountFactor> discounts);
185 template <
class RNG,
class S>
187 ext::shared_ptr<GeneralizedBlackScholesProcess> process,
189 Size timeStepsPerYear,
191 bool antitheticVariate,
192 Size requiredSamples,
193 Real requiredTolerance,
198 timeSteps_(timeSteps), timeStepsPerYear_(timeStepsPerYear), requiredSamples_(requiredSamples),
199 maxSamples_(maxSamples), requiredTolerance_(requiredTolerance), isBiased_(isBiased),
200 brownianBridge_(brownianBridge), seed_(seed) {
203 "no time steps provided");
206 "both time steps and time steps per year were provided");
208 "timeSteps must be positive, " << timeSteps <<
211 "timeStepsPerYear must be positive, " << timeStepsPerYear <<
216 template <
class RNG,
class S>
219 Time residualTime = process_->time(arguments_.exercise->lastDate());
221 return TimeGrid(residualTime, timeSteps_);
222 }
else if (timeStepsPerYear_ !=
Null<Size>()) {
223 Size steps =
static_cast<Size>(timeStepsPerYear_*residualTime);
224 return TimeGrid(residualTime, std::max<Size>(steps, 1));
226 QL_FAIL(
"time steps not specified");
231 template <
class RNG,
class S>
233 ext::shared_ptr<typename MCBarrierEngine<RNG,S>::path_pricer_type>
235 ext::shared_ptr<PlainVanillaPayoff>
payoff =
236 ext::dynamic_pointer_cast<PlainVanillaPayoff>(arguments_.payoff);
240 std::vector<DiscountFactor> discounts(grid.
size());
242 discounts[i] = process_->riskFreeRate()->discount(grid[i]);
246 return ext::shared_ptr<
249 arguments_.barrierType,
258 return ext::shared_ptr<
261 arguments_.barrierType,
273 template <
class RNG,
class S>
275 ext::shared_ptr<GeneralizedBlackScholesProcess> process)
279 template <
class RNG,
class S>
286 template <
class RNG,
class S>
289 stepsPerYear_ = steps;
293 template <
class RNG,
class S>
296 brownianBridge_ = brownianBridge;
300 template <
class RNG,
class S>
307 template <
class RNG,
class S>
311 "tolerance already set");
316 template <
class RNG,
class S>
320 "number of samples already set");
322 "chosen random generator policy "
323 "does not allow an error estimate");
324 tolerance_ = tolerance;
328 template <
class RNG,
class S>
331 maxSamples_ = samples;
335 template <
class RNG,
class S>
342 template <
class RNG,
class S>
349 template <
class RNG,
class S>
354 "number of steps not given");
356 "number of steps overspecified");
357 return ext::shared_ptr<PricingEngine>(
new
363 samples_, tolerance_,
Barrier option on a single asset.
Barrier-option engine base class
bool triggered(Real underlying) const
Barrier::Type barrierType_
PlainVanillaPayoff payoff_
std::vector< DiscountFactor > discounts_
PseudoRandom::ursg_type sequenceGen_
Real operator()(const Path &path) const override
ext::shared_ptr< StochasticProcess1D > diffProcess_
Barrier::Type barrierType_
PlainVanillaPayoff payoff_
std::vector< DiscountFactor > discounts_
Real operator()(const Path &path) const override
BarrierOption::results results_
Pricing engine for barrier options using Monte Carlo simulation.
McSimulation< SingleVariate, RNG, S >::path_generator_type path_generator_type
ext::shared_ptr< path_generator_type > pathGenerator() const override
ext::shared_ptr< path_pricer_type > pathPricer() const override
void calculate() const override
McSimulation< SingleVariate, RNG, S >::stats_type stats_type
MCBarrierEngine(ext::shared_ptr< GeneralizedBlackScholesProcess > process, Size timeSteps, Size timeStepsPerYear, bool brownianBridge, bool antitheticVariate, Size requiredSamples, Real requiredTolerance, Size maxSamples, bool isBiased, BigNatural seed)
McSimulation< SingleVariate, RNG, S >::path_pricer_type path_pricer_type
ext::shared_ptr< GeneralizedBlackScholesProcess > process_
TimeGrid timeGrid() const override
Monte Carlo barrier-option engine factory.
MakeMCBarrierEngine & withSamples(Size samples)
MakeMCBarrierEngine & withSeed(BigNatural seed)
MakeMCBarrierEngine & withMaxSamples(Size samples)
MakeMCBarrierEngine & withBrownianBridge(bool b=true)
MakeMCBarrierEngine & withBias(bool b=true)
MakeMCBarrierEngine & withSteps(Size steps)
MakeMCBarrierEngine & withStepsPerYear(Size steps)
MakeMCBarrierEngine & withAbsoluteTolerance(Real tolerance)
ext::shared_ptr< GeneralizedBlackScholesProcess > process_
MakeMCBarrierEngine(ext::shared_ptr< GeneralizedBlackScholesProcess >)
MakeMCBarrierEngine & withAntitheticVariate(bool b=true)
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.
std::pair< iterator, bool > registerWith(const ext::shared_ptr< Observable > &)
single-factor random walk
base class for path pricers
Random sequence generator based on a pseudo-random number generator.
#define QL_REQUIRE(condition, message)
throw an error if the given pre-condition is not verified
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Option exercise classes and payoff function.
ext::function< Real(Real)> b
Real Time
continuous quantity with 1-year units
std::size_t Size
size of a container
ext::shared_ptr< QuantLib::Payoff > payoff
framework for Monte Carlo engines
unsigned QL_BIG_INTEGER BigNatural
large positive integer
default Monte Carlo traits for single-variate models