package org.apache.poi.ss.formula.functions;
Calculates the internal rate of return.
Syntax is IRR(values) or IRR(values,guess)
- Author(s):
- Marcel May
- Yegor Kozlov
- See also:
- Wikipedia on IRR
- Excel IRR
if(args.length == 0 || args.length > 2) { double[] values = AggregateFunction.ValueCollector.collectValues(args[0]);
double result = irr(values, guess);
Computes the internal rate of return using an estimated irr of 10 percent.
- Parameters:
income the income values.- Returns:
- the irr.
public static double irr(double[] income) { return irr(income, 0.1d);
Calculates IRR using the Newton-Raphson Method.
Starting with the guess, the method cycles through the calculation until the result
is accurate within 0.00001 percent. If IRR can't find a result that works
after 20 tries, the Double.NaN<> is returned.
The implementation is inspired by the NewtonSolver from the Apache Commons-Math library,
- Parameters:
values the income values.guess the initial guess of irr.- Returns:
- the irr value. The method returns
Double.NaN
if the maximum iteration count is exceeded - See also:
- {http://commons.apache.org/}
- {http://en.wikipedia.org/wiki/Internal_rate_of_return.Numerical_solution}
- {http://en.wikipedia.org/wiki/Newton%27s_method}
public static double irr(double[] values, double guess) { int maxIterationCount = 20;
double absoluteAccuracy = 1E-7;
while (i < maxIterationCount) { for (int k = 0; k < values.length; k++) { fValue += values[k] / Math.pow(1.0 + x0, k);
fDerivative += -k * values[k] / Math.pow(1.0 + x0, k + 1);
x1 = x0 - fValue/fDerivative;
if (Math.abs(x1 - x0) <= absoluteAccuracy) {