You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2012/07/20 02:43:46 UTC
svn commit: r1363604 - in /commons/proper/math/trunk/src: changes/
main/java/org/apache/commons/math3/distribution/
test/java/org/apache/commons/math3/distribution/
Author: erans
Date: Fri Jul 20 00:43:45 2012
New Revision: 1363604
URL: http://svn.apache.org/viewvc?rev=1363604&view=rev
Log:
MATH-764
MATH-823
Allow explicit setting of RNG (parameter of the constructor).
Removed dependency on "RandomDataImpl" for the "sample" method.
Modified:
commons/proper/math/trunk/src/changes/changes.xml
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/AbstractIntegerDistribution.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/AbstractRealDistribution.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/BetaDistribution.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/BinomialDistribution.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/CauchyDistribution.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/ChiSquaredDistribution.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/ExponentialDistribution.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/FDistribution.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/GammaDistribution.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/HypergeometricDistribution.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/LogNormalDistribution.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/NormalDistribution.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/PascalDistribution.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/PoissonDistribution.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/TDistribution.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/TriangularDistribution.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/UniformIntegerDistribution.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/UniformRealDistribution.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/WeibullDistribution.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/ZipfDistribution.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/AbstractIntegerDistributionTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/AbstractRealDistributionTest.java
Modified: commons/proper/math/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/changes/changes.xml?rev=1363604&r1=1363603&r2=1363604&view=diff
==============================================================================
--- commons/proper/math/trunk/src/changes/changes.xml (original)
+++ commons/proper/math/trunk/src/changes/changes.xml Fri Jul 20 00:43:45 2012
@@ -52,6 +52,13 @@ If the output is not quite correct, chec
<body>
<release version="3.1" date="TBD" description="
">
+ <action dev="erans" type="add" issue="MATH-764,MATH-823">
+ For all distibution classes (in package "o.a.c.m.distribution"), a new
+ constructor takes a "RandomGenerator" parameter.
+ The "RandomDataImpl" instance has been superseded by this RNG.
+ All "sample()" methods have been modified to use this RNG instead of
+ delegating to the methods in "RandomData".
+ </action>
<action dev="tn" type="add" issue="MATH-235">
Added support for real asymmetric matrices to EigenDecomposition.
</action>
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/AbstractIntegerDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/AbstractIntegerDistribution.java?rev=1363604&r1=1363603&r2=1363604&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/AbstractIntegerDistribution.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/AbstractIntegerDistribution.java Fri Jul 20 00:43:45 2012
@@ -23,6 +23,7 @@ import org.apache.commons.math3.exceptio
import org.apache.commons.math3.exception.NumberIsTooLargeException;
import org.apache.commons.math3.exception.OutOfRangeException;
import org.apache.commons.math3.exception.util.LocalizedFormats;
+import org.apache.commons.math3.random.RandomGenerator;
import org.apache.commons.math3.random.RandomDataImpl;
import org.apache.commons.math3.util.FastMath;
@@ -37,14 +38,31 @@ public abstract class AbstractIntegerDis
implements IntegerDistribution, Serializable {
/** Serializable version identifier */
private static final long serialVersionUID = -1146319659338487221L;
+ /**
+ * RandomData instance used to generate samples from the distribution.
+ * @deprecated As of 3.1, to be removed in 4.0. Please use the
+ * {@link #random} instance variable instead.
+ */
+ protected final RandomDataImpl randomData = new RandomDataImpl();
+ /** RNG instance used to generate samples from the distribution. */
+ protected final RandomGenerator random;
/**
- * RandomData instance used to generate samples from the distribution.
+ * @deprecated As of 3.1, to be removed in 4.0. Please use
+ * {@link #AbstractIntegerDistribution(RandomGenerator)} instead.
*/
- protected final RandomDataImpl randomData = new RandomDataImpl();
-
- /** Default constructor. */
- protected AbstractIntegerDistribution() { }
+ @Deprecated
+ protected AbstractIntegerDistribution() {
+ // Legacy users are only allowed to access the deprecated "randomData".
+ // New users are forbidden to use this constructor.
+ random = null;
+ }
+ /**
+ * @param rng Random number generator.
+ */
+ protected AbstractIntegerDistribution(RandomGenerator rng) {
+ random = rng;
+ }
/**
* {@inheritDoc}
@@ -152,7 +170,7 @@ implements IntegerDistribution, Serializ
/** {@inheritDoc} */
public void reseedRandomGenerator(long seed) {
- randomData.reSeed(seed);
+ random.setSeed(seed);
}
/**
@@ -163,7 +181,7 @@ implements IntegerDistribution, Serializ
* inversion method</a>.
*/
public int sample() {
- return randomData.nextInversionDeviate(this);
+ return inverseCumulativeProbability(random.nextDouble());
}
/**
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/AbstractRealDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/AbstractRealDistribution.java?rev=1363604&r1=1363603&r2=1363604&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/AbstractRealDistribution.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/AbstractRealDistribution.java Fri Jul 20 00:43:45 2012
@@ -24,6 +24,7 @@ import org.apache.commons.math3.exceptio
import org.apache.commons.math3.exception.NumberIsTooLargeException;
import org.apache.commons.math3.exception.OutOfRangeException;
import org.apache.commons.math3.exception.util.LocalizedFormats;
+import org.apache.commons.math3.random.RandomGenerator;
import org.apache.commons.math3.random.RandomDataImpl;
import org.apache.commons.math3.util.FastMath;
@@ -39,18 +40,35 @@ public abstract class AbstractRealDistri
implements RealDistribution, Serializable {
/** Default accuracy. */
public static final double SOLVER_DEFAULT_ABSOLUTE_ACCURACY = 1e-6;
-
/** Serializable version identifier */
private static final long serialVersionUID = -38038050983108802L;
-
- /** RandomData instance used to generate samples from the distribution. */
+ /**
+ * RandomData instance used to generate samples from the distribution.
+ * @deprecated As of 3.1, to be removed in 4.0. Please use the
+ * {@link #random} instance variable instead.
+ */
protected final RandomDataImpl randomData = new RandomDataImpl();
-
+ /** RNG instance used to generate samples from the distribution. */
+ protected final RandomGenerator random;
/** Solver absolute accuracy for inverse cumulative computation */
private double solverAbsoluteAccuracy = SOLVER_DEFAULT_ABSOLUTE_ACCURACY;
- /** Default constructor. */
- protected AbstractRealDistribution() { }
+ /**
+ * @deprecated As of 3.1, to be removed in 4.0. Please use
+ * {@link #AbstractRealDistribution(RandomGenerator)} instead.
+ */
+ @Deprecated
+ protected AbstractRealDistribution() {
+ // Legacy users are only allowed to access the deprecated "randomData".
+ // New users are forbidden to use this constructor.
+ random = null;
+ }
+ /**
+ * @param rng Random number generator.
+ */
+ protected AbstractRealDistribution(RandomGenerator rng) {
+ random = rng;
+ }
/**
* {@inheritDoc}
@@ -193,7 +211,7 @@ implements RealDistribution, Serializabl
/** {@inheritDoc} */
public void reseedRandomGenerator(long seed) {
- randomData.reSeed(seed);
+ random.setSeed(seed);
}
/**
@@ -205,7 +223,7 @@ implements RealDistribution, Serializabl
* </a>
*/
public double sample() {
- return randomData.nextInversionDeviate(this);
+ return inverseCumulativeProbability(random.nextDouble());
}
/**
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/BetaDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/BetaDistribution.java?rev=1363604&r1=1363603&r2=1363604&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/BetaDistribution.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/BetaDistribution.java Fri Jul 20 00:43:45 2012
@@ -21,6 +21,8 @@ import org.apache.commons.math3.exceptio
import org.apache.commons.math3.special.Gamma;
import org.apache.commons.math3.special.Beta;
import org.apache.commons.math3.util.FastMath;
+import org.apache.commons.math3.random.RandomGenerator;
+import org.apache.commons.math3.random.Well19937c;
/**
* Implements the Beta distribution.
@@ -53,26 +55,46 @@ public class BetaDistribution extends Ab
*
* @param alpha First shape parameter (must be positive).
* @param beta Second shape parameter (must be positive).
+ */
+ public BetaDistribution(double alpha, double beta) {
+ this(alpha, beta, DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
+ }
+
+ /**
+ * Build 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.alpha = alpha;
- this.beta = beta;
- z = Double.NaN;
- solverAbsoluteAccuracy = inverseCumAccuracy;
+ this(new Well19937c(), alpha, beta, inverseCumAccuracy);
}
/**
- * Build a new instance.
+ * 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
*/
- public BetaDistribution(double alpha, double beta) {
- this(alpha, beta, DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
+ public BetaDistribution(RandomGenerator rng,
+ double alpha,
+ double beta,
+ double inverseCumAccuracy) {
+ super(rng);
+
+ this.alpha = alpha;
+ this.beta = beta;
+ z = Double.NaN;
+ solverAbsoluteAccuracy = inverseCumAccuracy;
}
/**
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/BinomialDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/BinomialDistribution.java?rev=1363604&r1=1363603&r2=1363604&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/BinomialDistribution.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/BinomialDistribution.java Fri Jul 20 00:43:45 2012
@@ -21,6 +21,8 @@ import org.apache.commons.math3.exceptio
import org.apache.commons.math3.exception.util.LocalizedFormats;
import org.apache.commons.math3.special.Beta;
import org.apache.commons.math3.util.FastMath;
+import org.apache.commons.math3.random.RandomGenerator;
+import org.apache.commons.math3.random.Well19937c;
/**
* Implementation of the binomial distribution.
@@ -47,6 +49,24 @@ public class BinomialDistribution extend
* @throws OutOfRangeException if {@code p < 0} or {@code p > 1}.
*/
public BinomialDistribution(int trials, double p) {
+ this(new Well19937c(), trials, p);
+ }
+
+ /**
+ * Creates a binomial distribution.
+ *
+ * @param rng Random number generator.
+ * @param trials Number of trials.
+ * @param p Probability of success.
+ * @throws NotPositiveException if {@code trials < 0}.
+ * @throws OutOfRangeException if {@code p < 0} or {@code p > 1}.
+ * @since 3.1
+ */
+ public BinomialDistribution(RandomGenerator rng,
+ int trials,
+ double p) {
+ super(rng);
+
if (trials < 0) {
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_TRIALS,
trials);
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/CauchyDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/CauchyDistribution.java?rev=1363604&r1=1363603&r2=1363604&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/CauchyDistribution.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/CauchyDistribution.java Fri Jul 20 00:43:45 2012
@@ -20,6 +20,8 @@ import org.apache.commons.math3.exceptio
import org.apache.commons.math3.exception.OutOfRangeException;
import org.apache.commons.math3.exception.util.LocalizedFormats;
import org.apache.commons.math3.util.FastMath;
+import org.apache.commons.math3.random.RandomGenerator;
+import org.apache.commons.math3.random.Well19937c;
/**
* Implementation of the Cauchy distribution.
@@ -74,7 +76,27 @@ public class CauchyDistribution extends
* @since 2.1
*/
public CauchyDistribution(double median, double scale,
- double inverseCumAccuracy) {
+ 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.
+ * @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,
+ double scale,
+ double inverseCumAccuracy) {
+ super(rng);
if (scale <= 0) {
throw new NotStrictlyPositiveException(LocalizedFormats.SCALE, scale);
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/ChiSquaredDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/ChiSquaredDistribution.java?rev=1363604&r1=1363603&r2=1363604&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/ChiSquaredDistribution.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/ChiSquaredDistribution.java Fri Jul 20 00:43:45 2012
@@ -16,6 +16,9 @@
*/
package org.apache.commons.math3.distribution;
+import org.apache.commons.math3.random.RandomGenerator;
+import org.apache.commons.math3.random.Well19937c;
+
/**
* Implementation of the chi-squared distribution.
*
@@ -56,7 +59,26 @@ public class ChiSquaredDistribution exte
* @since 2.1
*/
public ChiSquaredDistribution(double degreesOfFreedom,
- double inverseCumAccuracy) {
+ double inverseCumAccuracy) {
+ this(new Well19937c(), degreesOfFreedom, inverseCumAccuracy);
+ }
+
+ /**
+ * 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;
}
@@ -75,10 +97,10 @@ public class ChiSquaredDistribution exte
*
* For this distribution {@code P(X = x)} always evaluates to 0.
*
- * @return 0
+ * @return zero.
*/
public double probability(double x) {
- return 0.0;
+ return 0;
}
/** {@inheritDoc} */
@@ -109,9 +131,7 @@ public class ChiSquaredDistribution exte
/**
* {@inheritDoc}
*
- * For {@code k} degrees of freedom, the variance is {@code 2 * k}.
- *
- * @return {@inheritDoc}
+ * @return {@code 2 * k}, where {@code k} is the number of degrees of freedom.
*/
public double getNumericalVariance() {
return 2 * getDegreesOfFreedom();
@@ -123,7 +143,7 @@ public class ChiSquaredDistribution exte
* The lower bound of the support is always 0 no matter the
* degrees of freedom.
*
- * @return lower bound of the support (always 0)
+ * @return zero.
*/
public double getSupportLowerBound() {
return 0;
@@ -135,7 +155,7 @@ public class ChiSquaredDistribution exte
* The upper bound of the support is always positive infinity no matter the
* degrees of freedom.
*
- * @return upper bound of the support (always Double.POSITIVE_INFINITY)
+ * @return {@code Double.POSITIVE_INFINITY}.
*/
public double getSupportUpperBound() {
return Double.POSITIVE_INFINITY;
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/ExponentialDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/ExponentialDistribution.java?rev=1363604&r1=1363603&r2=1363604&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/ExponentialDistribution.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/ExponentialDistribution.java Fri Jul 20 00:43:45 2012
@@ -20,6 +20,10 @@ import org.apache.commons.math3.exceptio
import org.apache.commons.math3.exception.OutOfRangeException;
import org.apache.commons.math3.exception.util.LocalizedFormats;
import org.apache.commons.math3.util.FastMath;
+import org.apache.commons.math3.util.ArithmeticUtils;
+import org.apache.commons.math3.util.ResizableDoubleArray;
+import org.apache.commons.math3.random.RandomGenerator;
+import org.apache.commons.math3.random.Well19937c;
/**
* Implementation of the exponential distribution.
@@ -36,13 +40,56 @@ public class ExponentialDistribution ext
public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9;
/** Serializable version identifier */
private static final long serialVersionUID = 2401296428283614780L;
+ /**
+ * Used when generating Exponential samples.
+ * Table containing the constants
+ * q_i = sum_{j=1}^i (ln 2)^j/j! = ln 2 + (ln 2)^2/2 + ... + (ln 2)^i/i!
+ * until the largest representable fraction below 1 is exceeded.
+ *
+ * Note that
+ * 1 = 2 - 1 = exp(ln 2) - 1 = sum_{n=1}^infty (ln 2)^n / n!
+ * thus q_i -> 1 as i -> +inf,
+ * so the higher i, the closer to one we get (the series is not alternating).
+ *
+ * By trying, n = 16 in Java is enough to reach 1.0.
+ */
+ private static final double[] EXPONENTIAL_SA_QI;
/** The mean of this distribution. */
private final double mean;
/** Inverse cumulative probability accuracy. */
private final double solverAbsoluteAccuracy;
/**
- * Create a exponential distribution with the given mean.
+ * Initialize tables.
+ */
+ static {
+ /**
+ * Filling EXPONENTIAL_SA_QI table.
+ * Note that we don't want qi = 0 in the table.
+ */
+ final double LN2 = FastMath.log(2);
+ double qi = 0;
+ int i = 1;
+
+ /**
+ * ArithmeticUtils provides factorials up to 20, so let's use that
+ * limit together with Precision.EPSILON to generate the following
+ * code (a priori, we know that there will be 16 elements, but it is
+ * better to not hardcode it).
+ */
+ final ResizableDoubleArray ra = new ResizableDoubleArray(20);
+
+ while (qi < 1) {
+ qi += FastMath.pow(LN2, i) / ArithmeticUtils.factorial(i);
+ ra.addElement(qi);
+ ++i;
+ }
+
+ EXPONENTIAL_SA_QI = ra.getElements();
+ }
+
+ /**
+ * Create an exponential distribution with the given mean.
* @param mean mean of this distribution.
*/
public ExponentialDistribution(double mean) {
@@ -50,7 +97,7 @@ public class ExponentialDistribution ext
}
/**
- * Create a exponential distribution with the given mean.
+ * Create an exponential distribution with the given mean.
*
* @param mean Mean of this distribution.
* @param inverseCumAccuracy Maximum absolute error in inverse
@@ -59,8 +106,27 @@ public class ExponentialDistribution ext
* @throws NotStrictlyPositiveException if {@code mean <= 0}.
* @since 2.1
*/
- public ExponentialDistribution(double mean, double inverseCumAccuracy)
+ 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.
+ * @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
+ */
+ public ExponentialDistribution(RandomGenerator rng,
+ double mean,
+ double inverseCumAccuracy)
throws NotStrictlyPositiveException {
+ super(rng);
+
if (mean <= 0) {
throw new NotStrictlyPositiveException(LocalizedFormats.MEAN, mean);
}
@@ -150,7 +216,42 @@ public class ExponentialDistribution ext
*/
@Override
public double sample() {
- return randomData.nextExponential(mean);
+ // Step 1:
+ double a = 0;
+ double u = random.nextDouble();
+
+ // Step 2 and 3:
+ while (u < 0.5) {
+ a += EXPONENTIAL_SA_QI[0];
+ u *= 2;
+ }
+
+ // Step 4 (now u >= 0.5):
+ u += u - 1;
+
+ // Step 5:
+ if (u <= EXPONENTIAL_SA_QI[0]) {
+ return mean * (a + u);
+ }
+
+ // Step 6:
+ int i = 0; // Should be 1, be we iterate before it in while using 0
+ double u2 = random.nextDouble();
+ double umin = u2;
+
+ // Step 7 and 8:
+ do {
+ ++i;
+ u2 = random.nextDouble();
+
+ if (u2 < umin) {
+ umin = u2;
+ }
+
+ // Step 8:
+ } while (u > EXPONENTIAL_SA_QI[i]); // Ensured to exit since EXPONENTIAL_SA_QI[MAX] = 1
+
+ return mean * (a + umin * EXPONENTIAL_SA_QI[0]);
}
/** {@inheritDoc} */
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/FDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/FDistribution.java?rev=1363604&r1=1363603&r2=1363604&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/FDistribution.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/FDistribution.java Fri Jul 20 00:43:45 2012
@@ -21,6 +21,8 @@ import org.apache.commons.math3.exceptio
import org.apache.commons.math3.exception.util.LocalizedFormats;
import org.apache.commons.math3.special.Beta;
import org.apache.commons.math3.util.FastMath;
+import org.apache.commons.math3.random.RandomGenerator;
+import org.apache.commons.math3.random.Well19937c;
/**
* Implementation of the F-distribution.
@@ -35,27 +37,22 @@ public class FDistribution extends Abstr
* @since 2.1
*/
public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9;
-
/** Serializable version identifier. */
private static final long serialVersionUID = -8516354193418641566L;
-
/** The numerator degrees of freedom. */
private final double numeratorDegreesOfFreedom;
-
/** The numerator degrees of freedom. */
private final double denominatorDegreesOfFreedom;
-
/** Inverse cumulative probability accuracy. */
private final double solverAbsoluteAccuracy;
-
/** Cached numerical variance */
private double numericalVariance = Double.NaN;
-
/** Whether or not the numerical variance has been calculated */
private boolean numericalVarianceIsCalculated = false;
/**
- * Create a F distribution using the given degrees of freedom.
+ * Creates an F distribution using the given degrees of freedom.
+ *
* @param numeratorDegreesOfFreedom Numerator degrees of freedom.
* @param denominatorDegreesOfFreedom Denominator degrees of freedom.
* @throws NotStrictlyPositiveException if
@@ -63,29 +60,53 @@ public class FDistribution extends Abstr
* {@code denominatorDegreesOfFreedom <= 0}.
*/
public FDistribution(double numeratorDegreesOfFreedom,
- double denominatorDegreesOfFreedom)
+ double denominatorDegreesOfFreedom)
throws NotStrictlyPositiveException {
this(numeratorDegreesOfFreedom, denominatorDegreesOfFreedom,
DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
}
/**
- * Create an F distribution using the given degrees of freedom
+ * Creates an F distribution using the given degrees of freedom
* and inverse cumulative probability accuracy.
+ *
* @param numeratorDegreesOfFreedom Numerator degrees of freedom.
* @param denominatorDegreesOfFreedom Denominator 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 numeratorDegreesOfFreedom <= 0} or
* {@code denominatorDegreesOfFreedom <= 0}.
* @since 2.1
*/
public FDistribution(double numeratorDegreesOfFreedom,
- double denominatorDegreesOfFreedom,
- double inverseCumAccuracy)
+ 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.
+ * @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);
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/GammaDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/GammaDistribution.java?rev=1363604&r1=1363603&r2=1363604&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/GammaDistribution.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/GammaDistribution.java Fri Jul 20 00:43:45 2012
@@ -20,6 +20,8 @@ import org.apache.commons.math3.exceptio
import org.apache.commons.math3.exception.util.LocalizedFormats;
import org.apache.commons.math3.special.Gamma;
import org.apache.commons.math3.util.FastMath;
+import org.apache.commons.math3.random.RandomGenerator;
+import org.apache.commons.math3.random.Well19937c;
/**
* Implementation of the Gamma distribution.
@@ -34,22 +36,17 @@ public class GammaDistribution extends A
* @since 2.1
*/
public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9;
-
/** Serializable version identifier. */
private static final long serialVersionUID = 20120524L;
-
/** The shape parameter. */
private final double shape;
-
/** The scale parameter. */
private final double scale;
-
/**
* The constant value of {@code shape + g + 0.5}, where {@code g} is the
* Lanczos constant {@link Gamma#LANCZOS_G}.
*/
private final double shiftedShape;
-
/**
* The constant value of
* {@code shape / scale * sqrt(e / (2 * pi * (shape + g + 0.5))) / L(shape)},
@@ -59,7 +56,6 @@ public class GammaDistribution extends A
* calculation.
*/
private final double densityPrefactor1;
-
/**
* The constant value of
* {@code shape * sqrt(e / (2 * pi * (shape + g + 0.5))) / L(shape)},
@@ -69,21 +65,18 @@ public class GammaDistribution extends A
* calculation.
*/
private final double densityPrefactor2;
-
/**
* Lower bound on {@code y = x / scale} for the selection of the computation
* method in {@link #density(double)}. For {@code y <= minY}, the natural
* calculation overflows.
*/
private final double minY;
-
/**
* Upper bound on {@code log(y)} ({@code y = x / scale}) for the selection
* of the computation method in {@link #density(double)}. For
* {@code log(y) >= maxLogY}, the natural calculation overflows.
*/
private final double maxLogY;
-
/** Inverse cumulative probability accuracy. */
private final double solverAbsoluteAccuracy;
@@ -113,6 +106,29 @@ public class GammaDistribution extends A
*/
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
+ * @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
+ */
+ public GammaDistribution(RandomGenerator rng,
+ double shape,
+ double scale,
+ double inverseCumAccuracy)
+ throws NotStrictlyPositiveException {
+ super(rng);
+
if (shape <= 0) {
throw new NotStrictlyPositiveException(LocalizedFormats.SHAPE, shape);
}
@@ -360,6 +376,67 @@ public class GammaDistribution extends A
*/
@Override
public double sample() {
- return randomData.nextGamma(shape, scale);
+ if (shape < 1) {
+ // [1]: p. 228, Algorithm GS
+
+ while (true) {
+ // Step 1:
+ final double u = random.nextDouble();
+ final double bGS = 1 + shape / FastMath.E;
+ final double p = bGS * u;
+
+ if (p <= 1) {
+ // Step 2:
+
+ final double x = FastMath.pow(p, 1 / shape);
+ final double u2 = random.nextDouble();
+
+ if (u2 > FastMath.exp(-x)) {
+ // Reject
+ continue;
+ } else {
+ return scale * x;
+ }
+ } else {
+ // Step 3:
+
+ final double x = -1 * FastMath.log((bGS - p) / shape);
+ final double u2 = random.nextDouble();
+
+ if (u2 > FastMath.pow(x, shape - 1)) {
+ // Reject
+ continue;
+ } else {
+ return scale * x;
+ }
+ }
+ }
+ }
+
+ // Now shape >= 1
+
+ final double d = shape - 0.333333333333333333;
+ final double c = 1 / (3 * FastMath.sqrt(d));
+
+ while (true) {
+ final double x = random.nextGaussian();
+ final double v = (1 + c * x) * (1 + c * x) * (1 + c * x);
+
+ if (v <= 0) {
+ continue;
+ }
+
+ final double x2 = x * x;
+ final double u = random.nextDouble();
+
+ // Squeeze
+ if (u < 1 - 0.0331 * x2 * x2) {
+ return scale * d * v;
+ }
+
+ if (FastMath.log(u) < 0.5 * x2 + d * (1 - v + FastMath.log(v))) {
+ return scale * d * v;
+ }
+ }
}
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/HypergeometricDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/HypergeometricDistribution.java?rev=1363604&r1=1363603&r2=1363604&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/HypergeometricDistribution.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/HypergeometricDistribution.java Fri Jul 20 00:43:45 2012
@@ -22,6 +22,8 @@ import org.apache.commons.math3.exceptio
import org.apache.commons.math3.exception.NumberIsTooLargeException;
import org.apache.commons.math3.exception.util.LocalizedFormats;
import org.apache.commons.math3.util.FastMath;
+import org.apache.commons.math3.random.RandomGenerator;
+import org.apache.commons.math3.random.Well19937c;
/**
* Implementation of the hypergeometric distribution.
@@ -33,19 +35,14 @@ import org.apache.commons.math3.util.Fas
public class HypergeometricDistribution extends AbstractIntegerDistribution {
/** Serializable version identifier. */
private static final long serialVersionUID = -436928820673516179L;
-
/** The number of successes in the population. */
private final int numberOfSuccesses;
-
/** The population size. */
private final int populationSize;
-
/** The sample size. */
private final int sampleSize;
-
/** Cached numerical variance */
private double numericalVariance = Double.NaN;
-
/** Whether or not the numerical variance has been calculated */
private boolean numericalVarianceIsCalculated = false;
@@ -63,6 +60,29 @@ public class HypergeometricDistribution
*/
public HypergeometricDistribution(int populationSize, int numberOfSuccesses, int sampleSize)
throws NotPositiveException, NotStrictlyPositiveException, NumberIsTooLargeException {
+ this(new Well19937c(), populationSize, numberOfSuccesses, sampleSize);
+ }
+
+ /**
+ * Creates a new hypergeometric distribution.
+ *
+ * @param rng Random number generator.
+ * @param populationSize Population size.
+ * @param numberOfSuccesses Number of successes in the population.
+ * @param sampleSize Sample size.
+ * @throws NotPositiveException if {@code numberOfSuccesses < 0}.
+ * @throws NotStrictlyPositiveException if {@code populationSize <= 0}.
+ * @throws NumberIsTooLargeException if {@code numberOfSuccesses > populationSize},
+ * or {@code sampleSize > populationSize}.
+ * @since 3.1
+ */
+ public HypergeometricDistribution(RandomGenerator rng,
+ int populationSize,
+ int numberOfSuccesses,
+ int sampleSize)
+ throws NotPositiveException, NotStrictlyPositiveException, NumberIsTooLargeException {
+ super(rng);
+
if (populationSize <= 0) {
throw new NotStrictlyPositiveException(LocalizedFormats.POPULATION_SIZE,
populationSize);
@@ -272,7 +292,7 @@ public class HypergeometricDistribution
final double N = getPopulationSize();
final double m = getNumberOfSuccesses();
final double n = getSampleSize();
- return ( n * m * (N - n) * (N - m) ) / ( (N * N * (N - 1)) );
+ return (n * m * (N - n) * (N - m)) / (N * N * (N - 1));
}
/**
@@ -286,7 +306,7 @@ public class HypergeometricDistribution
*/
public int getSupportLowerBound() {
return FastMath.max(0,
- getSampleSize() + getNumberOfSuccesses() - getPopulationSize());
+ getSampleSize() + getNumberOfSuccesses() - getPopulationSize());
}
/**
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/LogNormalDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/LogNormalDistribution.java?rev=1363604&r1=1363603&r2=1363604&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/LogNormalDistribution.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/LogNormalDistribution.java Fri Jul 20 00:43:45 2012
@@ -22,6 +22,8 @@ import org.apache.commons.math3.exceptio
import org.apache.commons.math3.exception.util.LocalizedFormats;
import org.apache.commons.math3.special.Erf;
import org.apache.commons.math3.util.FastMath;
+import org.apache.commons.math3.random.RandomGenerator;
+import org.apache.commons.math3.random.Well19937c;
/**
* Implementation of the log-normal (gaussian) distribution.
@@ -74,6 +76,17 @@ public class LogNormalDistribution exten
private final double solverAbsoluteAccuracy;
/**
+ * Create 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}.
+ */
+ public LogNormalDistribution() {
+ this(0, 1);
+ }
+
+ /**
* Create a log-normal distribution using the specified scale and shape.
*
* @param scale the scale parameter of this distribution
@@ -94,8 +107,27 @@ public class LogNormalDistribution exten
* @param inverseCumAccuracy Inverse cumulative probability accuracy.
* @throws NotStrictlyPositiveException if {@code shape <= 0}.
*/
- public LogNormalDistribution(double scale, double shape,
- double inverseCumAccuracy) throws NotStrictlyPositiveException {
+ 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.
+ * @param inverseCumAccuracy Inverse cumulative probability accuracy.
+ * @throws NotStrictlyPositiveException if {@code shape <= 0}.
+ */
+ public LogNormalDistribution(RandomGenerator rng,
+ double scale,
+ double shape,
+ double inverseCumAccuracy)
+ throws NotStrictlyPositiveException {
+ super(rng);
+
if (shape <= 0) {
throw new NotStrictlyPositiveException(LocalizedFormats.SHAPE, shape);
}
@@ -106,17 +138,6 @@ public class LogNormalDistribution exten
}
/**
- * Create 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}.
- */
- public LogNormalDistribution() {
- this(0, 1);
- }
-
- /**
* Returns the scale parameter of this distribution.
*
* @return the scale parameter
@@ -285,7 +306,7 @@ public class LogNormalDistribution exten
/** {@inheritDoc} */
@Override
public double sample() {
- double n = randomData.nextGaussian(0, 1);
+ final double n = random.nextGaussian();
return FastMath.exp(scale + shape * n);
}
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/NormalDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/NormalDistribution.java?rev=1363604&r1=1363603&r2=1363604&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/NormalDistribution.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/NormalDistribution.java Fri Jul 20 00:43:45 2012
@@ -22,6 +22,8 @@ import org.apache.commons.math3.exceptio
import org.apache.commons.math3.exception.util.LocalizedFormats;
import org.apache.commons.math3.special.Erf;
import org.apache.commons.math3.util.FastMath;
+import org.apache.commons.math3.random.RandomGenerator;
+import org.apache.commons.math3.random.Well19937c;
/**
* Implementation of the normal (gaussian) distribution.
@@ -50,6 +52,14 @@ public class NormalDistribution extends
private final double solverAbsoluteAccuracy;
/**
+ * Create a normal distribution with mean equal to zero and standard
+ * deviation equal to one.
+ */
+ public NormalDistribution() {
+ this(0, 1);
+ }
+
+ /**
* Create a normal distribution using the given mean and standard deviation.
*
* @param mean Mean for this distribution.
@@ -73,6 +83,26 @@ public class NormalDistribution extends
*/
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.
+ * @param inverseCumAccuracy Inverse cumulative probability accuracy.
+ * @throws NotStrictlyPositiveException if {@code sd <= 0}.
+ * @since 3.1
+ */
+ public NormalDistribution(RandomGenerator rng,
+ double mean,
+ double sd,
+ double inverseCumAccuracy)
+ throws NotStrictlyPositiveException {
+ super(rng);
+
if (sd <= 0) {
throw new NotStrictlyPositiveException(LocalizedFormats.STANDARD_DEVIATION, sd);
}
@@ -83,14 +113,6 @@ public class NormalDistribution extends
}
/**
- * Create a normal distribution with mean equal to zero and standard
- * deviation equal to one.
- */
- public NormalDistribution() {
- this(0, 1);
- }
-
- /**
* Access the mean.
*
* @return the mean for this distribution.
@@ -113,10 +135,10 @@ public class NormalDistribution extends
*
* For this distribution {@code P(X = x)} always evaluates to 0.
*
- * @return 0
+ * @return zero.
*/
public double probability(double x) {
- return 0.0;
+ return 0;
}
/** {@inheritDoc} */
@@ -230,6 +252,6 @@ public class NormalDistribution extends
/** {@inheritDoc} */
@Override
public double sample() {
- return randomData.nextGaussian(mean, standardDeviation);
+ return standardDeviation * random.nextGaussian() + mean;
}
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/PascalDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/PascalDistribution.java?rev=1363604&r1=1363603&r2=1363604&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/PascalDistribution.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/PascalDistribution.java Fri Jul 20 00:43:45 2012
@@ -22,6 +22,8 @@ import org.apache.commons.math3.exceptio
import org.apache.commons.math3.special.Beta;
import org.apache.commons.math3.util.ArithmeticUtils;
import org.apache.commons.math3.util.FastMath;
+import org.apache.commons.math3.random.RandomGenerator;
+import org.apache.commons.math3.random.Well19937c;
/**
* <p>
@@ -75,13 +77,34 @@ public class PascalDistribution extends
* @param p Probability of success.
* @throws NotStrictlyPositiveException if the number of successes is not positive
* @throws OutOfRangeException if the probability of success is not in the
- * range [0, 1]
+ * range {@code [0, 1]}.
*/
public PascalDistribution(int r, double p)
throws NotStrictlyPositiveException, OutOfRangeException {
+ this(new Well19937c(), r, p);
+ }
+
+ /**
+ * Create a Pascal distribution with the given number of successes and
+ * probability of success.
+ *
+ * @param rng Random number generator.
+ * @param r Number of successes.
+ * @param p Probability of success.
+ * @throws NotStrictlyPositiveException if the number of successes is not positive
+ * @throws OutOfRangeException if the probability of success is not in the
+ * range {@code [0, 1]}.
+ * @since 3.1
+ */
+ public PascalDistribution(RandomGenerator rng,
+ int r,
+ double p)
+ throws NotStrictlyPositiveException, OutOfRangeException {
+ super(rng);
+
if (r <= 0) {
throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_SUCCESSES,
- r);
+ r);
}
if (p < 0 || p > 1) {
throw new OutOfRangeException(p, 0, 1);
@@ -194,4 +217,3 @@ public class PascalDistribution extends
return true;
}
}
-
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/PoissonDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/PoissonDistribution.java?rev=1363604&r1=1363603&r2=1363604&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/PoissonDistribution.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/PoissonDistribution.java Fri Jul 20 00:43:45 2012
@@ -20,7 +20,10 @@ import org.apache.commons.math3.exceptio
import org.apache.commons.math3.exception.util.LocalizedFormats;
import org.apache.commons.math3.special.Gamma;
import org.apache.commons.math3.util.MathUtils;
+import org.apache.commons.math3.util.ArithmeticUtils;
import org.apache.commons.math3.util.FastMath;
+import org.apache.commons.math3.random.RandomGenerator;
+import org.apache.commons.math3.random.Well19937c;
/**
* Implementation of the Poisson distribution.
@@ -35,26 +38,24 @@ public class PoissonDistribution extends
* @since 2.1
*/
public static final int DEFAULT_MAX_ITERATIONS = 10000000;
-
/**
* Default convergence criterion.
* @since 2.1
*/
public static final double DEFAULT_EPSILON = 1e-12;
-
/** Serializable version identifier. */
private static final long serialVersionUID = -3349935121172596109L;
-
/** Distribution used to compute normal approximation. */
private final NormalDistribution normal;
-
+ /** Distribution needed for the {@link #sample()} method. */
+ private final ExponentialDistribution exponential;
/** Mean of the distribution. */
private final double mean;
/**
* Maximum number of iterations for cumulative probability. Cumulative
- * probabilities are estimated using either Lanczos series approximation of
- * {@link Gamma#regularizedGammaP(double, double, double, int)}
+ * probabilities are estimated using either Lanczos series approximation
+ * of {@link Gamma#regularizedGammaP(double, double, double, int)}
* or continued fraction approximation of
* {@link Gamma#regularizedGammaQ(double, double, double, int)}.
*/
@@ -86,13 +87,40 @@ public class PoissonDistribution extends
*/
public PoissonDistribution(double p, double epsilon, int maxIterations)
throws NotStrictlyPositiveException {
+ this(new Well19937c(), p, epsilon, maxIterations);
+ }
+
+ /**
+ * Creates a new Poisson distribution with specified mean, convergence
+ * criterion and maximum number of iterations.
+ *
+ * @param rng Random number generator.
+ * @param p Poisson mean.
+ * @param epsilon Convergence criterion for cumulative probabilities.
+ * @param maxIterations the maximum number of iterations for cumulative
+ * probabilities.
+ * @throws NotStrictlyPositiveException if {@code p <= 0}.
+ * @since 3.1
+ */
+ public PoissonDistribution(RandomGenerator rng,
+ double p,
+ double epsilon,
+ int maxIterations)
+ throws NotStrictlyPositiveException {
+ super(rng);
+
if (p <= 0) {
throw new NotStrictlyPositiveException(LocalizedFormats.MEAN, p);
}
mean = p;
- normal = new NormalDistribution(p, FastMath.sqrt(p));
this.epsilon = epsilon;
this.maxIterations = maxIterations;
+
+ // Use the same RNG instance as the parent class.
+ normal = new NormalDistribution(rng, p, FastMath.sqrt(p),
+ NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
+ exponential = new ExponentialDistribution(rng, 1,
+ ExponentialDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
}
/**
@@ -252,6 +280,96 @@ public class PoissonDistribution extends
*/
@Override
public int sample() {
- return (int) FastMath.min(randomData.nextPoisson(mean), Integer.MAX_VALUE);
+ return (int) FastMath.min(nextPoisson(mean), Integer.MAX_VALUE);
+ }
+
+ /**
+ * @param meanPoisson Mean of the Poisson distribution.
+ * @return the next sample.
+ */
+ private long nextPoisson(double meanPoisson) {
+ final double pivot = 40.0d;
+ if (meanPoisson < pivot) {
+ double p = FastMath.exp(-meanPoisson);
+ long n = 0;
+ double r = 1.0d;
+ double rnd = 1.0d;
+
+ while (n < 1000 * meanPoisson) {
+ rnd = random.nextDouble();
+ r = r * rnd;
+ if (r >= p) {
+ n++;
+ } else {
+ return n;
+ }
+ }
+ return n;
+ } else {
+ final double lambda = FastMath.floor(meanPoisson);
+ final double lambdaFractional = meanPoisson - lambda;
+ final double logLambda = FastMath.log(lambda);
+ final double logLambdaFactorial = ArithmeticUtils.factorialLog((int) lambda);
+ final long y2 = lambdaFractional < Double.MIN_VALUE ? 0 : nextPoisson(lambdaFractional);
+ final double delta = FastMath.sqrt(lambda * FastMath.log(32 * lambda / FastMath.PI + 1));
+ final double halfDelta = delta / 2;
+ final double twolpd = 2 * lambda + delta;
+ final double a1 = FastMath.sqrt(FastMath.PI * twolpd) * FastMath.exp(1 / 8 * lambda);
+ final double a2 = (twolpd / delta) * FastMath.exp(-delta * (1 + delta) / twolpd);
+ final double aSum = a1 + a2 + 1;
+ final double p1 = a1 / aSum;
+ final double p2 = a2 / aSum;
+ final double c1 = 1 / (8 * lambda);
+
+ double x = 0;
+ double y = 0;
+ double v = 0;
+ int a = 0;
+ double t = 0;
+ double qr = 0;
+ double qa = 0;
+ for (;;) {
+ final double u = random.nextDouble();
+ if (u <= p1) {
+ final double n = random.nextGaussian();
+ x = n * FastMath.sqrt(lambda + halfDelta) - 0.5d;
+ if (x > delta || x < -lambda) {
+ continue;
+ }
+ y = x < 0 ? FastMath.floor(x) : FastMath.ceil(x);
+ final double e = exponential.sample();
+ v = -e - (n * n / 2) + c1;
+ } else {
+ if (u > p1 + p2) {
+ y = lambda;
+ break;
+ } else {
+ x = delta + (twolpd / delta) * exponential.sample();
+ y = FastMath.ceil(x);
+ v = -exponential.sample() - delta * (x + 1) / twolpd;
+ }
+ }
+ a = x < 0 ? 1 : 0;
+ t = y * (y + 1) / (2 * lambda);
+ if (v < -t && a == 0) {
+ y = lambda + y;
+ break;
+ }
+ qr = t * ((2 * y + 1) / (6 * lambda) - 1);
+ qa = qr - (t * t) / (3 * (lambda + a * (y + 1)));
+ if (v < qa) {
+ y = lambda + y;
+ break;
+ }
+ if (v > qr) {
+ continue;
+ }
+ if (v < y * logLambda - ArithmeticUtils.factorialLog((int) (y + lambda)) + logLambdaFactorial) {
+ y = lambda + y;
+ break;
+ }
+ }
+ return y2 + (long) y;
+ }
}
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/TDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/TDistribution.java?rev=1363604&r1=1363603&r2=1363604&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/TDistribution.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/TDistribution.java Fri Jul 20 00:43:45 2012
@@ -21,6 +21,8 @@ import org.apache.commons.math3.exceptio
import org.apache.commons.math3.special.Beta;
import org.apache.commons.math3.special.Gamma;
import org.apache.commons.math3.util.FastMath;
+import org.apache.commons.math3.random.RandomGenerator;
+import org.apache.commons.math3.random.Well19937c;
/**
* Implementation of Student's t-distribution.
@@ -43,6 +45,17 @@ public class TDistribution extends Abstr
private final double solverAbsoluteAccuracy;
/**
+ * Create a t distribution using the given degrees of freedom.
+ *
+ * @param degreesOfFreedom Degrees of freedom.
+ * @throws NotStrictlyPositiveException if {@code degreesOfFreedom <= 0}
+ */
+ public TDistribution(double degreesOfFreedom)
+ throws NotStrictlyPositiveException {
+ this(degreesOfFreedom, DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
+ }
+
+ /**
* Create a t distribution using the given degrees of freedom and the
* specified inverse cumulative probability absolute accuracy.
*
@@ -55,23 +68,32 @@ public class TDistribution extends Abstr
*/
public TDistribution(double degreesOfFreedom, double inverseCumAccuracy)
throws NotStrictlyPositiveException {
- if (degreesOfFreedom <= 0) {
- throw new NotStrictlyPositiveException(LocalizedFormats.DEGREES_OF_FREEDOM,
- degreesOfFreedom);
- }
- this.degreesOfFreedom = degreesOfFreedom;
- solverAbsoluteAccuracy = inverseCumAccuracy;
+ this(new Well19937c(), degreesOfFreedom, inverseCumAccuracy);
}
/**
- * Create a t distribution using the given degrees of freedom.
+ * 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(double degreesOfFreedom)
+ public TDistribution(RandomGenerator rng,
+ double degreesOfFreedom,
+ double inverseCumAccuracy)
throws NotStrictlyPositiveException {
- this(degreesOfFreedom, DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
+ super(rng);
+
+ if (degreesOfFreedom <= 0) {
+ throw new NotStrictlyPositiveException(LocalizedFormats.DEGREES_OF_FREEDOM,
+ degreesOfFreedom);
+ }
+ this.degreesOfFreedom = degreesOfFreedom;
+ solverAbsoluteAccuracy = inverseCumAccuracy;
}
/**
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/TriangularDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/TriangularDistribution.java?rev=1363604&r1=1363603&r2=1363604&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/TriangularDistribution.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/TriangularDistribution.java Fri Jul 20 00:43:45 2012
@@ -22,6 +22,8 @@ import org.apache.commons.math3.exceptio
import org.apache.commons.math3.exception.OutOfRangeException;
import org.apache.commons.math3.exception.util.LocalizedFormats;
import org.apache.commons.math3.util.FastMath;
+import org.apache.commons.math3.random.RandomGenerator;
+import org.apache.commons.math3.random.Well19937c;
/**
* Implementation of the triangular real distribution.
@@ -35,31 +37,48 @@ import org.apache.commons.math3.util.Fas
public class TriangularDistribution extends AbstractRealDistribution {
/** Serializable version identifier. */
private static final long serialVersionUID = 20120112L;
-
/** Lower limit of this distribution (inclusive). */
private final double a;
-
/** Upper limit of this distribution (inclusive). */
private final double b;
-
/** Mode of this distribution. */
private final double c;
-
/** Inverse cumulative probability accuracy. */
private final double solverAbsoluteAccuracy;
/**
- * Create a triangular real distribution using the given lower limit,
+ * Creates a triangular real distribution using the given lower limit,
* upper limit, and mode.
*
* @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}
+ * @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,
+ double c,
+ double b)
+ throws NumberIsTooLargeException, NumberIsTooSmallException {
+ super(rng);
+
if (a >= b) {
throw new NumberIsTooLargeException(
LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/UniformIntegerDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/UniformIntegerDistribution.java?rev=1363604&r1=1363603&r2=1363604&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/UniformIntegerDistribution.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/UniformIntegerDistribution.java Fri Jul 20 00:43:45 2012
@@ -19,6 +19,9 @@ package org.apache.commons.math3.distrib
import org.apache.commons.math3.exception.NumberIsTooLargeException;
import org.apache.commons.math3.exception.util.LocalizedFormats;
+import org.apache.commons.math3.random.RandomGenerator;
+import org.apache.commons.math3.random.Well19937c;
+import org.apache.commons.math3.util.FastMath;
/**
* Implementation of the uniform integer distribution.
@@ -32,10 +35,8 @@ import org.apache.commons.math3.exceptio
public class UniformIntegerDistribution extends AbstractIntegerDistribution {
/** Serializable version identifier. */
private static final long serialVersionUID = 20120109L;
-
/** Lower bound (inclusive) of this distribution. */
private final int lower;
-
/** Upper bound (inclusive) of this distribution. */
private final int upper;
@@ -47,7 +48,27 @@ public class UniformIntegerDistribution
* @param upper Upper bound (inclusive) of this distribution.
* @throws NumberIsTooLargeException if {@code lower >= upper}.
*/
- public UniformIntegerDistribution(int lower, int upper) throws NumberIsTooLargeException {
+ public UniformIntegerDistribution(int lower, int upper)
+ throws NumberIsTooLargeException {
+ this(new Well19937c(), lower, upper);
+ }
+
+ /**
+ * Creates a new uniform integer distribution using the given lower and
+ * upper bounds (both inclusive).
+ *
+ * @param rng Random number generator.
+ * @param lower Lower bound (inclusive) of this distribution.
+ * @param upper Upper bound (inclusive) of this distribution.
+ * @throws NumberIsTooLargeException if {@code lower >= upper}.
+ * @since 3.1
+ */
+ public UniformIntegerDistribution(RandomGenerator rng,
+ int lower,
+ int upper)
+ throws NumberIsTooLargeException {
+ super(rng);
+
if (lower >= upper) {
throw new NumberIsTooLargeException(
LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
@@ -135,6 +156,8 @@ public class UniformIntegerDistribution
/** {@inheritDoc} */
@Override
public int sample() {
- return randomData.nextInt(lower, upper);
+ final double r = random.nextDouble();
+ final double scaled = r * upper + (1 - r) * lower + r;
+ return (int) FastMath.floor(scaled);
}
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/UniformRealDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/UniformRealDistribution.java?rev=1363604&r1=1363603&r2=1363604&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/UniformRealDistribution.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/UniformRealDistribution.java Fri Jul 20 00:43:45 2012
@@ -19,6 +19,8 @@ package org.apache.commons.math3.distrib
import org.apache.commons.math3.exception.NumberIsTooLargeException;
import org.apache.commons.math3.exception.util.LocalizedFormats;
+import org.apache.commons.math3.random.RandomGenerator;
+import org.apache.commons.math3.random.Well19937c;
/**
* Implementation of the uniform real distribution.
@@ -32,20 +34,24 @@ import org.apache.commons.math3.exceptio
public class UniformRealDistribution extends AbstractRealDistribution {
/** Default inverse cumulative probability accuracy. */
public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9;
-
/** Serializable version identifier. */
private static final long serialVersionUID = 20120109L;
-
/** Lower bound of this distribution (inclusive). */
private final double lower;
-
/** Upper bound of this distribution (exclusive). */
private final double upper;
-
/** Inverse cumulative probability accuracy. */
private final double solverAbsoluteAccuracy;
/**
+ * Create a standard uniform real distribution with lower bound (inclusive)
+ * equal to zero and upper bound (exclusive) equal to one.
+ */
+ public UniformRealDistribution() {
+ this(0, 1);
+ }
+
+ /**
* Create a uniform real distribution using the given lower and upper
* bounds.
*
@@ -59,8 +65,7 @@ public class UniformRealDistribution ext
}
/**
- * Create a normal distribution using the given mean, standard deviation and
- * inverse cumulative distribution accuracy.
+ * Create a uniform distribution.
*
* @param lower Lower bound of this distribution (inclusive).
* @param upper Upper bound of this distribution (exclusive).
@@ -69,6 +74,25 @@ public class UniformRealDistribution ext
*/
public UniformRealDistribution(double lower, double upper, double inverseCumAccuracy)
throws NumberIsTooLargeException {
+ this(new Well19937c(), lower, upper, inverseCumAccuracy);
+ }
+
+ /**
+ * 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).
+ * @param inverseCumAccuracy Inverse cumulative probability accuracy.
+ * @throws NumberIsTooLargeException if {@code lower >= upper}.
+ * @since 3.1
+ */
+ public UniformRealDistribution(RandomGenerator rng,
+ double lower,
+ double upper,
+ double inverseCumAccuracy)
+ throws NumberIsTooLargeException {
+ super(rng);
if (lower >= upper) {
throw new NumberIsTooLargeException(
LocalizedFormats.LOWER_BOUND_NOT_BELOW_UPPER_BOUND,
@@ -81,14 +105,6 @@ public class UniformRealDistribution ext
}
/**
- * Create a standard uniform real distribution with lower bound (inclusive)
- * equal to zero and upper bound (exclusive) equal to one.
- */
- public UniformRealDistribution() {
- this(0, 1);
- }
-
- /**
* {@inheritDoc}
*
* For this distribution {@code P(X = x)} always evaluates to 0.
@@ -193,6 +209,7 @@ public class UniformRealDistribution ext
/** {@inheritDoc} */
@Override
public double sample() {
- return randomData.nextUniform(lower, upper, true);
+ final double u = random.nextDouble();
+ return u * upper + (1 - u) * lower;
}
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/WeibullDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/WeibullDistribution.java?rev=1363604&r1=1363603&r2=1363604&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/WeibullDistribution.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/WeibullDistribution.java Fri Jul 20 00:43:45 2012
@@ -22,6 +22,8 @@ import org.apache.commons.math3.exceptio
import org.apache.commons.math3.exception.util.LocalizedFormats;
import org.apache.commons.math3.special.Gamma;
import org.apache.commons.math3.util.FastMath;
+import org.apache.commons.math3.random.RandomGenerator;
+import org.apache.commons.math3.random.Well19937c;
/**
* Implementation of the Weibull distribution. This implementation uses the
@@ -40,28 +42,20 @@ public class WeibullDistribution extends
* @since 2.1
*/
public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9;
-
/** Serializable version identifier. */
private static final long serialVersionUID = 8589540077390120676L;
-
/** The shape parameter. */
private final double shape;
-
/** The scale parameter. */
private final double scale;
-
/** Inverse cumulative probability accuracy. */
private final double solverAbsoluteAccuracy;
-
/** Cached numerical mean */
private double numericalMean = Double.NaN;
-
/** Whether or not the numerical mean has been calculated */
private boolean numericalMeanIsCalculated = false;
-
/** Cached numerical variance */
private double numericalVariance = Double.NaN;
-
/** Whether or not the numerical variance has been calculated */
private boolean numericalVarianceIsCalculated = false;
@@ -93,8 +87,30 @@ public class WeibullDistribution extends
* @since 2.1
*/
public WeibullDistribution(double alpha, double beta,
- double inverseCumAccuracy)
+ 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.
+ * @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,
+ double beta,
+ double inverseCumAccuracy)
throws NotStrictlyPositiveException {
+ super(rng);
+
if (alpha <= 0) {
throw new NotStrictlyPositiveException(LocalizedFormats.SHAPE,
alpha);
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/ZipfDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/ZipfDistribution.java?rev=1363604&r1=1363603&r2=1363604&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/ZipfDistribution.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/ZipfDistribution.java Fri Jul 20 00:43:45 2012
@@ -20,6 +20,8 @@ package org.apache.commons.math3.distrib
import org.apache.commons.math3.exception.NotStrictlyPositiveException;
import org.apache.commons.math3.exception.util.LocalizedFormats;
import org.apache.commons.math3.util.FastMath;
+import org.apache.commons.math3.random.RandomGenerator;
+import org.apache.commons.math3.random.Well19937c;
/**
* Implementation of the Zipf distribution.
@@ -30,22 +32,16 @@ import org.apache.commons.math3.util.Fas
public class ZipfDistribution extends AbstractIntegerDistribution {
/** Serializable version identifier. */
private static final long serialVersionUID = -140627372283420404L;
-
/** Number of elements. */
private final int numberOfElements;
-
/** Exponent parameter of the distribution. */
private final double exponent;
-
/** Cached numerical mean */
private double numericalMean = Double.NaN;
-
/** Whether or not the numerical mean has been calculated */
private boolean numericalMeanIsCalculated = false;
-
/** Cached numerical variance */
private double numericalVariance = Double.NaN;
-
/** Whether or not the numerical variance has been calculated */
private boolean numericalVarianceIsCalculated = false;
@@ -58,8 +54,26 @@ public class ZipfDistribution extends Ab
* @exception NotStrictlyPositiveException if {@code numberOfElements <= 0}
* or {@code exponent <= 0}.
*/
- public ZipfDistribution(final int numberOfElements, final double exponent)
+ public ZipfDistribution(final int numberOfElements, final double exponent) {
+ this(new Well19937c(), numberOfElements, exponent);
+ }
+
+ /**
+ * Creates a Zipf distribution.
+ *
+ * @param rng Random number generator.
+ * @param numberOfElements Number of elements.
+ * @param exponent Exponent.
+ * @exception NotStrictlyPositiveException if {@code numberOfElements <= 0}
+ * or {@code exponent <= 0}.
+ * @since 3.1
+ */
+ public ZipfDistribution(RandomGenerator rng,
+ int numberOfElements,
+ double exponent)
throws NotStrictlyPositiveException {
+ super(rng);
+
if (numberOfElements <= 0) {
throw new NotStrictlyPositiveException(LocalizedFormats.DIMENSION,
numberOfElements);
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/AbstractIntegerDistributionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/AbstractIntegerDistributionTest.java?rev=1363604&r1=1363603&r2=1363604&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/AbstractIntegerDistributionTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/AbstractIntegerDistributionTest.java Fri Jul 20 00:43:45 2012
@@ -64,6 +64,10 @@ public class AbstractIntegerDistribution
private final double p = 1d/6d;
+ public DiceDistribution() {
+ super(null);
+ }
+
public double probability(int x) {
if (x < 1 || x > 6) {
return 0;
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/AbstractRealDistributionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/AbstractRealDistributionTest.java?rev=1363604&r1=1363603&r2=1363604&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/AbstractRealDistributionTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/AbstractRealDistributionTest.java Fri Jul 20 00:43:45 2012
@@ -34,7 +34,7 @@ public class AbstractRealDistributionTes
final double x3 = 3.0;
final double p12 = 0.5;
final AbstractRealDistribution distribution;
- distribution = new AbstractRealDistribution() {
+ distribution = new AbstractRealDistribution(null) {
private static final long serialVersionUID = 1L;
public double cumulativeProbability(final double x) {
@@ -118,7 +118,7 @@ public class AbstractRealDistributionTes
final double p12 = 1.0 / 3.0;
final double p23 = 2.0 / 3.0;
final AbstractRealDistribution distribution;
- distribution = new AbstractRealDistribution() {
+ distribution = new AbstractRealDistribution(null) {
private static final long serialVersionUID = 1L;
public double cumulativeProbability(final double x) {