**Those who monitor my personal home page may have been dismayed to hear of my recent return to mathematics.** I would like to point out that I am a man of
honour (that is, of course, British honour with a *u*) and have no intention of departing the software field in the near future.
However, you will have to put up with a few articles like this one.

As a way to bolster my study of quantitative finance, I've started work on implementing some numerical models for pricing derivatives in C++. So, my faithful audience, both of you will have to bear with me as I drag you kicking and screaming through a terrifying mathematical interlude. If this article were a horror film, this is the point where you cover your eyes, waiting desperately for it all to be over. That was precisely my reaction when I was around ten years old, watching the janitor peer inside the crate in Creepshow; just ask my parents as they'd be happy to validate that story for you.

### The 60 Second Option Pricing Primer

For those not familiar with derivatives, an option can be described roughly
as a right to buy or sell something at a particular price, at some point in the future. You don't have to buy or sell, and that's why it's
an *option*. Now an option is not free, but evaluating what the "correct" price should be for an option is a problem in itself.

Suppos you walk into your neighbourhood-friendly antique desk store (every town has one) and gaze upon an ornate beauty of a desk, available for only £2000. This is no ordinary dealer, though. Your cool store has a ton of these rare desks coming in next year, and is offering option contracts to buy one for £1900 in a year's time. If the market price for this sleek wooden beast is still £2000 in a year, then you could buy the option now, exercise the option in a year and instead of enjoying the varnished curves of your new purchase, flog it to someone else at an auction. In theory, you should make a profit of at least £100. Heck, why not buy *20 options* and then buy your own desk just from your profit? You essentially get the desk for nothing!

The option, of course, isn't free. You'd probably find the option price is over £100 eliminating the "obvious" profit in your cunning scheme. The final market price is far from certain, too. But it's pretty unrealistic to assume that the desks will actually be on the market at an extreme price such as £5 or £5000. Is it possible to come up with a "reasonable" figure for the option price? What should the store be charging for these options? Should it be £100, £150, £300?

Now there is a model known as the Black-Scholes model which culminates in a differential equation that could be solved, theoretically, to find the option price. An exact solution, such as the Black-Scholes option pricing formula, can be found for simple cases. Outside of simple cases, you need to turn to the computer to find a solution, because you're not going to find it on paper.

You could try to solve the Black-Scholes differential equation numerically, but this isn't your only recourse (I almost typed "isn't your only option", nasty). You could employ a Monte Carlo method, which involves running a market simulation many times and deducing an appropriate option price from the results. There is another alternative, which is to build a tree diagram of the possible price movements and then elicit an estimate for the option price from the tree.

Ladies and gentlemen, today we're going to build a *binomial tree*.

### A Simple Binomial Tree

I'm going to start simple for my C++ program, so I'm only going to consider a stock which doesn't pay any dividends, just like those cheapskates at Microsoft until last year, and an option that can't be exercised until the end of its life.

We begin with the current stock price and look at how it might move after a small period of time. Things are much easier if we assume
that there are only two outcomes after each time step: an up movement or a down movement, which is why this is called a *binomial* tree. If we
do this again for the two outcomes, and then keep on repeating this process until the option finally matures, we have a tree of stock
prices that is a model for the stock price behaviour over the option's life.

At the far end of the tree, there are a lot of possible stock prices, but it's quite easy to determine for each one what the value of the option is.
If you can make money by exercising the option, then the option value is the difference between the stock price offered by the option (known as the *strike price*) and the actual market price at that time, otherwise its zero. Once
we know that, we can use the probabilities assigned to stock movements on the tree to calculate what the option value should be on the
previous time step. And then the step before that and so on until we finally come up with the approximation for the option value at the
start of the tree. Finally, you have your estimate for the price of the option.

I'm not going to go into lurid detail on what equations are used in the binomial tree, but if you're really interested you can read
the book I've been working through,
Options, Futures and Other Derivatives by John Hull, or turn to the numerous websites that cover the binomial tree experience. I was
quite disturbed to find one featuring streamlined C++ algorithms
*after* I finished coding my C++ implementation.

### In Code

My first draft C++ implementation, **OptionTree 1** (source code available), is straightforward and barely optimised. The whole tree is stored in memory, even though you can get
away with just storing only a single time step at any one time. When I worked on my PhD in water wave modelling, the problem domain was
extremely large and optimising the code to be memory-efficient was essential. However, I started with a bulky prototype to make sure the ideas and concepts were solid. Rest assured, there is scope to optimise *OptionTree 1*.

Although the concepts are seemingly complex, the mechanism that emerges from the basic binomial tree is fairly easy to translate into an algorithm. The program took an evening to throw together and test, and I used the DerivaGem software that came with the Hull book to verify that the numbers coming out of this draft solver were correct.

The necessary inputs are: the option strike price; the stock price; the time to option maturity; the volatility of the stock; the risk-free interest
rate (assumed constant throughout); whether it is a *call* (option to buy) or *put* (option to sell) contract and finally, the parameter that
controls the accuracy of the binomial tree model, the number of time steps.

So assuming the interest rate is 2% (these guys aren't financial institutions, after all) and the volatility of the antique desk price is 20%, it looks like the appropriate option price is £232. I'll take that in cash, thanks. (Yes, well, don't read too much into this. I doubt the antique desk market is liquid enough to meet some hidden assumptions, but hey, it was fun while it lasted.)

It would be nice to include the ability to deal with dividends
as well as the options that *can* be exercised early. Also, I'm a big
automated testing freak (you know one of those people who love TDD, or is that ADD?) and would like to have a go at a formal test framework to manage automated testing. No guarantees, though, faithful reader.

There are also the more exotic options to consider. Amongst these you will find the crazy "path-dependent" options that are evaluated based on the actual movements of the stock that occurred just to mix it up a bit.

So, yes, there's enough here to keep one busy. See you later.