任何可用于计算特定日期不均匀付款的内部收益率的套餐?

时间:2012-07-25 23:25:47

标签: r finance

是否有任何可用的R套餐具有某种形式的功能,可根据特定日期的不均匀付款计算内部收益率。

示例:

df <- data.frame(date = c(as.Date("2010-1-24"), as.Date("2011-5-6"), as.Date("2012-3-24")), pmts=c(-2000,-1000,-800))
today <- as.Date("2012-7-25")
lumpsum <- 4580

我正在寻找一种简单的方法来计算今天收到的4580美元的回报率,以换取上面定义的付款时间表。

提前致谢, --JT

3 个答案:

答案 0 :(得分:7)

正如评论中已经指出的那样,写一些简单的东西会更容易:

NPV<-function(paym,pdates,IRR){
   ptimes<-as.Date(pdates)-min(as.Date(pdates))
   ptimes<-as.numeric(ptimes,units="days")/365.25
   NPV<-sum(paym*(1+IRR)^{-ptimes})
   NPV
}

nlm(function(p){NPV(c(lumpsum,df$pmts),c(today,df$date),p)^2},p=0.1)

的IRR为11.26%

编辑:

围绕lifecontingencies包的快速侦察有一个现值函数,如果你想使用它。

library(lifecontingencies)
capitals<-c(lumpsum,df$pmts)
times<-c(today,df$date)
times<-as.Date(times)-min(as.Date(times))
times<-as.numeric(times,units="days")/365.25
presentValue(cashFlows=capitals, timeIds=times,interestRates=0.03)
nlm(function(p){presentValue(capitals,times,p)^2},p=0.1)

答案 1 :(得分:6)

利用&#34;统计数据&#34;包uniroot功能IRR可编码如下:

   cf <- c(-10000, 1300, -1200, 12000) 
   npv <- function(i, cf, t=seq(along=cf)) sum(cf/(1+i)^t) 
   irr <- function(cf) { uniroot(npv, c(0,1), cf=cf)$root } 
   irr(cf)
   [1] 0.0686
   irrinpercent<- irr(cf)*100
   [1] 6.86

答案 2 :(得分:0)

pmr在他的代码示例中给出了一个很好的答案。谢谢!

但是,我在该代码中遇到的问题是,当现金流量具有任意时间时,问题(以及我的需要)是计算IRR。 pmr的代码需要修改,以使irr函数像npv函数一样将时间向量t作为arg。

为明确起见,我对该代码的修改是:

# Returns the Internal Rate of Return.
# See: https://www.investopedia.com/terms/i/irr.asp
irr = function(t, cf) {
    uniroot(f = npv, interval = c(0, 1), t = t, cf = cf)$root
}

# Returns the Net Present Value.
# See: https://www.investopedia.com/terms/n/npv.asp
npv = function(t, cf, i) {
    sum( cf / (1 + i)^t )
}

请注意,我更改了arg顺序(例如,先修改t)。同样,t没有默认值,但是如果您想要一个偶数序列,我认为landroni的评论是正确的:您投资的初始资本是时间= 0,而不是1,这在irr函数上方的investopedia链接中很明显

下面是一个如何使用我面对的功能的示例。我获得了投资ATM网络的机会。这些资产具有很高的年收益率(每月支付),而且是一种快速贬值的资产(也许您的本金的2%在最终清算时产生)。

首先定义上述功能后,执行以下代码。

# parameters:
numYears = 7
capitalInvest = 52000
retAnnual = 0.245
capitalLiquidation = 700

# convert yearly values to mpnthly:
numMonths = numYears * 12
retMonthly = retAnnual / 12    # assumes no compounding

# initialize the time to 0 and the cash flow to capital SPENT (so negative):
t = 0
cf = -capitalInvest

# add monthly returns:
for (m in 1:numMonths) {
    t = c(t, m / 12)    # divide the time by 12 to have units of year
    cf = c(cf, retMonthly * capitalInvest)
}

# add liquidation value also on the final year:
t = c(t, numMonths / 12)    # divide the time by 12 to have units of year
cf = c(cf, capitalLiquidation)

# calculate the IRR:
irr(t, cf)

该代码返回的值为0.1852015〜= 18.5%,与操作员引用我的18.6%的值非常接近。