From a3fdeb4da91d8aef50f40a3f9906494593ce2eca Mon Sep 17 00:00:00 2001 From: Phil Steitz Date: Mon, 13 Oct 2014 06:31:45 -0700 Subject: [PATCH] Changed inference classes to pass null RandomGenerators to distribution constructors. JIRA: MATH-1154. --- src/changes/changes.xml | 5 +++++ .../math3/stat/inference/BinomialTest.java | 3 ++- .../math3/stat/inference/ChiSquareTest.java | 14 ++++++++------ .../commons/math3/stat/inference/GTest.java | 18 ++++++++++-------- .../math3/stat/inference/MannWhitneyUTest.java | 3 ++- .../math3/stat/inference/OneWayAnova.java | 8 +++++--- .../commons/math3/stat/inference/TTest.java | 11 +++++++---- .../stat/inference/WilcoxonSignedRankTest.java | 3 ++- 8 files changed, 41 insertions(+), 24 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 7ea96c906..7762b8042 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -73,6 +73,11 @@ Users are encouraged to upgrade to this version as this release not 2. A few methods in the FastMath class are in fact slower that their counterpart in either Math or StrictMath (cf. MATH-740 and MATH-901). "> + + Changed classes in the inference package that instantiate distributions to + pass null RandomGenerators to avoid initialization overhead for the default + generator. + Added all Java 8 StrictMath methods to FastMath, so FastMath remains compatible with newer Java versions. diff --git a/src/main/java/org/apache/commons/math3/stat/inference/BinomialTest.java b/src/main/java/org/apache/commons/math3/stat/inference/BinomialTest.java index 8b914cb8a..2efe0910f 100644 --- a/src/main/java/org/apache/commons/math3/stat/inference/BinomialTest.java +++ b/src/main/java/org/apache/commons/math3/stat/inference/BinomialTest.java @@ -119,7 +119,8 @@ public class BinomialTest { throw new NullArgumentException(); } - final BinomialDistribution distribution = new BinomialDistribution(numberOfTrials, probability); + // pass a null rng to avoid unneeded overhead as we will not sample from this distribution + final BinomialDistribution distribution = new BinomialDistribution(null, numberOfTrials, probability); switch (alternativeHypothesis) { case GREATER_THAN: return 1 - distribution.cumulativeProbability(numberOfSuccesses - 1); diff --git a/src/main/java/org/apache/commons/math3/stat/inference/ChiSquareTest.java b/src/main/java/org/apache/commons/math3/stat/inference/ChiSquareTest.java index 09f55bf50..7e97ac14c 100644 --- a/src/main/java/org/apache/commons/math3/stat/inference/ChiSquareTest.java +++ b/src/main/java/org/apache/commons/math3/stat/inference/ChiSquareTest.java @@ -155,8 +155,9 @@ public class ChiSquareTest { throws NotPositiveException, NotStrictlyPositiveException, DimensionMismatchException, MaxCountExceededException { - ChiSquaredDistribution distribution = - new ChiSquaredDistribution(expected.length - 1.0); + // pass a null rng to avoid unneeded overhead as we will not sample from this distribution + final ChiSquaredDistribution distribution = + new ChiSquaredDistribution(null, expected.length - 1.0); return 1.0 - distribution.cumulativeProbability(chiSquare(expected, observed)); } @@ -311,8 +312,8 @@ public class ChiSquareTest { checkArray(counts); double df = ((double) counts.length -1) * ((double) counts[0].length - 1); - ChiSquaredDistribution distribution; - distribution = new ChiSquaredDistribution(df); + // pass a null rng to avoid unneeded overhead as we will not sample from this distribution + final ChiSquaredDistribution distribution = new ChiSquaredDistribution(df); return 1 - distribution.cumulativeProbability(chiSquare(counts)); } @@ -507,8 +508,9 @@ public class ChiSquareTest { throws DimensionMismatchException, NotPositiveException, ZeroException, MaxCountExceededException { - ChiSquaredDistribution distribution; - distribution = new ChiSquaredDistribution((double) observed1.length - 1); + // pass a null rng to avoid unneeded overhead as we will not sample from this distribution + final ChiSquaredDistribution distribution = + new ChiSquaredDistribution(null, (double) observed1.length - 1); return 1 - distribution.cumulativeProbability( chiSquareDataSetsComparison(observed1, observed2)); diff --git a/src/main/java/org/apache/commons/math3/stat/inference/GTest.java b/src/main/java/org/apache/commons/math3/stat/inference/GTest.java index a193644f5..de1fbe3e2 100644 --- a/src/main/java/org/apache/commons/math3/stat/inference/GTest.java +++ b/src/main/java/org/apache/commons/math3/stat/inference/GTest.java @@ -152,10 +152,10 @@ public class GTest { throws NotPositiveException, NotStrictlyPositiveException, DimensionMismatchException, MaxCountExceededException { + // pass a null rng to avoid unneeded overhead as we will not sample from this distribution final ChiSquaredDistribution distribution = - new ChiSquaredDistribution(expected.length - 1.0); - return 1.0 - distribution.cumulativeProbability( - g(expected, observed)); + new ChiSquaredDistribution(null, expected.length - 1.0); + return 1.0 - distribution.cumulativeProbability(g(expected, observed)); } /** @@ -183,10 +183,10 @@ public class GTest { throws NotPositiveException, NotStrictlyPositiveException, DimensionMismatchException, MaxCountExceededException { + // pass a null rng to avoid unneeded overhead as we will not sample from this distribution final ChiSquaredDistribution distribution = - new ChiSquaredDistribution(expected.length - 2.0); - return 1.0 - distribution.cumulativeProbability( - g(expected, observed)); + new ChiSquaredDistribution(null, expected.length - 2.0); + return 1.0 - distribution.cumulativeProbability(g(expected, observed)); } /** @@ -472,8 +472,10 @@ public class GTest { final long[] observed2) throws DimensionMismatchException, NotPositiveException, ZeroException, MaxCountExceededException { - final ChiSquaredDistribution distribution = new ChiSquaredDistribution( - (double) observed1.length - 1); + + // pass a null rng to avoid unneeded overhead as we will not sample from this distribution + final ChiSquaredDistribution distribution = + new ChiSquaredDistribution(null, (double) observed1.length - 1); return 1 - distribution.cumulativeProbability( gDataSetsComparison(observed1, observed2)); } diff --git a/src/main/java/org/apache/commons/math3/stat/inference/MannWhitneyUTest.java b/src/main/java/org/apache/commons/math3/stat/inference/MannWhitneyUTest.java index 0bb16d508..82fddb38d 100644 --- a/src/main/java/org/apache/commons/math3/stat/inference/MannWhitneyUTest.java +++ b/src/main/java/org/apache/commons/math3/stat/inference/MannWhitneyUTest.java @@ -181,7 +181,8 @@ public class MannWhitneyUTest { final double z = (Umin - EU) / FastMath.sqrt(VarU); // No try-catch or advertised exception because args are valid - final NormalDistribution standardNormal = new NormalDistribution(0, 1); + // pass a null rng to avoid unneeded overhead as we will not sample from this distribution + final NormalDistribution standardNormal = new NormalDistribution(null, 0, 1); return 2 * standardNormal.cumulativeProbability(z); } diff --git a/src/main/java/org/apache/commons/math3/stat/inference/OneWayAnova.java b/src/main/java/org/apache/commons/math3/stat/inference/OneWayAnova.java index e82731fae..d0c5fc17b 100644 --- a/src/main/java/org/apache/commons/math3/stat/inference/OneWayAnova.java +++ b/src/main/java/org/apache/commons/math3/stat/inference/OneWayAnova.java @@ -124,9 +124,10 @@ public class OneWayAnova { throws NullArgumentException, DimensionMismatchException, ConvergenceException, MaxCountExceededException { - AnovaStats a = anovaStats(categoryData); + final AnovaStats a = anovaStats(categoryData); // No try-catch or advertised exception because args are valid - FDistribution fdist = new FDistribution(a.dfbg, a.dfwg); + // pass a null rng to avoid unneeded overhead as we will not sample from this distribution + final FDistribution fdist = new FDistribution(null, a.dfbg, a.dfwg); return 1.0 - fdist.cumulativeProbability(a.F); } @@ -167,7 +168,8 @@ public class OneWayAnova { ConvergenceException, MaxCountExceededException { final AnovaStats a = anovaStats(categoryData, allowOneElementData); - final FDistribution fdist = new FDistribution(a.dfbg, a.dfwg); + // pass a null rng to avoid unneeded overhead as we will not sample from this distribution + final FDistribution fdist = new FDistribution(null, a.dfbg, a.dfwg); return 1.0 - fdist.cumulativeProbability(a.F); } diff --git a/src/main/java/org/apache/commons/math3/stat/inference/TTest.java b/src/main/java/org/apache/commons/math3/stat/inference/TTest.java index 9c1946ae3..b0f76f6d5 100644 --- a/src/main/java/org/apache/commons/math3/stat/inference/TTest.java +++ b/src/main/java/org/apache/commons/math3/stat/inference/TTest.java @@ -1056,8 +1056,9 @@ public class TTest { final double v, final double n) throws MaxCountExceededException, MathIllegalArgumentException { - double t = FastMath.abs(t(m, mu, v, n)); - TDistribution distribution = new TDistribution(n - 1); + final double t = FastMath.abs(t(m, mu, v, n)); + // pass a null rng to avoid unneeded overhead as we will not sample from this distribution + final TDistribution distribution = new TDistribution(null, n - 1); return 2.0 * distribution.cumulativeProbability(-t); } @@ -1086,7 +1087,8 @@ public class TTest { final double t = FastMath.abs(t(m1, m2, v1, v2, n1, n2)); final double degreesOfFreedom = df(v1, v2, n1, n2); - TDistribution distribution = new TDistribution(degreesOfFreedom); + // pass a null rng to avoid unneeded overhead as we will not sample from this distribution + final TDistribution distribution = new TDistribution(null, degreesOfFreedom); return 2.0 * distribution.cumulativeProbability(-t); } @@ -1115,7 +1117,8 @@ public class TTest { final double t = FastMath.abs(homoscedasticT(m1, m2, v1, v2, n1, n2)); final double degreesOfFreedom = n1 + n2 - 2; - TDistribution distribution = new TDistribution(degreesOfFreedom); + // pass a null rng to avoid unneeded overhead as we will not sample from this distribution + final TDistribution distribution = new TDistribution(null, degreesOfFreedom); return 2.0 * distribution.cumulativeProbability(-t); } diff --git a/src/main/java/org/apache/commons/math3/stat/inference/WilcoxonSignedRankTest.java b/src/main/java/org/apache/commons/math3/stat/inference/WilcoxonSignedRankTest.java index 3b2a5d076..bd4d7e24b 100644 --- a/src/main/java/org/apache/commons/math3/stat/inference/WilcoxonSignedRankTest.java +++ b/src/main/java/org/apache/commons/math3/stat/inference/WilcoxonSignedRankTest.java @@ -253,7 +253,8 @@ public class WilcoxonSignedRankTest { final double z = (Wmin - ES - 0.5) / FastMath.sqrt(VarS); // No try-catch or advertised exception because args are valid - final NormalDistribution standardNormal = new NormalDistribution(0, 1); + // pass a null rng to avoid unneeded overhead as we will not sample from this distribution + final NormalDistribution standardNormal = new NormalDistribution(null, 0, 1); return 2*standardNormal.cumulativeProbability(z); }