In the binomial option pricing model, the value of an option at expiration time is represented by the present value of the hereafter payoffs from owning the option. The main principle of the binomial model is that the choice price pattern is related to the stock price pattern. In this post, we will larn about how to build binomial selection pricing model in R.

Showtime we will value an pick with a single period until expiration. The cost of a call in the frame of the single menstruum binomial model tin be represented using the following equation that represents a synthetic European call pick:

### ct = N

*

St – Bt

N=Number of Shares in the Portfolio

St = Stock price at t

Bt\=Short position in a gamble-free bond

Using this equation we can replicate the value of a call at expiration by owning a portfolio with Due north number of shares for a stock with toll St and with an amount borrowed B at the risk-gratis rate. In a single menses if the stock price St goes up, the call choice toll will increase, and if the stock price St decreases, the telephone call selection would fall.

The value of the call tin can exist expressed in terms of the likelihood of price movements in the underlying asset on the next period. When the stock cost goes upward, the call pays CU at expiration, and when the stock price goes down the call pays CD. This led to the following equation:

Where q is the probability that the stock goes up and (1-q) is the probability that the stock goes down and R is the take chances gratis charge per unit, at which the pick payoff is discounted in each period. The representation of the likelihood of an increment or decrease on the stock price tin exist constructed using the stock price volatility ?.

Cox, Ross, and Rubinstein (1979) proposed the following formulas of U and D:

T represents the time. If T = ane means that T is i year, and if T=0.25 represent iii months. The number of periods we simulate the stock cost is N. And so the amount of time represented by each flow is:

Having the values of the volatility ?, the time of expiration, and the number of periods N, we can simulate the stock price in N periods with the following role called

**
build
_
stock
_
tree
**:

` ````
build_stock_tree <- part(Due south, sigma, delta_t, N) { tree = matrix(0, nrow=Northward+i, ncol=N+ane) U = exp(sigma*sqrt(delta_t)) D = exp(-sigma*sqrt(delta_t)) for (i in one:(N+1)) { for (j in 1:i) { tree[i, j] = S * U^(j-1) * D^((i-1)-(j-i)) } } return(tree) }
```

The above code first initializes a matrix which is chosen

*tree*

with Northward +1 number of rows and N+1 number of columns. Then, it defines

*U*

and

*D*

(the different paths of the stock price) with the formulas that we set above and afterwards, the for loop would make full the

*tree*

matrix with the valuation price in each of the foliage nodes.

If we accept a stock price of USD 80, with a volatility of 0.1 and ii periods nosotros can build a tree with the part

*build
_
stock
_
tree*

and simulate the stock price. The results are the following:

` ````
firstExample <- build_stock_tree(South=80, sigma=0.i, delta_t=1/2, Due north=2)
```

` ````
[,one] [,2] [,three] [1,] fourscore.00000 0.00000 0.00000 [2,] 74.53851 85.86165 0.00000 [3,] 69.44988 80.00000 92.15279
```

Each time we change N, ∆t will alter as well. In case Northward=4, ∆t=one/4 and the new stock tree is:

` ````
secondExample <- build_stock_tree(S=80, sigma=0.ane, delta_t=1/4, N=4)
```

` ````
[,ane] [,two] [,3] [,iv] [,5] [1,] 80.00000 0.00000 0.00000 0.00000 0.00000 [2,] 76.09835 84.10169 0.00000 0.00000 0.00000 [3,] 72.38699 lxxx.00000 88.41367 0.00000 0.00000 [four,] 68.85664 76.09835 84.10169 92.94674 0.00000 [5,] 65.49846 72.38699 80.00000 88.41367 97.71222
```

Adjacent step is to get the binomial tree for options pricing. A call option value at expiration is given by the

**
MAX(South-M,0)
**, where K is the strike toll of the call choice. In our second instance, if the strike price

*Grand*is USD 85, the intrinsic value of the option related to the different stock paths would exist:

` ````
K <- 85 IntrinsecValue <- secondExample[5,] – K IntrinsecValue[IntrinsecValue<0] <- 0
```

