1

内部收益率 (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可以计算分期付款,但令人讨厌的是我的结果与IRRMS Excel中的财务功能不同。

比如你从银行借了3600,管理费是0.006*360024个月等额本金,所以每个月都要还3600*0.006+3600/24=171.6

您产生的费用是cal_irr(3600,0.006,240) = 0.01104071每月,但在 Excel 中我得到了1.1054657%. 我的 R 代码有什么问题?

在此处输入图像描述

4

2 回答 2

3

您正在寻找小数字,这可能会导致公差问题。尝试:

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
于 2014-02-13T02:31:28.870 回答
0

如果你有一个看起来像这样的 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
于 2017-02-07T10:50:00.990 回答