MATH G6071:
Numerical Methods in Finance
Programming Assignments
The sample code found here is provided for educational purposes
only, without warranty of any kind. It was written in a hurry, specifically to be read by novice C
programmers, to illustrate numerical methods in an academic course. Some of
this code is reasonably good, but by the time you've figured out which
code you can rely on, it's your code and you have assumed all
responsibility for its proper use.
My assumption in providing this sample code is that novice programmers will
need to rely heavily on it, while more seasoned programmers will by temperament want
to write their own code, for ease of modification later. Students wishing to flaunt their programming prowess are encouraged to suggest ways in
which this sample code can be improved without sacrificing concision or clarity.
Assignment 1:
- Implement a trustworthy uniform random number generator, based on the
material in
Knuth, or in
Numerical Recipes, Section 7.1
(online in ps or
pdf formats).
Test your generator by using it to uniformly fill N bins, and checking
that the bins fill more or less uniformly.
If you are ambitious, use the chi-squared test
(Knuth, or
Numerical Recipes, Chapter 14) to carry
out more sophisticated tests of your generator.
- Implement a trustworthy normally distributed random number generator, based on the
material in
Knuth, or in
Numerical Recipes, Section 7.2
(online in ps or
pdf formats).
Test your generator by using it to uniformly fill 20 bins, using the 19 values
-1.645, -1.282, -1.037, -0.842, -0.674, -0.524, -0.385, -0.253, -0.126, 0.0,
0.126, 0.253, 0.385, 0.524, 0.674, 0.842, 1.037, 1.282, 1.645
as bin dividers. If you are ambitious, use the Kolmogorov-Smirnov test
(Knuth, or
Numerical Recipes, Chapter 14)
to compare your generator's output
to the actual normal distribution, using
normaldist.h,
normaldist.c from
Assignment 2.
- Use your normally distributed random number generator to generate sample
paths for a security price S following the stochastic differential equation (2.1)
dS/S = s dX + u dt
described in Section 2.1 of Option Pricing.
Find a convenient way to graph your sample paths, such as importing your output
into a spreadsheet with graphing capabilities.
Sample code:
random.h,
random.c,
xrandom.c,
output
implements a uniform random number generator
normal.h,
normal.c,
xnormal.c,
output
implements a normally distributed random number generator
samplepath.c,
output,
graph,
output2,
graph2
implements a security price sample path
Assignment 2:
- Following the method outlined in the Technical Point on pp. 25-26 of
Option Pricing, estimate the parameters
of the stochastic differential equation (2.1)
dS/S = s dX + u dt
for some real-world data. Consider your choice of time period, and whether to take
ordinary or exponentially-decaying averages.
One free source of real-world data is the
NYSE Statistics Archive, at the NYSE web site
under Market Information, Data Library. For example, "NYSE Index Closes 1990 thru 12/31/97"
gives the Composite, Industrial, Transportation, Utility, and Financial Indices for this time
period. To simplify data input and date calculations, the unlabeled file
nya90-97.data is in the format
Day,Composite,Industrial,Transportation,Utility,Financial
where Day is the number of elapsed days since 1/2/90. To avoid issues of how to
handle days when the NYSE was closed, you may only want to use data pairs that differ by one
day.
- Use the Kolmogorov-Smirnov test (Knuth, or
Numerical Recipes, Chapter 14) to measure how well
your real-world data conforms to the log-normal model of equation (2.1). You will need
a function which computes the normal distribution, for comparison purposes.
Partial sample code: