Google Ad sense



All source codes in this blog are also available in Gregory Choi's Github Source code Repository


  

Saturday, March 5, 2016

How to calculate IRR (Internal Rate of Return) in R

[Problem]

Our company has a project that invests $100M upfront. The expected cash flows are like
Y1: $50M, Y2: $50M, Y3: $50M
We don't know the discount rate, but we want to know the BEP(Break Even Point) of the discount rate.

[Source Code]

#IRR (Internal Rate of Return)
#The discount rate that makes NPV value zero.
#You can't use IRR function, if there is a cash outflow in the future.
#IRR can be used only when the all cash outflows take place at time 0.
#Please make sure that cash flow(cf) should take a form of vector
#cf=c(50,50,50)
#<Example>
#f.irr(upfront=-100, cf=c(50,50,50))

f.irr = function(upfront, cf) {
  f.npv = function(x) {
      npv <- upfront
      for(i in 1:length(cf)) {
          npv <- npv + cf[i]/(1+x)^i
      }
      return(npv)
  }
  solution <- uniroot(f.npv, interval=c(0,1))
  return(solution$root)
}

[Example]

f.irr(upfront=-100, cf=c(50,50,50))
[1] 0.2337583
(23.3%)


No comments:

Post a Comment