Chap. 7, pb 5

This forum is to discuss the book "C++ design patterns and derivatives pricing."

Chap. 7, pb 5

Postby akbar » Mon Sep 27, 2010 2:40 pm

What is the best approach for solving Exercise 7.5: "Write a class that pays the difference in pay-offs of two arbitrary path-dependent derivatives?"

The wording seemed to suggest the class should be an path dependent product, hence inheriting from the PathDependent class. This would enable to use a Decorator pattern by inheriting a class which would take two pathdependent options as members and expose a similiar interface. However, such an approach leads to the delicate issue of merging the times and cashflow arrays (so that the decorated class behaves from the outside as another pathdependent object), for which the MJArray class is not necessarily well suited (not even the order in time of the look-at times is checked) and in any case would lead to reallocation of vector space and thus slower code.

Another approach is to work on the engine class, in such a way as to define an engine which takes two pathdependent products and performs the necessary draws, cahflow and statistics for both. But the engine class is defined from the start with a single PathDependent object, so adding an extra one through inheritance looks cumbersome since one product is favored over the other. Rewritting the original engine class means we can't use any already written derived class, like ExoticBSEngine, which again violates the idea of code reuse.

Finally, one could simply write a whole new class and work on a statistics gatherers that would cumulate the difference of the results obtained through two different DoSimulation calls, for each product. This doesn't seem far away from simply working the difference at the output stage, but already appears far from the purpose of the problem...

Is there a better/simpler approach?
By advance, thank you very much for your help.
Last edited by akbar on Tue Sep 28, 2010 9:35 am, edited 1 time in total.
akbar
 
Posts: 28
Joined: Fri Aug 10, 2007 7:12 pm

Re: Chap. 7, pb 5

Postby mj » Tue Sep 28, 2010 12:10 am

Whilst merging the cash-flow times and look-at times would be a pain, it would only need to be done once at construction time so it would not be an efficiency issue.
mj
Site Admin
 
Posts: 1380
Joined: Fri Jul 27, 2007 7:21 am

Re: Chap. 7, pb 5

Postby akbar » Tue Sep 28, 2010 2:43 pm

Once the vectors are merged, there is also the issue of keeping track of the relevant index positions for each pathdependent product, so that the object knows which draws/cashflow correspond to which product. This avoids doing lengthy search through the merged vector at each iteration. One could fill the original look-at time and cashflow vectors of each product with these values, which means they must be kept as well...
Thanks for answering.
akbar
 
Posts: 28
Joined: Fri Aug 10, 2007 7:12 pm

Re: Chap. 7, pb 5

Postby chrissbliss » Tue Dec 14, 2010 5:27 pm

There's also the issue of delivering the maximal number of cash flows for such a "pair product", but this can't reasonably be expected to be "sharp", so there I would just use something like min(length of the merged vector, maxcashflows_prod1+maxcashflows_prod2)...
chrissbliss
 
Posts: 34
Joined: Wed Apr 30, 2008 8:31 pm

Re: Chap. 7, pb 5

Postby chrissbliss » Sun Jan 23, 2011 3:56 pm

Trying to solve it now but can't even get it started...
I have, in DifferencePay.h, the following:

class DifferencePay : public PathDependent
{
public:

DifferencePay(const Wrapper<PathDependent>& Product1_, const Wrapper<PathDependent>& Product2_);

...


and, correspondingly, in DifferencePay.cpp:

DifferencePay:: DifferencePay(const Wrapper<PathDependent>& Product1_, const Wrapper<PathDependent>& Product2_)

...

It tells me "No matching function for call to 'PathDependent::PathDependent()'"

I don't understand this? Help!
chrissbliss
 
Posts: 34
Joined: Wed Apr 30, 2008 8:31 pm

Re: Chap. 7, pb 5

Postby chrissbliss » Sun Jan 23, 2011 5:01 pm

Nevermind, I got it now. Just needed to initialize the LookAtTimes when creating the object.
chrissbliss
 
Posts: 34
Joined: Wed Apr 30, 2008 8:31 pm

Re: Chap. 7, pb 5

Postby emza0114 » Sun Feb 24, 2013 11:48 am

Hi I am also attempting to solve this one.

But I seem to be getting the following error

Code: Select all
PathDependentDifference.cpp: In constructor ‘PathDependentDifference::PathDependentDifference(const Wrapper<PathDependent>&, const Wrapper<PathDependent>&)’:
PathDependentDifference.cpp:17: error: no matching function for call to ‘PayOffBridge::PayOffBridge()’
/Users/Dropbox/Finance/DesignCPP/Chapter7/PayOffBridge.h:17: note: candidates are: PayOffBridge::PayOffBridge(const PayOff&)
/Users/Dropbox/Finance/DesignCPP/Chapter7/PayOffBridge.h:16: note:                 PayOffBridge::PayOffBridge(const PayOffBridge&)
PathDependentDifference.cpp: In member function ‘virtual long unsigned int PathDependentDifference::CashFlows(const MJArray&, std::vector<CashFlow, std::allocator<CashFlow> >&) const’:
PathDependentDifference.cpp:41: error: no matching function for call to ‘PathDependent::CashFlows(const MJArray&, const std::vector<CashFlow, std::allocator<CashFlow> >&) const’
/Users/Dropbox/Finance/DesignCPP/Chapter7/PathDependent.h:38: note: candidates are: virtual long unsigned int PathDependent::CashFlows(const MJArray&, std::vector<CashFlow, std::allocator<CashFlow> >&) const
PathDependentDifference.cpp:42: error: no matching function for call to ‘PathDependent::CashFlows(const MJArray&, const std::vector<CashFlow, std::allocator<CashFlow> >&) const’
/Users/Dropbox/Finance/DesignCPP/Chapter7/PathDependent.h:38: note: candidates are: virtual long unsigned int PathDependent::CashFlows(const MJArray&, std::vector<CashFlow, std::allocator<CashFlow> >&) const
make: *** [PathDependentDifference.o] Error 1


And I can't seem to see exactly what the problem is, any suggestions would be much appreciated I have included my header file and cpp file.
My header file is
Code: Select all
//
//
//                  PathDependentDifference.h
//
//

#ifndef PATH_DEPENDENT_DIFFERENCE_H
#define PATH_DEPENDENT_DIFFERENCE_H

#include <PathDependent.h>
#include <PayOffBridge.h>
#include <wrapper.h>
#include <vector>

class PathDependentDifference : public PathDependent
{
public:

