在R中使用二叉树定价美式期权

时间:2021-07-15 12:37:31

This is for all you R peps with finance backgrounds.

这适用于具有财务背景的所有人。

How the hell do you go backwards in the binomial tree, taking the max of either the exercise price or the value of the option along the way? For my code, I can create the tree as well as the exercise price tree, but I can't for the life of me figure out a way to go backwards in the tree to get the price of the american.

你怎么会在二叉树中倒退,在行进价格或期权价值的最大值?对于我的代码,我可以创建树和运动价格树,但我不能为我的生活找到一种方法在树中倒退以获得美国人的价格。

I'm also new to are, so my code might be very dirty. I don't know. Please help me. I've pasted my code below.

我也是新手,因此我的代码可能非常脏。我不知道。请帮帮我。我在下面粘贴了我的代码。

 Option_American <- function(S0,K,sig,steps,days,r,type) {
#get the infomation you'll need for the problem
  dt <- (days/252)/steps
    u <- exp(sig*sqrt(dt))
      d <- exp(-sig*sqrt(dt))
        p <- (exp(r*dt)-d)/(u-d)
#create the tree
  tree <- data.frame()
    count <- 1
      a<-1
      for (i in 1:steps) {
        for (j in 0:i) {
          tree[count,a] <- S0*u^j*d^(i-j)
          count<-count+1
        }
        a<-a+1
        count<-1
      }

  colnames(tree) <- c(1:steps)
#makes option tree showing exercise values    
  ex_values <- data.frame()
      this_row <- 2
  for (i in 1:steps) {
    if (type=="P") {
      ex_values[1:this_row,i] <- pmax(K-tree[1:this_row,i],0) }
        else   ex_value[1:this_row,i] <- pmax(tree[1:this_row,i]-K,0)
          this_row<-this_row+1
  }
  colnames(ex_values) <- c(1:steps)
    print(tree)
    print(ex_values)
#it gets dirty right here and confusing. It doesn't work... 
  vec <- c()
  count<-1
  start <- steps+1
    end <- steps
      what.col <- steps-1
        prob <- data.frame(c(p,1-p))
          n<-steps
  for (i in steps:1) {
    for (j in n:1) {
      disc <- sum(ex_values[start:end,i]*c(p,1-p)*exp(-r*dt))
      print(disc)
      vec[count] <- max(disc,ex_values[j,what.col])
        start<-start-1
        end<-end-1
    }
    n<-n-1
    what.col<-what.col-1
  }
          print(vec)
}

Option_American(100, 100,.05,4,252,.05, "C")

1 个解决方案

#1


0  

I was able to get it to work. I just need to make sure it's returning the correct price.

我能够让它发挥作用。我只需要确保它返回正确的价格。

    Option_American <- function(S0,K,sig,steps,days,r,type) {
#get the infomation you'll need for the problem
  dt <- (days/252)/steps
    u <- exp(sig*sqrt(dt))
      d <- exp(-sig*sqrt(dt))
        p <- (exp(r*dt)-d)/(u-d)
#create the tree
  tree <- data.frame()
   tree[1,1] <-S0
    count <- 1
      a<-2
      for (i in 1:steps) {
        for (j in 0:i) {
          tree[count,a] <- S0*u^j*d^(i-j)
          count<-count+1
        }
        a<-a+1
        count<-1
      }

  colnames(tree) <- c(1:(steps+1))
#makes option tree showing exercise values    
  ex_values <- data.frame()
      this_row <- 1
  for (i in 1:(steps+1)) {
    if (type=="P") {
      ex_values[1:this_row,i] <- pmax(K-tree[1:this_row,i],0) }
        else   ex_values[1:this_row,i] <- pmax(tree[1:this_row,i]-K,0)
          this_row<-this_row+1
  }
  colnames(ex_values) <- c(1:(steps+1))
    print(tree)
    print(ex_values)

#goes back in the tree to get option price today 
Z<-1
B <- steps+1
C <- steps
A <- steps
column2<-steps
row2<-steps
  disc <- data.frame()
    for (i in (steps+1):2) {
      for (j in A:1) { 
        values <- c(ex_values[B,i],ex_values[C,i])
        p_prob <- c(p,(1-p))
        a<- max((sum(values*p_prob))*exp(-r*dt),ex_values[row2,column2])
        ex_values[j,column2] <- a
        price<-a
          B<-B-1
          C<-C-1
          row2<-row2-1
        }
    A<-A-1
    B<-steps+1-Z
    C<-steps-Z
    column2<-column2-1
    row2<-steps-Z
    Z<-Z+1
    }
  print(price)
  }

Option_American(100, 95,.05,20,252,.05, "C")

#1


0  

I was able to get it to work. I just need to make sure it's returning the correct price.

我能够让它发挥作用。我只需要确保它返回正确的价格。

    Option_American <- function(S0,K,sig,steps,days,r,type) {
#get the infomation you'll need for the problem
  dt <- (days/252)/steps
    u <- exp(sig*sqrt(dt))
      d <- exp(-sig*sqrt(dt))
        p <- (exp(r*dt)-d)/(u-d)
#create the tree
  tree <- data.frame()
   tree[1,1] <-S0
    count <- 1
      a<-2
      for (i in 1:steps) {
        for (j in 0:i) {
          tree[count,a] <- S0*u^j*d^(i-j)
          count<-count+1
        }
        a<-a+1
        count<-1
      }

  colnames(tree) <- c(1:(steps+1))
#makes option tree showing exercise values    
  ex_values <- data.frame()
      this_row <- 1
  for (i in 1:(steps+1)) {
    if (type=="P") {
      ex_values[1:this_row,i] <- pmax(K-tree[1:this_row,i],0) }
        else   ex_values[1:this_row,i] <- pmax(tree[1:this_row,i]-K,0)
          this_row<-this_row+1
  }
  colnames(ex_values) <- c(1:(steps+1))
    print(tree)
    print(ex_values)

#goes back in the tree to get option price today 
Z<-1
B <- steps+1
C <- steps
A <- steps
column2<-steps
row2<-steps
  disc <- data.frame()
    for (i in (steps+1):2) {
      for (j in A:1) { 
        values <- c(ex_values[B,i],ex_values[C,i])
        p_prob <- c(p,(1-p))
        a<- max((sum(values*p_prob))*exp(-r*dt),ex_values[row2,column2])
        ex_values[j,column2] <- a
        price<-a
          B<-B-1
          C<-C-1
          row2<-row2-1
        }
    A<-A-1
    B<-steps+1-Z
    C<-steps-Z
    column2<-column2-1
    row2<-steps-Z
    Z<-Z+1
    }
  print(price)
  }

Option_American(100, 95,.05,20,252,.05, "C")