0

I have been doing stochastic cash flow modeling. In some of these scenarios, IRR is negative (cash flows out exceed cash flows in over time). R seems to hate this. I get a uniroot error. I have used the FinCal package irr function, and I even tried to write my own uniroot IRR formula. It's important that any formula solves for both positive and negative IRR scenarios.

Any suggestions or ideas? Is there an R package that handles this, or a simple uniroot formula?

Thank you!

James
  • 341
  • 3
  • 6
  • 1
    http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – shayaa Jul 25 '16 at 15:52
  • I think i figured out a way to make this work: uniroot(npv,c(-.2,1),cf=xd)$root where xd is the cash flows. – James Jul 27 '16 at 03:37
  • Share your solution with the SO community. :) – shayaa Jul 27 '16 at 03:38
  • How do I do that? Also, I just got the creators of the FinCal package to add irr2 which handles negative irr - but is slower than traditional irr – James Jul 27 '16 at 11:35
  • I am also relatively new to SO. Doesn't it give you a textbox to put your answer in? They give you a preview of your answer before you submit and a little tutorial on how to add links and whatnot. – shayaa Jul 27 '16 at 14:16
  • Well - maybe I spoke prematurely, the uniroot solution above works for simple cash flows but not the complex ones – James Jul 27 '16 at 23:39

1 Answers1

0

i ended up writing my own code (based upon irr from FinCal) where errors are ignored. I also changed the range to incorporate negative numbers when uniroot is looking for a solution. FYI - errors happen because a value is out of range or because there could be two solutions. Use irr4 to solve.

    irr3<-function (cf) 
    {  n <- length(cf)
    subcf <- cf[2:n]
    uniroot(function(r) -1 * pv.uneven(r, subcf) + cf[1], lower = -.2, upper =      .2, tol = .000001)$root}

   irr4<-function (x) {
   out<-tryCatch(irr3(x),error= function(e) NULL)
   return(out)}
James
  • 341
  • 3
  • 6
  • Updated version:irrplus<-function (cf) { n <- length(cf) subcf <- cf[2:n] uniroot(function(r) -1 * pv.uneven(r, subcf) + cf[1], lower = 0, upper = 2, tol = .000001)$root} irrneg<-function (cf) { n <- length(cf) subcf <- cf[2:n] uniroot(function(r) -1 * pv.uneven(r, subcf) + cf[1], lower = -.5, upper = 0, tol = .000001)$root} irr4<-function (x) { out<-tryCatch(irrplus(x),error= function(e) { out2<-tryCatch(irrneg(x),error= function(f) NULL) return(out2)}) return(out)} – James Oct 06 '16 at 18:33