Re: Error: MonteCarloModel

Posted by Kim Kuen Tang on
URL: http://quantlib.414.s1.nabble.com/Error-MonteCarloModel-tp7534p7535.html

Risco Mutelo schrieb:
> "
> Statistics sampleAccumulator;
>
> MonteCarloModel<SingleVariate, PseudoRandom>
>            myModel( myPathGenerator, myPathPricer,
> sampleAccumulator,antitheticVariate);
> "
The problem lies in the second parameter myPathPricer which is of type
    *boost::shared_ptr<MCEuropeanEngine<PseudoRandom,Statistics> >*.

This type doesnt match the signature of constructor from  MonteCarloModel:
    MonteCarloModel(
                  const boost::shared_ptr<path_generator_type>&
pathGenerator,
                  *const boost::shared_ptr<path_pricer_type>*& pathPricer,
                  const stats_type& sampleAccumulator,
                  bool antitheticVariate,
                  const boost::shared_ptr<path_pricer_type>& cvPathPricer
                        = boost::shared_ptr<path_pricer_type>(),
                  result_type cvOptionValue = result_type(),
                  const boost::shared_ptr<path_generator_type>&
cvPathGenerator
                        = boost::shared_ptr<path_generator_type>())

In your case the type *path_pricer_type *equals
*QuantLib::PathPricer<QuantLib::SingleVariate<RNG>::path_type>>. *So you
need first create an object from type
*QuantLib::PathPricer<QuantLib::SingleVariate<RNG>::path_type>> *and
store this object in a shared_ptr. The class *QuantLib::PathPricer *is
used to model the payoff function from your structure ( for example:
call, put, barrier,...). For an example take a look in file
mceuropeanengine.hpp

class EuropeanPathPricer : public PathPricer<Path> {
      public:
        EuropeanPathPricer(Option::Type type,
                           Real strike,
                           DiscountFactor discount);
        Real operator()(const Path& path) const;
      private:
        PlainVanillaPayoff payoff_;
        DiscountFactor discount_;
    };

Best reagrds,
Kim

Here is the complete code:
#include <ql/quantlib.hpp>
#include <boost/timer.hpp>
#include <iostream>
#include <iomanip>

using namespace QuantLib;

int main()
{

    try{
        Calendar calendar = TARGET();
        Date today = Date::todaysDate();
        DayCounter dayCount = Actual365Fixed();
        Rate r_ = 0.05;
        Real s0_ = 0.10;
        Volatility sigma_ = 0.20;
        Size nTimeSteps = 1;
        Time maturity_ = 1.0/12.0;

        Size timeSteps = 1;
        Size timeStepsPerYear = 1000;
        bool antitheticVariate = false;
        bool controlVariate = false;
        Size requiredSamples = 1000;
        Real requiredTolerance = 0.001;
        Size maxSamples = 1000;
        BigNatural seed = 42;

    // calculate Input parameters from the BlackScholesMertonProcess
        Handle<Quote> stateVariable(
                              boost::shared_ptr<Quote>(new
SimpleQuote(s0_)));
        Handle<YieldTermStructure> riskFreeRate(
                              boost::shared_ptr<YieldTermStructure>(
                                          new FlatForward(today, r_,
dayCount)));
        Handle<YieldTermStructure> dividendYield(
                              boost::shared_ptr<YieldTermStructure>(
                                          new FlatForward(today, 0.0,
dayCount)));
        Handle<BlackVolTermStructure> volatility(
                              boost::shared_ptr<BlackVolTermStructure>(
                                   new BlackConstantVol(today, calendar,
sigma_, dayCount)));
    boost::shared_ptr<GeneralizedBlackScholesProcess> diffusion(
                       new BlackScholesMertonProcess(stateVariable,
dividendYield,
                                                     riskFreeRate,
volatility));

        PseudoRandom::rsg_type rsg =
            PseudoRandom::make_sequence_generator(nTimeSteps, 0);

        bool brownianBridge = false;

        typedef SingleVariate<PseudoRandom>::path_generator_type
generator_type;
        boost::shared_ptr<generator_type> myPathGenerator(new
            generator_type(diffusion, maturity_, nTimeSteps,
                           rsg, brownianBridge));

    boost::shared_ptr<MCEuropeanEngine<PseudoRandom,Statistics> >
myeuropeanengine(new MCEuropeanEngine<PseudoRandom,        
Statistics>(diffusion, timeSteps, timeStepsPerYear,
             brownianBridge, antitheticVariate, controlVariate,
             requiredSamples, requiredTolerance, maxSamples, seed));

   
boost::shared_ptr<MCEuropeanEngine<PseudoRandom,Statistics>::path_pricer_type
 >
        myPathPricer(new EuropeanPathPricer(Option::Call,100.0,1.0) );
   
    Statistics sampleAccumulator;

    MonteCarloModel<SingleVariate, PseudoRandom>
           myModel( myPathGenerator, myPathPricer,
sampleAccumulator,antitheticVariate);

    return EXIT_SUCCESS;
    }
    catch(std::exception& e)
    {
        std::cout<<e.what()<<"\n";
        return EXIT_FAILURE;
    }
    catch(...)
    {
        std::cout<<"unknown error \n";
        return EXIT_FAILURE;
    }

return 0;
}

------------------------------------------------------------------------------
Register Now for Creativity and Technology (CaT), June 3rd, NYC. CaT
is a gathering of tech-side developers & brand creativity professionals. Meet
the minds behind Google Creative Lab, Visual Complexity, Processing, &
iPhoneDevCamp asthey present alongside digital heavyweights like Barbarian
Group, R/GA, & Big Spaceship. http://www.creativitycat.com 
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users