https://issues.apache.org/bugzilla/show_bug.cgi?id=50409

--- Comment #5 from Yegor Kozlov <[email protected]> 2010-12-09 12:28:00 EST ---
The proposed implementation is based on the Secant solver which relies on the
initial guesses. You start with  the bracket [estimatedIrr / 2, estimatedIrr]
and it seems not to work if estimatedIrr  is negative or the sum of cash flows
is negative (that's what I concluded from debugging). 

BTW, the Commons-Math's Secant solver seems to handle it OK, at least, I'm
getting the same result as Excel. Here is my code (requires
commons-math-2.1.jar in the classpath):

import org.apache.commons.math.analysis.UnivariateRealFunction;
import org.apache.commons.math.analysis.solvers.UnivariateRealSolverFactory;
import org.apache.commons.math.analysis.solvers.UnivariateRealSolver;
import org.apache.commons.math.FunctionEvaluationException;
import org.apache.poi.ss.formula.functions.FinanceLib;
import junit.framework.TestCase;

/**
 * @author Yegor Kozlov
 */
public class TestCommonsMathSolvers extends TestCase {

    public static void main(String[] args) throws Exception {

        double[] values = new double[]{-70000d, 12000, 15000};
        double guess = -0.1; // not used by the Commons-Math's secant solver
        double irr = irr_secant(values, guess);
        assertEquals(-0.44, Math.round(irr * 100d) / 100d);
    }

    public static double irr_secant(final double[] values, final double guess)
throws Exception {https://issues.apache.org/bugzilla/show_bug.cgi?id=50409
        UnivariateRealFunction function = new UnivariateRealFunction(){
            public double value(double x) throws FunctionEvaluationException {
                return FinanceLib.npv(x, values);
            }
        };

        UnivariateRealSolverFactory factory =
UnivariateRealSolverFactory.newInstance();
        UnivariateRealSolver solver = factory.newSecantSolver();
        return solver.solve(function, values[0], values[values.length - 1],
guess);
    }
}

Do you have a clue what solver is used by Excel? My guess it is Secant or
Newton or a combination to assure convergence. 

Yegor

-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to