# Multi-Period Binomial Options

Here is my latest program for calculating the prices of options using a multi-period binomial tree. It works, but represents a bunch of strange methods to solve unexpected problems.
First, I decided to calculate the price at each node by finding the price at the terminal nodes, then use a formula, rather than calculate the prices recursively. This technique, as you will see, led to the problem of calculating binomial coefficients.

The thing that I am most satisfied with is that I was able to get the program to output to a text file, which can be opened in Excel to visualize the data.  The only thing that the program does not do is to do the same calculation with the Black-Scholes formula, and visualize how the binomial calculation approaches the Black-Scholes result.

First, take a look at my output:

We can see how the results approach a limit with each iteration.  The pricing for periods 34 and 35 is starting to get weird because of limits of integer sizes and floating point number sizes.

Here is a chart of the above data:

Finally, here is the code:

#include <cstdlib>
#include <iostream>
#include <cmath>
#include <iomanip>
#include <fstream>

//Calculates price of European call or put option, and portfolio required to duplicate option, using binomial tree

int nchoosek(int n,int k);
//choice holds the binomial tree so that it doesn’t need to be recalculated
unsigned long long choice [50][50]={};

int main() {
using namespace std;
float S0, r,div, K, h,t,sigma, u, d,pStar, uS,dS,Cu,Cd,delta, bondValue;
double optionPrice;
double prices [50];
int n;
string callPut;
int putTrue;
//fill in nchoosek tree
for (int m=1; m<50;m++)
{ choice [m][0]=1;
choice [m][m]=1;}
for (int m=2;m<(50);m++)
{ for (int l=1;l<m;l++){
choice[m][l]=choice[m-1][l-1]+choice[m-1][l];
}
}
//get data from user
cout<<“Calculate the price of a European call or put, using a one period binary tree, then find rate with n period tree.”<<endl;
cout<<“Current Stock Price?”;
cin>>S0;
cout<<“Continuously Compounded Risk Free Rate?”;
cin>>r;
cout<<“Continuous Dividend Rate?”;
cin>>div;
cout<<“Volatility?”;
cin>>sigma;
cout<<“Strike Price?”;
cin>>K;
cout<<“Time to Expiration?”;
cin>>t;
cout<<“Number of Periods? (less than 50 please)”;
cin>>n;
cout<<“Call or Put?”;
cin>>callPut;
if ((callPut == “put”)||(callPut==”p”))
putTrue=-1;
else putTrue=1;
//calculate likely upward and downward motions
u=exp((r-div)*t+sigma*sqrt(t));
d=exp((r-div)*t-sigma*sqrt(t));
uS=u*S0;
dS=d*S0;
cout<<“Upward Move “<<uS<<endl;
cout<<“Downward Move “<<dS<<endl;
//calculate payoff of call, given motions above
//the variable putTrue turns Us-K into K-Us.  Cool.
Cu=fmax(0, putTrue*(uS-K));
Cd=fmax(0, putTrue*(dS-K));
//calculate Delta, Beta, and call option price
delta=exp(-div * t)*(Cu-Cd)/(S0*(u-d));
bondValue=exp(-r * t)*((u*Cd)-(d*Cu))/(u-d);
optionPrice=delta*S0+bondValue;
cout<<“To duplicate the option, buy “<<delta<<” shares=”” of=”” the=”” stock=”” for=”” “<<delta*s0<<“=”” dollars=”” and=”” borrow=”” “<<-1=”” *=”” bondvalue<<“=”” at=”” rate=”” “<<r<<“=”” a=”” total=”” price=”” “<<optionprice<<<span=”” class=”hiddenSpellError” pre=”” data-mce-bogus=”1″>endl;
cout<<“Price of Option “<<optionPrice<<endl;
cout<<“Prices above 30 levels deep may accumulate errors wildly”<<endl;

//Multi Period tree
int j=1;
while(j<(n+1)){
h=t/j;
u=exp((r-div)*h+sigma*sqrt(h));
d=exp((r-div)*h-sigma*sqrt(h));
pStar=(exp((r-div)*h)-d)/(u-d);
optionPrice=0;

for (int m=0; m<(j+1); m++) {
//This is the big doozy.
//Instead of computing the price at each interior node, only the terminal nodes are calculated
//and the price is found using the binomial theorem
optionPrice=optionPrice+nchoosek(j,m)*pow(pStar,j-m)*pow((1-pStar),m)*fmax(0,(putTrue* ((S0*pow(u, j-m)*pow(d,m))-K)));
}
optionPrice=optionPrice*exp(-r*t);
prices [j]=optionPrice;
cout<<“Periods: “<<j<<”  “;
cout<<“Option Price:  “<<optionPrice<<endl;
++j;  }

//output prices to text file
//open the text file in excel
//data is delimited by commas
ofstream outf(“Results.txt”);
if (!outf)
{  cerr << “File Error”<<endl;
exit(1); }
outf<<“Depth of Tree”<<“,”<<“Price”<<endl;
j=1;
while(j<(n+1)){
outf << j <<“,”<< prices[j]<< endl;
++j;  }
return 0;}

