Dave Klein
Research Analyst Credit Derivatives Research LLC
Two Grooks
Problems worthy of attack, prove their worth by hitting back. -----Shun advice at any price, that's what I call good advice. Piet Hein
2
This Session
Overview of C++
Program syntax Classes Pointers Arrays Strings Integrating with your project Sample program Geometric Brownian Motion (if time)
Software
developers are fond of having religious discussions In the MFE, there is no time
C++ Overview
has 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
is easy to introduce bugs It is often difficult to track them down Tip: build and test your programs incrementally
Program Syntax
Functions Loops
/ methods
Conditional
statements
Hopefully, syntax is not completely new to you. If it is, think about using a more familiar computer language.
7
Functions
... // this is a comment
Function name
int myFirstFunction(int a, int b, double c) { int rc = a + b + c; The functions return rc; } ...
code
For loop
... for (int i = 0; i < 100; i++) { ... do something ... } ...
Do loop
If statement
... if (i == 10) { .. do something .. } else { .. do something else } ... IMPORTANT: Note the double equal signs (==) to test for equality
10
Classes
Classes provide the basic data/code organization construct within C++ Classes are (roughly) comprised of two parts:
Data
if you are not familiar with inheritance, do not try to learn how to use it during the MFE
11
Classes (cont)
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 cont
They are extremely powerful and useful, but now is not the time to play with these.
13
Classes cont
Classic interview question: What is the difference between a class and an object?
Better interview question: Can an object ever be a class?
14
Pointers
Pointers are a special type of variable Pointers hold the address of data, not the data Pointers must be assigned values before they can be used.
15
Pointers (cont)
Pointers are a special type of variable Pointers hold the address of data, not the data
... int a1; int *a2; // a1 is not a pointer // a2 is a pointer
// a2 now points to a1
// we dereference a2 to assign a value // what will this print?
...
16
Pointers (cont)
Be very careful with pointers Someone once estimated that 90% of all C++ bugs can be traced back to bad pointers
17
Make
18
for (int i = 0; i < 10; i++) { // Assign a value to each member of the array // Notice that the array is 'referenced' from 0 to 9 // Arrays in C++ 'start' at 0 myArray[i] = i * i + 1; } ...
19
// first we allocate it myArray = new int[10]; // this is what a for loop looks like for (int i = 0; i < 10; i++) { // Assign a value to each member of the array // Notice that the array is 'reference' from 0 to 9 // Arrays in C++ 'start' at 0 myArray[i] = i * i + 1; }
21
is a string class within the Standard Template Library (STL) Unless you know how to use the STL, ignore it for this term Recommendation: for output, debugging purposes learn how to use printf, sprintf, fprintf The classic way of handling strings is to treat them as arrays of chars. Then use strcpy, strcmp, etc.
22
23
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
For
2. 3.
Recommended : copy the files you need (including nr.h) to your project directory and add the cpp files to your project Build a static library or DLL with all the NR routines in them Copy the code directly from the NR files into your code files
26
27
// let's generate 100 standard normal variables double normals[100]; // seed the random number generator int idum = -time(NULL); for (int i = 0; i < 100; i++) { normals[i] = NR::gasdev(idum); } ...
28
We want to:
Model
drift, diffusion Reuse the same object over and over to generate different paths
29
GBM cont
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 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 cont
. . . 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 cont
. . . void step(double nTime) { double nDeltaT = nTime - m_nCurrentTime; // how much time has elapsed? if (nDeltaT > 0) { // some time has elapsed // add to our diffusion relative to sqrt of elapsed time m_nCurrentDiffusion += sqrt(nDeltaT) * NR::gasdev(m_nIdum);
33
Code cont
. . . double getCurrentValue() { return m_nSInitial * exp(m_nDrift*m_nCurrentTime - .5* m_nSigma * m_nSigma*m_nCurrentTime + m_nSigma*m_nCurrentDiffusion) ); }
34
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