List of usage examples for org.apache.commons.math3.exception TooManyIterationsException TooManyIterationsException
public TooManyIterationsException(Number max)
From source file:cz.cuni.mff.spl.evaluator.statistics.KolmogorovSmirnovTestFlag.java
/** * Computes the Pelz-Good approximation for \(P(D_n < d)\) as described in [2] in the class javadoc. * * @param d value of d-statistic (x in [2]) * @param n sample size/*from www.j a v a 2 s .co m*/ * @return \(P(D_n < d)\) * @since 3.4 */ public double pelzGood(double d, int n) { // Change the variable since approximation is for the distribution evaluated at d / sqrt(n) final double sqrtN = FastMath.sqrt(n); final double z = d * sqrtN; final double z2 = d * d * n; final double z4 = z2 * z2; final double z6 = z4 * z2; final double z8 = z4 * z4; // Eventual return value double ret = 0; // Compute K_0(z) double sum = 0; double increment = 0; double kTerm = 0; double z2Term = MathUtils.PI_SQUARED / (8 * z2); int k = 1; for (; k < MAXIMUM_PARTIAL_SUM_COUNT; k++) { kTerm = 2 * k - 1; increment = FastMath.exp(-z2Term * kTerm * kTerm); sum += increment; if (increment <= PG_SUM_RELATIVE_ERROR * sum) { break; } } if (k == MAXIMUM_PARTIAL_SUM_COUNT) { throw new TooManyIterationsException(MAXIMUM_PARTIAL_SUM_COUNT); } ret = sum * FastMath.sqrt(2 * FastMath.PI) / z; // K_1(z) // Sum is -inf to inf, but k term is always (k + 1/2) ^ 2, so really have // twice the sum from k = 0 to inf (k = -1 is same as 0, -2 same as 1, ...) final double twoZ2 = 2 * z2; sum = 0; kTerm = 0; double kTerm2 = 0; for (k = 0; k < MAXIMUM_PARTIAL_SUM_COUNT; k++) { kTerm = k + 0.5; kTerm2 = kTerm * kTerm; increment = (MathUtils.PI_SQUARED * kTerm2 - z2) * FastMath.exp(-MathUtils.PI_SQUARED * kTerm2 / twoZ2); sum += increment; if (FastMath.abs(increment) < PG_SUM_RELATIVE_ERROR * FastMath.abs(sum)) { break; } } if (k == MAXIMUM_PARTIAL_SUM_COUNT) { throw new TooManyIterationsException(MAXIMUM_PARTIAL_SUM_COUNT); } final double sqrtHalfPi = FastMath.sqrt(FastMath.PI / 2); // Instead of doubling sum, divide by 3 instead of 6 ret += sum * sqrtHalfPi / (3 * z4 * sqrtN); // K_2(z) // Same drill as K_1, but with two doubly infinite sums, all k terms are even powers. final double z4Term = 2 * z4; final double z6Term = 6 * z6; z2Term = 5 * z2; final double pi4 = MathUtils.PI_SQUARED * MathUtils.PI_SQUARED; sum = 0; kTerm = 0; kTerm2 = 0; for (k = 0; k < MAXIMUM_PARTIAL_SUM_COUNT; k++) { kTerm = k + 0.5; kTerm2 = kTerm * kTerm; increment = (z6Term + z4Term + MathUtils.PI_SQUARED * (z4Term - z2Term) * kTerm2 + pi4 * (1 - twoZ2) * kTerm2 * kTerm2) * FastMath.exp(-MathUtils.PI_SQUARED * kTerm2 / twoZ2); sum += increment; if (FastMath.abs(increment) < PG_SUM_RELATIVE_ERROR * FastMath.abs(sum)) { break; } } if (k == MAXIMUM_PARTIAL_SUM_COUNT) { throw new TooManyIterationsException(MAXIMUM_PARTIAL_SUM_COUNT); } double sum2 = 0; kTerm2 = 0; for (k = 1; k < MAXIMUM_PARTIAL_SUM_COUNT; k++) { kTerm2 = k * k; increment = MathUtils.PI_SQUARED * kTerm2 * FastMath.exp(-MathUtils.PI_SQUARED * kTerm2 / twoZ2); sum2 += increment; if (FastMath.abs(increment) < PG_SUM_RELATIVE_ERROR * FastMath.abs(sum2)) { break; } } if (k == MAXIMUM_PARTIAL_SUM_COUNT) { throw new TooManyIterationsException(MAXIMUM_PARTIAL_SUM_COUNT); } // Again, adjust coefficients instead of doubling sum, sum2 ret += (sqrtHalfPi / n) * (sum / (36 * z2 * z2 * z2 * z) - sum2 / (18 * z2 * z)); // K_3(z) One more time with feeling - two doubly infinite sums, all k powers even. // Multiply coefficient denominators by 2, so omit doubling sums. final double pi6 = pi4 * MathUtils.PI_SQUARED; sum = 0; double kTerm4 = 0; double kTerm6 = 0; for (k = 0; k < MAXIMUM_PARTIAL_SUM_COUNT; k++) { kTerm = k + 0.5; kTerm2 = kTerm * kTerm; kTerm4 = kTerm2 * kTerm2; kTerm6 = kTerm4 * kTerm2; increment = (pi6 * kTerm6 * (5 - 30 * z2) + pi4 * kTerm4 * (-60 * z2 + 212 * z4) + MathUtils.PI_SQUARED * kTerm2 * (135 * z4 - 96 * z6) - 30 * z6 - 90 * z8) * FastMath.exp(-MathUtils.PI_SQUARED * kTerm2 / twoZ2); sum += increment; if (FastMath.abs(increment) < PG_SUM_RELATIVE_ERROR * FastMath.abs(sum)) { break; } } if (k == MAXIMUM_PARTIAL_SUM_COUNT) { throw new TooManyIterationsException(MAXIMUM_PARTIAL_SUM_COUNT); } sum2 = 0; for (k = 1; k < MAXIMUM_PARTIAL_SUM_COUNT; k++) { kTerm2 = k * k; kTerm4 = kTerm2 * kTerm2; increment = (-pi4 * kTerm4 + 3 * MathUtils.PI_SQUARED * kTerm2 * z2) * FastMath.exp(-MathUtils.PI_SQUARED * kTerm2 / twoZ2); sum2 += increment; if (FastMath.abs(increment) < PG_SUM_RELATIVE_ERROR * FastMath.abs(sum2)) { break; } } if (k == MAXIMUM_PARTIAL_SUM_COUNT) { throw new TooManyIterationsException(MAXIMUM_PARTIAL_SUM_COUNT); } return ret + (sqrtHalfPi / (sqrtN * n)) * (sum / (3240 * z6 * z4) + +sum2 / (108 * z6)); }
From source file:cz.cuni.mff.spl.evaluator.statistics.KolmogorovSmirnovTestFlag.java
/** * Computes \( 1 + 2 \sum_{i=1}^\infty (-1)^i e^{-2 i^2 t^2} \) stopping when successive partial * sums are within {@code tolerance} of one another, or when {@code maxIterations} partial sums * have been computed. If the sum does not converge before {@code maxIterations} iterations a * {@link TooManyIterationsException} is thrown. * * @param t argument/*from w ww.j ava 2s. c om*/ * @param tolerance Cauchy criterion for partial sums * @param maxIterations maximum number of partial sums to compute * @return Kolmogorov sum evaluated at t * @throws TooManyIterationsException if the series does not converge */ public double ksSum(double t, double tolerance, int maxIterations) { if (t == 0.0) { return 0.0; } // TODO: for small t (say less than 1), the alternative expansion in part 3 of [1] // from class javadoc should be used. final double x = -2 * t * t; int sign = -1; long i = 1; double partialSum = 0.5d; double delta = 1; while (delta > tolerance && i < maxIterations) { delta = FastMath.exp(x * i * i); partialSum += sign * delta; sign *= -1; i++; } if (i == maxIterations) { throw new TooManyIterationsException(maxIterations); } return partialSum * 2; }