【发布时间】:2018-07-07 01:24:45
【问题描述】:
这是为所有具有金融背景的 R peps 准备的。
你到底是如何在二叉树中倒退的,一路上取行使价或期权价值的最大值?对于我的代码,我可以创建树以及行使价格树,但我一辈子都无法找出一种在树中倒退以获取美国价格的方法。
我也是新手,所以我的代码可能很脏。我不知道。请帮我。我在下面粘贴了我的代码。
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")
【问题讨论】:
-
您似乎不清楚这个概念,而不是编程。 quant.stackexchange.com 将为您提供更好的服务