[英]Calculating XIRR using R
我試圖使用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)
}
您有一個數學問題,而不是編程問題;
接近-100%的收益率時,凈現值以上次現金流量的符號接近無窮大,因此無法計算內部收益率。
首先,讓我感謝您發表這篇文章,因為我已經找了很長時間了。 仍然您的功能無法控制NaN
。 您應該在while
循環[fval]
的表達式更改為
fval <- ifelse(is.na(fn(p.new, ...)),1,fn(p.new, ...))
希望能幫助到你。
tvm
軟件包中有一個xirr
函數。
library(tvm)
xirr_value <- xirr(cashflow, dates)
希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.