` ````
[1] 0.000000 0.000000 0.000000 three.413673 12.712221
```

The first iii values would be nix because the option is not worthless equally the Stock price is lower than the strike price. Finally we demand a part to discount the options prices at the unlike periods and which incorporate the likelihood of an increase or decrease in the stock price. This is given by the

*q*

parameter q that is formulated on different Financials books every bit:

This formula of tin be expressed in the following role

**
q
_
prob
**:

` ````
q_prob <- function(r, delta_t, sigma) { u = exp(sigma*sqrt(delta_t)) d = exp(-sigma*sqrt(delta_t)) return((exp(r*delta_t) - d)/(u-d)) }
```

Finally, the in a higher place functions can exist combined and create a consummate function to value options in each leaf node of the tree. The following part chosen

*value
_
binomial
_
pick*

would practice that job.

` ````
value_binomial_option <- office(tree, sigma, delta_t, r, X, type) { q = q_prob(r, delta_t, sigma) option_tree = matrix(0, nrow=nrow(tree), ncol=ncol(tree)) if(type == 'put') { option_tree[nrow(option_tree),] = pmax(X - tree[nrow(tree),], 0) } else { option_tree[nrow(option_tree),] = pmax(tree[nrow(tree),] - X, 0) } for (i in (nrow(tree)-1):1) { for(j in 1:i) { option_tree[i,j]=((1-q)*option_tree[i+1,j] + q*option_tree[i+1,j+ane])/exp(r*delta_t) } } return(option_tree) }
```

The

*value
_
binomial
_
option*

function receives 7 inputs that are:

*tree*,

*sigma*,

*delta*

_

t,

_

t

*r, X*, and

*type*. The tree parameter is the output of the

*build*

_

stock

_

tree_

stock

_

tree

office that was described previously. Sigma is the volatility of the stock toll, delta

_

t is the time period in which the option expires, r is the risk free interest rate,

*X*

is the strike cost of the option at expiration and

*type*

should be ‘put’ for put options valuation and ‘telephone call’ for call options valuation.

This function has three primary steps. The start step consists of calculating

*q*

given the values of

**r**,

*delta
_
t*

and

**. Then the function initializes the tree matrix with the right dimensions. For this purpose, we ascertain the**

*sigma*

*option*

_

tree_

tree

structure equally a matrix with the same dimensions of the stock tree.

Secondly, we summate the intrinsic value of the options at expiration fourth dimension. For this task, the last row of the

*option
_
tree*

matrix is filled with the max value between 0 and the deviation of the Strike price and the Stock Price at expiration if the option blazon is ‘put’, or the max value betwixt 0 and the difference between the Stock Toll and the Strike price if the type is ‘call’.

The third function consists of a for loop that would make full the

*option
_
tree*

structure in backward direction starting with the values from the terminal row (that were calculated and assigned in the previous step) of the matrix.

` ````
option_tree[i, j] = ((1-q)*option_tree[i+1,j] +q*option_tree[i+1,j+i])/exp(r*delta_t)
```

In this line,

*i*

refer to a specific row number of the tree and

*j*

refers to a specific column number. As we stated to a higher place the loop has backward direction and gets the values from the last row which are the intrinsic values of the options at expiration. For each intersection of the

*i row’southward*

and

*j cavalcade’s*

is calculated the option prices at the current period given the probability of an up movement in the price(**
q
**) and the probability of a down movement in the price(

**) on the next period divided by**

*one-q*

*R*which is the rate of discount.

The

*binomial
_
option*

function would join all the functions that we have described and volition return a tree with the values of the option at the different periods. These are the payoffs of the option on the different periods during the choice life.

` ````
binomial_option <- function(type, sigma, T, r, 10, S, N) { q <- q_prob(r=r, delta_t=T/N, sigma=sigma) tree <- build_stock_tree(Due south=S, sigma=sigma, delta_t=T/N, N=N) option <- value_binomial_option(tree, sigma=sigma, delta_t=T/Due north, r=r, X=X, type=type) return(list(q=q, stock=tree, option=option, price=option[1,1])) }
```

