rirrxirr

Calculating the annualized internal rate of return


How would i write a simple function to calculate the annualized internal rate of return for the below dates and payments in csv form:

19/10/2003  -13275
19/11/2003  940.49
19/12/2003  884.71
19/01/2004  832.11
19/02/2004  782.49
19/03/2004  735.74
19/04/2004  691.64
19/05/2004  650.09
19/06/2004  610.91
19/07/2004  573.99
19/08/2004  539.2
19/09/2004  506.42
19/10/2004  475.54
19/11/2004  441.05
19/12/2004  413.91
19/01/2005  388.37
19/02/2005  364.31
19/03/2005  341.66
19/04/2005  320.34
19/05/2005  300.28
19/06/2005  281.39
19/07/2005  263.63
19/08/2005  246.91
19/09/2005  231.2
19/10/2005  216.41
19/11/2005  202.51
19/12/2005  189.43
19/01/2006  177.15
19/02/2006  165.6
19/03/2006  154.75
19/04/2006  144.55
19/05/2006  134.98
19/06/2006  125.99
19/07/2006  117.55
19/08/2006  109.62
19/09/2006  102.18
 C <- c(-13275, 940.49, 884.71, 832.11, 782.49, 735.74, 691.64, 650.09, 610.91, 573.99, 539.20, 506.42, 475.54, 441.05, 413.91, 388.37, 364.31, 341.66, 320.34, 300.28, 281.39, 263.63, 246.91, 231.20, 216.41, 202.51, 189.43, 177.15, 165.60, 154.75, 144.55, 134.98, 125.99, 117.55, 109.62, 102.18) 

D <- c("19-10-2003", "19-11-2003", "19-12-2003", "19-01-2004", "19-02-2004", "19-03-2004", "19-04-2004", "19-05-2004", "19-06-2004", "19-07-2004", "19-08-2004", "19-09-2004", "19-10-2004", "19-11-2004", "19-12-2004", "19-01-2005", "19-02-2005, "19-03-2005", "19-04-2005", "19-05-2005", "19-06-2005", "19-07-2005", "19-08-2005", "19-09-2005", "19-10-2005", "19-11-2005", "19-12-2005", "19-01-2006", "19-02-2006", "19-03-2006", "19-04-2006", "19-05-2006", "19-06-2006", "19-07-2006", "19-08-2006","19-09-2006")

I am new to R and desperate for some assistance with this problem. Once i have obtained the function i am required to create a package that will convert a csv into a single numeric vector which outputs the IRR for that cashflow.

I have already tried using the uniroot function but cannot seem to get the correct answer of 2.9%

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)

and have looked at

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)

But cannot seem to work out how this could be applied to my problem.


Solution

  • I managed to get the required 2.9% by doing the following:

    > C <- c(-13275, 940.49, 884.71, 832.11, 782.49, 735.74, 691.64, 650.09, 610.91, 573.99, 539.20, 506.42, 475.54, 441.05, 413.91, 388.37, 364.31, 341.66, 320.34, 300.28, 281.39, 263.63, 246.91, 231.20, 216.41, 202.51, 189.43, 177.15, 165.60, 154.75, 144.55, 134.98, 125.99, 117.55, 109.62, 102.18)
    > 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(C)
    [1] 0.002384391
    > var <- irr(C)
    > var
    [1] 0.002384391
    > AIRR <- (1+var)^12-1
    > AIRR
    [1] 0.02899093
    

    Thanks to all the suggestions in the comments.