    PathDependentDifference(const Wrapper<PathDependent>& PathDep1_,
                            const Wrapper<PathDependent>& PathDep2_);

    virtual unsigned long MaxNumberOfCashFlows() const;
    virtual MJArray PossibleCashFlowTimes() const;
    virtual unsigned long CashFlows(const MJArray& LogSpotValues,
                                    std::vector<CashFlow>& GeneratedFlows) const;
    virtual ~PathDependentDifference(){}
    virtual PathDependent* clone() const;

private:
   Wrapper<PathDependent> PathDep1;
   Wrapper<PathDependent> PathDep2;
   std::vector<CashFlow> GeneratedFlows1;
   std::vector<CashFlow> GeneratedFlows2;
    double DeliveryTime;
    PayOffBridge ThePayOff;
    unsigned long NumberOfTimes;
};


#endif


and my cpp file is

Code: Select all
//
//
//
//                    PathDependentDifference.cpp
//
//

#include <PathDependentDifference.h>
#include <PathDependent.h>
#include <cmath>
#include <algorithm>
#include <vector>

PathDependentDifference::PathDependentDifference(const Wrapper<PathDependent>& PathDep1_,
                                       const Wrapper<PathDependent>& PathDep2_)
                                       :
                                        PathDependent(PathDep1_->GetLookAtTimes()),
                                        PathDep1(PathDep1_),
                                        PathDep2(PathDep2_)
                                        //NumberOfTimes(PathDep1->GetLookAtTimes().size())
{
   GeneratedFlows1.resize(PathDep1->MaxNumberOfCashFlows());
   GeneratedFlows2.resize(PathDep2->MaxNumberOfCashFlows());
}

unsigned long PathDependentDifference::MaxNumberOfCashFlows() const
{
   return std::max(PathDep1->MaxNumberOfCashFlows(), PathDep2->MaxNumberOfCashFlows());
}

MJArray PathDependentDifference::PossibleCashFlowTimes() const
{
    MJArray tmp(1UL);
    tmp[0] = DeliveryTime;
    return tmp;
}

unsigned long PathDependentDifference::CashFlows(const MJArray& LogSpotValues,
                                    std::vector<CashFlow>& GeneratedFlows) const
{
   PathDep1->CashFlows(LogSpotValues, GeneratedFlows1);
   PathDep2->CashFlows(LogSpotValues, GeneratedFlows2);
   for(int i =0; i<MaxNumberOfCashFlows();i++){
      if(GeneratedFlows1[i].TimeIndex == GeneratedFlows2[i].TimeIndex){
         GeneratedFlows[i].TimeIndex = GeneratedFlows1[i].TimeIndex;
         GeneratedFlows[i].Amount = abs(GeneratedFlows1[i].Amount - GeneratedFlows2[i].Amount);
      }
   }
   
    return 1UL;
}

PathDependent* PathDependentDifference::clone() const
{
    return new PathDependentDifference(*this);
}


thank you very much

Mark
emza0114
 
Posts: 8
Joined: Sun Nov 25, 2012 2:26 am

Re: Chap. 7, pb 5

Postby mj » Mon Feb 25, 2013 9:47 am

you haven't initialized
PayOffBridge ThePayOff;
in the constructor list

so it's looking for a default constructor for the PayOffBridge class and not finding one.
mj
Site Admin
 
Posts: 1380
Joined: Fri Jul 27, 2007 7:21 am

Re: Chap. 7, pb 5

Postby emza0114 » Mon Feb 25, 2013 10:55 am

So I have just had another look and I came up with the following solution which Im not sure is ideal.

Firstly I removed the data member PayOffBridge since I realized that it is not needed to determine the difference each PathDependent object PathDep1 and PathDep2 should know which payoff they have.

The second point I needed to change the two data members
std::vector<CashFlow> GeneratedFlows1
std::vector<CashFlow> GeneratedFlows2
to be mutable since they are passed to PathDep1 and PathDep2 inside a const member function

but I am not sure that this is ideal. Lastly I have the line
if(GeneratedFlows1[i].TimeIndex == GeneratedFlows2[i].TimeIndex) to check that the TimeIndex is the same for the two payoffs before I take there difference but it is not clear whether this is the best way to approach this?

thanks again
Mark
emza0114
 
Posts: 8
Joined: Sun Nov 25, 2012 2:26 am


Return to C++ design patterns and derivatives pricing

Who is online

Users browsing this forum: No registered users and 1 guest

cron