# Option Pricer: Binomial Trees

When pricing options, we don’t know whether the price will go up or down. This simple model captures that uncertainty.

## Parameters

Consider an option with the strike price $X = 120$ maturing in $T = 3$ months on a stock worth $S = 115$ having volatility $\sigma = 30\%$ and interest rate $r = 15\%$.

### u & d

The above stock price will either go up or down. In this model, these up/down movements are defined as:

where $\sigma$ is the volatility and $\Delta t$ is the time step.

### Time Step

We shall use a three-step bionimal model, with each time step representing one month.

## Building a Tree of Stock Prices

Applying our parameters, we get:

To get the stock price at the next time step we compute both $S_0 \cdot u$ and $S_0 \cdot d$.

That is:

We can represent this fledgling tree using a matrix.

Doing this for every timestep, we build our tree iteratively until $t = T$.

We have now $n = T+1$ predictions of $S_T$.

### Implementation

We can populate this tree in Java using a two-dimensional array.

private double[][] stockPrices(double S0, double u, double d, int T) {
double[][] stockPrice = new double[T][T];
stockPrice[0][0] = S0;
for (int hori = 1; hori < T; hori++) {
// compute increase by u on the extreme side only
stockPrice[0][hori] = stockPrice[0][hori - 1] * u;
for (int vert = 1; vert < T; vert++)
// compute all decrease by d
stockPrice[vert][hori] = stockPrice[vert - 1][hori - 1] * d;
}
return stockPrice;
}


This method will return a matrix of stock prices of size $T \times T$

## Building a Tree of Option Prices

We now construct a second matrix of option prices.

### Computing Payoff

Once again we seek to populate a matrix of size $T \times T$ iteratively. Only this time we move from right to left.

In the first step, we look at the stock prices at maturity and compute the Pay-Off.

We compute an option price for each value of $S_T$ we computed earlier, which, if the option is a Call, returns the following:

And the following if the option is a Put:

### Applying Discounting

Next we look at all stock prices prior to maturity $% $ and use the following formulas to iteratively compute option prices.

where:

which in this example gives us:

But note that for American Puts, we must consider early exercise such that our option price is:

Either way, we take the option price at the root of the tree, $f_{t=0}$.

Below are the option trees as fully developed for each type of derivative.

### Implementing Call Pricing

As the implementation of pricing a European Call the same as that for an American Put, we need only write one implementation.

Therefore we write the following method in TreeAbstract.java.

private double computeCall(double[][] stockPrice, double strike, double interest, double p, double dt, int T) {
double[][] optionPrice = new double[T][T];
for(int hori = T-1; hori >= 0; hori--)
for(int vert = hori; vert >= 0; vert--){
// compute option prices at maturity
if (hori == T -1)
optionPrice[vert][hori] = Math.max(stockPrice[vert][hori]-strike,   0.0);
// compute option prices prior to maturity
else
optionPrice[vert][hori] = Math.exp(-interest*dt)*((p*optionPrice[vert][hori+1])+(1-p)*optionPrice[vert+1][hori+1]);
}
return optionPrice[0][0];
}


### Implementing Put Pricing

As the implmentation for a European Put differs from the American Put, we place an abstract method in TreeAbstract.java.

abstract public double computePut(double[][] stockPrice, double strike, double interest, double p, double dt, int T);


Then we let the concrete implementation of this method be defined in the concrete classes TreeEuropean.java and TreeAmerican.java which both extend the abstract class.

#### TreeEuropean.java

public class TreeEuropean extends TreeAbstract {

public TreeEuropean(HashMap<String, Double> hashMap){
super(hashMap);
}

@Override
public double computePut(double[][] stockPrice, double strike, double interest, double p, double dt, int T) {
double[][] optionPrice = new double[T][T];
for(int hori = T-1; hori >= 0; hori--)
for(int vert = hori; vert >= 0; vert--){
// compute option prices at maturity
if (hori == T -1)
optionPrice[vert][hori] = Math.max(strike-stockPrice[vert][hori],0.0);
// compute option prices prior to maturity
else
optionPrice[vert][hori] = Math.exp(-interest*dt)*((p*optionPrice[vert][hori+1])+(1-p)*optionPrice[vert+1][hori+1]);
}
for (int i = 0; i < T; i++)
System.out.println(Arrays.toString(optionPrice[i]));
return optionPrice[0][0];
}
}


#### American

public class TreeAmerican extends TreeAbstract {

public TreeAmerican(HashMap<String, Double> hashMap){
super(hashMap);
}

@Override
public double computePut(double[][] stockPrice, double strike, double interest, double p, double dt, int T) {
double[][] optionPrice = new double[T][T];
for(int hori = T-1; hori >= 0; hori--)
for(int vert = hori; vert >= 0; vert--){
// compute option prices at maturity
if (hori == T -1)
optionPrice[vert][hori] = Math.max(strike-stockPrice[vert][hori],0.0);
// compute option prices prior to maturity
else
optionPrice[vert][hori] = Math.max(
Math.exp(-interest*dt)*((p*optionPrice[vert][hori+1])+(1-p)*optionPrice[vert+1][hori+1])
,   strike-stockPrice[vert][hori]);
}
for (int i = 0; i < T; i++)
System.out.println(Arrays.toString(optionPrice[i]));
return optionPrice[0][0];
}
}


### Returning Option Prices

TreeAbstract.java is an abstract class implementing the interface PricingType.java which is written as follows:

public interface PricingType {

double getCall();
double getPut();

}


This allows OptionPricer.java to remain boilerplate. Now the code to return a call or put is the same no matter the underlying pricing method being used.

In TreeAbstract.java, we define the body of the above methods:

    public double getCall(){
double fCall = computeCall(stockPrice, strike, interest, p, dt, T);
return fCall;
}
public double getPut(){
double fPut = computePut(stockPrice, strike, interest, p, dt, T);
return fPut;
}


That is, we simply invoke the methods computeCall and computePut.