jkndrkn - 1 year ago 119

Ruby Question

I'm currently looking for working and accurate implementations of Excel's

`RATE()`

`IRR()`

I've looked at and rejected the following options:

Finance Gem: No

`RATE()`

`IRR()`

Formula.js: No working

`RATE()`

`IRR()`

PHPExcel: Has a working

`RATE()`

`IRR()`

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

I have found a procedure for performing

`IRR()`

Answer Source

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
```