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

时间:2018-07-07 01:24:45

标签: r options finance

这是针对所有具有金融背景的R百事可乐。

您如何在二叉树中倒退,一路走高期权价格或期权价格?对于我的代码,我可以创建价格树和行使价树,但是我一生无法找到在树上倒退以获得美国人价格的方法。

我也是新手,所以我的代码可能很脏。我不知道。请帮我。我已经在下面粘贴了我的代码。

 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 个答案:

答案 0 :(得分:0)

我能够使它正常工作。我只需要确保它返回正确的价格即可。

    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")