Problem with Multipath

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem with Multipath

Benjamin Janson
Hi,

I was using Giorgio Pazmandi code (on Feb 10th: Subject: Problem by pricing
quanto asian basket options with MC simulation (multipath)) as a guide line
to implement a basket Option engine (thanks a lot to Giorgio for sharing his
code!).

Once I try to start generating paths the code causes an Exception in
multipathgenerator.hpp in Line:123 (Array asset =
process_->initialValues();). When I use the immediate Window (Visual Studio
2003) even process_->size() wont't work: No overloaded Operator for ->.

The call to Quantlib that triggers the Exception is:
MultiPath path = generator.next().value;
I was expecting to find an Array where path[j]

I also tried the approach in the test suite. with
----- Code -----
typedef MultiPathGenerator<rsg_type>::sample_type sample_type;
for (Size i = 1; i <= nbrSims; i++)
   sample_type sample = generator.next();
----- Code end ----

Which runs into the same Exception. So it is quite evident, that the mistake
is sitting in front of the computer. I probably forgot or instanciated
something wrong. I am using Quantlib 0.3.12, boost 1.33.0 and Visual Studio
2003.

Any help is highly appreciated. Thanks a lot in advance and a happy Easter.

Cheers,

Benjamin


----------- Code -------------
CBasketOption::CBasketOption(int nbrAssets, std::vector<double>
assets_weights,  std::vector<double> vols, Matrix correlation, Real drift,
Real interest) : nbrAssets_(nbrAssets), weights_(assets_weights),
vols_(vols),
        correlation_(correlation), drift_(drift), cc_rate_(interest) {}


bool CBasketOption::start_simulation(Size nbrSims, Real strike,
std::vector<double> u_prices, Date startDate, Date endDate, Option::Type
option, bool calcGreeks)
{
        bool brownianBridge = false;

        // Drift
        Handle<YieldTermStructure> bs_drift(
                boost::shared_ptr<YieldTermStructure>(new FlatForward(startDate, drift_,
Actual360())));

        //No dividends
        Handle<YieldTermStructure> noDivis(
                boost::shared_ptr<YieldTermStructure>(new FlatForward(startDate, 0,
Actual360())));


        // Create individual Stochastic Processes
        std::vector<boost::shared_ptr<StochasticProcess1D> > processes(nbrAssets_);
        for (int i = 0; i < (nbrAssets_ - 1); i++)
        {
                //Get underlying price as Starting Point for Simulation
                Handle<Quote> tempQuote(boost::shared_ptr<Quote>(new
SimpleQuote(u_prices[i])));
                //Get Volatility that goes with the Underlying
                Handle<BlackVolTermStructure>
tempVol(boost::shared_ptr<BlackVolTermStructure>(new
BlackConstantVol(startDate, (Volatility) vols_[i], ActualActual())));
                processes[i] = boost::shared_ptr<StochasticProcess1D> (new
BlackScholesProcess(tempQuote,
                                                noDivis, bs_drift, tempVol));
        }

        boost::shared_ptr<StochasticProcess> process_array =
boost::shared_ptr<StochasticProcess>( new StochasticProcessArray(processes,
correlation_));
        //Build Time Grid for Simualtion
        BigInteger daysDiff = endDate.serialNumber() - startDate.serialNumber();
        TimeGrid time_grid((Time) (daysDiff/365.0), (Size) daysDiff);
        Size nbrSamplingDates = time_grid.size() - 1;

        // Random number Generator
        typedef LowDiscrepancy::rsg_type rsg_type;
        BigNatural seed = (unsigned long)(std::time(0)); //to vary the seed
  rsg_type randomSequenceGenerator =
LowDiscrepancy::make_sequence_generator(
                                                                                ((Size) time_grid.size()-1)*process_array->size(), seed);
       
        //Invoke actual engine
        MultiPathGenerator<rsg_type> generator(process_array, time_grid,
randomSequenceGenerator, brownianBridge);

  Matrix prices(nbrAssets_, (Size) nbrSims);
        typedef MultiPathGenerator<rsg_type>::sample_type sample_type;

        for (Size i = 1; i <= nbrSims; i++)
        {
                sample_type sample = generator.next();
                //MultiPath path = generator.next().value;
        }
        //MultiPathGenerator<rsg_type>::sample_type sample = generator.next();
        return true;
}


Reply | Threaded
Open this post in threaded view
|

Re: Problem with Multipath

Luigi Ballabio
On 04/13/2006 05:03:01 PM, Benjamin Janson wrote:
> Once I try to start generating paths the code causes an Exception in
> multipathgenerator.hpp in Line:123 (Array asset =
> process_->initialValues();).

Benjamin,
        what does the generated exception say? (You can tell by  
enclosing your code in a try/catch block, as in:)

try {
     // run your code here
} catch (exception& e) {
     cout << "Error: " << e.what() << endl;
}

Later,
        Luigi


----------------------------------------

All generalizations are false, including this one.
-- Mark Twain


Reply | Threaded
Open this post in threaded view
|

Re: Problem with Multipath

Benjamin Janson
Luigi,

that was the catch - so to speak. That lead to my mistake (my for loop was
too "short").

Thanks a lot for your help and time.

Take care,

Benjamin

> --- Ursprüngliche Nachricht ---
> Von: Luigi Ballabio <[hidden email]>
> An: Benjamin Janson <[hidden email]>
> Kopie: [hidden email]
> Betreff: Re: [Quantlib-users] Problem with Multipath
> Datum: Thu, 13 Apr 2006 18:08:37 +0200
>
>
> On 04/13/2006 05:03:01 PM, Benjamin Janson wrote:
> > Once I try to start generating paths the code causes an Exception in
> > multipathgenerator.hpp in Line:123 (Array asset =
> > process_->initialValues();).
>
> Benjamin,
> what does the generated exception say? (You can tell by  
> enclosing your code in a try/catch block, as in:)
>
> try {
>      // run your code here
> } catch (exception& e) {
>      cout << "Error: " << e.what() << endl;
> }
>
> Later,
> Luigi
>
>
> ----------------------------------------
>
> All generalizations are false, including this one.
> -- Mark Twain
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by xPML, a groundbreaking scripting
> language
> that extends applications into web and mobile media. Attend the live
> webcast
> and join the prime developer group breaking into this new coding
> territory!