Logo Search packages:      
Sourcecode: quantlib version File versions


Go to the documentation of this file.
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

 Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
 Copyright (C) 2003, 2004, 2005, 2006 StatPro Italia srl

 This file is part of QuantLib, a free-software/open-source library
 for financial quantitative analysts and developers - http://quantlib.org/

 QuantLib is free software: you can redistribute it and/or modify it
 under the terms of the QuantLib license.  You should have received a
 copy of the license along with this program; if not, please email
 <quantlib-dev@lists.sf.net>. The license is also available online at

 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.  See the license for more details.

/*! \file mcpricer.hpp
    \brief base class for Monte Carlo pricers

#ifndef quantlib_montecarlo_pricer_hpp
#define quantlib_montecarlo_pricer_hpp

#include <ql/MonteCarlo/montecarlomodel.hpp>

namespace QuantLib {

    //! base class for Monte Carlo pricers
    /*! Eventually this class might be linked to the general tree of
        pricers, in order to have tools like impliedVolatility
        available.  Also, it could, eventually, offer greeks methods.
        Deriving a class from McPricer gives an easy way to write a
        Monte Carlo Pricer.  See McEuropean as example of one factor
        pricer, Basket as example of multi factor pricer.

    template <class MC, class S = Statistics>
00042     class McPricer {
        virtual ~McPricer() {}
        //! add samples until the required tolerance is reached
        Real value(Real tolerance,
                   Size maxSamples = QL_MAX_INTEGER,
                   Size minSamples = 1023) const;
        //! simulate a fixed number of samples
        Real valueWithSamples(Size samples,
                              Size minSamples = 1023) const;
        //! estimated error of the samples simulated so far
        Real errorEstimate() const;
        //! access to the sample accumulator for more statistics
        const S& sampleAccumulator(void) const;
        McPricer() {}
        mutable boost::shared_ptr<MonteCarloModel<MC,S> > mcModel_;

    // inline definitions
    template<class MC, class S>
00064     inline Real McPricer<MC,S>::value(Real tolerance,
                                      Size maxSamples,
                                      Size minSamples) const {

        Size sampleNumber =
        if (sampleNumber<minSamples) {
            sampleNumber = mcModel_->sampleAccumulator().samples();

        Size nextBatch;
        Real order;
        Real result = mcModel_->sampleAccumulator().mean();
        Real accuracy = mcModel_->sampleAccumulator().errorEstimate()/result;
        while (accuracy > tolerance) {
            // conservative estimate of how many samples are needed
            order = accuracy*accuracy/tolerance/tolerance;

            nextBatch =
            // do not exceed maxSamples
            nextBatch = std::min(nextBatch, maxSamples-sampleNumber);
                       "max number of samples exceeded");

            sampleNumber += nextBatch;
            result = mcModel_->sampleAccumulator().mean();
            accuracy = mcModel_->sampleAccumulator().errorEstimate()/

        return result;

    template<class MC, class S>
00103     inline Real McPricer<MC,S>::valueWithSamples(Size samples,
                                                 Size minSamples) const {

                   "number of requested samples (" << samples
                   << ") lower than minSamples (" << minSamples << ")");

        Size sampleNumber =

                   "number of already simulated samples (" << sampleNumber
                   << ") greater than requested samples (" << samples << ")");


        return mcModel_->sampleAccumulator().mean();

    template<class MC, class S>
00124     inline Real McPricer<MC,S>::errorEstimate() const {
        return mcModel_->sampleAccumulator().errorEstimate();

    template<class MC, class S>
00129     inline const S& McPricer<MC,S>::sampleAccumulator() const {
        return mcModel_->sampleAccumulator();



Generated by  Doxygen 1.6.0   Back to index