Ejemplo n.º 1
0
inline void McSimulation<MC,RNG,S>::calculate(Real requiredTolerance,
        Size requiredSamples,
        Size maxSamples) const {

    QL_REQUIRE(requiredTolerance != Null<Real>() ||
               requiredSamples != Null<Size>(),
               "neither tolerance nor number of samples set");

    //! Initialize the one-factor Monte Carlo
    if (this->controlVariate_) {

        result_type controlVariateValue = this->controlVariateValue();
        QL_REQUIRE(controlVariateValue != Null<result_type>(),
                   "engine does not provide "
                   "control-variation price");

        boost::shared_ptr<path_pricer_type> controlPP =
            this->controlPathPricer();
        QL_REQUIRE(controlPP,
                   "engine does not provide "
                   "control-variation path pricer");

        boost::shared_ptr<path_generator_type> controlPG =
            this->controlPathGenerator();

        this->mcModel_ =
            boost::shared_ptr<MonteCarloModel<MC,RNG,S> >(
                new MonteCarloModel<MC,RNG,S>(
                    pathGenerator(), this->pathPricer(), stats_type(),
                    this->antitheticVariate_, controlPP,
                    controlVariateValue, controlPG));
    } else {
        this->mcModel_ =
            boost::shared_ptr<MonteCarloModel<MC,RNG,S> >(
                new MonteCarloModel<MC,RNG,S>(
                    pathGenerator(), this->pathPricer(), S(),
                    this->antitheticVariate_));
    }

    if (requiredTolerance != Null<Real>()) {
        if (maxSamples != Null<Size>())
            this->value(requiredTolerance, maxSamples);
        else
            this->value(requiredTolerance);
    } else {
        this->valueWithSamples(requiredSamples);
    }

}
    inline
    void MCLongstaffSchwartzEngine<GenericEngine,MC,RNG,S>::calculate() const {
        pathPricer_ = this->lsmPathPricer();
        this->mcModel_ = boost::shared_ptr<MonteCarloModel<MC,RNG,S> >(
                          new MonteCarloModel<MC,RNG,S>
                              (pathGenerator(), pathPricer_,
                               stats_type(), this->antitheticVariate_));

        this->mcModel_->addSamples(nCalibrationSamples_);
        this->pathPricer_->calibrate();

        McSimulation<MC,RNG,S>::calculate(requiredTolerance_,
                                          requiredSamples_,
                                          maxSamples_);
        this->results_.value = this->mcModel_->sampleAccumulator().mean();
        if (RNG::allowsErrorEstimate) {
            this->results_.errorEstimate =
                this->mcModel_->sampleAccumulator().errorEstimate();
        }
    }