Design Pattern in Option Pricing Part I
Design Pattern in Option Pricing Part I
T
his is the first article in a series of
rewrite of the code! For example, let us take a in the future without having to reedit our code.
three on design patterns for finan-
simple example of a C++ class that models In other words, instead of the usual hard-coded
cial engineering applications. In
options (see Duffy 2003, 2003a). The class header constructors we would like, in some magical
this article we give an introduction
file looks something like: way to define virtual constructors that protect
to what design patterns are, why
client code from the specific code for creating
they are important and how they
class EuropeanOption objects.
are used in financial engineering applications.
{ 2. We would like to define a prototypical object
In the second article we shall discuss design
private: that contains default data. Client code can then
patterns in more detail (using Gamma 1995 as
create objects as clones of this prototype and can
the standard text) and we show how to apply
double r; // Interest rate then change the values of member data as
these so-called Gang-Of-Four (GOF) patterns to a
double sig; // Volatility desired. This is similar in a GUI environment
number of instrument pricing problems (as dis-
double K; // Strike price where we can drag a control object (for example,
cussed in Duffy 2004a). Finally, in the third arti-
double T; // Expiry date a command button) to the working canvas and
cle we shall integrate the class-level GOF patterns
double U; // Current under change the values of those properties that we
with higher-level and larger patterns (as in
lying price wish to be different from their ‘default’ values.
Buschmann 1996, Duffy 2004 and Jackson 2001)
double b; // Cost of carry 3. We wish to create portfolios of options and
in order to design a flexible and maintainable
that portfolios can consist of other sub-portfo-
option calculator program.
string optType; // Option name lios. In other words, we wish to create hierar-
chies of nested objects to any depth. This facility
Motivating design Patterns (call, put)
will allow us unlimited flexibility in the kinds of
In this section we motivate what a design pattern
public: structured products that we can create.
is (in a software development context) and we
// Functions that calculate option 4. Each of the member functions in the public
shall give a short account of how design patterns
price and sensitivities interface of EuropeanOption must be implement-
are being used in financial engineering software
double Price() const; ed by a piece of code that we call an algorithm.
projects. We focus on software systems using an
double Delta() const; For example, we can calculate the price of an
object-oriented language such as C++, Java, C# or
double Gamma() const; option by an exact formula (if we are lucky or
VB.NET because much of the design pattern liter-
double Vega() const; clever), by using the binomial method or by using
ature has been devoted to such languages. The
double Theta() const; a finite difference scheme (see for example, Duffy
examples in the current article will be in C++.
double Rho() const; 2004a). Of course, we could use a hard-coded
Imagine the following example. Let us sup-
}; switch (in this case an enum type in C++) but what
pose that you have written a class in C++ contain-
50 Wilmott magazine
happens if we now wish to use the Finite Element 1995 are grouped into three major categories (in Some examples
Method (FEM) in order to calculate the price? The total there are 23 specific patterns): We cannot describe all 23 design patterns in this
answer is that we would have to change the source ● Creational Patterns: these patterns abstract the introductory article. However, we return to the
code, change the definition of the enum and instantiation process. In general, they help make five problems mentioned earlier and we discuss
recompile: an application independent of how its objects are which patterns resolve these problems.
created, composed and represented. In rough Furthermore, we give a code example in C++ to
enum AlgorithmType {Exact, terms, creational patterns allow us to create “vir- show how one particular pattern is implemented.
Binomial, FDM, FEM}; tual constructor” mechanisms. Problem 1 is implemented by a Factory
● Structural Patterns: these are patterns that Method pattern that defines an interface (usually
This is not our idea of flexible software. The allow the developer to compose classes and in the form of pure virtual member functions)
problem is that the client code (in this case the objects to form larger structures. that you can override to suit your own needs.
EuropeanOption class) knows about each of the
above algorithms and must change when a new
one comes along. It is obvious that no amount of heroics on
5. Finally, what do we do if we wish to output
option price (and possibly its delta and gamma) the part of the software developer will help
to a spreadsheet program such as Excel? A possi-
ble answer would be to write three member func-
tions in EuropeanOption that write output to
resolve these problems unless he or she
Excel. That is indeed possible. The only problem
is that some clients do not need or want Excel
uses some clever design techniques
while others might wish to output to OpenGL,
Oracle, Access, GDI+ … Writing a member func- ● Behavioral Patterns: these are patterns that are There will then be a derived factory class for each
tion for each output choice leads to a class with concerned with algorithms and the assignment specific input option, for example a dialog box.
very many member functions and within no time of responsibilities between objects. In general, Problem 2 is resolved by application of the
becomes completely unmanageable. these patterns describe inter-object communica- Prototype pattern. In other words, you clone your
It is obvious that no amount of heroics on the tion and control flow in systems. own objects from a prototypical object. For exam-
part of the software developer will help resolve ● Each pattern has a name by which we can iden- ple, we have prototypical objects for different
these problems unless he or she uses some clever tify it and each one is documented in Gamma kinds of options.
design techniques. Fortunately, problems 1 to 5 1995 so that it can be applied to your own specif- Problem 3 is resolved by application of the
above have been encountered umpteen times in ic application. Each pattern is thus documented Composite pattern. This pattern is so important
software development projects during the last using the template: that we claim that it is always needed. By using
twenty years and so much experience has been ● The name and intent of the pattern this pattern developers can create complex
built for resolving these problems (in many con- ● A motivational example to show how it works objects such as portfolios, Collateral Debt
texts) that a number of authors have taken the ● A discussion of the applicability of the pattern Obligations (CDO) and other structured prod-
effort to document these design nuggets in hand- in general ucts. For example, using Composite we can create
book (or cookbook) form. Enter Design Patterns ● The classes that are needed in order to imple- a CDO object that contains other CDO objects
(Gamma 1995)! In short, a Design Pattern is a ment the pattern (the so-called structural view) which in their turn contain CDO objects.
description of a problem and its solution. The The interactions between the objects in order to Problem 4 is resolved by the Strategy pattern.
steps in solving the problem are always the same realise the pattern intent (the so-called behav- This pattern is very important in financial engi-
even though the context is different. ioural view) neering for obvious reasons: we need flexible
● The consequences of using the pattern algorithms for pricing options and other instru-
Design Patterns Classifications ● Tips and guidelines on applying the pattern ments. For example, we can define Strategies for
Design Patterns document proven experience in in C++ stochastic interest-rate models (for example Hull-
software development. Our interest is in apply- In short, this pattern documentation gives White) and branching algorithms (CRR, JR) in the
ing them to financial engineering applications the developer a kind of recipe that he or she can binomial method, to mention just a few.
in C++. In particular, we wish to create classes use and apply to financial engineering applica- Problem 5 is resolved by the Visitor pattern.
and objects in C++ that are easy to maintain and tions. This is a wonderful pattern because it allows us to
^
to extend. To this end, the patterns in Gamma extend the functionality of a class without hav-
Wilmott magazine 51
DANIEL DUFFY
<?xml version="1.0"?>
Visitor object is a kind of driver that <EuropeanOption>
encapsulates new functionality for an <StrikePrice>100</StrikePrice>
<InterestRate>0.06</InterestRate>
52 Wilmott magazine