Google Ad sense



All source codes in this blog are also available in Gregory Choi's Github Source code Repository


  

Thursday, March 24, 2016

Binomial option pricing in R & visualization of tree structure (1)

Keep in mind that ,as this is not the in depth discussion about finance, I would like to focus on R code more.

[The concept of Option]
It is right to buy or sell a stock. It has call-option and put-option. Call option refers to the right to buy a stock. I want to limit the discussion within the call-option for the sake of the simplicity.

[Payoff]
It has an exercise price. If the stock price gets higher than the exercise price, the option starts to pay off.














Again, it's the right to buy a stock at a certain price. If the exercise price is less than current stock price, the option becomes useless. If the exercise is higher than the current stock price, the option pays off. For example, Let's suppose that I buy a option whose exercise price is $100. If the stock price becomes $110, and then I exercise the option, I can get $10. (The underlying assumption is that I will sell the stock immediately, if it is profitable) If the stock price becomes $90, I don't exercise my option, which make itself useless. That's why the option pay-off has a linear graph like above.

[How do we value the option]

Black-scholes model is the most popular. But, I want to touch on black-scholes model later. The binomial option pricing model was a great foundation of black-scholes model. Just like other finance models, it needs to simplify the real-life situation. Then, we are going to expand the model into the area similar to the real-life model. It has only two cases - either stock price goes up or down. There is no "no change." Let's assume that our stock price is $100. And there's 50:50 chance to make $10 or lose $10. We can intuitively draw this diagram.


I limited the time horizon to T=3 (You can expand more). Now, I would like to translate this financial model to R. It's not easy task as you imagine. Finally, we are going to draw the tree at the end of the chapter. First, we need another mathematical model.

Volatility = How much volatile stock is. (Apple in 2015 was 21%)
U = the stock price when stock price goes up = exp(volatility)
D = the stock price when stock price goes down = exp(-volatility)
R = the risk free interest rate to calculate the time value of the money.
S = Stock price ($100 in this case)
E = Exercise price (Also $100 for the sake of the simplicity)

Plus, we are going to use library "igraph" to draw the tree. In order to draw the tree, we need to build an algorithm called binary tree search. However, in this case, we are going to draw the simple 3-level tree that has indices like below. In next post, I'll touch on binary tree search algorithm. You can learn a lot through this post - Finance, Algorithm, and R. It's fascinating! Please, visit my blog more often. I'll fill out the amazing information that you want.

Here's my code.

[Code]
#Binomial option pricing
#U = exp(volatility)
#D = exp(-volatility)
#p = 0.5 (We have the equal chance of making or losing money)
#Risk free rate = 0.02 => exp(0.02)
#For those who are not familiar with data structure, I deliberately used just array.
#I'll make a new code for those who are familiar with tree data structure

library(igraph)
G <- graph.tree(n=7, children=2) #I'll make a graph whose nodes are 7, and each node has two children
rate <- exp(0.02)
volatility <- 0.2
exercise_price <- 100

a <- NULL
a[1] <- 100 #Time0
a[2] <- 100 * exp(volatility) #Time1 when the stock price goes up
a[3] <- 100 * exp(-volatility) #Time1 when the stock price goes down
a[4] <- a[2] * exp(volatility) #Time2 Up-Up
a[5] <- a[2] * exp(-volatility) #Time2 Up-Down
a[6] <- a[3] * exp(volatility) #Time2 Down-up
a[7] <- a[3] * exp(-volatility) #Time2 Down-down => worst case

V(G)$name <- round(a) #Name of the tree
lay <- layout.reingold.tilford(G) #It's tree. You can try other shape with other layout options
plot(G, layout=lay, vertex.size=50, edge.arrow.size=0.5) #Draw the tree.

#As opposed to the stock price, the option pricing starts out with end nodes (bottom nodes)
#I already explained the logic. Just follow it from one by one.
option_price<-(1:7)
for(i in 4:7) {
  after_option <- a[i] - exercise_price

  if( after_option >0 ) {
    option_price[i] <- after_option
  } else {
    option_price[i] <- 0
  }
}

#I assumed that the each case(price up & price down) has an equal chance 50:50
#Let's get an expectation from them
option_price[2]<-(0.5*option_price[4]+0.5*option_price[5])/rate
option_price[3]<-(0.5*option_price[6]+0.5*option_price[7])/rate
option_price[1]<-(0.5*option_price[2]+0.5*option_price[3])/rate

V(G)$name <- round(option_price)
plot(G, layout=lay, vertex.size=50, edge.arrow.size=0.5)

[Outcome]
<Stock Price>





















<Corresponding Option Price>
The conclusion is that this option's theoretical value is $12.

No comments:

Post a Comment