vlambda博客
学习文章列表

二叉树法欧式看涨期权定价的代码


binarytree_eurocall=function(S,X,r,sigma,times,steps)

  

{

  R=exp(r*(times/steps))

  R_reciprocal=1/R

  u=exp(sigma*sqrt(times/steps))

  d=1/u

  u_square=u^2

  p_up=(R-d)/(u-d)

  p_down=1-p_up

  prices=numeric(steps+1)

  prices[1]=S*d^(steps)

  for(i in 2:(steps+1))

  {

    prices[i]=u_square*prices[i-1]

    

  }

  call_values=numeric(steps+1)

  for(i in 1:(steps+1))

  {

    call_values[i]=max(0,prices[i]-X)

  }

  for(j in steps:1)

  {

    for(i in 1:j)

    {

      call_values[i]=(p_up*call_values[i+1]+p_down*call_values[i])*R_reciprocal

    }

  }

  return(call_values[1])

}


S<-100

X<-100

r<-0.025

sigma<-0.25

times<-1

steps<-100


res1=binarytree_eurocall(S,X,r,sigma,times,steps)

res1


参考文献:

[1]罗文晋,朱顺泉. 基于R语言的金融工程计算[M]. 清华大学出版社, 2016.