我正在尝试使用R(类似于名为XIRR的Excel函数)计算非周期性现金流的IRR (内部收益率)。唯一的问题是它不能工作。我肯定需要一些帮助来找出哪里出了问题..
R>cashflow <- c(275,275,275,275,275,275,275,275,275,275,275,275,416.833333333333,416.833333333333,416.833333333333,416.833333333333,416.833333333333,416.833333333333,416.833333333333,416.833333333333,416.833333333333,416.833333333333,416.833333333333,416.833333333333,452.666666666667,452.666666666667,452.666666666667,452.666666666667,452.666666666667,452.666666666667,452.666666666667,452.666666666667,452.666666666667,452.666666666667,452.666666666667,452.666666666667,534.583333333333,534.583333333333,534.583333333333,534.583333333333,534.583333333333,534.583333333333,534.583333333333,534.583333333333,534.583333333333,534.583333333333,534.583333333333,534.583333333333,-9246.969046)
R>dates <- c("1997-01-31","1997-02-28","1997-03-31","1997-04-30","1997-05-31","1997-06-30","1997-07-31","1997-08-31","1997-09-30","1997-10-31","1997-11-30","1997-12-31","1998-01-31","1998-02-28","1998-03-31","1998-04-30","1998-05-31","1998-06-30","1998-07-31","1998-08-31","1998-09-30","1998-10-31","1998-11-30","1998-12-31","1999-01-31","1999-02-28","1999-03-31","1999-04-30","1999-05-31","1999-06-30","1999-07-31","1999-08-31","1999-09-30","1999-10-31","1999-11-30","1999-12-31","2000-01-31","2000-02-29","2000-03-31","2000-04-30","2000-05-31","2000-06-30","2000-07-31","2000-08-31","2000-09-30","2000-10-31","2000-11-30","2000-12-31","2001-01-31")
R>irr <- xirr(cashflow, dates)
它抛出错误:
Error in while (pchg >= tol & abs(fval) > tol & iter <= itmax) { : missing value where TRUE/FALSE needed
下面是函数:
xirr <- function(cf, dates) {
# Secant method.
secant <- function(par, fn, tol=1.e-07, itmax = 100, trace=FALSE, ...) {
# par = a starting vector with 2 starting values
# fn = a function whose first argument is the variable of interest
if (length(par) != 2) stop("You must specify a starting parameter vector of length 2")
p.2 <- par[1]
p.1 <- par[2]
f <- rep(NA, length(par))
f[1] <- fn(p.1, ...)
f[2] <- fn(p.2, ...)
iter <- 1
pchg <- abs(p.2 - p.1)
fval <- f[2]
if (trace) cat("par: ", par, "fval: ", f, "\n")
while (pchg >= tol & abs(fval) > tol & iter <= itmax) {
p.new <- p.2 - (p.2 - p.1) * f[2] / (f[2] - f[1])
pchg <- abs(p.new - p.2)
fval <- fn(p.new, ...)
p.1 <- p.2
p.2 <- p.new
f[1] <- f[2]
f[2] <- fval
iter <- iter + 1
if (trace) cat("par: ", p.new, "fval: ", fval, "\n")
}
list(par = p.new, value = fval, iter=iter)
}
# Net present value.
npv <- function(irr, cashflow, times) sum(cashflow / (1 + irr)^times)
times <- as.numeric(difftime(dates, dates[1], units="days"))/365.24
r <- secant(par=c(0,0.1), fn=npv, cashflow=cf, times=times)
return(r$par)
}
发布于 2013-01-15 18:52:11
你有一个数学问题,而不是编程问题;
对于−为100%的收益率,净现值与最后一次现金流的符号接近无穷大,因此无法计算内部收益率。
http://en.wikipedia.org/wiki/Internal_rate_of_return
发布于 2015-02-19 02:25:18
首先,让我感谢你写了这篇文章,因为我已经寻找这样一个函数很长一段时间了。但是,您的函数不能控制NaN
。您应该将while
循环中的[fval]
表达式更改为
fval <- ifelse(is.na(fn(p.new, ...)),1,fn(p.new, ...))
希望能有所帮助。
发布于 2019-10-05 19:20:43
tvm
包中有一个xirr
函数。
library(tvm)
xirr_value <- xirr(cashflow, dates)
希望这能有所帮助。
https://stackoverflow.com/questions/14259575
复制相似问题