R和MS Excel之间IRR计算的结果不同
内部收益率(IRR)或经济收益率(ERR)是资本预算中用来衡量和比较投资收益率的收益率。
我写了一些R代码来计算内部收益率(IRR),如下所示:
cal_irr <- function(amount,fee,duration) { cash<-c(amount,rep(-1*(amount*fee+amount/duration),duration)) NPV<-function(r){sum(cash /((1 + r) ^ (seq(along.with = cash)-1)))} return(uniroot(NPV, c(0, 1))$root) }
cal_irr
可以计算分期付款,但令人讨厌的是我的结果与MS Excel中的财务函数IRR
不同。
例如,你从银行借了3600,行政费是0.006*3600
,等于24个月的本金分期,所以每个月你要支付3600*0.006+3600/24=171.6
。
您的费用是cal_irr(3600,0.006,240) = 0.01104071
每个月,但在Excel中我得到了1.1054657%
。 我的R代码有什么问题?
你是unirootingfind小数字,这可能会导致宽容的问题。 尝试:
cal_irr <- function(amount,fee,duration) { cash<-c(amount,rep(-1*(amount*fee+amount/duration),duration)) NPV<-function(r){sum(cash /((1 + r) ^ (seq(along.with = cash)-1)))} return(uniroot(NPV, c(0, 1), tol=.0000001)$root)} cal_irr(3600,0.006,24) # [1] 0.01105466
如果你有一个看起来像这样的CF:
> cal_cash <- function(amount,fee,duration) c(amount,rep(-1*(amount*fee+amount/duration),duration)) > cal_cash(3600,0.006,24) [1] 3600.0 -171.6 -171.6 -171.6 -171.6 -171.6 -171.6 -171.6 -171.6 -171.6 -171.6 -171.6 -171.6 -171.6 -171.6 -171.6 -171.6 -171.6 -171.6 [20] -171.6 -171.6 -171.6 -171.6 -171.6 -171.6
那么使用financial
软件包来计算IRR是很容易的:
> require(financial) > cf(cal_cash(3600,0.006,24))$irr [1] -185.755352 1.105466