Question

I'm currently looking for working and accurate implementations of Excel's RATE() and IRR(). I am coding in Javascript targeting modern browsers and Ruby 2.0.0 within Rails 4. My goal is to be able to calculate APR within a responsive Javascript application with a Rails back-end.

I've looked at and rejected the following options:

Finance Gem: No RATE() function. IRR() appears to hang for many test vectors.

Formula.js: No working RATE() function. IRR() does not work for many test vectors.

PHPExcel: Has a working RATE() function that I converted to Ruby. This failed to work exactly like the PHP version and failed for important test vectors. This likely does have an IRR() function but I have not yet tested it. I am not hopeful that I will find one that works but I will test it regardless.

I'm not interested in writing my own implementation from mathematical formulas. I prefer code that appears to have been soundly tested.

Update:

I have found a procedure for performing IRR() using R that may be accurate enough for our purposes: https://stat.ethz.ch/pipermail/r-help/2008-August/169619.html I will evaluate this solution over the next day or so and update my question if I feel satisfied by it.

Was it helpful?

Solution 2

After some testing, I am comfortable with this implementation of IRR() in R:

https://stat.ethz.ch/pipermail/r-help/2008-August/169619.html

Here it is in the form of a script runnable via the Rscript command:

args <- commandArgs(TRUE)

periods = as.numeric(args[1])
payment = as.numeric(args[2])
value = as.numeric(args[3])
monthsInYear = 12

timeline <- (0:periods)
payments <- payment + (0 * timeline)
payments[1] <- value

f <- function(r) sum(payments * exp(-r * timeline))
irr <- function(f) uniroot(f, c(0, 1))$root

irr(f) * monthsInYear

OTHER TIPS

I built a gem called Exonio:

github.com/Noverde/exonio

This gem implements some Excel formulas in Ruby, maybe it could helps you. To implement the IRR method, I use the Newton ruby module, which is a root finding algorithm.

Would love to know any improvements to that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top