Summary notes on Mark Joshi's C++ Design Patterns and Derivatives Pricing
I was lucky to find this book in my journey to learn C++ programming in finance. This repo is an attempt to collect and share some notes and insights from the book. Please comment if you see any error or misconception. Thank you.
- A simple Monte Carlo model
- Encapsulation
- Inheritence and virtual functions
- Bridge with a virtual Constructor
- Strategies, decoration and statistics
- A random number class
- An exotics engine and the template pattern
- Trees
- Solvers, templates, and implied volatitlites
- The factory
- Design patterns revisited
SimpleMCMain1.cpp
Random1.h
Random1.cpp
Mark Joshi starts with a simple implementation of Monte Carlo simulation to price an option, while the next chapters improves the program step-by-step. The code simulates the underlying price
Example of the payoff for a call option:
In SimpleMCMain1.cpp, there is a function, SimpleMonteCarlo1, which shows one of the first ideas: pre-calculate fixed parts once to save time and computational power. In this particular case, the movedSpot represents part of the formula that remains fixed for all paths of the simulation.
// SimpleMCMain1.cpp
double SimpleMonteCarlo1(
double Expiry,
double Strike,
double Spot,
double Vol,
double r,
unsigned long NumberOfPaths
)
{
double variance = Vol*Vol*Expiry;
...
double itoCorrection = -.5 * variance;
double movedSpot = Spot * exp(r * Expiry + itoCorrection);
...
for (unsigned long i=0; i < NumberOfPaths; i++)
{
...
thisSpot = movedSpot * exp(...);
...
}
...
} PayOff1.h
PayOff1.cpp
SimpleMC.h
SimpleMC.cpp
SimpleMCMain2.cpp
Chapter 2 introduces encapsulation by creating the PayOff class. Another ideas is the overloading of operator() as a const:
// PayOff1.h
class PayOff
{
...
public:
...
enum OptionType {call, put};
PayOff(...);
double operator()(operator spot) const;
private:
double Strike;
OptionType TheOptionType; // enum
...
}which returns the payoff for the spot argument. The const part prevents the class from modifying the state of the object. The ```PayOff`` is now passed by reference to the Monte Carlo function:
// SimpleMC.cpp
double SimpleMonteCarlo2(
const PayOff& thePayOff
double Expiry,
double Spot,
double Vol,
double r,
unsigned long NumberOfPaths
)
{
...
for (unsigned long i=0; i < NumberOfPaths; i++)
{
...
thisSpot = ...;
double thisPayOff = thePayOff(thisSpot);
...
}
...
}Finally, it introduces the open-closed principle, or lack of it in the current PayOff class. The problem is is that if we make any change to this class, for example by adding a digital option as a new option type and make the corresponding changes to the operator() method, then any code that depends on this class needs to be recompiled. This sets the stage for introduction of inheritence and virtual functions in chapter 3.
PayOff2.h
PayOff2.cpp
SimpleMC2.h
SimpleMC2.cpp
SimpleMCMain3.cpp
SimpleMCMain4.cpp
DoubleDigital.h
DoubleDigital.cpp
SimpleMCMain5.cpp
This chapter introduces inheritence, virtual_function (including pure), interface and implementation, and dynamic memory allocation using new keword. It shows these ideas via a parent class PayOff and child classes of PayOffCall, PayOffPut, and PayOffDoubleDigital. A key concept is how to build functions with parameters while we don't know their exact type in advance, i.e. we want to keep it open for extention. Two ideas are shown:
-
Using reference: In
SimpleMCMain3.cpp, a referecne to the child class (e.g.PayOffCall) is passed as argument, even though the function expected a parent class reference. -
Using pointer: In
SimpleMCMain4.cpp, a pointer of type parent class is created (i.e.PayOff* thePayOffPtr) which will be pointed to a child class, e.g.thePayOffPtr = new PayOffCall(Strike). Then this pointer is passed as argument, after derefrencing, i.e.*thePayOffPtr.
The last part is demonestration of extendability of this design by creating a new payoff class, PayOffDoubleDigital. The point is that the codes that depend based on the interface, PayOff, do not need to be recompiled. The new payoff class is used in the SimpleMCMain5.cpp.
4.2. A first solution
Vanilla1.h
Vanilla1.cpp
SimpleMC3.h
SimpleMC3.cpp
VanillaMain1.cpp
The challenge is around VanillaOption, an option object with attribuets like Expiray and a payoff method OptionPayOff(Spot) which relies on a PayOff object. The idea is that if we construct the option object with a reference to the payoff then the option is no longer an independent. One approach is virtual copy constructor, which allows the option to create a new copy of the payoff object at the time of construction.
4.3 Virtual construction
PayOff3.h
PayOff3.cpp
Vanilla2.h
Vanilla2.cpp
SimpleMC4.h
SimpleMC4.cpp
VanillaMain2.cpp
The PayOff3 shows the virtual copy constructor via the clone() method, which remains of type PayOff* in child classes. Now the vanilla option looks like this:
// Vanilla2.cpp
VanillaOption::VanillaOption(const PayOff& ThePayOff_, ...)
...
{
// ThePayOffPtr is a private member of type PayOff*
ThePayOffPtr = ThePayOff_.clone();
}
...Other details leads the discussion to the rule of three, which states that "if any one of destructor, assignment operator, and copy constructor is needed for a class then all three are".
The next part moves the discussion to a desire to have "a pay-off class that has the nice polymorphic features that our class does, whilst taking care of its own memory management". Mark Joshi names some options:
- A wrapper class that has templatized, but "this is really a generic programming rather than object-oriented programming".
- The bridge pattern, which is demonestrated here, by stripping out
ExpiryandGetExpiry()from the vanilla option, and moving them to the newly createdPayOffBridge.
4.5 The bridge
PayOffBridge.h
PayOffBridge.cpp
Vanilla3.h
Vanilla3.cpp
SimpleMC5.h
SimpleMC5.cpp
VanillaMain3.cpp
A related concept here is memory, and the downside of using the new which uses heap vs stack memory. The final part is about using the bridge pattern to create a parameter class.
4.7. A parameter class
Parameters.h
Parameters.cpp
SimpleMC6.h
SimpleMC6.cpp
VanillaMain4.cpp
Via this parameter class, Mark Joshi shows another key idea:
"When implementing a financial models, we never actually need instantaneous value of parameter: it is always the integral or the integral of the square that is important." ~ Mark Joshi
1. A simple Monte Carlo Model
SimpleMCMain1.cpp
Random1.h
Random1.cpp
2. Encapsulation
PayOff1.h
PayOff1.cpp
SimpleMC.h
SimpleMC.cpp
SimpleMCMain2.cpp
3. Inheritence and virtual functions
PayOff2.h
PayOff2.cpp
SimpleMC2.h
SimpleMC2.cpp
SimpleMCMain3.cpp
3.5. Not knowing the type and virtual destruction
SimpleMCMain4.cpp
3.6. Adding extra pay-offs without changing files
DoubleDigital.h
DoubleDigital.cpp
SimpleMCMain5.cpp
4. Bridge with a virtual Constructor
4.2. A first solution
Vanilla1.h
Vanilla1.cpp
SimpleMC3.h
SimpleMC3.cpp
VanillaMain1.cpp
4.3 Virtual construction
PayOff3.h
PayOff3.cpp
Vanilla2.h
Vanilla2.cpp
SimpleMC4.h
SimpleMC4.cpp
VanillaMain2.cpp
4.5 The bridge
PayOffBridge.h
PayOffBridge.cpp
Vanilla3.h
Vanilla3.cpp
SimpleMC5.h
SimpleMC5.cpp
VanillaMain3.cpp
4.7. A parameter class
Parameters.h
Parameters.cpp
SimpleMC6.h
SimpleMC6.cpp
VanillaMain4.cpp
5. Strategies, decoration and statistics
MCStatistics.h
MCStatistics.cpp
SimpleMC7.h
SimpleMC7.cpp
StatsMain1.cpp
5.4. Templates and wrappers
Wrapper.h
Wrapper.cpp
ConvergenceTable.h
ConvergenceTable.cpp
StatsMain2.cpp
6. A random number class
Random2.h
Random2.cpp
ParkMiller.h
ParkMiller.cpp
AntiThetiic.h
AntiThetiic.cpp
SimpleMC8.h
SimpleMC8.cpp
RandomMain3.cpp
7. An exotics engine and the template pattern
PathDependent.h
PathDependent.cpp
ExoticEngine.cpp
ExoticBSEngine.h
ExoticBSEngine.cpp
PathDependentAsian.h
PathDependentAsian.cpp
EquityFXMain.cpp
8. Trees
TreeProducts.h
TreeProducts.cpp
TreeAmerican.h
TreeAmerican.cpp
TreeEuropean.h
TreeEuropean.cpp
BinomialTree.h
BinomialTree.cpp
TreeMain.cpp
PayOffForward.h
PayOffForward.cpp
9. Solvers, templates, and implied volatitlites
BSCallClass.h
BSCallClass.cpp
Bisection.h
Bisection.cpp
SolveMain1.cpp
NewtonRaphson.h
NewtonRaphson.cpp
BSCallTwo.h
BSCallTwo.cpp
SolveMain2.cpp
10. The factory
PayOffFactory.h
PayOffFactory.cpp
PayOffConstructible.h
PayOffConstructible.cpp
PayOffRegisteration.h
PayOffRegisteration.cpp
PayFactoryMain.cpp
11. Design patterns revisited
│
├─ Creational
│ ├─ Virtual copy constructor
│ ├─ The factory
│ ├─ Singleton
│ └─ Monostate
│
├─ Structural
│ ├─ Adapter
│ ├─ Bridge
│ └─ Decorator
│
└─ Behavioural
├─ Strategy
├─ Template
└─ Iterator
