内部收益率(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中的财务函数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码怎么了?

发布于 2014-02-13 02:31:28
你正在团结一致地寻找小数目,这会引起问题的宽容。尝试:
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发布于 2017-02-07 10:50:00
如果您有一个如下所示的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.105466https://stackoverflow.com/questions/21743967
复制相似问题