Merged changes in MATH_1_1 branch to trunk. This includes revision 232577 through revision 234481.
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@239294 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
71fb92ebd4
commit
fd07147f86
|
@ -66,7 +66,19 @@ public class Complex implements Serializable {
|
||||||
if (isNaN()) {
|
if (isNaN()) {
|
||||||
return Double.NaN;
|
return Double.NaN;
|
||||||
}
|
}
|
||||||
return Math.sqrt(squareSum());
|
if (Math.abs(real) < Math.abs(imaginary)) {
|
||||||
|
if (imaginary == 0.0) {
|
||||||
|
return Math.abs(real);
|
||||||
|
}
|
||||||
|
double q = real / imaginary;
|
||||||
|
return (Math.abs(imaginary) * Math.sqrt(1 + q*q));
|
||||||
|
} else {
|
||||||
|
if (real == 0.0) {
|
||||||
|
return Math.abs(imaginary);
|
||||||
|
}
|
||||||
|
double q = imaginary / real;
|
||||||
|
return (Math.abs(real) * Math.sqrt(1 + q*q));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -109,16 +121,28 @@ public class Complex implements Serializable {
|
||||||
return NaN;
|
return NaN;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Math.abs(rhs.getReal()) < Math.abs(rhs.getImaginary())) {
|
double c = rhs.getReal();
|
||||||
double q = rhs.getReal() / rhs.getImaginary();
|
double d = rhs.getImaginary();
|
||||||
double d = (rhs.getReal() * q) + rhs.getImaginary();
|
if (c == 0.0 && d == 0.0) {
|
||||||
return new Complex(((real * q) + imaginary) / d,
|
throw new ArithmeticException("Error: division by zero.");
|
||||||
((imaginary * q) - real) / d);
|
}
|
||||||
|
|
||||||
|
if (Math.abs(c) < Math.abs(d)) {
|
||||||
|
if (d == 0.0) {
|
||||||
|
return new Complex(real/c, imaginary/c);
|
||||||
|
}
|
||||||
|
double q = c / d;
|
||||||
|
double denominator = c * q + d;
|
||||||
|
return new Complex((real * q + imaginary) / denominator,
|
||||||
|
(imaginary * q - real) / denominator);
|
||||||
} else {
|
} else {
|
||||||
double q = rhs.getImaginary() / rhs.getReal();
|
if (c == 0.0) {
|
||||||
double d = (rhs.getImaginary() * q) + rhs.getReal();
|
return new Complex(imaginary/d, -real/c);
|
||||||
return new Complex(((imaginary * q) + real) / d,
|
}
|
||||||
(imaginary - (real * q)) / d);
|
double q = d / c;
|
||||||
|
double denominator = d * q + c;
|
||||||
|
return new Complex((imaginary * q + real) / denominator,
|
||||||
|
(imaginary - real * q) / denominator);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -215,15 +239,6 @@ public class Complex implements Serializable {
|
||||||
return new Complex(-real, -imaginary);
|
return new Complex(-real, -imaginary);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the sum of the squared terms.
|
|
||||||
*
|
|
||||||
* @return the square sum.
|
|
||||||
*/
|
|
||||||
private double squareSum() {
|
|
||||||
return real * real + imaginary * imaginary;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the difference between this complex number and the given complex
|
* Return the difference between this complex number and the given complex
|
||||||
* number.
|
* number.
|
||||||
|
|
|
@ -18,7 +18,6 @@ package org.apache.commons.math.distribution;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
import org.apache.commons.math.MathException;
|
|
||||||
import org.apache.commons.math.util.MathUtils;
|
import org.apache.commons.math.util.MathUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -54,7 +53,8 @@ public class HypergeometricDistributionImpl extends AbstractIntegerDistribution
|
||||||
super();
|
super();
|
||||||
if (numberOfSuccesses > populationSize) {
|
if (numberOfSuccesses > populationSize) {
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
"number of successes must be less than or equal to population size");
|
"number of successes must be less than or equal to " +
|
||||||
|
"population size");
|
||||||
}
|
}
|
||||||
if (sampleSize > populationSize) {
|
if (sampleSize > populationSize) {
|
||||||
throw new IllegalArgumentException(
|
throw new IllegalArgumentException(
|
||||||
|
@ -69,10 +69,8 @@ public class HypergeometricDistributionImpl extends AbstractIntegerDistribution
|
||||||
* For this disbution, X, this method returns P(X ≤ x).
|
* For this disbution, X, this method returns P(X ≤ x).
|
||||||
* @param x the value at which the PDF is evaluated.
|
* @param x the value at which the PDF is evaluated.
|
||||||
* @return PDF for this distribution.
|
* @return PDF for this distribution.
|
||||||
* @throws MathException if the cumulative probability can not be
|
|
||||||
* computed due to convergence or other numerical errors.
|
|
||||||
*/
|
*/
|
||||||
public double cumulativeProbability(int x) throws MathException{
|
public double cumulativeProbability(int x) {
|
||||||
double ret;
|
double ret;
|
||||||
|
|
||||||
int n = getPopulationSize();
|
int n = getPopulationSize();
|
||||||
|
@ -84,11 +82,10 @@ public class HypergeometricDistributionImpl extends AbstractIntegerDistribution
|
||||||
ret = 0.0;
|
ret = 0.0;
|
||||||
} else if(x >= domain[1]) {
|
} else if(x >= domain[1]) {
|
||||||
ret = 1.0;
|
ret = 1.0;
|
||||||
|
} else if (x - domain[0] < domain[1] - x) {
|
||||||
|
ret = lowerCumulativeProbability(domain[0], x, n, m, k);
|
||||||
} else {
|
} else {
|
||||||
ret = 0.0;
|
ret = 1.0 - upperCumulativeProbability(x + 1, domain[1], n, m, k);
|
||||||
for (int i = domain[0]; i <= x; ++i){
|
|
||||||
ret += probability(n, m, k, i);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -181,6 +178,28 @@ public class HypergeometricDistributionImpl extends AbstractIntegerDistribution
|
||||||
return Math.min(k, m);
|
return Math.min(k, m);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* For this disbution, X, this method returns P(x0 ≤ X ≤ x1). This
|
||||||
|
* probability is computed by summing the point probabilities for the values
|
||||||
|
* x0, x0 + 1, x0 + 2, ..., x1, in that order.
|
||||||
|
* @param x0 the inclusive, lower bound
|
||||||
|
* @param x1 the inclusive, upper bound
|
||||||
|
* @param n the population size.
|
||||||
|
* @param m number of successes in the population.
|
||||||
|
* @param k the sample size.
|
||||||
|
* @return P(x0 ≤ X ≤ x1).
|
||||||
|
*/
|
||||||
|
private double lowerCumulativeProbability(
|
||||||
|
int x0, int x1, int n, int m, int k)
|
||||||
|
{
|
||||||
|
double ret;
|
||||||
|
ret = 0.0;
|
||||||
|
for (int i = x0; i <= x1; ++i) {
|
||||||
|
ret += probability(n, m, k, i);
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* For this disbution, X, this method returns P(X = x).
|
* For this disbution, X, this method returns P(X = x).
|
||||||
*
|
*
|
||||||
|
@ -258,4 +277,52 @@ public class HypergeometricDistributionImpl extends AbstractIntegerDistribution
|
||||||
}
|
}
|
||||||
sampleSize = size;
|
sampleSize = size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* For this disbution, X, this method returns P(X ≥ x).
|
||||||
|
* @param x the value at which the CDF is evaluated.
|
||||||
|
* @return upper tail CDF for this distribution.
|
||||||
|
*/
|
||||||
|
public double upperCumulativeProbability(int x) {
|
||||||
|
double ret;
|
||||||
|
|
||||||
|
int n = getPopulationSize();
|
||||||
|
int m = getNumberOfSuccesses();
|
||||||
|
int k = getSampleSize();
|
||||||
|
|
||||||
|
int[] domain = getDomain(n, m, k);
|
||||||
|
if (x < domain[0]) {
|
||||||
|
ret = 1.0;
|
||||||
|
} else if(x >= domain[1]) {
|
||||||
|
ret = 0.0;
|
||||||
|
} else if (x - domain[0] < domain[1] - x) {
|
||||||
|
ret = 1.0 - lowerCumulativeProbability(domain[0], x - 1, n, m, k);
|
||||||
|
} else {
|
||||||
|
ret = upperCumulativeProbability(x, domain[1], n, m, k);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* For this disbution, X, this method returns P(x0 ≤ X ≤ x1). This
|
||||||
|
* probability is computed by summing the point probabilities for the values
|
||||||
|
* x1, x1 - 1, x1 - 2, ..., x0, in that order.
|
||||||
|
* @param x0 the inclusive, lower bound
|
||||||
|
* @param x1 the inclusive, upper bound
|
||||||
|
* @param n the population size.
|
||||||
|
* @param m number of successes in the population.
|
||||||
|
* @param k the sample size.
|
||||||
|
* @return P(x0 ≤ X ≤ x1).
|
||||||
|
*/
|
||||||
|
private double upperCumulativeProbability(
|
||||||
|
int x0, int x1, int n, int m, int k)
|
||||||
|
{
|
||||||
|
double ret = 0.0;
|
||||||
|
for (int i = x1; i >= x0; --i) {
|
||||||
|
ret += probability(n, m, k, i);
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -155,7 +155,7 @@ public class Gamma implements Serializable {
|
||||||
ret = Double.NaN;
|
ret = Double.NaN;
|
||||||
} else if (x == 0.0) {
|
} else if (x == 0.0) {
|
||||||
ret = 0.0;
|
ret = 0.0;
|
||||||
} else if (a > 1.0 && x > a) {
|
} else if (a >= 1.0 && x > a) {
|
||||||
// use regularizedGammaQ because it should converge faster in this
|
// use regularizedGammaQ because it should converge faster in this
|
||||||
// case.
|
// case.
|
||||||
ret = 1.0 - regularizedGammaQ(a, x, epsilon, maxIterations);
|
ret = 1.0 - regularizedGammaQ(a, x, epsilon, maxIterations);
|
||||||
|
@ -231,7 +231,7 @@ public class Gamma implements Serializable {
|
||||||
ret = Double.NaN;
|
ret = Double.NaN;
|
||||||
} else if (x == 0.0) {
|
} else if (x == 0.0) {
|
||||||
ret = 1.0;
|
ret = 1.0;
|
||||||
} else if (x < a || a <= 1.0) {
|
} else if (x < a || a < 1.0) {
|
||||||
// use regularizedGammaP because it should converge faster in this
|
// use regularizedGammaP because it should converge faster in this
|
||||||
// case.
|
// case.
|
||||||
ret = 1.0 - regularizedGammaP(a, x, epsilon, maxIterations);
|
ret = 1.0 - regularizedGammaP(a, x, epsilon, maxIterations);
|
||||||
|
|
|
@ -604,8 +604,8 @@ public final class StatUtils {
|
||||||
double sum2 = 0d;
|
double sum2 = 0d;
|
||||||
double diff = 0d;
|
double diff = 0d;
|
||||||
int n = sample1.length;
|
int n = sample1.length;
|
||||||
if (n < 2) {
|
if (n < 2 || n != sample2.length) {
|
||||||
throw new IllegalArgumentException("Input array lengths must be at least 2.");
|
throw new IllegalArgumentException("Input array lengths must be equal and at least 2.");
|
||||||
}
|
}
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
diff = sample1[i] - sample2[i];
|
diff = sample1[i] - sample2[i];
|
||||||
|
|
|
@ -100,15 +100,24 @@ public abstract class ContinuedFraction implements Serializable {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* <p>
|
||||||
* Evaluates the continued fraction at the value x.
|
* Evaluates the continued fraction at the value x.
|
||||||
|
* </p>
|
||||||
*
|
*
|
||||||
* The implementation of this method is based on:
|
* <p>
|
||||||
|
* The implementation of this method is based on equations 14-17 of:
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>O. E-gecio-glu, C . K. Koc, J. Rifa i Coma,
|
* <li>
|
||||||
* <a href="http://citeseer.ist.psu.edu/egecioglu91fast.html">
|
* Eric W. Weisstein. "Continued Fraction." From MathWorld--A Wolfram Web
|
||||||
* On Fast Computation of Continued Fractions</a>, Computers Math. Applic.,
|
* Resource. <a target="_blank"
|
||||||
* 21(2--3), 1991, 167--169.</li>
|
* href="http://mathworld.wolfram.com/ContinuedFraction.html">
|
||||||
|
* http://mathworld.wolfram.com/ContinuedFraction.html</a>
|
||||||
|
* </li>
|
||||||
* </ul>
|
* </ul>
|
||||||
|
* The recurrence relationship defined in those equations can result in
|
||||||
|
* very large intermediate results which can result in numerical overflow.
|
||||||
|
* As a means to combat these overflow conditions, the intermediate results
|
||||||
|
* are scaled whenever they threaten to become numerically unstable.
|
||||||
*
|
*
|
||||||
* @param x the evaluation point.
|
* @param x the evaluation point.
|
||||||
* @param epsilon maximum error allowed.
|
* @param epsilon maximum error allowed.
|
||||||
|
@ -119,72 +128,50 @@ public abstract class ContinuedFraction implements Serializable {
|
||||||
public double evaluate(double x, double epsilon, int maxIterations)
|
public double evaluate(double x, double epsilon, int maxIterations)
|
||||||
throws MathException
|
throws MathException
|
||||||
{
|
{
|
||||||
double[][] f = new double[2][2];
|
double p0 = 1.0;
|
||||||
double[][] a = new double[2][2];
|
double p1 = getA(0, x);
|
||||||
double[][] an = new double[2][2];
|
double q0 = 0.0;
|
||||||
|
double q1 = 1.0;
|
||||||
|
double c = p1 / q1;
|
||||||
|
int n = 0;
|
||||||
|
double relativeError = Double.MAX_VALUE;
|
||||||
|
while (n < maxIterations && relativeError > epsilon) {
|
||||||
|
++n;
|
||||||
|
double a = getA(n, x);
|
||||||
|
double b = getB(n, x);
|
||||||
|
double p2 = a * p1 + b * p0;
|
||||||
|
double q2 = a * q1 + b * q0;
|
||||||
|
if (Double.isInfinite(p2) || Double.isInfinite(q2)) {
|
||||||
|
// need to scale
|
||||||
|
if (a != 0.0) {
|
||||||
|
p2 = p1 + (b / a * p0);
|
||||||
|
q2 = q1 + (b / a * q0);
|
||||||
|
} else if (b != 0) {
|
||||||
|
p2 = (a / b * p1) + p0;
|
||||||
|
q2 = (a / b * q1) + q0;
|
||||||
|
} else {
|
||||||
|
// can not scale an convergent is unbounded.
|
||||||
|
throw new ConvergenceException(
|
||||||
|
"Continued fraction convergents diverged to +/- " +
|
||||||
|
"infinity.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
double r = p2 / q2;
|
||||||
|
relativeError = Math.abs(r / c - 1.0);
|
||||||
|
|
||||||
a[0][0] = getA(0, x);
|
// prepare for next iteration
|
||||||
a[0][1] = 1.0;
|
c = p2 / q2;
|
||||||
a[1][0] = 1.0;
|
p0 = p1;
|
||||||
a[1][1] = 0.0;
|
p1 = p2;
|
||||||
|
q0 = q1;
|
||||||
return evaluate(1, x, a, an, f, epsilon, maxIterations);
|
q1 = q2;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Evaluates the n-th convergent, fn = pn / qn, for this continued fraction
|
|
||||||
* at the value x.
|
|
||||||
* @param n the convergent to compute.
|
|
||||||
* @param x the evaluation point.
|
|
||||||
* @param a (n-1)-th convergent matrix. (Input)
|
|
||||||
* @param an the n-th coefficient matrix. (Output)
|
|
||||||
* @param f the n-th convergent matrix. (Output)
|
|
||||||
* @param epsilon maximum error allowed.
|
|
||||||
* @param maxIterations maximum number of convergents
|
|
||||||
* @return the value of the the n-th convergent for this continued fraction
|
|
||||||
* evaluated at x.
|
|
||||||
* @throws MathException if the algorithm fails to converge.
|
|
||||||
*/
|
|
||||||
private double evaluate(
|
|
||||||
int n,
|
|
||||||
double x,
|
|
||||||
double[][] a,
|
|
||||||
double[][] an,
|
|
||||||
double[][] f,
|
|
||||||
double epsilon,
|
|
||||||
int maxIterations) throws MathException
|
|
||||||
{
|
|
||||||
double ret;
|
|
||||||
|
|
||||||
// create next matrix
|
|
||||||
an[0][0] = getA(n, x);
|
|
||||||
an[0][1] = 1.0;
|
|
||||||
an[1][0] = getB(n, x);
|
|
||||||
an[1][1] = 0.0;
|
|
||||||
|
|
||||||
// multiply a and an, save as f
|
|
||||||
f[0][0] = (a[0][0] * an[0][0]) + (a[0][1] * an[1][0]);
|
|
||||||
f[0][1] = (a[0][0] * an[0][1]) + (a[0][1] * an[1][1]);
|
|
||||||
f[1][0] = (a[1][0] * an[0][0]) + (a[1][1] * an[1][0]);
|
|
||||||
f[1][1] = (a[1][0] * an[0][1]) + (a[1][1] * an[1][1]);
|
|
||||||
|
|
||||||
// determine if we're close enough
|
|
||||||
if (Math.abs((f[0][0] * f[1][1]) - (f[1][0] * f[0][1])) <
|
|
||||||
Math.abs(epsilon * f[1][0] * f[1][1]))
|
|
||||||
{
|
|
||||||
ret = f[0][0] / f[1][0];
|
|
||||||
} else {
|
|
||||||
if (n >= maxIterations) {
|
if (n >= maxIterations) {
|
||||||
throw new ConvergenceException(
|
throw new ConvergenceException(
|
||||||
"Continued fraction convergents failed to converge.");
|
"Continued fraction convergents failed to converge.");
|
||||||
}
|
}
|
||||||
// compute next
|
|
||||||
ret = evaluate(n + 1, x, f /* new a */
|
|
||||||
, an /* reuse an */
|
|
||||||
, a /* new f */
|
|
||||||
, epsilon, maxIterations);
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
return c;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,17 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2003-2005 The Apache Software Foundation. Licensed under the Apache
|
* Copyright 2003-2004 The Apache Software Foundation.
|
||||||
* 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
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
|
* you may not use this file except in compliance with the License.
|
||||||
* or agreed to in writing, software distributed under the License is
|
* You may obtain a copy of the License at
|
||||||
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
*
|
||||||
* KIND, either express or implied. See the License for the specific language
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
* governing permissions and limitations under the License.
|
*
|
||||||
|
* 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.commons.math.util;
|
package org.apache.commons.math.util;
|
||||||
|
@ -15,9 +20,7 @@ import java.math.BigDecimal;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Some useful additions to the built-in functions in {@link Math}.
|
* Some useful additions to the built-in functions in {@link Math}.
|
||||||
*
|
* @version $Revision$ $Date$
|
||||||
* @version $Revision$ $Date: 2005-07-30 02:25:26 -0500 (Sat, 30 Jul
|
|
||||||
* 2005) $
|
|
||||||
*/
|
*/
|
||||||
public final class MathUtils {
|
public final class MathUtils {
|
||||||
|
|
||||||
|
@ -496,7 +499,7 @@ public final class MathUtils {
|
||||||
* @since 1.1
|
* @since 1.1
|
||||||
*/
|
*/
|
||||||
public static double round(double x, int scale, int roundingMethod) {
|
public static double round(double x, int scale, int roundingMethod) {
|
||||||
double sign = sign(x);
|
double sign = indicator(x);
|
||||||
double factor = Math.pow(10.0, scale) * sign;
|
double factor = Math.pow(10.0, scale) * sign;
|
||||||
return roundUnscaled(x * factor, sign, roundingMethod) / factor;
|
return roundUnscaled(x * factor, sign, roundingMethod) / factor;
|
||||||
}
|
}
|
||||||
|
@ -527,7 +530,7 @@ public final class MathUtils {
|
||||||
* @since 1.1
|
* @since 1.1
|
||||||
*/
|
*/
|
||||||
public static float round(float x, int scale, int roundingMethod) {
|
public static float round(float x, int scale, int roundingMethod) {
|
||||||
float sign = sign(x);
|
float sign = indicator(x);
|
||||||
float factor = (float)Math.pow(10.0f, scale) * sign;
|
float factor = (float)Math.pow(10.0f, scale) * sign;
|
||||||
return (float)roundUnscaled(x * factor, sign, roundingMethod) / factor;
|
return (float)roundUnscaled(x * factor, sign, roundingMethod) / factor;
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,8 @@
|
||||||
*/
|
*/
|
||||||
package org.apache.commons.math.distribution;
|
package org.apache.commons.math.distribution;
|
||||||
|
|
||||||
|
import org.apache.commons.math.MathException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <code>PoissonDistributionTest</code>
|
* <code>PoissonDistributionTest</code>
|
||||||
*
|
*
|
||||||
|
@ -133,4 +135,46 @@ public class PoissonDistributionTest extends IntegerDistributionAbstractTest {
|
||||||
dist.setMean(10.0);
|
dist.setMean(10.0);
|
||||||
assertEquals(10.0, dist.getMean(), 0.0);
|
assertEquals(10.0, dist.getMean(), 0.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testLargeMeanCumulativeProbability() {
|
||||||
|
PoissonDistribution dist = DistributionFactory.newInstance().createPoissonDistribution(1.0);
|
||||||
|
double mean = 1.0;
|
||||||
|
while (mean <= 10000000.0) {
|
||||||
|
dist.setMean(mean);
|
||||||
|
|
||||||
|
double x = mean * 2.0;
|
||||||
|
double dx = x / 10.0;
|
||||||
|
while (x >= 0) {
|
||||||
|
try {
|
||||||
|
dist.cumulativeProbability(x);
|
||||||
|
} catch (MathException ex) {
|
||||||
|
fail("mean of " + mean + " and x of " + x + " caused " + ex.getMessage());
|
||||||
|
}
|
||||||
|
x -= dx;
|
||||||
|
}
|
||||||
|
|
||||||
|
mean *= 10.0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testLargeMeanInverseCumulativeProbability() {
|
||||||
|
PoissonDistribution dist = DistributionFactory.newInstance().createPoissonDistribution(1.0);
|
||||||
|
double mean = 1.0;
|
||||||
|
while (mean <= 10000000.0) {
|
||||||
|
dist.setMean(mean);
|
||||||
|
|
||||||
|
double p = 0.1;
|
||||||
|
double dp = p;
|
||||||
|
while (p < 1.0) {
|
||||||
|
try {
|
||||||
|
dist.inverseCumulativeProbability(p);
|
||||||
|
} catch (MathException ex) {
|
||||||
|
fail("mean of " + mean + " and p of " + p + " caused " + ex.getMessage());
|
||||||
|
}
|
||||||
|
p += dp;
|
||||||
|
}
|
||||||
|
|
||||||
|
mean *= 10.0;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -422,6 +422,11 @@ public final class RealMatrixImplTest extends TestCase {
|
||||||
RealMatrix lu = m.getLUMatrix();
|
RealMatrix lu = m.getLUMatrix();
|
||||||
assertClose("LU decomposition", lu, (RealMatrix) new RealMatrixImpl(testDataLU), normTolerance);
|
assertClose("LU decomposition", lu, (RealMatrix) new RealMatrixImpl(testDataLU), normTolerance);
|
||||||
verifyDecomposition(m, lu);
|
verifyDecomposition(m, lu);
|
||||||
|
// access LU decomposition on same object to verify caching.
|
||||||
|
lu = m.getLUMatrix();
|
||||||
|
assertClose("LU decomposition", lu, (RealMatrix) new RealMatrixImpl(testDataLU), normTolerance);
|
||||||
|
verifyDecomposition(m, lu);
|
||||||
|
|
||||||
m = new RealMatrixImpl(luData);
|
m = new RealMatrixImpl(luData);
|
||||||
lu = m.getLUMatrix();
|
lu = m.getLUMatrix();
|
||||||
assertClose("LU decomposition", lu, (RealMatrix) new RealMatrixImpl(luDataLUDecomposition), normTolerance);
|
assertClose("LU decomposition", lu, (RealMatrix) new RealMatrixImpl(luDataLUDecomposition), normTolerance);
|
||||||
|
@ -642,6 +647,19 @@ public final class RealMatrixImplTest extends TestCase {
|
||||||
} catch (MatrixIndexException e) {
|
} catch (MatrixIndexException e) {
|
||||||
// expected
|
// expected
|
||||||
}
|
}
|
||||||
|
// dimension underflow
|
||||||
|
try {
|
||||||
|
m.setSubMatrix(testData,-1,1);
|
||||||
|
fail("expecting MatrixIndexException");
|
||||||
|
} catch (MatrixIndexException e) {
|
||||||
|
// expected
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
m.setSubMatrix(testData,1,-1);
|
||||||
|
fail("expecting MatrixIndexException");
|
||||||
|
} catch (MatrixIndexException e) {
|
||||||
|
// expected
|
||||||
|
}
|
||||||
|
|
||||||
// null
|
// null
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -20,6 +20,8 @@ import java.io.IOException;
|
||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
import org.apache.commons.math.TestUtils;
|
||||||
|
|
||||||
import junit.framework.Test;
|
import junit.framework.Test;
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
import junit.framework.TestSuite;
|
import junit.framework.TestSuite;
|
||||||
|
@ -111,6 +113,21 @@ public final class FrequencyTest extends TestCase {
|
||||||
assertEquals("one count", 3 , f.getCount("one"));
|
assertEquals("one count", 3 , f.getCount("one"));
|
||||||
assertEquals("Z cumulative pct -- case insensitive", 1 , f.getCumPct("Z"), tolerance);
|
assertEquals("Z cumulative pct -- case insensitive", 1 , f.getCumPct("Z"), tolerance);
|
||||||
assertEquals("z cumulative pct -- case insensitive", 1 , f.getCumPct("z"), tolerance);
|
assertEquals("z cumulative pct -- case insensitive", 1 , f.getCumPct("z"), tolerance);
|
||||||
|
|
||||||
|
f = null;
|
||||||
|
f = new Frequency();
|
||||||
|
assertEquals(0L, f.getCount('a'));
|
||||||
|
assertEquals(0L, f.getCumFreq('b'));
|
||||||
|
TestUtils.assertEquals(Double.NaN, f.getPct('a'), 0.0);
|
||||||
|
TestUtils.assertEquals(Double.NaN, f.getCumPct('b'), 0.0);
|
||||||
|
f.addValue('a');
|
||||||
|
f.addValue('b');
|
||||||
|
f.addValue('c');
|
||||||
|
f.addValue('d');
|
||||||
|
assertEquals(1L, f.getCount('a'));
|
||||||
|
assertEquals(2L, f.getCumFreq('b'));
|
||||||
|
assertEquals(0.25, f.getPct('a'), 0.0);
|
||||||
|
assertEquals(0.5, f.getCumPct('b'), 0.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** test pcts */
|
/** test pcts */
|
||||||
|
|
|
@ -371,6 +371,19 @@ public final class StatUtilsTest extends TestCase {
|
||||||
} catch (IllegalArgumentException ex) {
|
} catch (IllegalArgumentException ex) {
|
||||||
// expected
|
// expected
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
|
StatUtils.varianceDifference(sample1, small, meanDifference);
|
||||||
|
fail("Expecting IllegalArgumentException");
|
||||||
|
} catch (IllegalArgumentException ex) {
|
||||||
|
// expected
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
double[] single = {1.0};
|
||||||
|
StatUtils.varianceDifference(single, single, meanDifference);
|
||||||
|
fail("Expecting IllegalArgumentException");
|
||||||
|
} catch (IllegalArgumentException ex) {
|
||||||
|
// expected
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGeometricMean() throws Exception {
|
public void testGeometricMean() throws Exception {
|
||||||
|
@ -384,5 +397,7 @@ public final class StatUtilsTest extends TestCase {
|
||||||
test = new double[] {2, 4, 6, 8};
|
test = new double[] {2, 4, 6, 8};
|
||||||
assertEquals(Math.exp(0.25d * StatUtils.sumLog(test)),
|
assertEquals(Math.exp(0.25d * StatUtils.sumLog(test)),
|
||||||
StatUtils.geometricMean(test), Double.MIN_VALUE);
|
StatUtils.geometricMean(test), Double.MIN_VALUE);
|
||||||
|
assertEquals(Math.exp(0.5 * StatUtils.sumLog(test, 0, 2)),
|
||||||
|
StatUtils.geometricMean(test, 0, 2), Double.MIN_VALUE);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -72,6 +72,20 @@ public class PercentileTest extends UnivariateStatisticAbstractTest{
|
||||||
assertEquals(3.75, p.evaluate(d), 1.0e-5);
|
assertEquals(3.75, p.evaluate(d), 1.0e-5);
|
||||||
p.setQuantile(50);
|
p.setQuantile(50);
|
||||||
assertEquals(2.5, p.evaluate(d), 1.0e-5);
|
assertEquals(2.5, p.evaluate(d), 1.0e-5);
|
||||||
|
|
||||||
|
// invalid percentiles
|
||||||
|
try {
|
||||||
|
p.evaluate(d, 0, d.length, -1.0);
|
||||||
|
fail();
|
||||||
|
} catch (IllegalArgumentException ex) {
|
||||||
|
// success
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
p.evaluate(d, 0, d.length, 101.0);
|
||||||
|
fail();
|
||||||
|
} catch (IllegalArgumentException ex) {
|
||||||
|
// success
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testNISTExample() {
|
public void testNISTExample() {
|
||||||
|
|
|
@ -17,6 +17,8 @@ package org.apache.commons.math.util;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
|
||||||
|
import org.apache.commons.math.TestUtils;
|
||||||
|
|
||||||
import junit.framework.Test;
|
import junit.framework.Test;
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
import junit.framework.TestSuite;
|
import junit.framework.TestSuite;
|
||||||
|
@ -556,6 +558,12 @@ public final class MathUtilsTest extends TestCase {
|
||||||
} catch (IllegalArgumentException ex) {
|
} catch (IllegalArgumentException ex) {
|
||||||
// success
|
// success
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// special values
|
||||||
|
TestUtils.assertEquals(Float.NaN, MathUtils.round(Float.NaN, 2), 0.0f);
|
||||||
|
assertEquals(0.0f, MathUtils.round(0.0f, 2), 0.0f);
|
||||||
|
assertEquals(Float.POSITIVE_INFINITY, MathUtils.round(Float.POSITIVE_INFINITY, 2), 0.0f);
|
||||||
|
assertEquals(Float.NEGATIVE_INFINITY, MathUtils.round(Float.NEGATIVE_INFINITY, 2), 0.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testRoundDouble() {
|
public void testRoundDouble() {
|
||||||
|
@ -646,5 +654,11 @@ public final class MathUtilsTest extends TestCase {
|
||||||
} catch (IllegalArgumentException ex) {
|
} catch (IllegalArgumentException ex) {
|
||||||
// success
|
// success
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// special values
|
||||||
|
TestUtils.assertEquals(Double.NaN, MathUtils.round(Double.NaN, 2), 0.0);
|
||||||
|
assertEquals(0.0, MathUtils.round(0.0, 2), 0.0);
|
||||||
|
assertEquals(Double.POSITIVE_INFINITY, MathUtils.round(Double.POSITIVE_INFINITY, 2), 0.0);
|
||||||
|
assertEquals(Double.NEGATIVE_INFINITY, MathUtils.round(Double.NEGATIVE_INFINITY, 2), 0.0);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -50,6 +50,20 @@ Commons Math Release Notes</title>
|
||||||
and numerical utilities, and a PRNG pluggability framework making it
|
and numerical utilities, and a PRNG pluggability framework making it
|
||||||
possible to replace the JDK-supplied random number generator in
|
possible to replace the JDK-supplied random number generator in
|
||||||
commons-math (and elsewhere) with alternative PRNG implementations.">
|
commons-math (and elsewhere) with alternative PRNG implementations.">
|
||||||
|
<action dev="brentworden" type="fix" issue="36300" due-to="Nikhil Gupte">
|
||||||
|
Fixed division by zero error in rounding methods.
|
||||||
|
</action>
|
||||||
|
<action dev="brentworden" type="fix" issue="36215" due-to="Mike Hu">
|
||||||
|
Added upper tail cumulative probability method to HypergeometricDistributionImpl.
|
||||||
|
</action>
|
||||||
|
<action dev="brentworden" type="fix" issue="36205" due-to="Xiaogang Zhang">
|
||||||
|
Added better handling of numerical overflow and division by zero in
|
||||||
|
Complex calculations.
|
||||||
|
</action>
|
||||||
|
<action dev="brentworden" type="fix" issue="36105" due-to="Mikael Weigelt">
|
||||||
|
Changed ContinuedFraction to better handle infinite convergents that
|
||||||
|
resulted in divergent continued fraction evaluations.
|
||||||
|
</action>
|
||||||
<action dev="brentworden" type="fix" issue="35904" due-to="Srinivas Vemury">
|
<action dev="brentworden" type="fix" issue="35904" due-to="Srinivas Vemury">
|
||||||
Changed rounding methods to not rely on BigDecimal conversions which
|
Changed rounding methods to not rely on BigDecimal conversions which
|
||||||
was causing numerical error.
|
was causing numerical error.
|
||||||
|
|
Loading…
Reference in New Issue