Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* ====================================================================
     Licensed to the Apache Software Foundation (ASF) under one or more
     contributor license agreements.  See the NOTICE file distributed with
     this work for additional information regarding copyright ownership.
     The ASF licenses this file to You under the Apache License, Version 2.0
     (the "License"); you may not use this file except in compliance with
     the License.  You may obtain a copy of the License at
  
         http://www.apache.org/licenses/LICENSE-2.0
 
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
 ==================================================================== */
 
 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
 
 public final class Irr implements Function {
 
 
     public ValueEval evaluate(final ValueEval[] argsfinal int srcRowIndexfinal int srcColumnIndex) {
         if(args.length == 0 || args.length > 2) {
             // Wrong number of arguments
             return .;
         }
 
         try {
             double[] values = AggregateFunction.ValueCollector.collectValues(args[0]);
             double guess;
             if(args.length == 2) {
                 guess = NumericFunction.singleOperandEvaluate(args[1], srcRowIndexsrcColumnIndex);
             } else {
                 guess = 0.1d;
             }
             double result = irr(valuesguess);
             NumericFunction.checkValue(result);
             return new NumberEval(result);
         } catch (EvaluationException e){
             return e.getErrorEval();
         }
     }

    
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[] valuesdouble guess) {
         int maxIterationCount = 20;
         double absoluteAccuracy = 1E-7;
 
         double x0 = guess;
         double x1;
 
         int i = 0;
         while (i < maxIterationCount) {
 
             // the value of the function (NPV) and its derivate can be calculated in the same loop
            double fValue = 0;
            double fDerivative = 0;
            for (int k = 0; k < values.lengthk++) {
                fValue += values[k] / Math.pow(1.0 + x0k);
                fDerivative += -k * values[k] / Math.pow(1.0 + x0k + 1);
            }
            // the essense of the Newton-Raphson Method
            x1 = x0 - fValue/fDerivative;
            if (Math.abs(x1 - x0) <= absoluteAccuracy) {
                return x1;
            }
            x0 = x1;
            ++i;
        }
        // maximum number of iterations is exceeded
        return .;
    }
New to GrepCode? Check out our FAQ X