diff --git a/src/main/java/org/apache/commons/math4/distribution/AbstractRealDistribution.java b/src/main/java/org/apache/commons/math4/distribution/AbstractRealDistribution.java index f584253f2..1f6c006f7 100644 --- a/src/main/java/org/apache/commons/math4/distribution/AbstractRealDistribution.java +++ b/src/main/java/org/apache/commons/math4/distribution/AbstractRealDistribution.java @@ -48,23 +48,15 @@ public abstract class AbstractRealDistribution /** Default absolute accuracy for inverse cumulative computation. */ public static final double SOLVER_DEFAULT_ABSOLUTE_ACCURACY = 1e-6; /** Serializable version identifier */ - private static final long serialVersionUID = -38038050983108802L; + private static final long serialVersionUID = 20160311L; /** * RNG instance used to generate samples from the distribution. * @since 3.1 + * XXX: hard-coded value to prevent "NullPointerException". */ @Deprecated - protected final RandomGenerator random; - - /** - * @param rng Random number generator. - * @since 3.1 - */ - @Deprecated - protected AbstractRealDistribution(RandomGenerator rng) { - random = rng; - } + protected final RandomGenerator random = new org.apache.commons.math4.random.Well19937c(); /** * For a random variable {@code X} whose values are distributed according diff --git a/src/main/java/org/apache/commons/math4/distribution/BetaDistribution.java b/src/main/java/org/apache/commons/math4/distribution/BetaDistribution.java index 3009abfe9..0644eb128 100644 --- a/src/main/java/org/apache/commons/math4/distribution/BetaDistribution.java +++ b/src/main/java/org/apache/commons/math4/distribution/BetaDistribution.java @@ -18,8 +18,6 @@ package org.apache.commons.math4.distribution; import org.apache.commons.math4.exception.NumberIsTooSmallException; import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.math4.random.RandomGenerator; -import org.apache.commons.math4.random.Well19937c; import org.apache.commons.math4.rng.UniformRandomProvider; import org.apache.commons.math4.special.Beta; import org.apache.commons.math4.special.Gamma; @@ -39,7 +37,7 @@ public class BetaDistribution extends AbstractRealDistribution { */ public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9; /** Serializable version identifier. */ - private static final long serialVersionUID = -1221965979403477668L; + private static final long serialVersionUID = 20160311L; /** First shape parameter. */ private final double alpha; /** Second shape parameter. */ @@ -52,14 +50,7 @@ public class BetaDistribution extends AbstractRealDistribution { private final double solverAbsoluteAccuracy; /** - * Build a new instance. - *

- * Note: this constructor will implicitly create an instance of - * {@link Well19937c} as random generator to be used for sampling only (see - * {@link #sample()} and {@link #sample(int)}). In case no sampling is - * needed for the created distribution, it is advised to pass {@code null} - * as random generator via the appropriate constructors to avoid the - * additional initialisation overhead. + * Creates a new instance. * * @param alpha First shape parameter (must be positive). * @param beta Second shape parameter (must be positive). @@ -69,57 +60,19 @@ public class BetaDistribution extends AbstractRealDistribution { } /** - * Build a new instance. - *

- * Note: this constructor will implicitly create an instance of - * {@link Well19937c} as random generator to be used for sampling only (see - * {@link #sample()} and {@link #sample(int)}). In case no sampling is - * needed for the created distribution, it is advised to pass {@code null} - * as random generator via the appropriate constructors to avoid the - * additional initialisation overhead. + * Creates a new instance. * * @param alpha First shape parameter (must be positive). * @param beta Second shape parameter (must be positive). * @param inverseCumAccuracy Maximum absolute error in inverse * cumulative probability estimates (defaults to * {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). - * @since 2.1 - */ - public BetaDistribution(double alpha, double beta, double inverseCumAccuracy) { - this(new Well19937c(), alpha, beta, inverseCumAccuracy); - } - - /** - * Creates a β distribution. * - * @param rng Random number generator. - * @param alpha First shape parameter (must be positive). - * @param beta Second shape parameter (must be positive). - * @since 3.3 - */ - @Deprecated - public BetaDistribution(RandomGenerator rng, double alpha, double beta) { - this(rng, alpha, beta, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); - } - - /** - * Creates a β distribution. - * - * @param rng Random number generator. - * @param alpha First shape parameter (must be positive). - * @param beta Second shape parameter (must be positive). - * @param inverseCumAccuracy Maximum absolute error in inverse - * cumulative probability estimates (defaults to - * {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). * @since 3.1 */ - @Deprecated - public BetaDistribution(RandomGenerator rng, - double alpha, + public BetaDistribution(double alpha, double beta, double inverseCumAccuracy) { - super(rng); - this.alpha = alpha; this.beta = beta; z = Double.NaN; diff --git a/src/main/java/org/apache/commons/math4/distribution/CauchyDistribution.java b/src/main/java/org/apache/commons/math4/distribution/CauchyDistribution.java index 39870fb68..a16cf22a0 100644 --- a/src/main/java/org/apache/commons/math4/distribution/CauchyDistribution.java +++ b/src/main/java/org/apache/commons/math4/distribution/CauchyDistribution.java @@ -19,8 +19,6 @@ package org.apache.commons.math4.distribution; import org.apache.commons.math4.exception.NotStrictlyPositiveException; import org.apache.commons.math4.exception.OutOfRangeException; import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.math4.random.RandomGenerator; -import org.apache.commons.math4.random.Well19937c; import org.apache.commons.math4.util.FastMath; /** @@ -37,7 +35,7 @@ public class CauchyDistribution extends AbstractRealDistribution { */ public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9; /** Serializable version identifier */ - private static final long serialVersionUID = 8589540077390120676L; + private static final long serialVersionUID = 20160311L; /** The median of this distribution. */ private final double median; /** The scale of this distribution. */ @@ -54,31 +52,19 @@ public class CauchyDistribution extends AbstractRealDistribution { } /** - * Creates a Cauchy distribution using the given median and scale. - *

- * Note: this constructor will implicitly create an instance of - * {@link Well19937c} as random generator to be used for sampling only (see - * {@link #sample()} and {@link #sample(int)}). In case no sampling is - * needed for the created distribution, it is advised to pass {@code null} - * as random generator via the appropriate constructors to avoid the - * additional initialisation overhead. + * Creates a distribution. * * @param median Median for this distribution. * @param scale Scale parameter for this distribution. + * @throws NotStrictlyPositiveException if {@code scale <= 0}. */ - public CauchyDistribution(double median, double scale) { + public CauchyDistribution(double median, + double scale) { this(median, scale, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); } /** - * Creates a Cauchy distribution using the given median and scale. - *

- * Note: this constructor will implicitly create an instance of - * {@link Well19937c} as random generator to be used for sampling only (see - * {@link #sample()} and {@link #sample(int)}). In case no sampling is - * needed for the created distribution, it is advised to pass {@code null} - * as random generator via the appropriate constructors to avoid the - * additional initialisation overhead. + * Creates a distribution. * * @param median Median for this distribution. * @param scale Scale parameter for this distribution. @@ -86,43 +72,10 @@ public class CauchyDistribution extends AbstractRealDistribution { * cumulative probability estimates * (defaults to {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). * @throws NotStrictlyPositiveException if {@code scale <= 0}. - * @since 2.1 */ - public CauchyDistribution(double median, double scale, - double inverseCumAccuracy) { - this(new Well19937c(), median, scale, inverseCumAccuracy); - } - - /** - * Creates a Cauchy distribution. - * - * @param rng Random number generator. - * @param median Median for this distribution. - * @param scale Scale parameter for this distribution. - * @throws NotStrictlyPositiveException if {@code scale <= 0}. - * @since 3.3 - */ - public CauchyDistribution(RandomGenerator rng, double median, double scale) { - this(rng, median, scale, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); - } - - /** - * Creates a Cauchy distribution. - * - * @param rng Random number generator. - * @param median Median for this distribution. - * @param scale Scale parameter for this distribution. - * @param inverseCumAccuracy Maximum absolute error in inverse - * cumulative probability estimates - * (defaults to {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). - * @throws NotStrictlyPositiveException if {@code scale <= 0}. - * @since 3.1 - */ - public CauchyDistribution(RandomGenerator rng, - double median, + public CauchyDistribution(double median, double scale, double inverseCumAccuracy) { - super(rng); if (scale <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.SCALE, scale); } diff --git a/src/main/java/org/apache/commons/math4/distribution/ChiSquaredDistribution.java b/src/main/java/org/apache/commons/math4/distribution/ChiSquaredDistribution.java index b91754bbb..afd35c0bf 100644 --- a/src/main/java/org/apache/commons/math4/distribution/ChiSquaredDistribution.java +++ b/src/main/java/org/apache/commons/math4/distribution/ChiSquaredDistribution.java @@ -16,9 +16,6 @@ */ package org.apache.commons.math4.distribution; -import org.apache.commons.math4.random.RandomGenerator; -import org.apache.commons.math4.random.Well19937c; - /** * Implementation of the chi-squared distribution. * @@ -32,14 +29,14 @@ public class ChiSquaredDistribution extends AbstractRealDistribution { */ public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9; /** Serializable version identifier */ - private static final long serialVersionUID = -8352658048349159782L; + private static final long serialVersionUID = 20160311L; /** Internal Gamma distribution. */ private final GammaDistribution gamma; /** Inverse cumulative probability accuracy */ private final double solverAbsoluteAccuracy; /** - * Create a Chi-Squared distribution with the given degrees of freedom. + * Creates distribution with the given degrees of freedom. * * @param degreesOfFreedom Degrees of freedom. */ @@ -48,54 +45,18 @@ public class ChiSquaredDistribution extends AbstractRealDistribution { } /** - * Create a Chi-Squared distribution with the given degrees of freedom and + * Creates a distribution with the given degrees of freedom and * inverse cumulative probability accuracy. - *

- * Note: this constructor will implicitly create an instance of - * {@link Well19937c} as random generator to be used for sampling only (see - * {@link #sample()} and {@link #sample(int)}). In case no sampling is - * needed for the created distribution, it is advised to pass {@code null} - * as random generator via the appropriate constructors to avoid the - * additional initialisation overhead. * * @param degreesOfFreedom Degrees of freedom. * @param inverseCumAccuracy the maximum absolute error in inverse * cumulative probability estimates (defaults to * {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). + * * @since 2.1 */ public ChiSquaredDistribution(double degreesOfFreedom, double inverseCumAccuracy) { - this(new Well19937c(), degreesOfFreedom, inverseCumAccuracy); - } - - /** - * Create a Chi-Squared distribution with the given degrees of freedom. - * - * @param rng Random number generator. - * @param degreesOfFreedom Degrees of freedom. - * @since 3.3 - */ - public ChiSquaredDistribution(RandomGenerator rng, double degreesOfFreedom) { - this(rng, degreesOfFreedom, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); - } - - /** - * Create a Chi-Squared distribution with the given degrees of freedom and - * inverse cumulative probability accuracy. - * - * @param rng Random number generator. - * @param degreesOfFreedom Degrees of freedom. - * @param inverseCumAccuracy the maximum absolute error in inverse - * cumulative probability estimates (defaults to - * {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). - * @since 3.1 - */ - public ChiSquaredDistribution(RandomGenerator rng, - double degreesOfFreedom, - double inverseCumAccuracy) { - super(rng); - gamma = new GammaDistribution(degreesOfFreedom / 2, 2); solverAbsoluteAccuracy = inverseCumAccuracy; } diff --git a/src/main/java/org/apache/commons/math4/distribution/ConstantRealDistribution.java b/src/main/java/org/apache/commons/math4/distribution/ConstantRealDistribution.java index 862fe7551..a9b3383bc 100644 --- a/src/main/java/org/apache/commons/math4/distribution/ConstantRealDistribution.java +++ b/src/main/java/org/apache/commons/math4/distribution/ConstantRealDistribution.java @@ -39,7 +39,6 @@ public class ConstantRealDistribution extends AbstractRealDistribution { * @param value the constant value of this distribution */ public ConstantRealDistribution(double value) { - super(null); // Avoid creating RandomGenerator this.value = value; } diff --git a/src/main/java/org/apache/commons/math4/distribution/EnumeratedDistribution.java b/src/main/java/org/apache/commons/math4/distribution/EnumeratedDistribution.java index 40af6e4d0..fd339b271 100644 --- a/src/main/java/org/apache/commons/math4/distribution/EnumeratedDistribution.java +++ b/src/main/java/org/apache/commons/math4/distribution/EnumeratedDistribution.java @@ -30,7 +30,6 @@ import org.apache.commons.math4.exception.NotStrictlyPositiveException; import org.apache.commons.math4.exception.NullArgumentException; import org.apache.commons.math4.exception.util.LocalizedFormats; import org.apache.commons.math4.random.RandomGenerator; -import org.apache.commons.math4.random.Well19937c; import org.apache.commons.math4.rng.UniformRandomProvider; import org.apache.commons.math4.util.MathArrays; import org.apache.commons.math4.util.Pair; @@ -61,7 +60,7 @@ public class EnumeratedDistribution implements Serializable { * RNG instance used to generate samples from the distribution. */ @Deprecated - protected final RandomGenerator random; + protected RandomGenerator random = null; /** * List of random variable values. @@ -81,45 +80,57 @@ public class EnumeratedDistribution implements Serializable { private final double[] cumulativeProbabilities; /** - * Create an enumerated distribution using the given probability mass function - * enumeration. - *

- * Note: this constructor will implicitly create an instance of - * {@link Well19937c} as random generator to be used for sampling only (see - * {@link #sample()} and {@link #sample(int)}). In case no sampling is - * needed for the created distribution, it is advised to pass {@code null} - * as random generator via the appropriate constructors to avoid the - * additional initialisation overhead. - * - * @param pmf probability mass function enumerated as a list of - * pairs. - * @throws NotPositiveException if any of the probabilities are negative. - * @throws NotFiniteNumberException if any of the probabilities are infinite. - * @throws NotANumberException if any of the probabilities are NaN. - * @throws MathArithmeticException all of the probabilities are 0. + * XXX TODO: remove once "EnumeratedIntegerDistribution" has been changed. */ - public EnumeratedDistribution(final List> pmf) + @Deprecated + public EnumeratedDistribution(final RandomGenerator rng, final List> pmf) throws NotPositiveException, MathArithmeticException, NotFiniteNumberException, NotANumberException { - this(new Well19937c(), pmf); + random = rng; + singletons = new ArrayList(pmf.size()); + final double[] probs = new double[pmf.size()]; + + for (int i = 0; i < pmf.size(); i++) { + final Pair sample = pmf.get(i); + singletons.add(sample.getKey()); + final double p = sample.getValue(); + if (p < 0) { + throw new NotPositiveException(sample.getValue()); + } + if (Double.isInfinite(p)) { + throw new NotFiniteNumberException(p); + } + if (Double.isNaN(p)) { + throw new NotANumberException(); + } + probs[i] = p; + } + + probabilities = MathArrays.normalizeArray(probs, 1.0); + + cumulativeProbabilities = new double[probabilities.length]; + double sum = 0; + for (int i = 0; i < probabilities.length; i++) { + sum += probabilities[i]; + cumulativeProbabilities[i] = sum; + } } /** * Create an enumerated distribution using the given random number generator * and probability mass function enumeration. * - * @param rng random number generator. - * @param pmf probability mass function enumerated as a list of - * pairs. + * @param pmf probability mass function enumerated as a list of + * {@code } pairs. * @throws NotPositiveException if any of the probabilities are negative. * @throws NotFiniteNumberException if any of the probabilities are infinite. * @throws NotANumberException if any of the probabilities are NaN. * @throws MathArithmeticException all of the probabilities are 0. */ - @Deprecated - public EnumeratedDistribution(final RandomGenerator rng, final List> pmf) - throws NotPositiveException, MathArithmeticException, NotFiniteNumberException, NotANumberException { - random = rng; - + public EnumeratedDistribution(final List> pmf) + throws NotPositiveException, + MathArithmeticException, + NotFiniteNumberException, + NotANumberException { singletons = new ArrayList(pmf.size()); final double[] probs = new double[pmf.size()]; diff --git a/src/main/java/org/apache/commons/math4/distribution/EnumeratedRealDistribution.java b/src/main/java/org/apache/commons/math4/distribution/EnumeratedRealDistribution.java index 9e03b2bb7..7cf834bd4 100644 --- a/src/main/java/org/apache/commons/math4/distribution/EnumeratedRealDistribution.java +++ b/src/main/java/org/apache/commons/math4/distribution/EnumeratedRealDistribution.java @@ -28,8 +28,6 @@ import org.apache.commons.math4.exception.NotANumberException; import org.apache.commons.math4.exception.NotFiniteNumberException; import org.apache.commons.math4.exception.NotPositiveException; import org.apache.commons.math4.exception.OutOfRangeException; -import org.apache.commons.math4.random.RandomGenerator; -import org.apache.commons.math4.random.Well19937c; import org.apache.commons.math4.rng.UniformRandomProvider; import org.apache.commons.math4.util.Pair; @@ -37,7 +35,7 @@ import org.apache.commons.math4.util.Pair; *

Implementation of a real-valued {@link EnumeratedDistribution}. * *

Values with zero-probability are allowed but they do not extend the - * support.
+ * support.
* Duplicate values are allowed. Probabilities of duplicate values are combined * when computing cumulative probabilities and statistics.

* @@ -46,7 +44,7 @@ import org.apache.commons.math4.util.Pair; public class EnumeratedRealDistribution extends AbstractRealDistribution { /** Serializable UID. */ - private static final long serialVersionUID = 20130308L; + private static final long serialVersionUID = 20160311L; /** * {@link EnumeratedDistribution} (using the {@link Double} wrapper) @@ -54,37 +52,10 @@ public class EnumeratedRealDistribution extends AbstractRealDistribution { */ protected final EnumeratedDistribution innerDistribution; - /** - * Create a discrete real-valued distribution using the given probability mass function - * enumeration. - *

- * Note: this constructor will implicitly create an instance of - * {@link Well19937c} as random generator to be used for sampling only (see - * {@link #sample()} and {@link #sample(int)}). In case no sampling is - * needed for the created distribution, it is advised to pass {@code null} - * as random generator via the appropriate constructors to avoid the - * additional initialisation overhead. - * - * @param singletons array of random variable values. - * @param probabilities array of probabilities. - * @throws DimensionMismatchException if - * {@code singletons.length != probabilities.length} - * @throws NotPositiveException if any of the probabilities are negative. - * @throws NotFiniteNumberException if any of the probabilities are infinite. - * @throws NotANumberException if any of the probabilities are NaN. - * @throws MathArithmeticException all of the probabilities are 0. - */ - public EnumeratedRealDistribution(final double[] singletons, final double[] probabilities) - throws DimensionMismatchException, NotPositiveException, MathArithmeticException, - NotFiniteNumberException, NotANumberException { - this(new Well19937c(), singletons, probabilities); - } - /** * Create a discrete real-valued distribution using the given random number generator * and probability mass function enumeration. * - * @param rng random number generator. * @param singletons array of random variable values. * @param probabilities array of probabilities. * @throws DimensionMismatchException if @@ -94,28 +65,23 @@ public class EnumeratedRealDistribution extends AbstractRealDistribution { * @throws NotANumberException if any of the probabilities are NaN. * @throws MathArithmeticException all of the probabilities are 0. */ - @Deprecated - public EnumeratedRealDistribution(final RandomGenerator rng, - final double[] singletons, final double[] probabilities) - throws DimensionMismatchException, NotPositiveException, MathArithmeticException, - NotFiniteNumberException, NotANumberException { - super(rng); - - innerDistribution = new EnumeratedDistribution( - rng, createDistribution(singletons, probabilities)); + public EnumeratedRealDistribution(final double[] singletons, + final double[] probabilities) + throws DimensionMismatchException, + NotPositiveException, + MathArithmeticException, + NotFiniteNumberException, + NotANumberException { + innerDistribution = new EnumeratedDistribution(createDistribution(singletons, probabilities)); } /** - * Create a discrete real-valued distribution from the input data. Values are assigned - * mass based on their frequency. + * Creates a discrete real-valued distribution from the input data. + * Values are assigned mass based on their frequency. * - * @param rng random number generator used for sampling * @param data input dataset - * @since 3.6 */ - @Deprecated - public EnumeratedRealDistribution(final RandomGenerator rng, final double[] data) { - super(rng); + public EnumeratedRealDistribution(final double[] data) { final Map dataMap = new HashMap(); for (double value : data) { Integer count = dataMap.get(value); @@ -134,20 +100,9 @@ public class EnumeratedRealDistribution extends AbstractRealDistribution { probabilities[index] = entry.getValue().intValue() / denom; index++; } - innerDistribution = new EnumeratedDistribution(rng, createDistribution(values, probabilities)); + innerDistribution = new EnumeratedDistribution(createDistribution(values, probabilities)); } - /** - * Create a discrete real-valued distribution from the input data. Values are assigned - * mass based on their frequency. For example, [0,1,1,2] as input creates a distribution - * with values 0, 1 and 2 having probability masses 0.25, 0.5 and 0.25 respectively, - * - * @param data input dataset - * @since 3.6 - */ - public EnumeratedRealDistribution(final double[] data) { - this(new Well19937c(), data); - } /** * Create the list of Pairs representing the distribution from singletons and probabilities. * diff --git a/src/main/java/org/apache/commons/math4/distribution/ExponentialDistribution.java b/src/main/java/org/apache/commons/math4/distribution/ExponentialDistribution.java index 133321e36..1dbd6b181 100644 --- a/src/main/java/org/apache/commons/math4/distribution/ExponentialDistribution.java +++ b/src/main/java/org/apache/commons/math4/distribution/ExponentialDistribution.java @@ -19,8 +19,6 @@ package org.apache.commons.math4.distribution; import org.apache.commons.math4.exception.NotStrictlyPositiveException; import org.apache.commons.math4.exception.OutOfRangeException; import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.math4.random.RandomGenerator; -import org.apache.commons.math4.random.Well19937c; import org.apache.commons.math4.rng.UniformRandomProvider; import org.apache.commons.math4.util.CombinatoricsUtils; import org.apache.commons.math4.util.FastMath; @@ -39,7 +37,7 @@ public class ExponentialDistribution extends AbstractRealDistribution { */ public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9; /** Serializable version identifier */ - private static final long serialVersionUID = 2401296428283614780L; + private static final long serialVersionUID = 20160311L; /** * Used when generating Exponential samples. * Table containing the constants @@ -91,14 +89,7 @@ public class ExponentialDistribution extends AbstractRealDistribution { } /** - * Create an exponential distribution with the given mean. - *

- * Note: this constructor will implicitly create an instance of - * {@link Well19937c} as random generator to be used for sampling only (see - * {@link #sample()} and {@link #sample(int)}). In case no sampling is - * needed for the created distribution, it is advised to pass {@code null} - * as random generator via the appropriate constructors to avoid the - * additional initialisation overhead. + * Creates a distribution. * * @param mean mean of this distribution. */ @@ -107,58 +98,19 @@ public class ExponentialDistribution extends AbstractRealDistribution { } /** - * Create an exponential distribution with the given mean. - *

- * Note: this constructor will implicitly create an instance of - * {@link Well19937c} as random generator to be used for sampling only (see - * {@link #sample()} and {@link #sample(int)}). In case no sampling is - * needed for the created distribution, it is advised to pass {@code null} - * as random generator via the appropriate constructors to avoid the - * additional initialisation overhead. + * Creates a distribution. * * @param mean Mean of this distribution. * @param inverseCumAccuracy Maximum absolute error in inverse * cumulative probability estimates (defaults to * {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). * @throws NotStrictlyPositiveException if {@code mean <= 0}. + * * @since 2.1 */ - public ExponentialDistribution(double mean, double inverseCumAccuracy) { - this(new Well19937c(), mean, inverseCumAccuracy); - } - - /** - * Creates an exponential distribution. - * - * @param rng Random number generator. - * @param mean Mean of this distribution. - * @throws NotStrictlyPositiveException if {@code mean <= 0}. - * @since 3.3 - */ - @Deprecated - public ExponentialDistribution(RandomGenerator rng, double mean) - throws NotStrictlyPositiveException { - this(rng, mean, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); - } - - /** - * Creates an exponential distribution. - * - * @param rng Random number generator. - * @param mean Mean of this distribution. - * @param inverseCumAccuracy Maximum absolute error in inverse - * cumulative probability estimates (defaults to - * {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). - * @throws NotStrictlyPositiveException if {@code mean <= 0}. - * @since 3.1 - */ - @Deprecated - public ExponentialDistribution(RandomGenerator rng, - double mean, + public ExponentialDistribution(double mean, double inverseCumAccuracy) throws NotStrictlyPositiveException { - super(rng); - if (mean <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.MEAN, mean); } diff --git a/src/main/java/org/apache/commons/math4/distribution/FDistribution.java b/src/main/java/org/apache/commons/math4/distribution/FDistribution.java index 907d20187..bcde9aab2 100644 --- a/src/main/java/org/apache/commons/math4/distribution/FDistribution.java +++ b/src/main/java/org/apache/commons/math4/distribution/FDistribution.java @@ -19,8 +19,6 @@ package org.apache.commons.math4.distribution; import org.apache.commons.math4.exception.NotStrictlyPositiveException; import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.math4.random.RandomGenerator; -import org.apache.commons.math4.random.Well19937c; import org.apache.commons.math4.special.Beta; import org.apache.commons.math4.util.FastMath; @@ -50,14 +48,7 @@ public class FDistribution extends AbstractRealDistribution { private boolean numericalVarianceIsCalculated = false; /** - * Creates an F distribution using the given degrees of freedom. - *

- * Note: this constructor will implicitly create an instance of - * {@link Well19937c} as random generator to be used for sampling only (see - * {@link #sample()} and {@link #sample(int)}). In case no sampling is - * needed for the created distribution, it is advised to pass {@code null} - * as random generator via the appropriate constructors to avoid the - * additional initialisation overhead. + * Creates a using the given degrees of freedom. * * @param numeratorDegreesOfFreedom Numerator degrees of freedom. * @param denominatorDegreesOfFreedom Denominator degrees of freedom. @@ -73,69 +64,19 @@ public class FDistribution extends AbstractRealDistribution { } /** - * Creates an F distribution using the given degrees of freedom - * and inverse cumulative probability accuracy. - *

- * Note: this constructor will implicitly create an instance of - * {@link Well19937c} as random generator to be used for sampling only (see - * {@link #sample()} and {@link #sample(int)}). In case no sampling is - * needed for the created distribution, it is advised to pass {@code null} - * as random generator via the appropriate constructors to avoid the - * additional initialisation overhead. + * Creates a distribution. * * @param numeratorDegreesOfFreedom Numerator degrees of freedom. * @param denominatorDegreesOfFreedom Denominator degrees of freedom. * @param inverseCumAccuracy the maximum absolute error in inverse * cumulative probability estimates. - * @throws NotStrictlyPositiveException if - * {@code numeratorDegreesOfFreedom <= 0} or + * @throws NotStrictlyPositiveException if {@code numeratorDegreesOfFreedom <= 0} or * {@code denominatorDegreesOfFreedom <= 0}. - * @since 2.1 */ public FDistribution(double numeratorDegreesOfFreedom, double denominatorDegreesOfFreedom, double inverseCumAccuracy) throws NotStrictlyPositiveException { - this(new Well19937c(), numeratorDegreesOfFreedom, - denominatorDegreesOfFreedom, inverseCumAccuracy); - } - - /** - * Creates an F distribution. - * - * @param rng Random number generator. - * @param numeratorDegreesOfFreedom Numerator degrees of freedom. - * @param denominatorDegreesOfFreedom Denominator degrees of freedom. - * @throws NotStrictlyPositiveException if {@code numeratorDegreesOfFreedom <= 0} or - * {@code denominatorDegreesOfFreedom <= 0}. - * @since 3.3 - */ - public FDistribution(RandomGenerator rng, - double numeratorDegreesOfFreedom, - double denominatorDegreesOfFreedom) - throws NotStrictlyPositiveException { - this(rng, numeratorDegreesOfFreedom, denominatorDegreesOfFreedom, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); - } - - /** - * Creates an F distribution. - * - * @param rng Random number generator. - * @param numeratorDegreesOfFreedom Numerator degrees of freedom. - * @param denominatorDegreesOfFreedom Denominator degrees of freedom. - * @param inverseCumAccuracy the maximum absolute error in inverse - * cumulative probability estimates. - * @throws NotStrictlyPositiveException if {@code numeratorDegreesOfFreedom <= 0} or - * {@code denominatorDegreesOfFreedom <= 0}. - * @since 3.1 - */ - public FDistribution(RandomGenerator rng, - double numeratorDegreesOfFreedom, - double denominatorDegreesOfFreedom, - double inverseCumAccuracy) - throws NotStrictlyPositiveException { - super(rng); - if (numeratorDegreesOfFreedom <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.DEGREES_OF_FREEDOM, numeratorDegreesOfFreedom); diff --git a/src/main/java/org/apache/commons/math4/distribution/GammaDistribution.java b/src/main/java/org/apache/commons/math4/distribution/GammaDistribution.java index 2377df79b..c50c9742a 100644 --- a/src/main/java/org/apache/commons/math4/distribution/GammaDistribution.java +++ b/src/main/java/org/apache/commons/math4/distribution/GammaDistribution.java @@ -18,8 +18,6 @@ package org.apache.commons.math4.distribution; import org.apache.commons.math4.exception.NotStrictlyPositiveException; import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.math4.random.RandomGenerator; -import org.apache.commons.math4.random.Well19937c; import org.apache.commons.math4.rng.UniformRandomProvider; import org.apache.commons.math4.special.Gamma; import org.apache.commons.math4.util.FastMath; @@ -37,7 +35,7 @@ public class GammaDistribution extends AbstractRealDistribution { */ public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9; /** Serializable version identifier. */ - private static final long serialVersionUID = 20120524L; + private static final long serialVersionUID = 20160311L; /** The shape parameter. */ private final double shape; /** The scale parameter. */ @@ -99,35 +97,20 @@ public class GammaDistribution extends AbstractRealDistribution { private final double solverAbsoluteAccuracy; /** - * Creates a new gamma distribution with specified values of the shape and - * scale parameters. - *

- * Note: this constructor will implicitly create an instance of - * {@link Well19937c} as random generator to be used for sampling only (see - * {@link #sample()} and {@link #sample(int)}). In case no sampling is - * needed for the created distribution, it is advised to pass {@code null} - * as random generator via the appropriate constructors to avoid the - * additional initialisation overhead. + * Creates a distribution. * * @param shape the shape parameter * @param scale the scale parameter * @throws NotStrictlyPositiveException if {@code shape <= 0} or * {@code scale <= 0}. */ - public GammaDistribution(double shape, double scale) throws NotStrictlyPositiveException { + public GammaDistribution(double shape, double scale) + throws NotStrictlyPositiveException { this(shape, scale, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); } /** - * Creates a new gamma distribution with specified values of the shape and - * scale parameters. - *

- * Note: this constructor will implicitly create an instance of - * {@link Well19937c} as random generator to be used for sampling only (see - * {@link #sample()} and {@link #sample(int)}). In case no sampling is - * needed for the created distribution, it is advised to pass {@code null} - * as random generator via the appropriate constructors to avoid the - * additional initialisation overhead. + * Creates a distribution. * * @param shape the shape parameter * @param scale the scale parameter @@ -136,50 +119,11 @@ public class GammaDistribution extends AbstractRealDistribution { * {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). * @throws NotStrictlyPositiveException if {@code shape <= 0} or * {@code scale <= 0}. - * @since 2.1 */ - public GammaDistribution(double shape, double scale, double inverseCumAccuracy) - throws NotStrictlyPositiveException { - this(new Well19937c(), shape, scale, inverseCumAccuracy); - } - - /** - * Creates a Gamma distribution. - * - * @param rng Random number generator. - * @param shape the shape parameter - * @param scale the scale parameter - * @throws NotStrictlyPositiveException if {@code shape <= 0} or - * {@code scale <= 0}. - * @since 3.3 - */ - @Deprecated - public GammaDistribution(RandomGenerator rng, double shape, double scale) - throws NotStrictlyPositiveException { - this(rng, shape, scale, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); - } - - /** - * Creates a Gamma distribution. - * - * @param rng Random number generator. - * @param shape the shape parameter - * @param scale the scale parameter - * @param inverseCumAccuracy the maximum absolute error in inverse - * cumulative probability estimates (defaults to - * {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). - * @throws NotStrictlyPositiveException if {@code shape <= 0} or - * {@code scale <= 0}. - * @since 3.1 - */ - @Deprecated - public GammaDistribution(RandomGenerator rng, - double shape, + public GammaDistribution(double shape, double scale, double inverseCumAccuracy) throws NotStrictlyPositiveException { - super(rng); - if (shape <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.SHAPE, shape); } diff --git a/src/main/java/org/apache/commons/math4/distribution/GumbelDistribution.java b/src/main/java/org/apache/commons/math4/distribution/GumbelDistribution.java index 0aa3fb238..be06d09ac 100644 --- a/src/main/java/org/apache/commons/math4/distribution/GumbelDistribution.java +++ b/src/main/java/org/apache/commons/math4/distribution/GumbelDistribution.java @@ -19,8 +19,6 @@ package org.apache.commons.math4.distribution; import org.apache.commons.math4.exception.NotStrictlyPositiveException; import org.apache.commons.math4.exception.OutOfRangeException; import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.math4.random.RandomGenerator; -import org.apache.commons.math4.random.Well19937c; import org.apache.commons.math4.util.FastMath; import org.apache.commons.math4.util.MathUtils; @@ -35,7 +33,7 @@ import org.apache.commons.math4.util.MathUtils; public class GumbelDistribution extends AbstractRealDistribution { /** Serializable version identifier. */ - private static final long serialVersionUID = 20141003; + private static final long serialVersionUID = 20160311L; /** * Approximation of Euler's constant @@ -49,34 +47,13 @@ public class GumbelDistribution extends AbstractRealDistribution { private final double beta; /** - * Build a new instance. - *

- * Note: this constructor will implicitly create an instance of - * {@link Well19937c} as random generator to be used for sampling only (see - * {@link #sample()} and {@link #sample(int)}). In case no sampling is - * needed for the created distribution, it is advised to pass {@code null} - * as random generator via the appropriate constructors to avoid the - * additional initialisation overhead. + * Creates a distribution. * * @param mu location parameter * @param beta scale parameter (must be positive) * @throws NotStrictlyPositiveException if {@code beta <= 0} */ public GumbelDistribution(double mu, double beta) { - this(new Well19937c(), mu, beta); - } - - /** - * Build a new instance. - * - * @param rng Random number generator - * @param mu location parameter - * @param beta scale parameter (must be positive) - * @throws NotStrictlyPositiveException if {@code beta <= 0} - */ - public GumbelDistribution(RandomGenerator rng, double mu, double beta) { - super(rng); - if (beta <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.SCALE, beta); } diff --git a/src/main/java/org/apache/commons/math4/distribution/LaplaceDistribution.java b/src/main/java/org/apache/commons/math4/distribution/LaplaceDistribution.java index 7e41a29a7..c85618ba4 100644 --- a/src/main/java/org/apache/commons/math4/distribution/LaplaceDistribution.java +++ b/src/main/java/org/apache/commons/math4/distribution/LaplaceDistribution.java @@ -19,8 +19,6 @@ package org.apache.commons.math4.distribution; import org.apache.commons.math4.exception.NotStrictlyPositiveException; import org.apache.commons.math4.exception.OutOfRangeException; import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.math4.random.RandomGenerator; -import org.apache.commons.math4.random.Well19937c; import org.apache.commons.math4.util.FastMath; /** @@ -33,7 +31,7 @@ import org.apache.commons.math4.util.FastMath; public class LaplaceDistribution extends AbstractRealDistribution { /** Serializable version identifier. */ - private static final long serialVersionUID = 20141003; + private static final long serialVersionUID = 20160311L; /** The location parameter. */ private final double mu; @@ -41,34 +39,13 @@ public class LaplaceDistribution extends AbstractRealDistribution { private final double beta; /** - * Build a new instance. - *

- * Note: this constructor will implicitly create an instance of - * {@link Well19937c} as random generator to be used for sampling only (see - * {@link #sample()} and {@link #sample(int)}). In case no sampling is - * needed for the created distribution, it is advised to pass {@code null} - * as random generator via the appropriate constructors to avoid the - * additional initialisation overhead. + * Creates a distribution. * * @param mu location parameter * @param beta scale parameter (must be positive) * @throws NotStrictlyPositiveException if {@code beta <= 0} */ public LaplaceDistribution(double mu, double beta) { - this(new Well19937c(), mu, beta); - } - - /** - * Build a new instance. - * - * @param rng Random number generator - * @param mu location parameter - * @param beta scale parameter (must be positive) - * @throws NotStrictlyPositiveException if {@code beta <= 0} - */ - public LaplaceDistribution(RandomGenerator rng, double mu, double beta) { - super(rng); - if (beta <= 0.0) { throw new NotStrictlyPositiveException(LocalizedFormats.NOT_POSITIVE_SCALE, beta); } diff --git a/src/main/java/org/apache/commons/math4/distribution/LevyDistribution.java b/src/main/java/org/apache/commons/math4/distribution/LevyDistribution.java index 5f55c83dd..a8d579367 100644 --- a/src/main/java/org/apache/commons/math4/distribution/LevyDistribution.java +++ b/src/main/java/org/apache/commons/math4/distribution/LevyDistribution.java @@ -17,8 +17,6 @@ package org.apache.commons.math4.distribution; import org.apache.commons.math4.exception.OutOfRangeException; -import org.apache.commons.math4.random.RandomGenerator; -import org.apache.commons.math4.random.Well19937c; import org.apache.commons.math4.special.Erf; import org.apache.commons.math4.util.FastMath; @@ -31,7 +29,7 @@ import org.apache.commons.math4.util.FastMath; public class LevyDistribution extends AbstractRealDistribution { /** Serializable UID. */ - private static final long serialVersionUID = 20130314L; + private static final long serialVersionUID = 20630311L; /** Location parameter. */ private final double mu; @@ -43,33 +41,14 @@ public class LevyDistribution extends AbstractRealDistribution { private final double halfC; /** - * Build a new instance. - *

- * Note: this constructor will implicitly create an instance of - * {@link Well19937c} as random generator to be used for sampling only (see - * {@link #sample()} and {@link #sample(int)}). In case no sampling is - * needed for the created distribution, it is advised to pass {@code null} - * as random generator via the appropriate constructors to avoid the - * additional initialisation overhead. + * Creates a distribution. * - * @param mu location parameter - * @param c scale parameter - * @since 3.4 - */ - public LevyDistribution(final double mu, final double c) { - this(new Well19937c(), mu, c); - } - - /** - * Creates a LevyDistribution. - * @param rng random generator to be used for sampling * @param mu location * @param c scale parameter */ - public LevyDistribution(final RandomGenerator rng, final double mu, final double c) { - super(rng); - this.mu = mu; - this.c = c; + public LevyDistribution(final double mu, final double c) { + this.mu = mu; + this.c = c; this.halfC = 0.5 * c; } diff --git a/src/main/java/org/apache/commons/math4/distribution/LogNormalDistribution.java b/src/main/java/org/apache/commons/math4/distribution/LogNormalDistribution.java index 8bec045e4..eafb90e8f 100644 --- a/src/main/java/org/apache/commons/math4/distribution/LogNormalDistribution.java +++ b/src/main/java/org/apache/commons/math4/distribution/LogNormalDistribution.java @@ -20,8 +20,6 @@ package org.apache.commons.math4.distribution; import org.apache.commons.math4.exception.NotStrictlyPositiveException; import org.apache.commons.math4.exception.NumberIsTooLargeException; import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.math4.random.RandomGenerator; -import org.apache.commons.math4.random.Well19937c; import org.apache.commons.math4.rng.UniformRandomProvider; import org.apache.commons.math4.special.Erf; import org.apache.commons.math4.util.FastMath; @@ -78,35 +76,21 @@ public class LogNormalDistribution extends AbstractRealDistribution { private final double solverAbsoluteAccuracy; /** - * Create a log-normal distribution, where the mean and standard deviation + * Creates a log-normal distribution, where the mean and standard deviation * of the {@link NormalDistribution normally distributed} natural * logarithm of the log-normal distribution are equal to zero and one * respectively. In other words, the scale of the returned distribution is * {@code 0}, while its shape is {@code 1}. - *

- * Note: this constructor will implicitly create an instance of - * {@link Well19937c} as random generator to be used for sampling only (see - * {@link #sample()} and {@link #sample(int)}). In case no sampling is - * needed for the created distribution, it is advised to pass {@code null} - * as random generator via the appropriate constructors to avoid the - * additional initialisation overhead. */ public LogNormalDistribution() { this(0, 1); } /** - * Create a log-normal distribution using the specified scale and shape. - *

- * Note: this constructor will implicitly create an instance of - * {@link Well19937c} as random generator to be used for sampling only (see - * {@link #sample()} and {@link #sample(int)}). In case no sampling is - * needed for the created distribution, it is advised to pass {@code null} - * as random generator via the appropriate constructors to avoid the - * additional initialisation overhead. + * Creates a log-normal distribution. * - * @param scale the scale parameter of this distribution - * @param shape the shape parameter of this distribution + * @param scale Scale parameter of this distribution. + * @param shape Shape parameter of this distribution. * @throws NotStrictlyPositiveException if {@code shape <= 0}. */ public LogNormalDistribution(double scale, double shape) @@ -114,60 +98,18 @@ public class LogNormalDistribution extends AbstractRealDistribution { this(scale, shape, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); } - /** - * Create a log-normal distribution using the specified scale, shape and - * inverse cumulative distribution accuracy. - *

- * Note: this constructor will implicitly create an instance of - * {@link Well19937c} as random generator to be used for sampling only (see - * {@link #sample()} and {@link #sample(int)}). In case no sampling is - * needed for the created distribution, it is advised to pass {@code null} - * as random generator via the appropriate constructors to avoid the - * additional initialisation overhead. - * - * @param scale the scale parameter of this distribution - * @param shape the shape parameter of this distribution - * @param inverseCumAccuracy Inverse cumulative probability accuracy. - * @throws NotStrictlyPositiveException if {@code shape <= 0}. - */ - public LogNormalDistribution(double scale, double shape, double inverseCumAccuracy) - throws NotStrictlyPositiveException { - this(new Well19937c(), scale, shape, inverseCumAccuracy); - } - /** * Creates a log-normal distribution. * - * @param rng Random number generator. - * @param scale Scale parameter of this distribution. - * @param shape Shape parameter of this distribution. - * @throws NotStrictlyPositiveException if {@code shape <= 0}. - * @since 3.3 - */ - @Deprecated - public LogNormalDistribution(RandomGenerator rng, double scale, double shape) - throws NotStrictlyPositiveException { - this(rng, scale, shape, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); - } - - /** - * Creates a log-normal distribution. - * - * @param rng Random number generator. * @param scale Scale parameter of this distribution. * @param shape Shape parameter of this distribution. * @param inverseCumAccuracy Inverse cumulative probability accuracy. * @throws NotStrictlyPositiveException if {@code shape <= 0}. - * @since 3.1 */ - @Deprecated - public LogNormalDistribution(RandomGenerator rng, - double scale, + public LogNormalDistribution(double scale, double shape, double inverseCumAccuracy) throws NotStrictlyPositiveException { - super(rng); - if (shape <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.SHAPE, shape); } diff --git a/src/main/java/org/apache/commons/math4/distribution/LogisticDistribution.java b/src/main/java/org/apache/commons/math4/distribution/LogisticDistribution.java index 14c2a9887..1c69804bd 100644 --- a/src/main/java/org/apache/commons/math4/distribution/LogisticDistribution.java +++ b/src/main/java/org/apache/commons/math4/distribution/LogisticDistribution.java @@ -19,8 +19,6 @@ package org.apache.commons.math4.distribution; import org.apache.commons.math4.exception.NotStrictlyPositiveException; import org.apache.commons.math4.exception.OutOfRangeException; import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.math4.random.RandomGenerator; -import org.apache.commons.math4.random.Well19937c; import org.apache.commons.math4.util.FastMath; import org.apache.commons.math4.util.MathUtils; @@ -35,7 +33,7 @@ import org.apache.commons.math4.util.MathUtils; public class LogisticDistribution extends AbstractRealDistribution { /** Serializable version identifier. */ - private static final long serialVersionUID = 20141003; + private static final long serialVersionUID = 20160311L; /** The location parameter. */ private final double mu; @@ -43,34 +41,14 @@ public class LogisticDistribution extends AbstractRealDistribution { private final double s; /** - * Build a new instance. - *

- * Note: this constructor will implicitly create an instance of - * {@link Well19937c} as random generator to be used for sampling only (see - * {@link #sample()} and {@link #sample(int)}). In case no sampling is - * needed for the created distribution, it is advised to pass {@code null} - * as random generator via the appropriate constructors to avoid the - * additional initialisation overhead. + * Creates a distribution. * * @param mu location parameter * @param s scale parameter (must be positive) * @throws NotStrictlyPositiveException if {@code beta <= 0} */ - public LogisticDistribution(double mu, double s) { - this(new Well19937c(), mu, s); - } - - /** - * Build a new instance. - * - * @param rng Random number generator - * @param mu location parameter - * @param s scale parameter (must be positive) - * @throws NotStrictlyPositiveException if {@code beta <= 0} - */ - public LogisticDistribution(RandomGenerator rng, double mu, double s) { - super(rng); - + public LogisticDistribution(double mu, + double s) { if (s <= 0.0) { throw new NotStrictlyPositiveException(LocalizedFormats.NOT_POSITIVE_SCALE, s); } diff --git a/src/main/java/org/apache/commons/math4/distribution/NakagamiDistribution.java b/src/main/java/org/apache/commons/math4/distribution/NakagamiDistribution.java index fa8b0f5bc..5f7e405a1 100644 --- a/src/main/java/org/apache/commons/math4/distribution/NakagamiDistribution.java +++ b/src/main/java/org/apache/commons/math4/distribution/NakagamiDistribution.java @@ -19,8 +19,6 @@ package org.apache.commons.math4.distribution; import org.apache.commons.math4.exception.NotStrictlyPositiveException; import org.apache.commons.math4.exception.NumberIsTooSmallException; import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.math4.random.RandomGenerator; -import org.apache.commons.math4.random.Well19937c; import org.apache.commons.math4.special.Gamma; import org.apache.commons.math4.util.FastMath; @@ -37,7 +35,7 @@ public class NakagamiDistribution extends AbstractRealDistribution { public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9; /** Serializable version identifier. */ - private static final long serialVersionUID = 20141003; + private static final long serialVersionUID = 20160311L; /** The shape parameter. */ private final double mu; @@ -47,33 +45,20 @@ public class NakagamiDistribution extends AbstractRealDistribution { private final double inverseAbsoluteAccuracy; /** - * Build a new instance. - *

- * Note: this constructor will implicitly create an instance of - * {@link Well19937c} as random generator to be used for sampling only (see - * {@link #sample()} and {@link #sample(int)}). In case no sampling is - * needed for the created distribution, it is advised to pass {@code null} - * as random generator via the appropriate constructors to avoid the - * additional initialisation overhead. + * Creates a distribution. * * @param mu shape parameter * @param omega scale parameter (must be positive) * @throws NumberIsTooSmallException if {@code mu < 0.5} * @throws NotStrictlyPositiveException if {@code omega <= 0} */ - public NakagamiDistribution(double mu, double omega) { + public NakagamiDistribution(double mu, + double omega) { this(mu, omega, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); } /** - * Build a new instance. - *

- * Note: this constructor will implicitly create an instance of - * {@link Well19937c} as random generator to be used for sampling only (see - * {@link #sample()} and {@link #sample(int)}). In case no sampling is - * needed for the created distribution, it is advised to pass {@code null} - * as random generator via the appropriate constructors to avoid the - * additional initialisation overhead. + * Creates a distribution. * * @param mu shape parameter * @param omega scale parameter (must be positive) @@ -82,24 +67,9 @@ public class NakagamiDistribution extends AbstractRealDistribution { * @throws NumberIsTooSmallException if {@code mu < 0.5} * @throws NotStrictlyPositiveException if {@code omega <= 0} */ - public NakagamiDistribution(double mu, double omega, double inverseAbsoluteAccuracy) { - this(new Well19937c(), mu, omega, inverseAbsoluteAccuracy); - } - - /** - * Build a new instance. - * - * @param rng Random number generator - * @param mu shape parameter - * @param omega scale parameter (must be positive) - * @param inverseAbsoluteAccuracy the maximum absolute error in inverse - * cumulative probability estimates (defaults to {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). - * @throws NumberIsTooSmallException if {@code mu < 0.5} - * @throws NotStrictlyPositiveException if {@code omega <= 0} - */ - public NakagamiDistribution(RandomGenerator rng, double mu, double omega, double inverseAbsoluteAccuracy) { - super(rng); - + public NakagamiDistribution(double mu, + double omega, + double inverseAbsoluteAccuracy) { if (mu < 0.5) { throw new NumberIsTooSmallException(mu, 0.5, true); } diff --git a/src/main/java/org/apache/commons/math4/distribution/NormalDistribution.java b/src/main/java/org/apache/commons/math4/distribution/NormalDistribution.java index 01de515c2..a8330d5e7 100644 --- a/src/main/java/org/apache/commons/math4/distribution/NormalDistribution.java +++ b/src/main/java/org/apache/commons/math4/distribution/NormalDistribution.java @@ -21,8 +21,6 @@ import org.apache.commons.math4.exception.NotStrictlyPositiveException; import org.apache.commons.math4.exception.NumberIsTooLargeException; import org.apache.commons.math4.exception.OutOfRangeException; import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.math4.random.RandomGenerator; -import org.apache.commons.math4.random.Well19937c; import org.apache.commons.math4.rng.UniformRandomProvider; import org.apache.commons.math4.special.Erf; import org.apache.commons.math4.util.FastMath; @@ -55,92 +53,37 @@ public class NormalDistribution extends AbstractRealDistribution { /** * Create a normal distribution with mean equal to zero and standard * deviation equal to one. - *

- * Note: this constructor will implicitly create an instance of - * {@link Well19937c} as random generator to be used for sampling only (see - * {@link #sample()} and {@link #sample(int)}). In case no sampling is - * needed for the created distribution, it is advised to pass {@code null} - * as random generator via the appropriate constructors to avoid the - * additional initialisation overhead. */ public NormalDistribution() { this(0, 1); } /** - * Create a normal distribution using the given mean and standard deviation. - *

- * Note: this constructor will implicitly create an instance of - * {@link Well19937c} as random generator to be used for sampling only (see - * {@link #sample()} and {@link #sample(int)}). In case no sampling is - * needed for the created distribution, it is advised to pass {@code null} - * as random generator via the appropriate constructors to avoid the - * additional initialisation overhead. + * Creates a distribution. * * @param mean Mean for this distribution. * @param sd Standard deviation for this distribution. * @throws NotStrictlyPositiveException if {@code sd <= 0}. */ - public NormalDistribution(double mean, double sd) + public NormalDistribution(double mean, + double sd) throws NotStrictlyPositiveException { this(mean, sd, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); } + /** - * Create a normal distribution using the given mean, standard deviation and - * inverse cumulative distribution accuracy. - *

- * Note: this constructor will implicitly create an instance of - * {@link Well19937c} as random generator to be used for sampling only (see - * {@link #sample()} and {@link #sample(int)}). In case no sampling is - * needed for the created distribution, it is advised to pass {@code null} - * as random generator via the appropriate constructors to avoid the - * additional initialisation overhead. + * Creates a distribution. * * @param mean Mean for this distribution. * @param sd Standard deviation for this distribution. * @param inverseCumAccuracy Inverse cumulative probability accuracy. * @throws NotStrictlyPositiveException if {@code sd <= 0}. - * @since 2.1 */ - public NormalDistribution(double mean, double sd, double inverseCumAccuracy) - throws NotStrictlyPositiveException { - this(new Well19937c(), mean, sd, inverseCumAccuracy); - } - - /** - * Creates a normal distribution. - * - * @param rng Random number generator. - * @param mean Mean for this distribution. - * @param sd Standard deviation for this distribution. - * @throws NotStrictlyPositiveException if {@code sd <= 0}. - * @since 3.3 - */ - @Deprecated - public NormalDistribution(RandomGenerator rng, double mean, double sd) - throws NotStrictlyPositiveException { - this(rng, mean, sd, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); - } - - /** - * Creates a normal distribution. - * - * @param rng Random number generator. - * @param mean Mean for this distribution. - * @param sd Standard deviation for this distribution. - * @param inverseCumAccuracy Inverse cumulative probability accuracy. - * @throws NotStrictlyPositiveException if {@code sd <= 0}. - * @since 3.1 - */ - @Deprecated - public NormalDistribution(RandomGenerator rng, - double mean, + public NormalDistribution(double mean, double sd, double inverseCumAccuracy) throws NotStrictlyPositiveException { - super(rng); - if (sd <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.STANDARD_DEVIATION, sd); } diff --git a/src/main/java/org/apache/commons/math4/distribution/ParetoDistribution.java b/src/main/java/org/apache/commons/math4/distribution/ParetoDistribution.java index bd074faa5..217432231 100644 --- a/src/main/java/org/apache/commons/math4/distribution/ParetoDistribution.java +++ b/src/main/java/org/apache/commons/math4/distribution/ParetoDistribution.java @@ -19,8 +19,6 @@ package org.apache.commons.math4.distribution; import org.apache.commons.math4.exception.NotStrictlyPositiveException; import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.math4.random.RandomGenerator; -import org.apache.commons.math4.random.Well19937c; import org.apache.commons.math4.rng.UniformRandomProvider; import org.apache.commons.math4.util.FastMath; @@ -52,7 +50,7 @@ public class ParetoDistribution extends AbstractRealDistribution { public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9; /** Serializable version identifier. */ - private static final long serialVersionUID = 20130424; + private static final long serialVersionUID = 20160311L; /** The scale parameter of this distribution. */ private final double scale; @@ -64,83 +62,37 @@ public class ParetoDistribution extends AbstractRealDistribution { private final double solverAbsoluteAccuracy; /** - * Create a Pareto distribution with a scale of {@code 1} and a shape of {@code 1}. + * Creates a Pareto distribution with a scale of {@code 1} and a shape of {@code 1}. */ public ParetoDistribution() { this(1, 1); } /** - * Create a Pareto distribution using the specified scale and shape. - *

- * Note: this constructor will implicitly create an instance of - * {@link Well19937c} as random generator to be used for sampling only (see - * {@link #sample()} and {@link #sample(int)}). In case no sampling is - * needed for the created distribution, it is advised to pass {@code null} - * as random generator via the appropriate constructors to avoid the - * additional initialisation overhead. + * Creates a Pareto distribution. * * @param scale the scale parameter of this distribution * @param shape the shape parameter of this distribution * @throws NotStrictlyPositiveException if {@code scale <= 0} or {@code shape <= 0}. */ - public ParetoDistribution(double scale, double shape) + public ParetoDistribution(double scale, + double shape) throws NotStrictlyPositiveException { this(scale, shape, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); } - /** - * Create a Pareto distribution using the specified scale, shape and - * inverse cumulative distribution accuracy. - *

- * Note: this constructor will implicitly create an instance of - * {@link Well19937c} as random generator to be used for sampling only (see - * {@link #sample()} and {@link #sample(int)}). In case no sampling is - * needed for the created distribution, it is advised to pass {@code null} - * as random generator via the appropriate constructors to avoid the - * additional initialisation overhead. - * - * @param scale the scale parameter of this distribution - * @param shape the shape parameter of this distribution - * @param inverseCumAccuracy Inverse cumulative probability accuracy. - * @throws NotStrictlyPositiveException if {@code scale <= 0} or {@code shape <= 0}. - */ - public ParetoDistribution(double scale, double shape, double inverseCumAccuracy) - throws NotStrictlyPositiveException { - this(new Well19937c(), scale, shape, inverseCumAccuracy); - } - /** * Creates a Pareto distribution. * - * @param rng Random number generator. - * @param scale Scale parameter of this distribution. - * @param shape Shape parameter of this distribution. - * @throws NotStrictlyPositiveException if {@code scale <= 0} or {@code shape <= 0}. - */ - @Deprecated - public ParetoDistribution(RandomGenerator rng, double scale, double shape) - throws NotStrictlyPositiveException { - this(rng, scale, shape, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); - } - - /** - * Creates a Pareto distribution. - * - * @param rng Random number generator. * @param scale Scale parameter of this distribution. * @param shape Shape parameter of this distribution. * @param inverseCumAccuracy Inverse cumulative probability accuracy. * @throws NotStrictlyPositiveException if {@code scale <= 0} or {@code shape <= 0}. */ - @Deprecated - public ParetoDistribution(RandomGenerator rng, - double scale, + public ParetoDistribution(double scale, double shape, double inverseCumAccuracy) throws NotStrictlyPositiveException { - super(rng); - if (scale <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.SCALE, scale); } diff --git a/src/main/java/org/apache/commons/math4/distribution/PoissonDistribution.java b/src/main/java/org/apache/commons/math4/distribution/PoissonDistribution.java index 731a6ac37..6cb0129c0 100644 --- a/src/main/java/org/apache/commons/math4/distribution/PoissonDistribution.java +++ b/src/main/java/org/apache/commons/math4/distribution/PoissonDistribution.java @@ -24,6 +24,7 @@ import org.apache.commons.math4.special.Gamma; import org.apache.commons.math4.util.CombinatoricsUtils; import org.apache.commons.math4.util.FastMath; import org.apache.commons.math4.util.MathUtils; +import org.apache.commons.math4.rng.RandomSource; /** * Implementation of the Poisson distribution. @@ -47,7 +48,7 @@ public class PoissonDistribution extends AbstractIntegerDistribution { /** Distribution used to compute normal approximation. */ private final NormalDistribution normal; /** Distribution needed for the {@link #sample()} method. */ - private final ExponentialDistribution exponential; + private final RealDistribution.Sampler exponentialSampler; /** Mean of the distribution. */ private final double mean; @@ -130,10 +131,11 @@ public class PoissonDistribution extends AbstractIntegerDistribution { this.maxIterations = maxIterations; // Use the same RNG instance as the parent class. - normal = new NormalDistribution(rng, p, FastMath.sqrt(p), + normal = new NormalDistribution(p, FastMath.sqrt(p), NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY); - exponential = new ExponentialDistribution(rng, 1, - ExponentialDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY); + + // XXX TODO: RNG source should not be hard-coded. + exponentialSampler = new ExponentialDistribution(1).createSampler(RandomSource.create(RandomSource.WELL_19937_C)); } /** @@ -364,16 +366,16 @@ public class PoissonDistribution extends AbstractIntegerDistribution { continue; } y = x < 0 ? FastMath.floor(x) : FastMath.ceil(x); - final double e = exponential.sample(); + final double e = exponentialSampler.sample(); v = -e - (n * n / 2) + c1; } else { if (u > p1 + p2) { y = lambda; break; } else { - x = delta + (twolpd / delta) * exponential.sample(); + x = delta + (twolpd / delta) * exponentialSampler.sample(); y = FastMath.ceil(x); - v = -exponential.sample() - delta * (x + 1) / twolpd; + v = -exponentialSampler.sample() - delta * (x + 1) / twolpd; } } a = x < 0 ? 1 : 0; diff --git a/src/main/java/org/apache/commons/math4/distribution/TDistribution.java b/src/main/java/org/apache/commons/math4/distribution/TDistribution.java index 302d47610..e51055626 100644 --- a/src/main/java/org/apache/commons/math4/distribution/TDistribution.java +++ b/src/main/java/org/apache/commons/math4/distribution/TDistribution.java @@ -18,8 +18,6 @@ package org.apache.commons.math4.distribution; import org.apache.commons.math4.exception.NotStrictlyPositiveException; import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.math4.random.RandomGenerator; -import org.apache.commons.math4.random.Well19937c; import org.apache.commons.math4.special.Beta; import org.apache.commons.math4.special.Gamma; import org.apache.commons.math4.util.FastMath; @@ -37,7 +35,7 @@ public class TDistribution extends AbstractRealDistribution { */ public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9; /** Serializable version identifier */ - private static final long serialVersionUID = -5852615386664158222L; + private static final long serialVersionUID = 20160311L; /** The degrees of freedom. */ private final double degreesOfFreedom; /** Inverse cumulative probability accuracy. */ @@ -46,14 +44,7 @@ public class TDistribution extends AbstractRealDistribution { private final double factor; /** - * Create a t distribution using the given degrees of freedom. - *

- * Note: this constructor will implicitly create an instance of - * {@link Well19937c} as random generator to be used for sampling only (see - * {@link #sample()} and {@link #sample(int)}). In case no sampling is - * needed for the created distribution, it is advised to pass {@code null} - * as random generator via the appropriate constructors to avoid the - * additional initialisation overhead. + * Creates a distribution. * * @param degreesOfFreedom Degrees of freedom. * @throws NotStrictlyPositiveException if {@code degreesOfFreedom <= 0} @@ -64,58 +55,17 @@ public class TDistribution extends AbstractRealDistribution { } /** - * Create a t distribution using the given degrees of freedom and the - * specified inverse cumulative probability absolute accuracy. - *

- * Note: this constructor will implicitly create an instance of - * {@link Well19937c} as random generator to be used for sampling only (see - * {@link #sample()} and {@link #sample(int)}). In case no sampling is - * needed for the created distribution, it is advised to pass {@code null} - * as random generator via the appropriate constructors to avoid the - * additional initialisation overhead. + * Creates a distribution. * * @param degreesOfFreedom Degrees of freedom. * @param inverseCumAccuracy the maximum absolute error in inverse * cumulative probability estimates * (defaults to {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). * @throws NotStrictlyPositiveException if {@code degreesOfFreedom <= 0} - * @since 2.1 */ - public TDistribution(double degreesOfFreedom, double inverseCumAccuracy) - throws NotStrictlyPositiveException { - this(new Well19937c(), degreesOfFreedom, inverseCumAccuracy); - } - - /** - * Creates a t distribution. - * - * @param rng Random number generator. - * @param degreesOfFreedom Degrees of freedom. - * @throws NotStrictlyPositiveException if {@code degreesOfFreedom <= 0} - * @since 3.3 - */ - public TDistribution(RandomGenerator rng, double degreesOfFreedom) - throws NotStrictlyPositiveException { - this(rng, degreesOfFreedom, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); - } - - /** - * Creates a t distribution. - * - * @param rng Random number generator. - * @param degreesOfFreedom Degrees of freedom. - * @param inverseCumAccuracy the maximum absolute error in inverse - * cumulative probability estimates - * (defaults to {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). - * @throws NotStrictlyPositiveException if {@code degreesOfFreedom <= 0} - * @since 3.1 - */ - public TDistribution(RandomGenerator rng, - double degreesOfFreedom, + public TDistribution(double degreesOfFreedom, double inverseCumAccuracy) throws NotStrictlyPositiveException { - super(rng); - if (degreesOfFreedom <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.DEGREES_OF_FREEDOM, degreesOfFreedom); diff --git a/src/main/java/org/apache/commons/math4/distribution/TriangularDistribution.java b/src/main/java/org/apache/commons/math4/distribution/TriangularDistribution.java index 072f5c571..4013c4769 100644 --- a/src/main/java/org/apache/commons/math4/distribution/TriangularDistribution.java +++ b/src/main/java/org/apache/commons/math4/distribution/TriangularDistribution.java @@ -21,8 +21,6 @@ import org.apache.commons.math4.exception.NumberIsTooLargeException; import org.apache.commons.math4.exception.NumberIsTooSmallException; import org.apache.commons.math4.exception.OutOfRangeException; import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.math4.random.RandomGenerator; -import org.apache.commons.math4.random.Well19937c; import org.apache.commons.math4.util.FastMath; /** @@ -35,7 +33,7 @@ import org.apache.commons.math4.util.FastMath; */ public class TriangularDistribution extends AbstractRealDistribution { /** Serializable version identifier. */ - private static final long serialVersionUID = 20120112L; + private static final long serialVersionUID = 20160311L; /** Lower limit of this distribution (inclusive). */ private final double a; /** Upper limit of this distribution (inclusive). */ @@ -46,45 +44,20 @@ public class TriangularDistribution extends AbstractRealDistribution { private final double solverAbsoluteAccuracy; /** - * Creates a triangular real distribution using the given lower limit, - * upper limit, and mode. - *

- * Note: this constructor will implicitly create an instance of - * {@link Well19937c} as random generator to be used for sampling only (see - * {@link #sample()} and {@link #sample(int)}). In case no sampling is - * needed for the created distribution, it is advised to pass {@code null} - * as random generator via the appropriate constructors to avoid the - * additional initialisation overhead. + * Creates a distribution. * * @param a Lower limit of this distribution (inclusive). * @param b Upper limit of this distribution (inclusive). * @param c Mode of this distribution. * @throws NumberIsTooLargeException if {@code a >= b} or if {@code c > b}. * @throws NumberIsTooSmallException if {@code c < a}. - */ - public TriangularDistribution(double a, double c, double b) - throws NumberIsTooLargeException, NumberIsTooSmallException { - this(new Well19937c(), a, c, b); - } - - /** - * Creates a triangular distribution. - * - * @param rng Random number generator. - * @param a Lower limit of this distribution (inclusive). - * @param b Upper limit of this distribution (inclusive). - * @param c Mode of this distribution. - * @throws NumberIsTooLargeException if {@code a >= b} or if {@code c > b}. - * @throws NumberIsTooSmallException if {@code c < a}. * @since 3.1 */ - public TriangularDistribution(RandomGenerator rng, - double a, + public TriangularDistribution(double a, double c, double b) - throws NumberIsTooLargeException, NumberIsTooSmallException { - super(rng); - + throws NumberIsTooLargeException, + NumberIsTooSmallException { if (a >= b) { throw new NumberIsTooLargeException( LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND, diff --git a/src/main/java/org/apache/commons/math4/distribution/UniformRealDistribution.java b/src/main/java/org/apache/commons/math4/distribution/UniformRealDistribution.java index 704aa9765..404cc0286 100644 --- a/src/main/java/org/apache/commons/math4/distribution/UniformRealDistribution.java +++ b/src/main/java/org/apache/commons/math4/distribution/UniformRealDistribution.java @@ -20,8 +20,6 @@ package org.apache.commons.math4.distribution; import org.apache.commons.math4.exception.NumberIsTooLargeException; import org.apache.commons.math4.exception.OutOfRangeException; import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.math4.random.RandomGenerator; -import org.apache.commons.math4.random.Well19937c; import org.apache.commons.math4.rng.UniformRandomProvider; /** @@ -34,7 +32,7 @@ import org.apache.commons.math4.rng.UniformRandomProvider; */ public class UniformRealDistribution extends AbstractRealDistribution { /** Serializable version identifier. */ - private static final long serialVersionUID = 20120109L; + private static final long serialVersionUID = 20160311L; /** Lower bound of this distribution (inclusive). */ private final double lower; /** Upper bound of this distribution (exclusive). */ @@ -43,53 +41,21 @@ public class UniformRealDistribution extends AbstractRealDistribution { /** * Create a standard uniform real distribution with lower bound (inclusive) * equal to zero and upper bound (exclusive) equal to one. - *

- * Note: this constructor will implicitly create an instance of - * {@link Well19937c} as random generator to be used for sampling only (see - * {@link #sample()} and {@link #sample(int)}). In case no sampling is - * needed for the created distribution, it is advised to pass {@code null} - * as random generator via the appropriate constructors to avoid the - * additional initialisation overhead. */ public UniformRealDistribution() { this(0, 1); } - /** - * Create a uniform real distribution using the given lower and upper - * bounds. - *

- * Note: this constructor will implicitly create an instance of - * {@link Well19937c} as random generator to be used for sampling only (see - * {@link #sample()} and {@link #sample(int)}). In case no sampling is - * needed for the created distribution, it is advised to pass {@code null} - * as random generator via the appropriate constructors to avoid the - * additional initialisation overhead. - * - * @param lower Lower bound of this distribution (inclusive). - * @param upper Upper bound of this distribution (exclusive). - * @throws NumberIsTooLargeException if {@code lower >= upper}. - */ - public UniformRealDistribution(double lower, double upper) - throws NumberIsTooLargeException { - this(new Well19937c(), lower, upper); - } - /** * Creates a uniform distribution. * - * @param rng Random number generator. * @param lower Lower bound of this distribution (inclusive). * @param upper Upper bound of this distribution (exclusive). * @throws NumberIsTooLargeException if {@code lower >= upper}. - * @since 3.1 */ - @Deprecated - public UniformRealDistribution(RandomGenerator rng, - double lower, + public UniformRealDistribution(double lower, double upper) throws NumberIsTooLargeException { - super(rng); if (lower >= upper) { throw new NumberIsTooLargeException( LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND, @@ -207,7 +173,7 @@ public class UniformRealDistribution extends AbstractRealDistribution { /** {@inheritDoc} */ @Override public double sample() { - final double u = random.nextDouble(); + final double u = rng.nextDouble(); return u * upper + (1 - u) * lower; } }; diff --git a/src/main/java/org/apache/commons/math4/distribution/WeibullDistribution.java b/src/main/java/org/apache/commons/math4/distribution/WeibullDistribution.java index 893501ee3..844bf4a75 100644 --- a/src/main/java/org/apache/commons/math4/distribution/WeibullDistribution.java +++ b/src/main/java/org/apache/commons/math4/distribution/WeibullDistribution.java @@ -20,8 +20,6 @@ package org.apache.commons.math4.distribution; import org.apache.commons.math4.exception.NotStrictlyPositiveException; import org.apache.commons.math4.exception.OutOfRangeException; import org.apache.commons.math4.exception.util.LocalizedFormats; -import org.apache.commons.math4.random.RandomGenerator; -import org.apache.commons.math4.random.Well19937c; import org.apache.commons.math4.special.Gamma; import org.apache.commons.math4.util.FastMath; @@ -33,7 +31,8 @@ import org.apache.commons.math4.util.FastMath; * * @see Weibull distribution (Wikipedia) * @see Weibull distribution (MathWorld) - * @since 1.1 (changed to concrete class in 3.0) + * + * @since 1.1 */ public class WeibullDistribution extends AbstractRealDistribution { /** @@ -42,7 +41,7 @@ public class WeibullDistribution extends AbstractRealDistribution { */ public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9; /** Serializable version identifier. */ - private static final long serialVersionUID = 8589540077390120676L; + private static final long serialVersionUID = 20160311L; /** The shape parameter. */ private final double shape; /** The scale parameter. */ @@ -59,15 +58,7 @@ public class WeibullDistribution extends AbstractRealDistribution { private boolean numericalVarianceIsCalculated = false; /** - * Create a Weibull distribution with the given shape and scale and a - * location equal to zero. - *

- * Note: this constructor will implicitly create an instance of - * {@link Well19937c} as random generator to be used for sampling only (see - * {@link #sample()} and {@link #sample(int)}). In case no sampling is - * needed for the created distribution, it is advised to pass {@code null} - * as random generator via the appropriate constructors to avoid the - * additional initialisation overhead. + * Creates a distribution. * * @param alpha Shape parameter. * @param beta Scale parameter. @@ -80,63 +71,19 @@ public class WeibullDistribution extends AbstractRealDistribution { } /** - * Create a Weibull distribution with the given shape, scale and inverse - * cumulative probability accuracy and a location equal to zero. - *

- * Note: this constructor will implicitly create an instance of - * {@link Well19937c} as random generator to be used for sampling only (see - * {@link #sample()} and {@link #sample(int)}). In case no sampling is - * needed for the created distribution, it is advised to pass {@code null} - * as random generator via the appropriate constructors to avoid the - * additional initialisation overhead. + * Creates a distribution. * * @param alpha Shape parameter. * @param beta Scale parameter. * @param inverseCumAccuracy Maximum absolute error in inverse * cumulative probability estimates * (defaults to {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). - * @throws NotStrictlyPositiveException if {@code alpha <= 0} or - * {@code beta <= 0}. - * @since 2.1 - */ - public WeibullDistribution(double alpha, double beta, - double inverseCumAccuracy) { - this(new Well19937c(), alpha, beta, inverseCumAccuracy); - } - - /** - * Creates a Weibull distribution. - * - * @param rng Random number generator. - * @param alpha Shape parameter. - * @param beta Scale parameter. * @throws NotStrictlyPositiveException if {@code alpha <= 0} or {@code beta <= 0}. - * @since 3.3 */ - public WeibullDistribution(RandomGenerator rng, double alpha, double beta) - throws NotStrictlyPositiveException { - this(rng, alpha, beta, DEFAULT_INVERSE_ABSOLUTE_ACCURACY); - } - - /** - * Creates a Weibull distribution. - * - * @param rng Random number generator. - * @param alpha Shape parameter. - * @param beta Scale parameter. - * @param inverseCumAccuracy Maximum absolute error in inverse - * cumulative probability estimates - * (defaults to {@link #DEFAULT_INVERSE_ABSOLUTE_ACCURACY}). - * @throws NotStrictlyPositiveException if {@code alpha <= 0} or {@code beta <= 0}. - * @since 3.1 - */ - public WeibullDistribution(RandomGenerator rng, - double alpha, + public WeibullDistribution(double alpha, double beta, double inverseCumAccuracy) throws NotStrictlyPositiveException { - super(rng); - if (alpha <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.SHAPE, alpha); diff --git a/src/main/java/org/apache/commons/math4/random/EmpiricalDistribution.java b/src/main/java/org/apache/commons/math4/random/EmpiricalDistribution.java index 6d2722e13..926ad093c 100644 --- a/src/main/java/org/apache/commons/math4/random/EmpiricalDistribution.java +++ b/src/main/java/org/apache/commons/math4/random/EmpiricalDistribution.java @@ -41,6 +41,8 @@ import org.apache.commons.math4.exception.NotStrictlyPositiveException; import org.apache.commons.math4.exception.util.LocalizedFormats; import org.apache.commons.math4.stat.descriptive.StatisticalSummary; import org.apache.commons.math4.stat.descriptive.SummaryStatistics; +import org.apache.commons.math4.rng.UniformRandomProvider; +import org.apache.commons.math4.rng.RandomSource; import org.apache.commons.math4.util.FastMath; import org.apache.commons.math4.util.MathUtils; @@ -112,6 +114,7 @@ public class EmpiricalDistribution extends AbstractRealDistribution { private static final long serialVersionUID = 5729073523949762654L; /** RandomDataGenerator instance to use in repeated calls to getNext() */ + @Deprecated protected final RandomDataGenerator randomData; /** List of SummaryStatistics objects characterizing the bins */ @@ -152,8 +155,13 @@ public class EmpiricalDistribution extends AbstractRealDistribution { * @throws NotStrictlyPositiveException if {@code binCount <= 0}. */ public EmpiricalDistribution(int binCount) { - this(binCount, new RandomDataGenerator()); - } + if (binCount <= 0) { + throw new NotStrictlyPositiveException(binCount); + } + this.binCount = binCount; + binStats = new ArrayList(); + randomData = null; // XXX remove + } /** * Creates a new EmpiricalDistribution with the specified bin count using the @@ -164,8 +172,9 @@ public class EmpiricalDistribution extends AbstractRealDistribution { * @throws NotStrictlyPositiveException if {@code binCount <= 0}. * @since 3.0 */ + @Deprecated public EmpiricalDistribution(int binCount, RandomGenerator generator) { - this(binCount, new RandomDataGenerator(generator)); + this(binCount, new RandomDataGenerator(generator)); } /** @@ -175,8 +184,9 @@ public class EmpiricalDistribution extends AbstractRealDistribution { * @param generator random data generator (may be null, resulting in default JDK generator) * @since 3.0 */ + @Deprecated public EmpiricalDistribution(RandomGenerator generator) { - this(DEFAULT_BIN_COUNT, generator); + this(DEFAULT_BIN_COUNT, generator); } /** @@ -187,9 +197,9 @@ public class EmpiricalDistribution extends AbstractRealDistribution { * @param randomData Random data generator. * @throws NotStrictlyPositiveException if {@code binCount <= 0}. */ + @Deprecated private EmpiricalDistribution(int binCount, RandomDataGenerator randomData) { - super(randomData.getRandomGenerator()); if (binCount <= 0) { throw new NotStrictlyPositiveException(binCount); } @@ -452,12 +462,11 @@ public class EmpiricalDistribution extends AbstractRealDistribution { * @return the random value. * @throws MathIllegalStateException if the distribution has not been loaded */ + @Deprecated public double getNextValue() throws MathIllegalStateException { - if (!loaded) { throw new MathIllegalStateException(LocalizedFormats.DISTRIBUTION_NOT_LOADED); } - return sample(); } @@ -552,7 +561,9 @@ public class EmpiricalDistribution extends AbstractRealDistribution { * * @param seed random generator seed * @since 3.0 + * XXX REMOVE */ + @Deprecated public void reSeed(long seed) { randomData.reSeed(seed); } @@ -736,10 +747,20 @@ public class EmpiricalDistribution extends AbstractRealDistribution { * @since 3.1 */ @Override + @Deprecated public void reseedRandomGenerator(long seed) { randomData.reSeed(seed); } + /**{@inheritDoc} */ + @Override + public RealDistribution.Sampler createSampler(final UniformRandomProvider rng) { + if (!loaded) { + throw new MathIllegalStateException(LocalizedFormats.DISTRIBUTION_NOT_LOADED); + } + return super.createSampler(rng); + } + /** * The probability of bin i. * @@ -808,9 +829,8 @@ public class EmpiricalDistribution extends AbstractRealDistribution { if (bStats.getN() == 1 || bStats.getVariance() == 0) { return new ConstantRealDistribution(bStats.getMean()); } else { - return new NormalDistribution(randomData.getRandomGenerator(), - bStats.getMean(), bStats.getStandardDeviation(), - NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY); + return new NormalDistribution(bStats.getMean(), bStats.getStandardDeviation(), + NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY); } } } diff --git a/src/main/java/org/apache/commons/math4/random/RandomDataGenerator.java b/src/main/java/org/apache/commons/math4/random/RandomDataGenerator.java index 7488a5ff0..d66808dc8 100644 --- a/src/main/java/org/apache/commons/math4/random/RandomDataGenerator.java +++ b/src/main/java/org/apache/commons/math4/random/RandomDataGenerator.java @@ -18,6 +18,9 @@ package org.apache.commons.math4.random; import java.io.Serializable; +import java.io.IOException; +import java.io.ObjectOutputStream; +import java.io.ObjectInputStream; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; @@ -46,6 +49,8 @@ import org.apache.commons.math4.exception.NotStrictlyPositiveException; import org.apache.commons.math4.exception.NumberIsTooLargeException; import org.apache.commons.math4.exception.OutOfRangeException; import org.apache.commons.math4.exception.util.LocalizedFormats; +import org.apache.commons.math4.rng.RandomSource; +import org.apache.commons.math4.rng.UniformRandomProvider; import org.apache.commons.math4.util.MathArrays; /** @@ -115,8 +120,14 @@ public class RandomDataGenerator implements Serializable { private static final long serialVersionUID = -626730818244969716L; /** underlying random number generator */ + @Deprecated private RandomGenerator rand = null; + /** Underlying random number generator. */ + private transient UniformRandomProvider randomProvider = null; + /** Underlying source of randomness. */ + private final RandomSource randomSource; + /** underlying secure random number generator */ private RandomGenerator secRand = null; @@ -129,6 +140,7 @@ public class RandomDataGenerator implements Serializable { * The generator is initialized and seeded on first use.

*/ public RandomDataGenerator() { + randomSource = RandomSource.WELL_19937_C; } /** @@ -138,8 +150,20 @@ public class RandomDataGenerator implements Serializable { * @param rand the source of (non-secure) random data * (may be null, resulting in the default generator) */ + @Deprecated public RandomDataGenerator(RandomGenerator rand) { this.rand = rand; + randomSource = RandomSource.WELL_19937_C; + } + + /** + * Creates a new instance. + * + * @param source Source of (non-secure) random data. + * If {@code null}, {@link RandomSource#WELL_19937_C} will be used. + */ + public RandomDataGenerator(RandomSource source) { + randomSource = source == null ? RandomSource.WELL_19937_C : source; } /** @@ -491,8 +515,7 @@ public class RandomDataGenerator implements Serializable { * @throws NotStrictlyPositiveException if {@code mean <= 0}. */ public double nextExponential(double mean) throws NotStrictlyPositiveException { - return new ExponentialDistribution(getRandomGenerator(), mean, - ExponentialDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).sample(); + return new ExponentialDistribution(mean, ExponentialDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).createSampler(getRandomProvider()).sample(); } /** @@ -518,8 +541,7 @@ public class RandomDataGenerator implements Serializable { * {@code scale <= 0}. */ public double nextGamma(double shape, double scale) throws NotStrictlyPositiveException { - return new GammaDistribution(getRandomGenerator(),shape, scale, - GammaDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).sample(); + return new GammaDistribution(shape, scale, GammaDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).createSampler(getRandomProvider()).sample(); } /** @@ -535,8 +557,7 @@ public class RandomDataGenerator implements Serializable { * @throws NotPositiveException if {@code numberOfSuccesses < 0}. */ public int nextHypergeometric(int populationSize, int numberOfSuccesses, int sampleSize) throws NotPositiveException, NotStrictlyPositiveException, NumberIsTooLargeException { - return new HypergeometricDistribution(getRandomGenerator(),populationSize, - numberOfSuccesses, sampleSize).sample(); + return new HypergeometricDistribution(getRandomGenerator(), populationSize, numberOfSuccesses, sampleSize).sample(); } /** @@ -561,8 +582,7 @@ public class RandomDataGenerator implements Serializable { * @throws NotStrictlyPositiveException if {@code df <= 0} */ public double nextT(double df) throws NotStrictlyPositiveException { - return new TDistribution(getRandomGenerator(), df, - TDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).sample(); + return new TDistribution(df, TDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).createSampler(getRandomProvider()).sample(); } /** @@ -575,8 +595,7 @@ public class RandomDataGenerator implements Serializable { * {@code scale <= 0}. */ public double nextWeibull(double shape, double scale) throws NotStrictlyPositiveException { - return new WeibullDistribution(getRandomGenerator(), shape, scale, - WeibullDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).sample(); + return new WeibullDistribution(shape, scale, WeibullDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).createSampler(getRandomProvider()).sample(); } /** @@ -600,8 +619,7 @@ public class RandomDataGenerator implements Serializable { * @return random value sampled from the beta(alpha, beta) distribution */ public double nextBeta(double alpha, double beta) { - return new BetaDistribution(getRandomGenerator(), alpha, beta, - BetaDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).sample(); + return new BetaDistribution(alpha, beta, BetaDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).createSampler(getRandomProvider()).sample(); } /** @@ -623,8 +641,7 @@ public class RandomDataGenerator implements Serializable { * @return random value sampled from the Cauchy(median, scale) distribution */ public double nextCauchy(double median, double scale) { - return new CauchyDistribution(getRandomGenerator(), median, scale, - CauchyDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).sample(); + return new CauchyDistribution(median, scale, CauchyDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).createSampler(getRandomProvider()).sample(); } /** @@ -634,8 +651,7 @@ public class RandomDataGenerator implements Serializable { * @return random value sampled from the ChiSquare(df) distribution */ public double nextChiSquare(double df) { - return new ChiSquaredDistribution(getRandomGenerator(), df, - ChiSquaredDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).sample(); + return new ChiSquaredDistribution(df, ChiSquaredDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).createSampler(getRandomProvider()).sample(); } /** @@ -648,8 +664,7 @@ public class RandomDataGenerator implements Serializable { * {@code numeratorDf <= 0} or {@code denominatorDf <= 0}. */ public double nextF(double numeratorDf, double denominatorDf) throws NotStrictlyPositiveException { - return new FDistribution(getRandomGenerator(), numeratorDf, denominatorDf, - FDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).sample(); + return new FDistribution(numeratorDf, denominatorDf, FDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).createSampler(getRandomProvider()).sample(); } /** @@ -830,7 +845,8 @@ public class RandomDataGenerator implements Serializable { * @param seed the seed value to use */ public void reSeed(long seed) { - getRandomGenerator().setSeed(seed); + randomProvider = RandomSource.create(randomSource, seed); + getRandomGenerator().setSeed(seed); } /** @@ -861,7 +877,7 @@ public class RandomDataGenerator implements Serializable { * {@code System.currentTimeMillis() + System.identityHashCode(this))}. */ public void reSeed() { - getRandomGenerator().setSeed(System.currentTimeMillis() + System.identityHashCode(this)); + reSeed(System.currentTimeMillis() + System.identityHashCode(this)); } /** @@ -896,6 +912,7 @@ public class RandomDataGenerator implements Serializable { * @return the Random used to generate random data * @since 3.2 */ + @Deprecated public RandomGenerator getRandomGenerator() { if (rand == null) { initRan(); @@ -903,10 +920,24 @@ public class RandomDataGenerator implements Serializable { return rand; } + /** + * @return the generator used to generate non-secure random data. + * + * XXX TODO: method cannot be "private" because of its use in "ValueServer" in "DIGEST_MODE". + * "ValueServer" should be fixed to not use the internals of another class! + */ + UniformRandomProvider getRandomProvider() { + if (randomProvider == null) { + randomProvider = RandomSource.create(randomSource); + } + return randomProvider; + } + /** * Sets the default generator to a {@link Well19937c} generator seeded with * {@code System.currentTimeMillis() + System.identityHashCode(this))}. */ + @Deprecated private void initRan() { rand = new Well19937c(System.currentTimeMillis() + System.identityHashCode(this)); } @@ -928,4 +959,42 @@ public class RandomDataGenerator implements Serializable { } return secRand; } + + /** + * @param out Output stream. + * @throws IOException if an error occurs. + */ + private void writeObject(ObjectOutputStream out) + throws IOException { + // Write non-transient fields. + out.defaultWriteObject(); + + if (randomProvider != null) { + // Save state of "randomProvider". + out.writeObject(RandomSource.saveState(randomProvider)); + } else { + out.writeObject(null); + } + } + + /** + * @param in Input stream. + * @throws IOException if an error occurs. + * @throws ClassNotFoundException if an error occurs. + */ + private void readObject(ObjectInputStream in) + throws IOException, + ClassNotFoundException { + // Read non-transient fields. + in.defaultReadObject(); + + // Read "randomProvider" state (can be null). + final Object state = in.readObject(); + if (state != null) { + // Recreate "randomProvider" from serialized info. + randomProvider = RandomSource.create(randomSource); + // And restore its state. + RandomSource.restoreState(randomProvider, (RandomSource.State) state); + } + } } diff --git a/src/main/java/org/apache/commons/math4/random/ValueServer.java b/src/main/java/org/apache/commons/math4/random/ValueServer.java index de9118a13..9e32035e7 100644 --- a/src/main/java/org/apache/commons/math4/random/ValueServer.java +++ b/src/main/java/org/apache/commons/math4/random/ValueServer.java @@ -202,7 +202,7 @@ public class ValueServer { * @throws ZeroException if URL contains no data */ public void computeDistribution(int binCount) throws NullArgumentException, IOException, ZeroException { - empiricalDistribution = new EmpiricalDistribution(binCount, randomData.getRandomGenerator()); + empiricalDistribution = new EmpiricalDistribution(binCount); empiricalDistribution.load(valuesFileURL); mu = empiricalDistribution.getSampleStats().getMean(); sigma = empiricalDistribution.getSampleStats().getStandardDeviation(); @@ -372,7 +372,7 @@ public class ValueServer { (empiricalDistribution.getBinStats().size() == 0)) { throw new MathIllegalStateException(LocalizedFormats.DIGEST_NOT_INITIALIZED); } - return empiricalDistribution.getNextValue(); + return empiricalDistribution.createSampler(randomData.getRandomProvider()).sample(); } /** diff --git a/src/main/java/org/apache/commons/math4/stat/inference/ChiSquareTest.java b/src/main/java/org/apache/commons/math4/stat/inference/ChiSquareTest.java index fead59e01..add6c01ce 100644 --- a/src/main/java/org/apache/commons/math4/stat/inference/ChiSquareTest.java +++ b/src/main/java/org/apache/commons/math4/stat/inference/ChiSquareTest.java @@ -157,7 +157,7 @@ public class ChiSquareTest { // 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); + new ChiSquaredDistribution(expected.length - 1.0); return 1.0 - distribution.cumulativeProbability(chiSquare(expected, observed)); } @@ -510,7 +510,7 @@ public class ChiSquareTest { // 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); + new ChiSquaredDistribution((double) observed1.length - 1); return 1 - distribution.cumulativeProbability( chiSquareDataSetsComparison(observed1, observed2)); diff --git a/src/main/java/org/apache/commons/math4/stat/inference/GTest.java b/src/main/java/org/apache/commons/math4/stat/inference/GTest.java index 24958040e..0ee009d29 100644 --- a/src/main/java/org/apache/commons/math4/stat/inference/GTest.java +++ b/src/main/java/org/apache/commons/math4/stat/inference/GTest.java @@ -154,7 +154,7 @@ public class GTest { // 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); + new ChiSquaredDistribution(expected.length - 1.0); return 1.0 - distribution.cumulativeProbability(g(expected, observed)); } @@ -185,7 +185,7 @@ public class GTest { // pass a null rng to avoid unneeded overhead as we will not sample from this distribution final ChiSquaredDistribution distribution = - new ChiSquaredDistribution(null, expected.length - 2.0); + new ChiSquaredDistribution(expected.length - 2.0); return 1.0 - distribution.cumulativeProbability(g(expected, observed)); } @@ -475,7 +475,7 @@ public class GTest { // 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); + new ChiSquaredDistribution((double) observed1.length - 1); return 1 - distribution.cumulativeProbability( gDataSetsComparison(observed1, observed2)); } diff --git a/src/main/java/org/apache/commons/math4/stat/inference/KolmogorovSmirnovTest.java b/src/main/java/org/apache/commons/math4/stat/inference/KolmogorovSmirnovTest.java index 04296a301..b75de0e8e 100644 --- a/src/main/java/org/apache/commons/math4/stat/inference/KolmogorovSmirnovTest.java +++ b/src/main/java/org/apache/commons/math4/stat/inference/KolmogorovSmirnovTest.java @@ -23,6 +23,7 @@ import java.util.HashSet; import org.apache.commons.math4.distribution.EnumeratedRealDistribution; import org.apache.commons.math4.distribution.RealDistribution; +import org.apache.commons.math4.distribution.AbstractRealDistribution; import org.apache.commons.math4.distribution.UniformRealDistribution; import org.apache.commons.math4.exception.InsufficientDataException; import org.apache.commons.math4.exception.MathArithmeticException; @@ -39,9 +40,8 @@ import org.apache.commons.math4.linear.Array2DRowFieldMatrix; import org.apache.commons.math4.linear.FieldMatrix; import org.apache.commons.math4.linear.MatrixUtils; import org.apache.commons.math4.linear.RealMatrix; -import org.apache.commons.math4.random.JDKRandomGenerator; -import org.apache.commons.math4.random.RandomGenerator; -import org.apache.commons.math4.random.Well19937c; +import org.apache.commons.math4.rng.RandomSource; +import org.apache.commons.math4.rng.UniformRandomProvider; import org.apache.commons.math4.util.CombinatoricsUtils; import org.apache.commons.math4.util.FastMath; import org.apache.commons.math4.util.MathArrays; @@ -145,14 +145,14 @@ public class KolmogorovSmirnovTest { @Deprecated protected static final int MONTE_CARLO_ITERATIONS = 1000000; - /** Random data generator used by {@link #monteCarloP(double, int, int, boolean, int)} */ - private final RandomGenerator rng; + @Deprecated + private final UniformRandomProvider rng; /** * Construct a KolmogorovSmirnovTest instance with a default random data generator. */ public KolmogorovSmirnovTest() { - rng = new Well19937c(); + rng = RandomSource.create(RandomSource.WELL_19937_C); } /** @@ -163,8 +163,9 @@ public class KolmogorovSmirnovTest { * @param rng random data generator used by {@link #monteCarloP(double, int, int, boolean, int)} */ @Deprecated - public KolmogorovSmirnovTest(RandomGenerator rng) { - this.rng = rng; + public KolmogorovSmirnovTest(RandomSource source, + long seed) { + rng = RandomSource.create(source, seed); } /** @@ -419,7 +420,7 @@ public class KolmogorovSmirnovTest { final double[] combined = new double[xLength + yLength]; System.arraycopy(x, 0, combined, 0, xLength); System.arraycopy(y, 0, combined, xLength, yLength); - final EnumeratedRealDistribution dist = new EnumeratedRealDistribution(rng, combined); + final RealDistribution.Sampler sampler = new EnumeratedRealDistribution(combined).createSampler(rng); final long d = integralKolmogorovSmirnovStatistic(x, y); int greaterCount = 0; int equalCount = 0; @@ -427,8 +428,8 @@ public class KolmogorovSmirnovTest { double[] curY; long curD; for (int i = 0; i < iterations; i++) { - curX = dist.sample(xLength); - curY = dist.sample(yLength); + curX = AbstractRealDistribution.sample(xLength, sampler); + curY = AbstractRealDistribution.sample(yLength, sampler); curD = integralKolmogorovSmirnovStatistic(curX, curY); if (curD > d) { greaterCount++; @@ -1041,7 +1042,7 @@ public class KolmogorovSmirnovTest { * @param numberOfTrueValues number of {@code true} values the boolean array should finally have * @param rng random data generator */ - static void fillBooleanArrayRandomlyWithFixedNumberTrueValues(final boolean[] b, final int numberOfTrueValues, final RandomGenerator rng) { + private static void fillBooleanArrayRandomlyWithFixedNumberTrueValues(final boolean[] b, final int numberOfTrueValues, final UniformRandomProvider rng) { Arrays.fill(b, true); for (int k = numberOfTrueValues; k < b.length; k++) { final int r = rng.nextInt(k + 1); @@ -1154,16 +1155,16 @@ public class KolmogorovSmirnovTest { // Add jitter using a fixed seed (so same arguments always give same results), // low-initialization-overhead generator - final RealDistribution dist = - new UniformRealDistribution(new JDKRandomGenerator(100), -minDelta, minDelta); + final RealDistribution.Sampler sampler = + new UniformRealDistribution(-minDelta, minDelta).createSampler(RandomSource.create(RandomSource.JDK, 100)); // It is theoretically possible that jitter does not break ties, so repeat // until all ties are gone. Bound the loop and throw MIE if bound is exceeded. int ct = 0; boolean ties = true; do { - jitter(x, dist); - jitter(y, dist); + jitter(x, sampler); + jitter(y, sampler); ties = hasTies(x, y); ct++; } while (ties && ct < 1000); @@ -1202,13 +1203,16 @@ public class KolmogorovSmirnovTest { * values are overwritten with the result of applying jitter.

* * @param data input/output data array - entries overwritten by the method - * @param dist probability distribution to sample for jitter values + * @param sampler probability distribution to sample for jitter values * @throws NullPointerException if either of the parameters is null */ - private static void jitter(double[] data, RealDistribution dist) { + private static void jitter(double[] data, RealDistribution.Sampler sampler) { for (int i = 0; i < data.length; i++) { - data[i] += dist.sample(); + final double d = sampler.sample(); + System.out.println("d=" + d); // XXX + data[i] += d; } + System.out.println(); // XXX } /** diff --git a/src/main/java/org/apache/commons/math4/stat/inference/MannWhitneyUTest.java b/src/main/java/org/apache/commons/math4/stat/inference/MannWhitneyUTest.java index 3fa34a36c..44528163c 100644 --- a/src/main/java/org/apache/commons/math4/stat/inference/MannWhitneyUTest.java +++ b/src/main/java/org/apache/commons/math4/stat/inference/MannWhitneyUTest.java @@ -182,7 +182,7 @@ public class MannWhitneyUTest { // No try-catch or advertised exception because args are valid // pass a null rng to avoid unneeded overhead as we will not sample from this distribution - final NormalDistribution standardNormal = new NormalDistribution(null, 0, 1); + final NormalDistribution standardNormal = new NormalDistribution(0, 1); return 2 * standardNormal.cumulativeProbability(z); } diff --git a/src/main/java/org/apache/commons/math4/stat/inference/OneWayAnova.java b/src/main/java/org/apache/commons/math4/stat/inference/OneWayAnova.java index 90f4e9680..1befaa2de 100644 --- a/src/main/java/org/apache/commons/math4/stat/inference/OneWayAnova.java +++ b/src/main/java/org/apache/commons/math4/stat/inference/OneWayAnova.java @@ -127,7 +127,7 @@ public class OneWayAnova { final AnovaStats a = anovaStats(categoryData); // No try-catch or advertised exception because args are valid // 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); + final FDistribution fdist = new FDistribution(a.dfbg, a.dfwg); return 1.0 - fdist.cumulativeProbability(a.F); } @@ -169,7 +169,7 @@ public class OneWayAnova { final AnovaStats a = anovaStats(categoryData, allowOneElementData); // 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); + final FDistribution fdist = new FDistribution(a.dfbg, a.dfwg); return 1.0 - fdist.cumulativeProbability(a.F); } diff --git a/src/main/java/org/apache/commons/math4/stat/inference/TTest.java b/src/main/java/org/apache/commons/math4/stat/inference/TTest.java index 2168099ba..577ac29d0 100644 --- a/src/main/java/org/apache/commons/math4/stat/inference/TTest.java +++ b/src/main/java/org/apache/commons/math4/stat/inference/TTest.java @@ -1058,7 +1058,7 @@ public class TTest { 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); + final TDistribution distribution = new TDistribution(n - 1); return 2.0 * distribution.cumulativeProbability(-t); } @@ -1088,7 +1088,7 @@ public class TTest { final double t = FastMath.abs(t(m1, m2, v1, v2, n1, n2)); final double degreesOfFreedom = df(v1, v2, n1, n2); // pass a null rng to avoid unneeded overhead as we will not sample from this distribution - final TDistribution distribution = new TDistribution(null, degreesOfFreedom); + final TDistribution distribution = new TDistribution(degreesOfFreedom); return 2.0 * distribution.cumulativeProbability(-t); } @@ -1118,7 +1118,7 @@ public class TTest { final double t = FastMath.abs(homoscedasticT(m1, m2, v1, v2, n1, n2)); final double degreesOfFreedom = n1 + n2 - 2; // pass a null rng to avoid unneeded overhead as we will not sample from this distribution - final TDistribution distribution = new TDistribution(null, degreesOfFreedom); + final TDistribution distribution = new TDistribution(degreesOfFreedom); return 2.0 * distribution.cumulativeProbability(-t); } diff --git a/src/main/java/org/apache/commons/math4/stat/inference/WilcoxonSignedRankTest.java b/src/main/java/org/apache/commons/math4/stat/inference/WilcoxonSignedRankTest.java index 2bd303ce7..537d1c490 100644 --- a/src/main/java/org/apache/commons/math4/stat/inference/WilcoxonSignedRankTest.java +++ b/src/main/java/org/apache/commons/math4/stat/inference/WilcoxonSignedRankTest.java @@ -254,7 +254,7 @@ public class WilcoxonSignedRankTest { // No try-catch or advertised exception because args are valid // pass a null rng to avoid unneeded overhead as we will not sample from this distribution - final NormalDistribution standardNormal = new NormalDistribution(null, 0, 1); + final NormalDistribution standardNormal = new NormalDistribution(0, 1); return 2*standardNormal.cumulativeProbability(z); } diff --git a/src/test/java/org/apache/commons/math4/analysis/interpolation/AkimaSplineInterpolatorTest.java b/src/test/java/org/apache/commons/math4/analysis/interpolation/AkimaSplineInterpolatorTest.java index 15e4281fb..9eeb33ace 100644 --- a/src/test/java/org/apache/commons/math4/analysis/interpolation/AkimaSplineInterpolatorTest.java +++ b/src/test/java/org/apache/commons/math4/analysis/interpolation/AkimaSplineInterpolatorTest.java @@ -20,13 +20,14 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import org.apache.commons.math4.analysis.UnivariateFunction; +import org.apache.commons.math4.distribution.RealDistribution; import org.apache.commons.math4.distribution.UniformRealDistribution; import org.apache.commons.math4.exception.DimensionMismatchException; import org.apache.commons.math4.exception.NonMonotonicSequenceException; import org.apache.commons.math4.exception.NullArgumentException; import org.apache.commons.math4.exception.NumberIsTooSmallException; -import org.apache.commons.math4.random.RandomGenerator; -import org.apache.commons.math4.random.Well19937c; +import org.apache.commons.math4.rng.UniformRandomProvider; +import org.apache.commons.math4.rng.RandomSource; import org.apache.commons.math4.util.FastMath; import org.apache.commons.math4.util.Precision; import org.junit.Assert; @@ -212,9 +213,9 @@ public class AkimaSplineInterpolatorTest assertTrue( Precision.equals( expected, actual ) ); } - final RandomGenerator rng = new Well19937c( 1234567L ); // "tol" depends on the seed. - final UniformRealDistribution distX = - new UniformRealDistribution( rng, xValues[0], xValues[xValues.length - 1] ); + final UniformRandomProvider rng = RandomSource.create(RandomSource.WELL_19937_C, 1234567L); // "tol" depends on the seed. + final RealDistribution.Sampler distX = + new UniformRealDistribution(xValues[0], xValues[xValues.length - 1]).createSampler(rng); double sumError = 0; for ( int i = 0; i < numberOfSamples; i++ ) diff --git a/src/test/java/org/apache/commons/math4/analysis/interpolation/BicubicInterpolatingFunctionTest.java b/src/test/java/org/apache/commons/math4/analysis/interpolation/BicubicInterpolatingFunctionTest.java index 0ec77aa42..105cf70f7 100644 --- a/src/test/java/org/apache/commons/math4/analysis/interpolation/BicubicInterpolatingFunctionTest.java +++ b/src/test/java/org/apache/commons/math4/analysis/interpolation/BicubicInterpolatingFunctionTest.java @@ -17,12 +17,13 @@ package org.apache.commons.math4.analysis.interpolation; import org.apache.commons.math4.analysis.BivariateFunction; +import org.apache.commons.math4.distribution.RealDistribution; import org.apache.commons.math4.distribution.UniformRealDistribution; import org.apache.commons.math4.exception.DimensionMismatchException; import org.apache.commons.math4.exception.MathIllegalArgumentException; import org.apache.commons.math4.exception.OutOfRangeException; -import org.apache.commons.math4.random.RandomGenerator; -import org.apache.commons.math4.random.Well19937c; +import org.apache.commons.math4.rng.UniformRandomProvider; +import org.apache.commons.math4.rng.RandomSource; import org.apache.commons.math4.util.FastMath; import org.apache.commons.math4.util.Precision; import org.junit.Assert; @@ -360,9 +361,9 @@ public final class BicubicInterpolatingFunctionTest { } } - final RandomGenerator rng = new Well19937c(1234567L); - final UniformRealDistribution distX = new UniformRealDistribution(rng, xValues[0], xValues[xValues.length - 1]); - final UniformRealDistribution distY = new UniformRealDistribution(rng, yValues[0], yValues[yValues.length - 1]); + final UniformRandomProvider rng = RandomSource.create(RandomSource.WELL_19937_C, 1234567L); + final RealDistribution.Sampler distX = new UniformRealDistribution(xValues[0], xValues[xValues.length - 1]).createSampler(rng); + final RealDistribution.Sampler distY = new UniformRealDistribution(yValues[0], yValues[yValues.length - 1]).createSampler(rng); double sumError = 0; for (int i = 0; i < numberOfSamples; i++) { diff --git a/src/test/java/org/apache/commons/math4/analysis/interpolation/BicubicInterpolatorTest.java b/src/test/java/org/apache/commons/math4/analysis/interpolation/BicubicInterpolatorTest.java index 6a2a9d760..f88ec55a8 100644 --- a/src/test/java/org/apache/commons/math4/analysis/interpolation/BicubicInterpolatorTest.java +++ b/src/test/java/org/apache/commons/math4/analysis/interpolation/BicubicInterpolatorTest.java @@ -17,11 +17,12 @@ package org.apache.commons.math4.analysis.interpolation; import org.apache.commons.math4.analysis.BivariateFunction; +import org.apache.commons.math4.distribution.RealDistribution; import org.apache.commons.math4.distribution.UniformRealDistribution; import org.apache.commons.math4.exception.DimensionMismatchException; import org.apache.commons.math4.exception.MathIllegalArgumentException; -import org.apache.commons.math4.random.RandomGenerator; -import org.apache.commons.math4.random.Well19937c; +import org.apache.commons.math4.rng.UniformRandomProvider; +import org.apache.commons.math4.rng.RandomSource; import org.junit.Assert; import org.junit.Test; @@ -146,9 +147,9 @@ public final class BicubicInterpolatorTest { final BicubicInterpolatingFunction p = interpolator.interpolate(xval, yval, zval); double x, y; - final RandomGenerator rng = new Well19937c(); - final UniformRealDistribution distX = new UniformRealDistribution(rng, xval[0], xval[xval.length - 1]); - final UniformRealDistribution distY = new UniformRealDistribution(rng, yval[0], yval[yval.length - 1]); + final UniformRandomProvider rng = RandomSource.create(RandomSource.WELL_19937_C); + final RealDistribution.Sampler distX = new UniformRealDistribution(xval[0], xval[xval.length - 1]).createSampler(rng); + final RealDistribution.Sampler distY = new UniformRealDistribution(yval[0], yval[yval.length - 1]).createSampler(rng); int count = 0; while (true) { diff --git a/src/test/java/org/apache/commons/math4/analysis/interpolation/PiecewiseBicubicSplineInterpolatingFunctionTest.java b/src/test/java/org/apache/commons/math4/analysis/interpolation/PiecewiseBicubicSplineInterpolatingFunctionTest.java index c6701b302..48ff9a094 100644 --- a/src/test/java/org/apache/commons/math4/analysis/interpolation/PiecewiseBicubicSplineInterpolatingFunctionTest.java +++ b/src/test/java/org/apache/commons/math4/analysis/interpolation/PiecewiseBicubicSplineInterpolatingFunctionTest.java @@ -17,13 +17,14 @@ package org.apache.commons.math4.analysis.interpolation; import org.apache.commons.math4.analysis.BivariateFunction; +import org.apache.commons.math4.distribution.RealDistribution; import org.apache.commons.math4.distribution.UniformRealDistribution; import org.apache.commons.math4.exception.DimensionMismatchException; import org.apache.commons.math4.exception.InsufficientDataException; import org.apache.commons.math4.exception.NonMonotonicSequenceException; import org.apache.commons.math4.exception.NullArgumentException; -import org.apache.commons.math4.random.RandomGenerator; -import org.apache.commons.math4.random.Well19937c; +import org.apache.commons.math4.rng.UniformRandomProvider; +import org.apache.commons.math4.rng.RandomSource; import org.apache.commons.math4.util.FastMath; import org.apache.commons.math4.util.Precision; import org.junit.Assert; @@ -175,7 +176,7 @@ public final class PiecewiseBicubicSplineInterpolatingFunctionTest { final double maximumY = 10; final int numberOfSamples = 100; - final double interpolationTolerance = 2e-14; + final double interpolationTolerance = 1e-13; final double maxTolerance = 6e-14; // Function values @@ -250,9 +251,9 @@ public final class PiecewiseBicubicSplineInterpolatingFunctionTest { } } - final RandomGenerator rng = new Well19937c(1234567L); - final UniformRealDistribution distX = new UniformRealDistribution(rng, xValues[0], xValues[xValues.length - 1]); - final UniformRealDistribution distY = new UniformRealDistribution(rng, yValues[0], yValues[yValues.length - 1]); + final UniformRandomProvider rng = RandomSource.create(RandomSource.WELL_19937_C, 1234567L); + final RealDistribution.Sampler distX = new UniformRealDistribution(xValues[0], xValues[xValues.length - 1]).createSampler(rng); + final RealDistribution.Sampler distY = new UniformRealDistribution(yValues[0], yValues[yValues.length - 1]).createSampler(rng); double sumError = 0; for (int i = 0; i < numberOfSamples; i++) { diff --git a/src/test/java/org/apache/commons/math4/analysis/interpolation/PiecewiseBicubicSplineInterpolatorTest.java b/src/test/java/org/apache/commons/math4/analysis/interpolation/PiecewiseBicubicSplineInterpolatorTest.java index 1645294d4..69bd8b449 100644 --- a/src/test/java/org/apache/commons/math4/analysis/interpolation/PiecewiseBicubicSplineInterpolatorTest.java +++ b/src/test/java/org/apache/commons/math4/analysis/interpolation/PiecewiseBicubicSplineInterpolatorTest.java @@ -17,13 +17,14 @@ package org.apache.commons.math4.analysis.interpolation; import org.apache.commons.math4.analysis.BivariateFunction; +import org.apache.commons.math4.distribution.RealDistribution; import org.apache.commons.math4.distribution.UniformRealDistribution; import org.apache.commons.math4.exception.DimensionMismatchException; import org.apache.commons.math4.exception.InsufficientDataException; import org.apache.commons.math4.exception.NonMonotonicSequenceException; import org.apache.commons.math4.exception.NullArgumentException; -import org.apache.commons.math4.random.RandomGenerator; -import org.apache.commons.math4.random.Well19937c; +import org.apache.commons.math4.rng.UniformRandomProvider; +import org.apache.commons.math4.rng.RandomSource; import org.junit.Assert; import org.junit.Test; @@ -157,9 +158,9 @@ public final class PiecewiseBicubicSplineInterpolatorTest { BivariateFunction p = interpolator.interpolate(xval, yval, zval); double x, y; - final RandomGenerator rng = new Well19937c(1234567L); // "tol" depends on the seed. - final UniformRealDistribution distX = new UniformRealDistribution( rng, xval[0], xval[xval.length - 1] ); - final UniformRealDistribution distY = new UniformRealDistribution( rng, yval[0], yval[yval.length - 1] ); + final UniformRandomProvider rng = RandomSource.create(RandomSource.WELL_19937_C, 1234567L); + final RealDistribution.Sampler distX = new UniformRealDistribution(xval[0], xval[xval.length - 1]).createSampler(rng); + final RealDistribution.Sampler distY = new UniformRealDistribution(yval[0], yval[yval.length - 1]).createSampler(rng); final int numSamples = 50; final double tol = 2e-14; @@ -209,9 +210,9 @@ public final class PiecewiseBicubicSplineInterpolatorTest { BivariateFunction p = interpolator.interpolate(xval, yval, zval); double x, y; - final RandomGenerator rng = new Well19937c(1234567L); // "tol" depends on the seed. - final UniformRealDistribution distX = new UniformRealDistribution( rng, xval[0], xval[xval.length - 1] ); - final UniformRealDistribution distY = new UniformRealDistribution( rng, yval[0], yval[yval.length - 1] ); + final UniformRandomProvider rng = RandomSource.create(RandomSource.WELL_19937_C, 1234567L); + final RealDistribution.Sampler distX = new UniformRealDistribution(xval[0], xval[xval.length - 1]).createSampler(rng); + final RealDistribution.Sampler distY = new UniformRealDistribution(yval[0], yval[yval.length - 1]).createSampler(rng); final int numSamples = 50; final double tol = 5e-13; diff --git a/src/test/java/org/apache/commons/math4/analysis/interpolation/TricubicInterpolatingFunctionTest.java b/src/test/java/org/apache/commons/math4/analysis/interpolation/TricubicInterpolatingFunctionTest.java index 44f06a051..deac81fac 100644 --- a/src/test/java/org/apache/commons/math4/analysis/interpolation/TricubicInterpolatingFunctionTest.java +++ b/src/test/java/org/apache/commons/math4/analysis/interpolation/TricubicInterpolatingFunctionTest.java @@ -17,11 +17,12 @@ package org.apache.commons.math4.analysis.interpolation; import org.apache.commons.math4.analysis.TrivariateFunction; +import org.apache.commons.math4.distribution.RealDistribution; import org.apache.commons.math4.distribution.UniformRealDistribution; import org.apache.commons.math4.exception.DimensionMismatchException; import org.apache.commons.math4.exception.MathIllegalArgumentException; -import org.apache.commons.math4.random.RandomGenerator; -import org.apache.commons.math4.random.Well19937c; +import org.apache.commons.math4.rng.UniformRandomProvider; +import org.apache.commons.math4.rng.RandomSource; import org.apache.commons.math4.util.FastMath; import org.apache.commons.math4.util.Precision; import org.junit.Assert; @@ -379,10 +380,10 @@ public final class TricubicInterpolatingFunctionTest { } } - final RandomGenerator rng = new Well19937c(1234567L); - final UniformRealDistribution distX = new UniformRealDistribution(rng, xValues[0], xValues[xValues.length - 1]); - final UniformRealDistribution distY = new UniformRealDistribution(rng, yValues[0], yValues[yValues.length - 1]); - final UniformRealDistribution distZ = new UniformRealDistribution(rng, zValues[0], zValues[zValues.length - 1]); + final UniformRandomProvider rng = RandomSource.create(RandomSource.WELL_19937_C, 1234568L); + final RealDistribution.Sampler distX = new UniformRealDistribution(xValues[0], xValues[xValues.length - 1]).createSampler(rng); + final RealDistribution.Sampler distY = new UniformRealDistribution(yValues[0], yValues[yValues.length - 1]).createSampler(rng); + final RealDistribution.Sampler distZ = new UniformRealDistribution(zValues[0], zValues[zValues.length - 1]).createSampler(rng); double sumError = 0; for (int i = 0; i < numberOfSamples; i++) { diff --git a/src/test/java/org/apache/commons/math4/distribution/AbstractRealDistributionTest.java b/src/test/java/org/apache/commons/math4/distribution/AbstractRealDistributionTest.java index cff0c0837..449f4a607 100644 --- a/src/test/java/org/apache/commons/math4/distribution/AbstractRealDistributionTest.java +++ b/src/test/java/org/apache/commons/math4/distribution/AbstractRealDistributionTest.java @@ -35,7 +35,7 @@ public class AbstractRealDistributionTest { final double x3 = 3.0; final double p12 = 0.5; final AbstractRealDistribution distribution; - distribution = new AbstractRealDistribution(null) { + distribution = new AbstractRealDistribution() { private static final long serialVersionUID = 1L; @Override @@ -119,7 +119,7 @@ public class AbstractRealDistributionTest { final double p12 = 1.0 / 3.0; final double p23 = 2.0 / 3.0; final AbstractRealDistribution distribution; - distribution = new AbstractRealDistribution(null) { + distribution = new AbstractRealDistribution() { private static final long serialVersionUID = 1L; @Override diff --git a/src/test/java/org/apache/commons/math4/distribution/BetaDistributionTest.java b/src/test/java/org/apache/commons/math4/distribution/BetaDistributionTest.java index 373271d0c..51275036f 100644 --- a/src/test/java/org/apache/commons/math4/distribution/BetaDistributionTest.java +++ b/src/test/java/org/apache/commons/math4/distribution/BetaDistributionTest.java @@ -346,8 +346,6 @@ public class BetaDistributionTest { public void testGoodnessOfFit() { final UniformRandomProvider rng = RandomSource.create(RandomSource.WELL_19937_A, 123456789L); - final RandomGenerator random = new Well19937a(0x237db1db907b089fL); - final int numSamples = 1000; final double level = 0.01; for (final double alpha : alphaBetas) { @@ -360,7 +358,7 @@ public class BetaDistributionTest { Assert.assertFalse("G goodness-of-fit test rejected null at alpha = " + level, gTest(betaDistribution, observed) < level); Assert.assertFalse("KS goodness-of-fit test rejected null at alpha = " + level, - new KolmogorovSmirnovTest(random).kolmogorovSmirnovTest(betaDistribution, observed) < level); + new KolmogorovSmirnovTest(RandomSource.JDK, 3448845623L).kolmogorovSmirnovTest(betaDistribution, observed) < level); } } } diff --git a/src/test/java/org/apache/commons/math4/distribution/ConstantRealDistributionTest.java b/src/test/java/org/apache/commons/math4/distribution/ConstantRealDistributionTest.java index 211a6703e..a2c77060e 100644 --- a/src/test/java/org/apache/commons/math4/distribution/ConstantRealDistributionTest.java +++ b/src/test/java/org/apache/commons/math4/distribution/ConstantRealDistributionTest.java @@ -82,15 +82,6 @@ public class ConstantRealDistributionTest extends RealDistributionAbstractTest { Assert.assertEquals(dist.getNumericalVariance(), 0, 0d); } - @Test - @Override - public void testSampling() { - ConstantRealDistribution dist = new ConstantRealDistribution(0); - for (int i = 0; i < 10; i++) { - Assert.assertEquals(0, dist.sample(), 0); - } - } - @Test @Override public void testSampler() { diff --git a/src/test/java/org/apache/commons/math4/distribution/EnumeratedRealDistributionTest.java b/src/test/java/org/apache/commons/math4/distribution/EnumeratedRealDistributionTest.java index 0300e5e14..7cfebf7b7 100644 --- a/src/test/java/org/apache/commons/math4/distribution/EnumeratedRealDistributionTest.java +++ b/src/test/java/org/apache/commons/math4/distribution/EnumeratedRealDistributionTest.java @@ -30,6 +30,7 @@ import org.apache.commons.math4.exception.NotFiniteNumberException; import org.apache.commons.math4.exception.NotPositiveException; import org.apache.commons.math4.util.FastMath; import org.apache.commons.math4.util.Pair; +import org.apache.commons.math4.rng.UniformRandomProvider; import org.apache.commons.math4.rng.RandomSource; import org.junit.Assert; import org.junit.Test; @@ -197,7 +198,8 @@ public class EnumeratedRealDistributionTest { List> list = new ArrayList>(); list.add(new Pair(new Object() {}, new Double(0))); list.add(new Pair(new Object() {}, new Double(1))); - Assert.assertEquals(1, new EnumeratedDistribution(list).sample(1).length); + final UniformRandomProvider rng = RandomSource.create(RandomSource.WELL_512_A); + Assert.assertEquals(1, new EnumeratedDistribution(list).createSampler(rng).sample(1).length); } @Test diff --git a/src/test/java/org/apache/commons/math4/distribution/LevyDistributionTest.java b/src/test/java/org/apache/commons/math4/distribution/LevyDistributionTest.java index d0af056d2..659e41859 100644 --- a/src/test/java/org/apache/commons/math4/distribution/LevyDistributionTest.java +++ b/src/test/java/org/apache/commons/math4/distribution/LevyDistributionTest.java @@ -17,7 +17,6 @@ package org.apache.commons.math4.distribution; import org.apache.commons.math4.distribution.LevyDistribution; -import org.apache.commons.math4.random.Well19937a; import org.apache.commons.math4.util.Precision; import org.junit.Assert; import org.junit.Test; @@ -41,7 +40,7 @@ public class LevyDistributionTest extends RealDistributionAbstractTest { @Override public LevyDistribution makeDistribution() { - return new LevyDistribution(new Well19937a(0xc5a5506bbb17e57al), 1.2, 0.4); + return new LevyDistribution(1.2, 0.4); } @Override diff --git a/src/test/java/org/apache/commons/math4/distribution/RealDistributionAbstractTest.java b/src/test/java/org/apache/commons/math4/distribution/RealDistributionAbstractTest.java index 61d01ffeb..7d9db82c0 100644 --- a/src/test/java/org/apache/commons/math4/distribution/RealDistributionAbstractTest.java +++ b/src/test/java/org/apache/commons/math4/distribution/RealDistributionAbstractTest.java @@ -327,22 +327,6 @@ public abstract class RealDistributionAbstractTest { * Test sampling */ @Test - public void testSampling() { - final int sampleSize = 1000; - distribution.reseedRandomGenerator(1000); // Use fixed seed - double[] sample = distribution.sample(sampleSize); - double[] quartiles = TestUtils.getDistributionQuartiles(distribution); - double[] expected = {250, 250, 250, 250}; - long[] counts = new long[4]; - for (int i = 0; i < sampleSize; i++) { - TestUtils.updateCounts(sample[i], counts, quartiles); - } - TestUtils.assertChiSquareAccept(expected, counts, 0.001); - } - - - // New design - @Test public void testSampler() { final int sampleSize = 1000; final RealDistribution.Sampler sampler = @@ -403,16 +387,18 @@ public abstract class RealDistributionAbstractTest { ClassNotFoundException { // Construct a distribution and initialize its internal random // generator, using a fixed seed for deterministic results. - distribution.reseedRandomGenerator(123); - distribution.sample(); + final long seed = 123; + RandomSource source = RandomSource.WELL_512_A; + RealDistribution.Sampler origSampler = distribution.createSampler(RandomSource.create(source, seed)); // Clone the distribution. final RealDistribution cloned = deepClone(); + RealDistribution.Sampler clonedSampler = cloned.createSampler(RandomSource.create(source, seed)); // Make sure they still produce the same samples. - final double s1 = distribution.sample(); - final double s2 = cloned.sample(); - Assert.assertEquals(s1, s2, 0d); + Assert.assertEquals(origSampler.sample(), + clonedSampler.sample(), + 0d); } //------------------ Getters / Setters for test instance data ----------- diff --git a/src/test/java/org/apache/commons/math4/filter/KalmanFilterTest.java b/src/test/java/org/apache/commons/math4/filter/KalmanFilterTest.java index 99d73c541..2db564de8 100644 --- a/src/test/java/org/apache/commons/math4/filter/KalmanFilterTest.java +++ b/src/test/java/org/apache/commons/math4/filter/KalmanFilterTest.java @@ -14,6 +14,7 @@ package org.apache.commons.math4.filter; +import org.apache.commons.math4.distribution.RealDistribution; import org.apache.commons.math4.distribution.NormalDistribution; import org.apache.commons.math4.filter.DefaultMeasurementModel; import org.apache.commons.math4.filter.DefaultProcessModel; @@ -26,9 +27,10 @@ import org.apache.commons.math4.linear.MatrixDimensionMismatchException; import org.apache.commons.math4.linear.MatrixUtils; import org.apache.commons.math4.linear.RealMatrix; import org.apache.commons.math4.linear.RealVector; -import org.apache.commons.math4.random.JDKRandomGenerator; import org.apache.commons.math4.random.RandomGenerator; -import org.apache.commons.math4.random.Well19937c; +import org.apache.commons.math4.random.JDKRandomGenerator; +import org.apache.commons.math4.rng.UniformRandomProvider; +import org.apache.commons.math4.rng.RandomSource; import org.apache.commons.math4.util.FastMath; import org.apache.commons.math4.util.Precision; import org.junit.Assert; @@ -392,8 +394,8 @@ public class KalmanFilterTest { final MeasurementModel mm = new DefaultMeasurementModel(H, R); final KalmanFilter filter = new KalmanFilter(pm, mm); - final RandomGenerator rng = new Well19937c(1000); - final NormalDistribution dist = new NormalDistribution(rng, 0, measurementNoise); + final UniformRandomProvider rng = RandomSource.create(RandomSource.WELL_19937_C, 1000); + final RealDistribution.Sampler dist = new NormalDistribution(0, measurementNoise).createSampler(rng); for (int i = 0; i < iterations; i++) { // get the "real" cannonball position diff --git a/src/test/java/org/apache/commons/math4/fitting/leastsquares/LevenbergMarquardtOptimizerTest.java b/src/test/java/org/apache/commons/math4/fitting/leastsquares/LevenbergMarquardtOptimizerTest.java index 6198eb71c..5c0ebd933 100644 --- a/src/test/java/org/apache/commons/math4/fitting/leastsquares/LevenbergMarquardtOptimizerTest.java +++ b/src/test/java/org/apache/commons/math4/fitting/leastsquares/LevenbergMarquardtOptimizerTest.java @@ -262,7 +262,7 @@ public class LevenbergMarquardtOptimizerTest final double ySigma = 15; final double radius = 111.111; // The test is extremely sensitive to the seed. - final long seed = 59421061L; + final long seed = 59321761412L; final RandomCirclePointGenerator factory = new RandomCirclePointGenerator(xCenter, yCenter, radius, xSigma, ySigma, diff --git a/src/test/java/org/apache/commons/math4/fitting/leastsquares/RandomCirclePointGenerator.java b/src/test/java/org/apache/commons/math4/fitting/leastsquares/RandomCirclePointGenerator.java index 8bc856947..59dd8afe0 100644 --- a/src/test/java/org/apache/commons/math4/fitting/leastsquares/RandomCirclePointGenerator.java +++ b/src/test/java/org/apache/commons/math4/fitting/leastsquares/RandomCirclePointGenerator.java @@ -20,8 +20,8 @@ import org.apache.commons.math4.distribution.NormalDistribution; import org.apache.commons.math4.distribution.RealDistribution; import org.apache.commons.math4.distribution.UniformRealDistribution; import org.apache.commons.math4.geometry.euclidean.twod.Vector2D; -import org.apache.commons.math4.random.RandomGenerator; -import org.apache.commons.math4.random.Well44497b; +import org.apache.commons.math4.rng.UniformRandomProvider; +import org.apache.commons.math4.rng.RandomSource; import org.apache.commons.math4.util.FastMath; import org.apache.commons.math4.util.MathUtils; @@ -30,11 +30,11 @@ import org.apache.commons.math4.util.MathUtils; */ public class RandomCirclePointGenerator { /** RNG for the x-coordinate of the center. */ - private final RealDistribution cX; + private final RealDistribution.Sampler cX; /** RNG for the y-coordinate of the center. */ - private final RealDistribution cY; + private final RealDistribution.Sampler cY; /** RNG for the parametric position of the point. */ - private final RealDistribution tP; + private final RealDistribution.Sampler tP; /** Radius of the circle. */ private final double radius; @@ -52,13 +52,11 @@ public class RandomCirclePointGenerator { double xSigma, double ySigma, long seed) { - final RandomGenerator rng = new Well44497b(seed); + final UniformRandomProvider rng = RandomSource.create(RandomSource.WELL_44497_B, seed); this.radius = radius; - cX = new NormalDistribution(rng, x, xSigma, - NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY); - cY = new NormalDistribution(rng, y, ySigma, - NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY); - tP = new UniformRealDistribution(rng, 0, MathUtils.TWO_PI); + cX = new NormalDistribution(x, xSigma).createSampler(rng); + cY = new NormalDistribution(y, ySigma).createSampler(rng); + tP = new UniformRealDistribution(0, MathUtils.TWO_PI).createSampler(rng); } /** diff --git a/src/test/java/org/apache/commons/math4/fitting/leastsquares/RandomStraightLinePointGenerator.java b/src/test/java/org/apache/commons/math4/fitting/leastsquares/RandomStraightLinePointGenerator.java index 024f561a8..beff240a1 100644 --- a/src/test/java/org/apache/commons/math4/fitting/leastsquares/RandomStraightLinePointGenerator.java +++ b/src/test/java/org/apache/commons/math4/fitting/leastsquares/RandomStraightLinePointGenerator.java @@ -22,8 +22,8 @@ import java.awt.geom.Point2D; import org.apache.commons.math4.distribution.NormalDistribution; import org.apache.commons.math4.distribution.RealDistribution; import org.apache.commons.math4.distribution.UniformRealDistribution; -import org.apache.commons.math4.random.RandomGenerator; -import org.apache.commons.math4.random.Well44497b; +import org.apache.commons.math4.rng.UniformRandomProvider; +import org.apache.commons.math4.rng.RandomSource; /** * Factory for generating a cloud of points that approximate a straight line. @@ -34,9 +34,9 @@ public class RandomStraightLinePointGenerator { /** Intercept. */ private final double intercept; /** RNG for the x-coordinate. */ - private final RealDistribution x; + private final RealDistribution.Sampler x; /** RNG for the error on the y-coordinate. */ - private final RealDistribution error; + private final RealDistribution.Sampler error; /** * The generator will create a cloud of points whose x-coordinates @@ -61,12 +61,11 @@ public class RandomStraightLinePointGenerator { double lo, double hi, long seed) { - final RandomGenerator rng = new Well44497b(seed); + final UniformRandomProvider rng = RandomSource.create(RandomSource.WELL_44497_B, seed); slope = a; intercept = b; - error = new NormalDistribution(rng, 0, sigma, - NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY); - x = new UniformRealDistribution(rng, lo, hi); + error = new NormalDistribution(0, sigma).createSampler(rng); + x = new UniformRealDistribution(lo, hi).createSampler(rng); } /** diff --git a/src/test/java/org/apache/commons/math4/random/EmpiricalDistributionTest.java b/src/test/java/org/apache/commons/math4/random/EmpiricalDistributionTest.java index 4854979c0..3c0564fbe 100644 --- a/src/test/java/org/apache/commons/math4/random/EmpiricalDistributionTest.java +++ b/src/test/java/org/apache/commons/math4/random/EmpiricalDistributionTest.java @@ -36,8 +36,7 @@ import org.apache.commons.math4.distribution.UniformRealDistribution; import org.apache.commons.math4.exception.MathIllegalStateException; import org.apache.commons.math4.exception.NullArgumentException; import org.apache.commons.math4.exception.NotStrictlyPositiveException; -import org.apache.commons.math4.random.EmpiricalDistribution; -import org.apache.commons.math4.random.RandomGenerator; +import org.apache.commons.math4.rng.RandomSource; import org.apache.commons.math4.stat.descriptive.SummaryStatistics; import org.apache.commons.math4.util.FastMath; import org.junit.Assert; @@ -156,14 +155,27 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes } /** - * Make sure exception thrown if digest getNext is attempted - * before loading empiricalDistribution. + * Make sure exception thrown if sampling is attempted + * before loading empiricalDistribution. */ @Test - public void testNexFail() { + public void testNextFail1() { try { - empiricalDistribution.getNextValue(); - empiricalDistribution2.getNextValue(); + empiricalDistribution.createSampler(RandomSource.create(RandomSource.JDK)).sample(); + Assert.fail("Expecting MathIllegalStateException"); + } catch (MathIllegalStateException ex) { + // expected + } + } + + /** + * Make sure exception thrown if sampling is attempted + * before loading empiricalDistribution. + */ + @Test + public void testNextFail2() { + try { + empiricalDistribution2.createSampler(RandomSource.create(RandomSource.JDK)).sample(); Assert.fail("Expecting MathIllegalStateException"); } catch (MathIllegalStateException ex) { // expected @@ -245,36 +257,38 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes TestUtils.assertEquals(expectedGeneratorUpperBounds, dist.getGeneratorUpperBounds(), tol); } - @Test - public void testGeneratorConfig() { - double[] testData = {0, 1, 2, 3, 4}; - RandomGenerator generator = new RandomAdaptorTest.ConstantGenerator(0.5); + // XXX REMOVE (test "embedded RNG" which is to be removed) +// @Test +// public void testGeneratorConfig() { +// double[] testData = {0, 1, 2, 3, 4}; +// RandomGenerator generator = new RandomAdaptorTest.ConstantGenerator(0.5); - EmpiricalDistribution dist = new EmpiricalDistribution(5, generator); - dist.load(testData); - for (int i = 0; i < 5; i++) { - Assert.assertEquals(2.0, dist.getNextValue(), 0d); - } +// EmpiricalDistribution dist = new EmpiricalDistribution(5, generator); +// dist.load(testData); +// for (int i = 0; i < 5; i++) { +// Assert.assertEquals(2.0, dist.getNextValue(), 0d); +// } - // Verify no NPE with null generator argument - dist = new EmpiricalDistribution(5, (RandomGenerator) null); - dist.load(testData); - dist.getNextValue(); - } +// // Verify no NPE with null generator argument +// dist = new EmpiricalDistribution(5, (RandomGenerator) null); +// dist.load(testData); +// dist.getNextValue(); +// } - @Test - public void testReSeed() throws Exception { - empiricalDistribution.load(url); - empiricalDistribution.reSeed(100); - final double [] values = new double[10]; - for (int i = 0; i < 10; i++) { - values[i] = empiricalDistribution.getNextValue(); - } - empiricalDistribution.reSeed(100); - for (int i = 0; i < 10; i++) { - Assert.assertEquals(values[i],empiricalDistribution.getNextValue(), 0d); - } - } + // XXX REMOVE (test "embedded RNG" which is to be removed) +// @Test +// public void testReSeed() throws Exception { +// empiricalDistribution.load(url); +// empiricalDistribution.reSeed(100); +// final double [] values = new double[10]; +// for (int i = 0; i < 10; i++) { +// values[i] = empiricalDistribution.getNextValue(); +// } +// empiricalDistribution.reSeed(100); +// for (int i = 0; i < 10; i++) { +// Assert.assertEquals(values[i],empiricalDistribution.getNextValue(), 0d); +// } +// } private void verifySame(EmpiricalDistribution d1, EmpiricalDistribution d2) { Assert.assertEquals(d1.isLoaded(), d2.isLoaded()); @@ -290,10 +304,11 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes private void tstGen(double tolerance)throws Exception { empiricalDistribution.load(url); - empiricalDistribution.reSeed(1000); + RealDistribution.Sampler sampler + = empiricalDistribution.createSampler(RandomSource.create(RandomSource.WELL_19937_C, 1000)); SummaryStatistics stats = new SummaryStatistics(); for (int i = 1; i < 1000; i++) { - stats.addValue(empiricalDistribution.getNextValue()); + stats.addValue(sampler.sample()); } Assert.assertEquals("mean", 5.069831575018909, stats.getMean(),tolerance); Assert.assertEquals("std dev", 1.0173699343977738, stats.getStandardDeviation(),tolerance); @@ -301,10 +316,11 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes private void tstDoubleGen(double tolerance)throws Exception { empiricalDistribution2.load(dataArray); - empiricalDistribution2.reSeed(1000); + RealDistribution.Sampler sampler + = empiricalDistribution2.createSampler(RandomSource.create(RandomSource.WELL_19937_C, 1000)); SummaryStatistics stats = new SummaryStatistics(); for (int i = 1; i < 1000; i++) { - stats.addValue(empiricalDistribution2.getNextValue()); + stats.addValue(sampler.sample()); } Assert.assertEquals("mean", 5.069831575018909, stats.getMean(), tolerance); Assert.assertEquals("std dev", 1.0173699343977738, stats.getStandardDeviation(), tolerance); @@ -431,9 +447,10 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes } EmpiricalDistribution dist = new EmpiricalDistribution(10); dist.load(data); - dist.reseedRandomGenerator(1000); + RealDistribution.Sampler sampler + = dist.createSampler(RandomSource.create(RandomSource.WELL_19937_C, 1000)); for (int i = 0; i < 1000; i++) { - final double dev = dist.sample(); + final double dev = sampler.sample(); Assert.assertTrue(dev < 1); Assert.assertTrue(dev > 0); } @@ -447,9 +464,10 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes final double[] data = {0, 0, 1, 1}; EmpiricalDistribution dist = new EmpiricalDistribution(2); dist.load(data); - dist.reseedRandomGenerator(1000); + RealDistribution.Sampler sampler + = dist.createSampler(RandomSource.create(RandomSource.WELL_19937_C, 1000)); for (int i = 0; i < 1000; i++) { - final double dev = dist.sample(); + final double dev = sampler.sample(); Assert.assertTrue(dev == 0 || dev == 1); } Assert.assertEquals(0.5, dist.cumulativeProbability(0), Double.MIN_VALUE); @@ -489,10 +507,12 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes final EmpiricalDistribution dist = new ConstantKernelEmpiricalDistribution(5); final double[] data = {1d,2d,3d, 4d,5d,6d, 7d,8d,9d, 10d,11d,12d, 13d,14d,15d}; dist.load(data); + RealDistribution.Sampler sampler + = dist.createSampler(RandomSource.create(RandomSource.WELL_19937_C, 1000)); // Bin masses concentrated on 2, 5, 8, 11, 14 <- effectively discrete uniform distribution over these double[] values = {2d, 5d, 8d, 11d, 14d}; for (int i = 0; i < 20; i++) { - Assert.assertTrue(Arrays.binarySearch(values, dist.sample()) >= 0); + Assert.assertTrue(Arrays.binarySearch(values, sampler.sample()) >= 0); } final double tol = 10E-12; Assert.assertEquals(0.0, dist.cumulativeProbability(1), tol); @@ -515,11 +535,13 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes final EmpiricalDistribution dist = new UniformKernelEmpiricalDistribution(5); final double[] data = {1d,2d,3d, 4d,5d,6d, 7d,8d,9d, 10d,11d,12d, 13d,14d,15d}; dist.load(data); + RealDistribution.Sampler sampler + = dist.createSampler(RandomSource.create(RandomSource.WELL_19937_C, 1000)); // Kernels are uniform distributions on [1,3], [4,6], [7,9], [10,12], [13,15] final double bounds[] = {3d, 6d, 9d, 12d}; final double tol = 10E-12; for (int i = 0; i < 20; i++) { - final double v = dist.sample(); + final double v = sampler.sample(); // Make sure v is not in the excluded range between bins - that is (bounds[i], bounds[i] + 1) for (int j = 0; j < bounds.length; j++) { Assert.assertFalse(v > bounds[j] + tol && v < bounds[j] + 1 - tol); @@ -566,7 +588,7 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes } @Override protected RealDistribution getKernel(SummaryStatistics bStats) { - return new UniformRealDistribution(randomData.getRandomGenerator(), bStats.getMin(), bStats.getMax()); + return new UniformRealDistribution(bStats.getMin(), bStats.getMax()); } } } diff --git a/src/test/java/org/apache/commons/math4/stat/descriptive/rank/PSquarePercentileTest.java b/src/test/java/org/apache/commons/math4/stat/descriptive/rank/PSquarePercentileTest.java index dfdcd4ece..fdbf689f0 100644 --- a/src/test/java/org/apache/commons/math4/stat/descriptive/rank/PSquarePercentileTest.java +++ b/src/test/java/org/apache/commons/math4/stat/descriptive/rank/PSquarePercentileTest.java @@ -32,6 +32,7 @@ import org.apache.commons.math4.exception.NullArgumentException; import org.apache.commons.math4.exception.OutOfRangeException; import org.apache.commons.math4.random.RandomGenerator; import org.apache.commons.math4.random.Well19937c; +import org.apache.commons.math4.rng.RandomSource; import org.apache.commons.math4.stat.descriptive.StorelessUnivariateStatistic; import org.apache.commons.math4.stat.descriptive.StorelessUnivariateStatisticAbstractTest; import org.apache.commons.math4.stat.descriptive.UnivariateStatistic; @@ -709,6 +710,8 @@ public class PSquarePercentileTest extends VERY_LARGE = 10000000; private void doDistributionTest(RealDistribution distribution) { + final RealDistribution.Sampler sampler = + distribution.createSampler(RandomSource.create(RandomSource.WELL_19937_C, 1000)); double data[]; data = distribution.sample(VERY_LARGE); @@ -754,8 +757,8 @@ public class PSquarePercentileTest extends */ @Test public void testDistribution() { - doDistributionTest(new NormalDistribution(randomGenerator, 4000, 50)); - doDistributionTest(new LogNormalDistribution(randomGenerator,4000, 50)); + doDistributionTest(new NormalDistribution(4000, 50)); + doDistributionTest(new LogNormalDistribution(4000, 50)); // doDistributionTest((new ExponentialDistribution(4000)); // doDistributionTest(new GammaDistribution(5d,1d),0.1); } diff --git a/src/test/java/org/apache/commons/math4/stat/descriptive/rank/PercentileTest.java b/src/test/java/org/apache/commons/math4/stat/descriptive/rank/PercentileTest.java index 65a25b18b..98b6f913d 100644 --- a/src/test/java/org/apache/commons/math4/stat/descriptive/rank/PercentileTest.java +++ b/src/test/java/org/apache/commons/math4/stat/descriptive/rank/PercentileTest.java @@ -18,14 +18,16 @@ package org.apache.commons.math4.stat.descriptive.rank; import java.util.Arrays; +import org.apache.commons.math4.distribution.RealDistribution; +import org.apache.commons.math4.distribution.AbstractRealDistribution; import org.apache.commons.math4.distribution.NormalDistribution; import org.apache.commons.math4.exception.MathIllegalArgumentException; import org.apache.commons.math4.exception.NotANumberException; import org.apache.commons.math4.exception.NullArgumentException; import org.apache.commons.math4.exception.OutOfRangeException; -import org.apache.commons.math4.random.JDKRandomGenerator; -import org.apache.commons.math4.random.RandomGenerator; import org.apache.commons.math4.random.Well1024a; +import org.apache.commons.math4.rng.RandomSource; +import org.apache.commons.math4.rng.UniformRandomProvider; import org.apache.commons.math4.stat.descriptive.UnivariateStatistic; import org.apache.commons.math4.stat.descriptive.UnivariateStatisticAbstractTest; import org.apache.commons.math4.stat.descriptive.rank.Percentile.EstimationType; @@ -587,22 +589,23 @@ public class PercentileTest extends UnivariateStatisticAbstractTest{ @Test public void testStoredVsDirect() { - final RandomGenerator rand= new JDKRandomGenerator(); - rand.setSeed(Long.MAX_VALUE); - for (final int sampleSize:sampleSizes) { - final double[] data = new NormalDistribution(rand,4000, 50) - .sample(sampleSize); - for (final double p:new double[] {50d,95d}) { + final RealDistribution.Sampler sampler = + new NormalDistribution(4000, 50).createSampler(RandomSource.create(RandomSource.JDK, + Long.MAX_VALUE)); + + for (final int sampleSize : sampleSizes) { + final double[] data = AbstractRealDistribution.sample(sampleSize, sampler); + for (final double p : new double[] { 50d, 95d }) { for (final Percentile.EstimationType e : Percentile.EstimationType.values()) { reset(p, e); final Percentile pStoredData = getUnivariateStatistic(); pStoredData.setData(data); - final double storedDataResult=pStoredData.evaluate(); + final double storedDataResult = pStoredData.evaluate(); pStoredData.setData(null); final Percentile pDirect = getUnivariateStatistic(); - Assert.assertEquals("Sample="+sampleSize+",P="+p+" e="+e, - storedDataResult, - pDirect.evaluate(data),0d); + Assert.assertEquals("Sample=" + sampleSize + ", P=" + p + " e=" + e, + storedDataResult, + pDirect.evaluate(data), 0d); } } } diff --git a/src/test/java/org/apache/commons/math4/stat/inference/KolmogorovSmirnovTestTest.java b/src/test/java/org/apache/commons/math4/stat/inference/KolmogorovSmirnovTestTest.java index 5871fd30f..0c9c0dd5d 100644 --- a/src/test/java/org/apache/commons/math4/stat/inference/KolmogorovSmirnovTestTest.java +++ b/src/test/java/org/apache/commons/math4/stat/inference/KolmogorovSmirnovTestTest.java @@ -23,8 +23,8 @@ import java.util.Arrays; import org.apache.commons.math4.TestUtils; import org.apache.commons.math4.distribution.NormalDistribution; import org.apache.commons.math4.distribution.UniformRealDistribution; -import org.apache.commons.math4.random.RandomGenerator; -import org.apache.commons.math4.random.Well19937c; +import org.apache.commons.math4.rng.RandomSource; +import org.apache.commons.math4.rng.UniformRandomProvider; import org.apache.commons.math4.util.CombinatoricsUtils; import org.apache.commons.math4.util.FastMath; import org.apache.commons.math4.util.MathArrays; @@ -318,7 +318,7 @@ public class KolmogorovSmirnovTestTest { */ @Test public void testTwoSampleMonteCarlo() { - final KolmogorovSmirnovTest test = new KolmogorovSmirnovTest(new Well19937c(1000)); + final KolmogorovSmirnovTest test = new KolmogorovSmirnovTest(RandomSource.WELL_19937_C, 1000); final int sampleSize = 14; final double tol = .001; final double[] shortUniform = new double[sampleSize]; @@ -345,7 +345,7 @@ public class KolmogorovSmirnovTestTest { @Test public void testTwoSampleMonteCarloDifferentSampleSizes() { - final KolmogorovSmirnovTest test = new KolmogorovSmirnovTest(new Well19937c(1000)); + final KolmogorovSmirnovTest test = new KolmogorovSmirnovTest(RandomSource.WELL_19937_C, 1000); final int sampleSize1 = 14; final int sampleSize2 = 7; final double d = 0.3; @@ -364,7 +364,7 @@ public class KolmogorovSmirnovTestTest { public void testTwoSampleMonteCarloPerformance() { int numIterations = 100_000; int N = (int)Math.sqrt(KolmogorovSmirnovTest.LARGE_SAMPLE_PRODUCT); - final KolmogorovSmirnovTest test = new KolmogorovSmirnovTest(new Well19937c(1000)); + final KolmogorovSmirnovTest test = new KolmogorovSmirnovTest(RandomSource.WELL_19937_C, 1000); for (int n = 2; n <= N; ++n) { long startMillis = System.currentTimeMillis(); int m = KolmogorovSmirnovTest.LARGE_SAMPLE_PRODUCT/n; @@ -500,7 +500,7 @@ public class KolmogorovSmirnovTestTest { public void testDRoundingMonteCarlo() { final double tol = 1e-2; final int iterations = 1000000; - final KolmogorovSmirnovTest test = new KolmogorovSmirnovTest(new Well19937c(1000)); + final KolmogorovSmirnovTest test = new KolmogorovSmirnovTest(RandomSource.WELL_19937_C, 1000); final double[] x = {0, 2, 3, 4, 5, 6, 7, 8, 9, 12}; final double[] y = {1, 10, 11, 13, 14, 15, 16, 17, 18}; @@ -519,14 +519,17 @@ public class KolmogorovSmirnovTestTest { } @Test - public void testFillBooleanArrayRandomlyWithFixedNumberTrueValues() { + public void testFillBooleanArrayRandomlyWithFixedNumberTrueValues() throws Exception { + Method method = KolmogorovSmirnovTest.class.getDeclaredMethod("fillBooleanArrayRandomlyWithFixedNumberTrueValues", + boolean[].class, Integer.TYPE, UniformRandomProvider.class); + method.setAccessible(true); final int[][] parameters = {{5, 1}, {5, 2}, {5, 3}, {5, 4}, {8, 1}, {8, 2}, {8, 3}, {8, 4}, {8, 5}, {8, 6}, {8, 7}}; final double alpha = 0.001; final int numIterations = 1000000; - final RandomGenerator rng = new Well19937c(0); + final UniformRandomProvider rng = RandomSource.create(RandomSource.WELL_19937_C, 0); for (final int[] parameter : parameters) { @@ -537,7 +540,7 @@ public class KolmogorovSmirnovTestTest { final long[] counts = new long[1 << arraySize]; for (int i = 0; i < numIterations; ++i) { - KolmogorovSmirnovTest.fillBooleanArrayRandomlyWithFixedNumberTrueValues(b, numberOfTrueValues, rng); + method.invoke(KolmogorovSmirnovTest.class, b, numberOfTrueValues, rng); int x = 0; for (int j = 0; j < arraySize; ++j) { x = ((x << 1) | ((b[j])?1:0)); @@ -576,7 +579,7 @@ public class KolmogorovSmirnovTestTest { public void testBootstrapSmallSamplesWithTies() { final double[] x = {0, 2, 4, 6, 8, 8, 10, 15, 22, 30, 33, 36, 38}; final double[] y = {9, 17, 20, 33, 40, 51, 60, 60, 72, 90, 101}; - final KolmogorovSmirnovTest test = new KolmogorovSmirnovTest(new Well19937c(2000)); + final KolmogorovSmirnovTest test = new KolmogorovSmirnovTest(RandomSource.WELL_19937_C, 2000); Assert.assertEquals(0.0059, test.bootstrap(x, y, 10000, false), 1E-3); } @@ -586,7 +589,7 @@ public class KolmogorovSmirnovTestTest { */ @Test public void testBootstrapLargeSamples() { - final KolmogorovSmirnovTest test = new KolmogorovSmirnovTest(new Well19937c(1000)); + final KolmogorovSmirnovTest test = new KolmogorovSmirnovTest(RandomSource.WELL_19937_C, 1000); Assert.assertEquals(0.0237, test.bootstrap(gaussian, gaussian2, 10000), 1E-2); } @@ -599,7 +602,7 @@ public class KolmogorovSmirnovTestTest { public void testBootstrapRounding() { final double[] x = {2,4,6,8,9,10,11,12,13}; final double[] y = {0,1,3,5,7}; - final KolmogorovSmirnovTest test = new KolmogorovSmirnovTest(new Well19937c(1000)); + final KolmogorovSmirnovTest test = new KolmogorovSmirnovTest(RandomSource.WELL_19937_C, 1000); Assert.assertEquals(0.06303, test.bootstrap(x, y, 10000, false), 1E-2); } @@ -629,7 +632,7 @@ public class KolmogorovSmirnovTestTest { final double[] fixedY = MathArrays.copyOf(y); checkFixTies(xP, yP); Assert.assertArrayEquals(fixedX, xP, 0); - Assert.assertArrayEquals(fixedY, yP, 0); + Assert.assertArrayEquals(fixedY, yP, 0); } @Test @@ -699,7 +702,7 @@ public class KolmogorovSmirnovTestTest { */ private static void fixTies(double[] x, double[] y) throws Exception { Method method = KolmogorovSmirnovTest.class.getDeclaredMethod("fixTies", - double[].class, double[].class); + double[].class, double[].class); method.setAccessible(true); method.invoke(KolmogorovSmirnovTest.class, x, y); } @@ -709,7 +712,7 @@ public class KolmogorovSmirnovTestTest { */ private static boolean hasTies(double[] x, double[] y) throws Exception { Method method = KolmogorovSmirnovTest.class.getDeclaredMethod("hasTies", - double[].class, double[].class); + double[].class, double[].class); method.setAccessible(true); return (boolean) method.invoke(KolmogorovSmirnovTest.class, x, y); }