public static double irr(double[] values, double guess) {double x0 = guess;for (int i = 0; i < MAX_ITERATION_COUNT; i++) {final double factor = 1.0 + x0;double denominator = factor;if (denominator == 0) {return Double.NaN;}double fValue = values[0];double fDerivative = 0;for (int k = 1; k < values.length; k++) {final double value = values[k];fValue += value / denominator;denominator *= factor;fDerivative -= k * value / denominator;}if (fDerivative == 0) {return Double.NaN;}double x1 =  x0 - fValue/fDerivative;if (Math.abs(x1 - x0) <= ABSOLUTE_ACCURACY) {return x1;}x0 = x1;}return Double.NaN;}

public static double irr(double[] values, double guess){int maxIterationCount = 20;double absoluteAccuracy = 1E-7;double x0 = guess;double x1;int i = 0;while (i < maxIterationCount){double fValue = 0;double fDerivative = 0;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){return x1;}x0 = x1;++i;}return Double.NaN;}
