[英]Different results for IRR computation between R and MS Excel
The internal rate of return (IRR) or economic rate of return (ERR) is a rate of return used in capital budgeting to measure and compare the profitability of investments. 内部收益率(IRR)或经济收益率(ERR)是资本预算中用来衡量和比较投资收益率的收益率。
I wrote some R code to calculate the internal rate of return (IRR) like this: 我写了一些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
can calculate Instalment Payments, but the annoying thing is that my result is different from the financial function IRR
in MS Excel. cal_irr
可以计算分期付款,但令人讨厌的是,我的结果与MS Excel中的财务功能IRR
不同。
For example, you borrow 3600 from the bank, the administrative fee is 0.006*3600
, equal principal instalments in 24 months, so every month you have to pay 3600*0.006+3600/24=171.6
. 例如,您从银行借了3600,管理费是0.006*3600
,等于24个月的本金,所以每个月您都必须支付3600*0.006+3600/24=171.6
。
The cost you incur is cal_irr(3600,0.006,240) = 0.01104071
per month, but in Excel I got 1.1054657%
. 您产生的成本是cal_irr(3600,0.006,240) = 0.01104071
,但是在Excel中,我得到了1.1054657%
。 What is wrong with my R code? 我的R代码有什么问题?
You are unirooting to find small numbers, which can cause issues with tolerance. 您无所不能地找到了小数目,这可能会引起容忍度的问题。 Try: 尝试:
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
If you have a CF that looks like this: 如果您的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
Then it's easy to use the financial
package to compute the IRR: 然后,很容易使用financial
软件包来计算IRR:
> require(financial)
> cf(cal_cash(3600,0.006,24))$irr
[1] -185.755352 1.105466
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.