Quantitative Finance: New Delhi
Quantitative Finance: New Delhi
Quantitative Finance: New Delhi
New Delhi
One Grook
This Session
Overview of C++
Program syntax
Classes
Pointers
Arrays
Strings
Integrating with your project
Sample program Geometric Brownian Motion (if time)
Software
C++ Overview
created as a successor to C
Program Syntax
Functions
/ methods
Loops
Conditional
statements
Functions
Function
return type
...
// this is a comment
Function name
code
return rc;
}
...
For loop
...
for (int i = 0; i < 100; i++)
{
... do something ...
}
...
Do loop
...
i = 0;
do
{
... do something ...
i++;
} while (i < 100);
If statement
...
if (i == 10)
{
.. do something ..
} else {
.. do something else
}
...
10
Classes
members (properties)
Code members (methods)
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
13
Classes cont
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)
// a1 is not a pointer
int *a2;
// a2 is a pointer
a1 = 10;
a2 = &a1;
*a2 = 5;
// a2 now points to a1
// we dereference a2 to assign a value
...
16
Pointers (cont)
17
Make
18
19
20
21
data
printf(format_string, data1, data2, )
Example:
23
24
For
2.
3.
26
27
28
We want to:
Model
drift, diffusion
Reuse the same object over and over to
generate different paths
29
GBM cont
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);
// update our current time
m_nCurrentTime = nTime;
}
}
33
Code cont
. . .
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
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