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).
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).
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.
- 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:
- Implement a numerical solution to pricing a European call option, not paying dividends, using
one or more of the methods given in Chapters 18 and 19 of
Option Pricing and following the code models given
there.
Sample code for this assignment will appear here in the next week. We will be reusing this project for extensions such as American options, so write code you
will be willing to look at again.
Please submit physical printouts. Please do not
submit assignments by email.