The

*binomial
_
option*

office runs the three functions that we described which are

**,**

*q*

_

prob_

prob

*build*

_

stock

_

tree_

stock

_

tree

and

**, and returns a list with the values of**

*value*

_

binomial

_

choice_

binomial

_

choice

**, the**

*q***, the**

*stock tree***, and the**

*choice tree***.**

*option price*In order to run the binomial function, we demand to insert the correct inputs. If nosotros desire to value a call option with X = 100, S = 110, N=5, sigma = 0.15, r=01

` ````
results <- binomial_option(type='call', sigma=0.fifteen, T=i, r=0.ane, Ten=100, S=110, N=5)
```

And the results of this is a list construction with 4 items (**
q
**,

**,**

*stock tree*

*option tree*and the

**). Nosotros can retrieve each element of the consequence list with ‘$’ operator.**

*selection value*` ````
results$q [i] 0.6336948 results$stock [,i] [,two] [,three] [,four] [,5] [,6] [i,] 110.00000 0.00000 0.0000 0.0000 0.0000 0.0000 [2,] 102.86303 117.63215 0.0000 0.0000 0.0000 0.0000 [3,] 96.18912 110.00000 125.7938 0.0000 0.0000 0.0000 [4,] 89.94823 102.86303 117.6322 134.5218 0.0000 0.0000 [5,] 84.11225 96.18912 110.0000 125.7938 143.8554 0.0000 [half-dozen,] 78.65492 89.94823 102.8630 117.6322 134.5218 153.8365 results$option [,one] [,2] [,3] [,iv] [,v] [,half dozen] [ane,] 20.162476 0.000000 0.000000 0.00000 0.00000 0.00000 [ii,] 12.062180 25.487578 0.000000 0.00000 0.00000 0.00000 [3,] 5.415450 xvi.288826 31.617395 0.00000 0.00000 0.00000 [4,] ane.104625 8.079946 21.553209 38.44289 0.00000 0.00000 [five,] 0.000000 1.778364 11.980133 27.77398 45.83552 0.00000 [6,] 0.000000 0.000000 2.863033 17.63215 34.52183 53.83653 results$price [ane] 20.16248
```

To conclude, we can utilise this model to show the relationship between options and Strikes prices. We can create an array of Strike prices and wrap the

*binomial
_
choice*

role in another function and vary the

*Strike*parameter to get an array of option prices for different Strikes at expiration. We would define the

*option*

_

toll

_

vary

_

function_

toll

_

vary

_

function

that wraps the

*binomial*

_

function_

function

and allows unlike values to the strike parameter.

` ````
# Define an assortment with strike prices between 80 and 120 strikes <- seq(lxxx, 120) option_price_vary_function <- office(strike) { option = binomial_option(type='call', sigma=0.15, T=1, r=0, X=strike, Southward=110, Due north=5) return(option$price) } # Use the sapply control to pass each value of the strikes array into the option_price_vary_strike office: values <- sapply(strikes, option_price_vary_period) # Create a information frame with the Strikes prices and the selection prices (values) from higher up data <- as.data.frame(listing(strikes=strikes, values=values)) head(data) strikes values 1 fourscore 30.04957 2 81 29.08642 iii 82 28.12327 four 83 27.16013 5 84 26.19698 six 85 25.23383
```

Lastly we utilize the

*ggplot*

package to plot the relationship between choice prices and strikes values.

` ````
ggplot(data=data) + geom_line(aes(x=strikes, y=values)) + labs(title="Phone call Value", x="Strikes", y="Value") + geom_vline(xintercept = 100, linetype="dotted", color = "red", size=ane.5)
```

**Telephone call Prices vs Strike Prices**

This graph shows the relationship betwixt option prices and strikes prices. Having a stock price of USD100 at expiration, options with strikes prices higher than USD100 have lower values because they are

**
out-of-the-money
**. On the other paw options that are

*in-the-money*(left side of the red line), accept higher values.

**Source: https://financetrain.com/binomial-option-pricing-model-in-r**