Professional Documents
Culture Documents
C++ For Computational Finance
C++ For Computational Finance
Dave Klein
Research Analyst
Credit Derivatives Research LLC
Two Grooks
Problems worthy of attack,
prove their worth by hitting back.
------
2
This Session
Overview of C++
Program syntax
Classes
Pointers
Arrays
Strings
Using Numerical Recipes
Integrating with your project
Sample program – Geometric Brownian Motion (if time)
Next Session – using C++ to model a derivative
3
Things we won’t cover
Object-oriented Design / Programming
The “right” way to do anything
Software developers are fond of having
“religious” discussions
In the MFE, there is no time
4
C++ Overview
C++ is about 25 years old
Originally created as a successor to C
C was created about 35 years ago as a more generic
assembly language
C++ is a very “big” language
Ithas many, many features
Recommendation: during MFE program, only use
fundamental language features
Unless you are an expert, avoid constructs like templates,
polymorphism, operator overloading, multiple inheritance
5
C++ Overview con’t
C++ is a “dangerous” language
It is easy to introduce bugs
It is often difficult to track them down
Tip: build and test your programs
incrementally
6
Language Features – Program
Syntax
Program Syntax
Functions / methods
Loops
Conditional statements
Hopefully, syntax is not completely new to
you. If it is, think about using a more
familiar computer language.
7
Program Syntax (con’t)
Functions
...
// this is a comment Function name
Function
return type int myFirstFunction(int a, int b, double c)
{
int rc = a + b + c; The function’s code
return rc;
}
...
Return the value
8
Program Syntax (con’t)
For loop
...
for (int i = 0; i < 100; i++)
{
... do something ...
}
...
Do loop ...
i = 0;
do
{
... do something ...
i++;
} while (i < 100);
…
9
Program Syntax (con’t)
If statement
...
if (i == 10) IMPORTANT: Note the
{ double equal signs (==) to
.. do something .. test for equality
} else {
.. do something else
}
...
10
Classes
Classes provide the basic data/code
organization construct within C++
Classes are (roughly) comprised of two parts:
Data members (properties)
Code members (methods)
Class support inheritance – we don’t have time
to cover this
Recommendation – if you are not familiar with
inheritance, do not try to learn how to use it during the
MFE
11
Classes (con’t)
class myFirstClass
{
public:
// Some properties
int integerProperty;
double floatingPointProperty;
char characterArray[254];
// some methods
// a constructor
myFirstClass()
{
integerProperty = 12;
floatingPointProperty = 25.2;
strcpy(characterArray, "yo yo yo");
}
// a destructor
virtual ~myFirstClass()
{
}
void doSomething()
{
... some code would go here ...
}
};
12
Classes con’t
There are other features to classes
including:
Information hiding (public, protected, private)
Virtual functions
13
Classes con’t
Classic interview question: What is the
difference between a class and an object?
14
Pointers
15
Pointers (con’t)
a1 = 10;
a2 = &a1; // a2 now points to a1
17
Memory Allocation / Arrays
18
Memory Allocation / Arrays (con’t)
...
...
19
Memory Allocation / Arrays (con’t)
...
// first we allocate it
myArray = new int[10];
// now we deallocate it
delete[] myArray;
... 20
Memory Allocation / Arrays con’t
Question: when should you dynamically
allocate an array?
21
Strings (or lack thereof)
22
Strings (or lack thereof) – printf()
23
Using Numerical Recipes
There are many numerical libraries available
Numerical Recipes for C++ is easy to use
DO NOT RE-INVENT THE WHEEL
If you do not have NR, search on-line for numerical class
libraries
Do not write your own random-number generator
Do not write your own matrix classes
Do not implement complex numerical algorithms if there are
“canned” routines already available
Exception: if the goal of a homework assignment is to implement
an algorithm.
24
Using Numerical Recipes con’t
Warning: there are “Numerical Recipes”
books for FORTRAN, C, C++, etc.
Each one is slightly different
NR originally implemented in FORTRAN
C & C++ versions different enough from
each other to cause problems
For example, arrays in C version are handled
differently than in C++ version
25
Using Numerical Recipes con’t
Three different ways to add NR to your project
1. Recommended : copy the files you need (including
nr.h) to your project directory and add the cpp files
to your project
2. Build a static library or DLL with all the NR routines
in them
3. Copy the code directly from the NR files into your
code files
26
Using Numerical Recipes con’t
Example: Using an NR random number
generator
Problem: Want standard normal
pseudorandom variable
Solution: use gasdev() from NR
27
Using Numerical Recipes con’t
#include <time.h>
#include "nr.h"
...
...
28
Putting it All Together – A
Geometric Brownian Motion Class
We want to:
Model drift, diffusion
Reuse the same object over and over to
generate different paths
29
GBM con’t
Our class properties
m_nSInitial – the initial security value (constant)
m_nDrift – the drift term (constant)
m_nSigma – our volatility term (constant)
m_nCurrentTime – the current ‘time’ in our simulation
m_nSCurrent – the current security value
Our class methods
CGBMotion - our constructor
void step – moves time forward
double getCurrentValue – returns m_nSCurrent
void reset - resets current time & security value
30
Code
#include <math.h>
#include "nr.h"
class CGBMotion
{
public:
// our properties
int m_nIdum; // used by NR::gasdev
double m_nSInitial; // initial security value (constant)
double m_nDrift; // our drift (constant)
double m_nSigma; // our volatility (constant)
double m_nCurrentTime; // the current elapsed time
double m_nCurrentDiffusion; // how much the process has diffused
31
Code con’t
. . .
public:
// our constructor
CGBMotion(double nSInitial, double nDrift, double nSigma, int seed)
{
m_nSInitial = nSInitial;
m_nDrift = nDrift;
m_nSigma = nSigma;
m_nCurrentTime = 0;
m_nCurrentDiffusion = 0;
m_nIdum = seed;
}
32
Code con’t
. . .
33
Code con’t
. . .
double getCurrentValue()
{
return m_nSInitial * exp(m_nDrift*m_nCurrentTime
- .5* m_nSigma * m_nSigma*m_nCurrentTime
+ m_nSigma*m_nCurrentDiffusion)
);
}
double reset()
{
m_nCurrentTime = 0;
m_nCurrentDiffusion = 0;
}
};
34
GBM Sample Program
int main(int argc, char* argv[])
{
CGBMotion oGBM(100.0, .05, .2, -10); // our brownian motion object
return 0;
} 35
3 Great Resources
Wikipedia: http://www.wikipedia.org
Wilmott: http://www.wilmott.com
Google (of course) :
http://www.google.com
36
Questions / Discussion
37