Search code examples
rfinance

Any r package available to calculate IRR from uneven payments on specific dates?


Are there any R packages available that have some form of function that can calculate IRR based on uneven payments on specific dates for a lump sum distribution.

Example:

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

I'm looking for an easy way to calculate the rate of return of $4580 received today in exchange for the payment schedule defined above.

Thanks in advance, --JT


Solution

  • As already noted in the comments it would be easier to write something simple:

    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)
    

    gives a IRR of 11.26%

    EDIT:

    after a quick scout around the lifecontingencies package has a present value function if you want to use that instead.

    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)