int nchoosek(int n, int k)
{ if (n>49 ||k>49)return 0;
else { return choice[n][k]; }}

The heart of everything is this ugly beast:

optionPrice=optionPrice+nchoosek(j,m)*pow(pStar,j-m)*pow((1-pStar),m)*fmax(0,(putTrue* ((S0*pow(u, j-m)*pow(d,m))-K)));

This is better put as $C=C+\binom j m p^{*^{j-m}}(1-p*)^m*max(0,put(S_ou^{j-m}d^m-K)$

You can figure out why this works fairly easily.  I like the bit where I multiply (F_{0,T}-K) by -1 to get (K-F_{0,T}.

More than anything, this exercise shows how errors creep into these long calculations.  Looking at the graph, you can see how our numbers are already wandering away from the correct numbers after the 20th iteration.

# A Simple C++ Program for Computing Binomial Option Prices

I have been advised that C++ and VBA are commonly requested languages for actuaries. So, while I am studying for exam MFE, I have been writing simple programs.  My native language is LISP.  I like its beauty, and its adaptability.  I am sure that C++ has its promoters, but I think that it is a shame that such an ugly programming language has become one of the most used.

Of course, I am still a novice to the language, so my code is still extra ugly.

Computes call price using binomial model:

#include <cstdlib>
#include <iostream>
#include <cmath>
#include <iomanip>

//Calculates price of call option, and portfolio required to duplicate option, using binomial tree

int main() {
using namespace std;
float S0, r,div, K, h, sigma, u, d, uS,dS,Cu,Cd,delta, bondValue, callPrice;
//get data
cout<<“Calculate the price of a call, using a one period binary tree”<<endl;
cout<<“Current Stock Price?”;
cin>>S0;
cout<<“Continuously Compounded Risk Free Rate?”;
cin>>r;
cout<<“Continuous Dividend Rate?”;
cin>>div;
cout<<“Strike Price?”;
cin>>K;
cout<<“Time to Expiration?”;
cin>>h;
cout<<“Volatility?”;
cin>>sigma;
//calculate likely upward and downward motions
u=exp((r-div)*h+sigma*sqrt(h));
d=exp((r-div)*h-sigma*sqrt(h));
uS=u*S0;
dS=d*S0;
cout<<“Upward Move “<<uS<<endl;
cout<<“Downward Move “<<dS<<endl;
//calculate payoff of call, given motions above
if (uS > K)
Cu = uS-K;
else Cu = 0;
if (dS > K)
Cd = dS-K;
else Cd = 0;
//calculate Delta, Beta, and call option price
delta=exp(-div * h)*(Cu-Cd)/(S0*(u-d));
bondValue=exp(-r * h)*((u*Cd)-(d*Cu))/(u-d);
callPrice=delta*S0+bondValue;
cout<<“To duplicate the call, buy “<<delta<<” shares=”” of=”” the=”” stock=”” for=”” “<<delta*s0<<“=”” dollars=”” and=”” borrow=”” “<<-1=”” *=”” bondvalue<<“=”” at=”” rate=”” “<<r<<“=”” a=”” total=”” price=”” “<<callprice<<<span=”” class=”hiddenSpellError” pre=”” data-mce-bogus=”1″>endl;
cout<<“Price of Call “<<callPrice<<endl;

//find the price of some other related calls
cout<<“Some other Call prices: “<<endl;
float step;
step = 0.05*K;
K= K-5*step;
for (int z=0;z<11;z++){
if (uS > K)
Cu = uS-K;
else Cu = 0;
if (dS > K)
Cd = dS-K;
else Cd = 0;

delta=exp(-div * h)*(Cu-Cd)/(S0*(u-d));
bondValue=exp(-r * h)*((u*Cd)-(d*Cu))/(u-d);
callPrice=delta*S0+bondValue;

cout<<setw(8)<<k<<” “<<callprice<<<span=”” class=”hiddenSpellError” pre=”” data-mce-bogus=”1″>endl;
K=K+step;
}
return 0;
}
//Wow what an ugly program

Here is a sample run:

# New Study Materials for Exam FM

You may have noticed that I have been adding some study materials to my blog, as a side project. If you click on the “Easy FM Quizes” tab, you will find some interactive quiz problems. These are dumped over from my easy practice problems I made when I was studying for the exam. I have more than 1000 more exercises that I will transfer over in the coming weeks.
I am even more excited about the exercises that I am putting into nice PDF problem sets.  If you have heard me talk about math, you know that I learned algebra and calculus from early 20th century text books.  I have a sentimental attachment to the problem sets at the ends of chapters.  It has given me great satisfaction to write at simple LaTeX class that duplicates the feel of these textbook problems.  When I am done, they will be a single document.
The links for these problem sets will be posted on the right side bar.

Enjoy!

# JobSearch Database: Hacked

Work season is slowing.  Hence, it is time for us to resume our search for a new job.  When last we visited this project, we were building a database application which would streamline the job search process.  At this point, however, we have better things to do (for example, study for our next exam) than to build a theoretically correct database.  And yet, we still have need of a way of making our search for employment more efficient.  What we need is a workflow.

After digging through our computer for a utility for the task, we settle on FreeMind.  It is intuitive, adaptable, dependable, and we have used it in the past.

The best way to illustrate this is with screenshots.

Job Search Workflow Mindmap

FreeMind is mind mapping software that uses a hierarchical tree structure (compare this to something like Cmap tools that is more free-form).  We don’t need a tree structure, but we do need easy hyperlinks, which we know that FreeMind has.

Format of companies to add.

We don’t have anything in our “Companies to Add” file right now, except for a model of the vital information that we need about each company ( the login, password, and HRC rating).

Some prospective companies which employ actuaries.

The “Companies” file contains links for each companies website, plus the vital login info (each node may be unfolded).

Nodes for job listings.

When we identify a job to apply for, we drag a copy of the company info down to the job prospects file, then copy the job listing webpage on to the system, and link to it.

Nodes for jobs applied.

Once we apply for a job, the listing is dragged down into the jobs applied file, and links for the resume and cover letter are added.

From a database point of view, there are flaws, such as data duplication.  However, thinking about this from a workflow point of view was very helpful.

# Some Study Materials For Exam MFE

It looks like I will take MFE, Models For Financial Economics, in March, 2015. The best source for the information is the SOA page for the test. It has links for the current Syllabus, the sample problems, and video solutions from the Wisconsin School of Business. The great thing about this exam is that the only recommended study resource is the MacDonald book, which we all still have from exam FM.

G. Stolyarov II has a very nice free study guide. At first glance I like it, and I like his studying hints.

Beyond that, I will be creating study cards again, reviewing FM material, and probably using Coaching Actuaries. Once again I will be studying to instrumental jazz music.  My goal is to get to the point of taking timed exams a few weeks before the actual exam.  So, my countdown on the sidebar will be to February 20, but I will be taking the actual exam somewhere around the ides of March.

Wish me luck.

# Project: Job Search Database

I need to organize my job search. I am not in hyper job search mode: I have a good job, and I have priorities other than looking for a new employment. And yet, I am doing just enough searching to make for an organizational challenge. Suppose that in the near future I apply for a total of thirty to fifty positions at twenty different employers. At this level, I have plenty of applications, job descriptions, resumes, and letters to track but very few human contacts, responses, and interviews.

Here is what my database application should be able to do:

1. Store potential employers and information about these employers: websites, passwords, notes, HRC rating.

1.5. Store preliminary data about possible employers requiring more research.

2. Store specific jobs and application history, resumes used, letters, description, title, location, job ID.

3. Have a form for adding new job.

4. Perform queries on jobs previously applied to.

5. Produce reports on same.

6. When I do get a response from a potential employer, I need to be able to sit down in front of the computer and instantly see the particulars of the job, plus the particulars of my submission.

I have a little bit of a start already. In a short while, I will post a diagram of my data structure.

# Eight Secret Tricks to Passing (Exam FM)

1.  Be over-prepared: The syllabus is so extensive, and there are so many twists that may be put on standard material, that there are always going to be questions that look foreign. The more that you are prepared, the more that you will be able to handle some of these. Being over prepared also helps to deal with emotions. There were points during the exam when a little voice in my head said “There is no way that you are going to pass this.” Yet, that same voice was in my head during many sample exams which I passed with flying colors. Experience and over-preparation reveals that you can succeed even when you are not at your best. If you walk into the exam center with worries, or a cold,or the flu, or pain in your back, you know that you can still succeed.

2.  Fill in the tough spots: It can hurt your head to learn new things. Identify the areas with which you struggle, and try to get your head around them. Two weeks before the exam, I still had some head-pain topics. Things like duration matching, convexity, interest rate swaps, and put-call parity. But, they were on my daily study schedule, and I forced myself to confront them each day. It paid off.

3.  Fill in the fundamentals: Once you gain proficiency in a topic, the fundamentals look different. In the month before the test, I went back and reviewed some of the basics of interest theory. At this point, the basics were easy, and I picked up on some of the finer points that I missed the first time around. Amortization schedules, for instance.  After I learned the basic formulas, it was easy to ignore the schedules they are based on. Yet many problems are made much easier by using schedules, rather than relying on formulas.

4.  Memorization is easy: Once you understand things thoroughly, you don’t really need to do much memory work for these exams. After months of working problems, most things will be anchored in your head. And yet suppose that you find yourself trapped in a dark alley by two annuities, one of which is twice as long as the other. Then you will be glad that you memorized $\frac{a_{\overline{2n}\lvert }}{a_{\overline{n}\lvert }}=1-v^n$ I have never encountered this fact in practice, but if I did, recognizing this identity might save me a couple of minutes. Spend a little time on memory work, it is easy.

5.  Test symbolic solutions with numbers: If you have a problem with a purely symbolic solution, and you can narrow it down to a couple of possible solutions, you can frequently replace the variables with reasonable numbers, and see if the result is true. This is my favorite new solution technique I learned while studying for this exam.

6.  Read MacDonald: Actually read it. After you learn the math, go back and read it. If you look at the notes from the sample problems, you will notice that the exam writers have made it pretty clear that the exam will be based on the book. Those non-numeric multiple choice questions can be very challenging. Read the book.

7.  Number your Scrap Paper: I learned this trick when taking practice exams.  If you have time to review problems at the end of the exam, you will need to find the scratch-work easily.

8.  Practice with dull number two pencils: Two weeks before the exam, I put all of my mechanical pencils away, and worked only with old fangled sharpenable pencils. Two hours into the exam, you will be working with dull stubs, so you need to be prepared.

# Onward to Exam MFE

The title indicates that I passed FM. Here is how it happened.

I went easy on studying for the two days before the exam. I did some light reviewing of formulas, shoveled snow, walked the dog, and made candy. Weather was a concern. We have been getting blasted by winter each week, along with most of the rest of you in the Northeast. My spouse had generously agreed to come with me to the testing site (40 minutes away, in Lancaster), but I know that winter driving is very stressful for her, and that it was a pretty big thing for her to agree to come with me at all. I also knew that I would really like to have her emotional support after the exam, pass or fail.

We are buried under snow. I am glad that I checked online, because sometime in the last couple of years the Fruiteville Pike Prometric location moved down the street to the next plaza. I might have had trouble finding it, under the twenty-foot tall snow piles.

So, the test itself. I am so glad that I was over prepared. I had plenty of negative thoughts during the test. But I have been doing these problems under all sorts of conditions for months. It is like having a bad day at work: you know that you can still get your job done well, even on a bad day.

When I was all done with the exam, I still had about 21 minutes to go. This is about how I have been timing my sample exams. In those few minutes, I found errors on three marked questions. These were questions that I had reached the end of, and then found that my answer was not on the list. So frustrating. A little time away from the problem, though, and the mistake is often obvious. In these problems, it is often some vital misreading at the front end or back end of the problem.

Anyways, I worked right up to the end. I filled the survey at the end out in a big hurry, because I couldn’t wait to see the result. I was so surprised to see a “pass.”

That’s it. Now that I am done, it is like this big thing that has been dominating my life since early fall is just gone.

When I got home, I pulled out the exam MFE syllabus.

# Problems in the Queue

A week left until the financial mathematics exam. The best thing that I can say is that I can honestly not have worked any harder at studying. In the last three months, I have studied at least three hours a day, and frequently I have done eight to ten hours a day. Much of that time has been absolutely focused. The question is: have I studied as smartly as possible? I won’t know the answer to that until after the examination.

I have done about two hours of problems today. I see that there are 19 problems remaining in the queue. These are problems that I have seen before, and that I still wish to spend some time with. I spend enough time with each problem that I feel I am confident that I understand several ways to get to a solution, or until I get sick of looking at it. If I get sick of looking at it, it comes up on the queue again later today or tomorrow. If I am fluent with it, I don’t look at it for a few days. Eventually, it disappears from the queue.

Earlier today, I already did some easy numerical exercises. Later today, I will probably do an exam, which is a whole other kind of problem solving, because of time pressure (5 minutes per problem.)

What I am going to do here is type in problems as they come up on my screen, then write up solutions. If I don’t understand the problem, you will get to watch me struggle.

Person X enters into a long forward contract. If the spot price at expiration were S, the payoff would be -20. If the spot price at expiration were 1.2S, the payoff would be X.

Person Y enters into a short forward contract. If the spot price at expiration were 0.8S, the payoff would be 40. If the spot price at expiration were 1.1S, the payoff would be Y.

The forward price on each contract is the same.

What is X+Y?

We aren’t going to do a diagram for this one. I tried, on paper, and since we don’t know the values of 0.8S, 1.1S, or 1.2S, it is hard to know where to place them in relation to the forward price. So, let’s go with straight algebra, and see if that leads us to a reasonable solution.

#### From Givens:

$S-F = -20 \\ 1.2S=S=X \\ F-0.8S=40 \\ F-1.1S = Y$

#### That wasn’t too bad. These derivative problems can be a little intimidating at first.

A 15 year bond with semiannual coupons has a redemption value of $100. It is purchased at a discount to yield 10% compounded semiannually. If the amount for accumulation of discount in the 27th payment is$2.25, which of the following is closest to the total amount of discount in the original purchase price?

We have all sorts of information here, so it should be easy to get to an answer. First, since the coupons are semiannual, we can just think entirely in 6 month terms. Since the bond is sold at a discount, we know that the coupon rate is less than the interest rate. We know that we can solve this problem by finding the original purchase price, and subtracting it from the redemption price. First, we need the coupon, which we can find using the premium discount formula.

Let’s start with that. Assuming that F=C, the amount of discount in the kth payment is:

$F(i-r)v^{n-k+1}$

Here are our givens:

$n=30 \\ F=C=100 \\ i=0.1 \\ F(i-r)v^{n-1+k} = 2.25 \\ 100 (0.05-r)1.05^{-(31-27)} = 2.25 \\ r=0.02265 \\$

Now solve for the original sale price. We might as well continue with the premium discount formula:

$P = C+(Fr-Ci) a_{\overline{n}\lvert } \\ P=100-0.2735\frac{1-1.05^{-30}}{0.05} \\ P=57.96$

Discount in original price = 42.04.

Let’s just do one more.

To accumulate 8000 at the end of 3n years, deposits are made at the end of the each of the first n years and 196 at the end of each of the next 2n years.  (1+i)^n = 2.

What is n?

These problems are much simplified by visualizing them in the right way. The easiest way to think of it is payments of 98 from 1 to 3n years, plus payments of 98 in years 2n+1 through 3n.

In math:

$98s_{\overline{3n}\lvert }+98_{\overline{2n}\lvert }=8000 \\ \frac{(1+i)^{3n}}{i} + \frac{(1+i)^{2n}}{i}=81.633 \\ \frac{2^3-1}{i} + \frac{2^2-1}{i} = 81.633 \\ i= 12.25%$

I am going to post one more, because it is a real bear. To solve it, you trust that the math will lead you to the answer.

Given a k year bond with semiannual coupons, and a yield rate of 10% convertible semi-annually, sold at a price p.

If the coupon rate had been r-0.04, the price would be P-200.

Calculate the present value of a 3k year annuity immediate paying 100 at the end of each 6 month period, at a rate of 10% semiannually.

Start at the end. We need to find:

$100 \frac{1-1.05^{-6k}}{0.005}$

Which means that what we really need is k (although v^k will do).

Dive in:

$P=1000\frac r 2 \frac{1-1.05^{-2k}}{0.05}+1000(1.05)^{-2k} \\ P-200 = 1000\frac{r-0.04}{2}\times \frac{1-1.05^{-2k}}{0.05}+1000(1.05)^{-2k} \\ \text{We can see that the redemption values will not be significant}\\ 1000 \frac r 2 a_{\overline{2k}\lvert }=1000 \frac{r-0.04}{2}a_{\overline{2k}\lvert }+200 \\ 1000 \frac r 2 a_{\overline{2k}\lvert }-1000 \frac{r-0.04}{2}a_{\overline{2k}\lvert }=200 \\ 500a_{\overline{2k}\lvert }(r-r+0.04)=200 \\ a_{\overline{2k}\lvert } = 10 \\ k = 7.1 \\ 100 \frac{1-1.05^{-6\times7.1}}{0.05} =1749.75$

# Forward Contracts

The thing about math at a certain level is that there are no more easy exercises.  I have learned to make a habit of creating simple exercises.  These are for the forward price, which is the contracted price to buy an asset at time T in the future; and the prepaid forward price, which is the price paid now for an asset that will be delivered at time T.  In these problems, r is the continuous interest rate, and delta is the continuous dividend rate.
$F^P_{0, T} = S_o = S_o -PV(divs) = S_0 e^{-\delta} T$
$F_{0, T} = S_0 e^{rT} = S_oE^{rT} - AV(divs) = S_0e^{(r-\delta)}T$

1. $S_0 =1000, r=0.04, \delta = 0.01\quad F^P_{0, 6m}?$
2. $S_0 =800, r=0.02, \delta = 0\quad F_{0, 2m}?$
3. $S_0 =800, r=0.02, \delta = 0\quad F^P_{0, 2yr}?$
4. $S_0 =500, r=0.04, \delta = 0\quad F_{0, 2yr}?$
5. $S_0 =500, r=0.04, \delta = 0\quad F_{0, 6m}?$
6. $S_0 =500, r=0.04, \delta = 0\quad F^P_{0, 1yr}?$
7. $S_0 =100, r=0.03, \delta = 0.01\quad F^P_{0, 2yr}?$
8. $S_0 =100, r=0.03, \delta = 0.01\quad F^P_{0, 3m}?$
9. $S_0 =1000, r=0.04, \delta = 0.01\quad F_{0, 1yr}?$
10. $S_0 =1000, r=0.04, \delta = 0.01\quad F_{0, 6m}?$
11. $S_0 =800, r=0.02, \delta = 0\quad F^P_{0, 5m}?$
12. $S_0 =100, r=0.03, \delta = 0.01\quad F_{0, 1yr}?$
13. $S_0 =100, r=0.03, \delta = 0.01\quad F_{0, 9m}?$
14. $S_0 =500, r=0.04, \delta = 0\quad F^P_{0, 9m}?$
15. $S_0 =1000, r=0.04, \delta = 0.01\quad F^P_{0, 1yr}?$
16. $S_0 =800, r=0.02, \delta = 0\quad F_{0, 1yr}?$

Solutions:

1. $1000e^{-0.01*0.5}=995.01$
2. $800e^{0.02*(\frac 1 6)}=802.67$
3. $800$
4. $500e^{.04*2}=541.64$
5. $500e^{0.04*.5}=510.10$
6. $500$
7. $100e^{-0.01 *2}=98.02$
8. $100e^{-0.01*0.25}=99.75$
9. $1000e^{0.04-0.01}=1030.45$
10. $1000e^{(0.04-0.01)*0.5} =1015.11$
11. $800$
12. $100e^{0.03-0.01}=102.02$
13. $100e^{(0.03-0.01)*0.75}=101.51$
14. $500$
15. $1000e^{-0.01}=990.05$
16. $800e^{0.02}=816.16$

Later today, I will post some tougher ones that require a little thinking.