You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ce...@apache.org on 2011/11/26 08:25:28 UTC

svn commit: r1206406 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/distribution/ main/java/org/apache/commons/math/random/ test/java/org/apache/commons/math/distribution/ test/java/org/apache/commons/math/random/

Author: celestin
Date: Sat Nov 26 07:25:26 2011
New Revision: 1206406

URL: http://svn.apache.org/viewvc?rev=1206406&view=rev
Log:
Merged HypergeometricDistribution and HypergeometricDistributionImpl (MATH-711).

Added:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/HypergeometricDistribution.java
      - copied, changed from r1206052, commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/HypergeometricDistributionImpl.java
Removed:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/HypergeometricDistributionImpl.java
Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/random/RandomDataImpl.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/HypergeometricDistributionTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomDataTest.java

Copied: commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/HypergeometricDistribution.java (from r1206052, commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/HypergeometricDistributionImpl.java)
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/HypergeometricDistribution.java?p2=commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/HypergeometricDistribution.java&p1=commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/HypergeometricDistributionImpl.java&r1=1206052&r2=1206406&rev=1206406&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/HypergeometricDistributionImpl.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/HypergeometricDistribution.java Sat Nov 26 07:25:26 2011
@@ -27,12 +27,14 @@ import org.apache.commons.math.util.Arit
 import org.apache.commons.math.util.FastMath;
 
 /**
- * The default implementation of {@link HypergeometricDistribution}.
+ * Implementation of the hypergeometric distribution.
  *
+ * @see <a href="http://en.wikipedia.org/wiki/Hypergeometric_distribution">Hypergeometric distribution (Wikipedia)</a>
+ * @see <a href="http://mathworld.wolfram.com/HypergeometricDistribution.html">Hypergeometric distribution (MathWorld)</a>
  * @version $Id$
  */
-public class HypergeometricDistributionImpl extends AbstractIntegerDistribution
-    implements HypergeometricDistribution, Serializable {
+public class HypergeometricDistribution extends AbstractIntegerDistribution
+    implements Serializable {
     /** Serializable version identifier. */
     private static final long serialVersionUID = -436928820673516179L;
     /** The number of successes in the population. */
@@ -43,22 +45,24 @@ public class HypergeometricDistributionI
     private final int sampleSize;
 
     /**
-     * Construct a new hypergeometric distribution with the given the
-     * population size, the number of successes in the population, and
-     * the sample size.
+     * Construct a new hypergeometric distribution with the specified population
+     * size, number of successes in the population, and sample size.
      *
      * @param populationSize Population size.
      * @param numberOfSuccesses Number of successes in the population.
      * @param sampleSize Sample size.
-     * @throws NotPositiveException if {@code numberOfSuccesses < 0}.
+     * @throws NotPositiveException if {@code numberOfSuccesses < 0},
+     * or {@code populationSize < 0}.
      * @throws NotStrictlyPositiveException if {@code populationSize <= 0}.
-     * @throws NotPositiveException if {@code populationSize < 0}.
-     * @throws NumberIsTooLargeException if {@code numberOfSuccesses > populationSize}.
-     * @throws NumberIsTooLargeException if {@code sampleSize > populationSize}.
+     * @throws NumberIsTooLargeException if {@code numberOfSuccesses > populationSize},
+     * or {@code sampleSize > populationSize}.
      */
-    public HypergeometricDistributionImpl(int populationSize,
+    public HypergeometricDistribution(int populationSize,
                                           int numberOfSuccesses,
-                                          int sampleSize) {
+                                          int sampleSize)
+        throws NotPositiveException,
+        NotStrictlyPositiveException,
+        NumberIsTooLargeException {
         if (populationSize <= 0) {
             throw new NotStrictlyPositiveException(LocalizedFormats.POPULATION_SIZE,
                                                    populationSize);
@@ -86,12 +90,7 @@ public class HypergeometricDistributionI
         this.sampleSize = sampleSize;
     }
 
-    /**
-     * For this distribution, {@code X}, this method returns {@code P(X <= x)}.
-     *
-     * @param x Value at which the PDF is evaluated.
-     * @return PDF for this distribution.
-     */
+    /** {@inheritDoc} */
     @Override
     public double cumulativeProbability(int x) {
         double ret;
@@ -122,25 +121,13 @@ public class HypergeometricDistributionI
         return new int[] { getLowerDomain(n, m, k), getUpperDomain(m, k) };
     }
 
-    /**
-     * Access the domain value lower bound, based on {@code p}, used to
-     * bracket a PDF root.
-     *
-     * @param p Desired probability for the critical value.
-     * @return the domain value lower bound, i.e. {@code P(X < 'lower bound') < p}.
-     */
+    /** {@inheritDoc} */
     @Override
     protected int getDomainLowerBound(double p) {
         return getLowerDomain(populationSize, numberOfSuccesses, sampleSize);
     }
 
-    /**
-     * Access the domain value upper bound, based on {@code p}, used to
-     * bracket a PDF root.
-     *
-     * @param p Desired probability for the critical value
-     * @return the domain value upper bound, i.e. {@code P(X < 'upper bound') > p}.
-     */
+    /** {@inheritDoc} */
     @Override
     protected int getDomainUpperBound(double p) {
         return getUpperDomain(sampleSize, numberOfSuccesses);
@@ -160,21 +147,27 @@ public class HypergeometricDistributionI
     }
 
     /**
-     * {@inheritDoc}
+     * Access the number of successes.
+     *
+     * @return the number of successes.
      */
     public int getNumberOfSuccesses() {
         return numberOfSuccesses;
     }
 
     /**
-     * {@inheritDoc}
+     * Access the population size.
+     *
+     * @return the population size.
      */
     public int getPopulationSize() {
         return populationSize;
     }
 
     /**
-     * {@inheritDoc}
+     * Access the sample size.
+     *
+     * @return the sample size.
      */
     public int getSampleSize() {
         return sampleSize;
@@ -192,12 +185,7 @@ public class HypergeometricDistributionI
         return FastMath.min(k, m);
     }
 
-    /**
-     * For this distribution, {@code X}, this method returns {@code P(X = x)}.
-     *
-     * @param x Value at which the PMF is evaluated.
-     * @return PMF for this distribution.
-     */
+    /** {@inheritDoc} */
     public double probability(int x) {
         double ret;
 
@@ -222,7 +210,7 @@ public class HypergeometricDistributionI
 
     /**
      * For this distribution, {@code X}, defined by the given hypergeometric
-     *  distribution parameters, this method returns {@code P(X = x)}.
+     * distribution parameters, this method returns {@code P(X = x)}.
      *
      * @param x Value at which the PMF is evaluated.
      * @param n the population size.
@@ -288,11 +276,9 @@ public class HypergeometricDistributionI
     /**
      * {@inheritDoc}
      *
-     * For population size <code>N</code>,
-     * number of successes <code>m</code>, and
-     * sample size <code>n</code>,
-     * the lower bound of the support is
-     * <code>max(0, n + m - N)</code>
+     * For population size {@code N}, number of successes {@code m}, and sample
+     * size {@code n}, the lower bound of the support is
+     * {@code max(0, n + m - N)}.
      *
      * @return lower bound of the support
      */
@@ -305,10 +291,8 @@ public class HypergeometricDistributionI
     /**
      * {@inheritDoc}
      *
-     * For number of successes <code>m</code> and
-     * sample size <code>n</code>,
-     * the upper bound of the support is
-     * <code>min(m, n)</code>
+     * For number of successes {@code m} and sample size {@code n}, the upper
+     * bound of the support is {@code min(m, n)}.
      *
      * @return upper bound of the support
      */
@@ -320,12 +304,8 @@ public class HypergeometricDistributionI
     /**
      * {@inheritDoc}
      *
-     * For population size <code>N</code>,
-     * number of successes <code>m</code>, and
-     * sample size <code>n</code>, the mean is
-     * <code>n * m / N</code>
-     *
-     * @return {@inheritDoc}
+     * For population size {@code N}, number of successes {@code m}, and sample
+     * size {@code n}, the mean is {@code n * m / N}.
      */
     @Override
     protected double calculateNumericalMean() {
@@ -335,12 +315,9 @@ public class HypergeometricDistributionI
     /**
      * {@inheritDoc}
      *
-     * For population size <code>N</code>,
-     * number of successes <code>m</code>, and
-     * sample size <code>n</code>, the variance is
-     * <code>[ n * m * (N - n) * (N - m) ] / [ N^2 * (N - 1) ]</code>
-     *
-     * @return {@inheritDoc}
+     * For population size {@code N}, number of successes {@code m}, and sample
+     * size {@code n}, the variance is
+     * {@code [n * m * (N - n) * (N - m)] / [N^2 * (N - 1)]}.
      */
     @Override
     protected double calculateNumericalVariance() {

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/random/RandomDataImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/random/RandomDataImpl.java?rev=1206406&r1=1206405&r2=1206406&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/random/RandomDataImpl.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/random/RandomDataImpl.java Sat Nov 26 07:25:26 2011
@@ -30,7 +30,7 @@ import org.apache.commons.math.distribut
 import org.apache.commons.math.distribution.ChiSquaredDistribution;
 import org.apache.commons.math.distribution.ContinuousDistribution;
 import org.apache.commons.math.distribution.FDistribution;
-import org.apache.commons.math.distribution.HypergeometricDistributionImpl;
+import org.apache.commons.math.distribution.HypergeometricDistribution;
 import org.apache.commons.math.distribution.IntegerDistribution;
 import org.apache.commons.math.distribution.PascalDistributionImpl;
 import org.apache.commons.math.distribution.TDistributionImpl;
@@ -755,7 +755,7 @@ public class RandomDataImpl implements R
     }
 
     /**
-     * Generates a random value from the {@link HypergeometricDistributionImpl Hypergeometric Distribution}.
+     * Generates a random value from the {@link HypergeometricDistribution Hypergeometric Distribution}.
      * This implementation uses {@link #nextInversionDeviate(IntegerDistribution) inversion}
      * to generate random values.
      *
@@ -766,7 +766,7 @@ public class RandomDataImpl implements R
      * @since 2.2
      */
     public int nextHypergeometric(int populationSize, int numberOfSuccesses, int sampleSize) {
-        return nextInversionDeviate(new HypergeometricDistributionImpl(populationSize, numberOfSuccesses, sampleSize));
+        return nextInversionDeviate(new HypergeometricDistribution(populationSize, numberOfSuccesses, sampleSize));
     }
 
     /**

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/HypergeometricDistributionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/HypergeometricDistributionTest.java?rev=1206406&r1=1206405&r2=1206406&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/HypergeometricDistributionTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/HypergeometricDistributionTest.java Sat Nov 26 07:25:26 2011
@@ -38,7 +38,7 @@ public class HypergeometricDistributionT
     /** Creates the default discrete distribution instance to use in tests. */
     @Override
     public IntegerDistribution makeDistribution() {
-        return new HypergeometricDistributionImpl(10,5, 5);
+        return new HypergeometricDistribution(10,5, 5);
     }
 
     /** Creates the default probability density test input values */
@@ -85,7 +85,7 @@ public class HypergeometricDistributionT
     /** Verify that if there are no failures, mass is concentrated on sampleSize */
     @Test
     public void testDegenerateNoFailures() throws Exception {
-        setDistribution(new HypergeometricDistributionImpl(5,5,3));
+        setDistribution(new HypergeometricDistribution(5,5,3));
         setCumulativeTestPoints(new int[] {-1, 0, 1, 3, 10 });
         setCumulativeTestValues(new double[] {0d, 0d, 0d, 1d, 1d});
         setDensityTestPoints(new int[] {-1, 0, 1, 3, 10});
@@ -100,7 +100,7 @@ public class HypergeometricDistributionT
     /** Verify that if there are no successes, mass is concentrated on 0 */
     @Test
     public void testDegenerateNoSuccesses() throws Exception {
-        setDistribution(new HypergeometricDistributionImpl(5,0,3));
+        setDistribution(new HypergeometricDistribution(5,0,3));
         setCumulativeTestPoints(new int[] {-1, 0, 1, 3, 10 });
         setCumulativeTestValues(new double[] {0d, 1d, 1d, 1d, 1d});
         setDensityTestPoints(new int[] {-1, 0, 1, 3, 10});
@@ -115,7 +115,7 @@ public class HypergeometricDistributionT
     /** Verify that if sampleSize = populationSize, mass is concentrated on numberOfSuccesses */
     @Test
     public void testDegenerateFullSample() throws Exception {
-        setDistribution(new HypergeometricDistributionImpl(5,3,5));
+        setDistribution(new HypergeometricDistribution(5,3,5));
         setCumulativeTestPoints(new int[] {-1, 0, 1, 3, 10 });
         setCumulativeTestValues(new double[] {0d, 0d, 0d, 1d, 1d});
         setDensityTestPoints(new int[] {-1, 0, 1, 3, 10});
@@ -130,31 +130,31 @@ public class HypergeometricDistributionT
     @Test
     public void testPreconditions() {
         try {
-            new HypergeometricDistributionImpl(0, 3, 5);
+            new HypergeometricDistribution(0, 3, 5);
             Assert.fail("negative population size. NotStrictlyPositiveException expected");
         } catch(NotStrictlyPositiveException ex) {
             // Expected.
         }
         try {
-            new HypergeometricDistributionImpl(5, -1, 5);
+            new HypergeometricDistribution(5, -1, 5);
             Assert.fail("negative number of successes. NotPositiveException expected");
         } catch(NotPositiveException ex) {
             // Expected.
         }
         try {
-            new HypergeometricDistributionImpl(5, 3, -1);
+            new HypergeometricDistribution(5, 3, -1);
             Assert.fail("negative sample size. NotPositiveException expected");
         } catch(NotPositiveException ex) {
             // Expected.
         }
         try {
-            new HypergeometricDistributionImpl(5, 6, 5);
+            new HypergeometricDistribution(5, 6, 5);
             Assert.fail("numberOfSuccesses > populationSize. NumberIsTooLargeException expected");
         } catch(NumberIsTooLargeException ex) {
             // Expected.
         }
         try {
-            new HypergeometricDistributionImpl(5, 3, 6);
+            new HypergeometricDistribution(5, 3, 6);
             Assert.fail("sampleSize > populationSize. NumberIsTooLargeException expected");
         } catch(NumberIsTooLargeException ex) {
             // Expected.
@@ -163,7 +163,7 @@ public class HypergeometricDistributionT
 
     @Test
     public void testAccessors() {
-        HypergeometricDistribution dist = new HypergeometricDistributionImpl(5, 3, 4);
+        HypergeometricDistribution dist = new HypergeometricDistribution(5, 3, 4);
         Assert.assertEquals(5, dist.getPopulationSize());
         Assert.assertEquals(3, dist.getNumberOfSuccesses());
         Assert.assertEquals(4, dist.getSampleSize());
@@ -199,7 +199,7 @@ public class HypergeometricDistributionT
     }
 
     private void testHypergeometricDistributionProbabilities(int populationSize, int sampleSize, int numberOfSucceses, double[][] data) {
-        HypergeometricDistributionImpl dist = new HypergeometricDistributionImpl(populationSize, numberOfSucceses, sampleSize);
+        HypergeometricDistribution dist = new HypergeometricDistribution(populationSize, numberOfSucceses, sampleSize);
         for (int i = 0; i < data.length; ++i) {
             int x = (int)data[i][0];
             double pdf = data[i][1];
@@ -248,12 +248,12 @@ public class HypergeometricDistributionT
     public void testMoments() {
         final double tol = 1e-9;
         HypergeometricDistribution dist;
-        
-        dist = new HypergeometricDistributionImpl(1500, 40, 100);
+
+        dist = new HypergeometricDistribution(1500, 40, 100);
         Assert.assertEquals(dist.getNumericalMean(), 40d * 100d / 1500d, tol);
-        Assert.assertEquals(dist.getNumericalVariance(), ( 100d * 40d * (1500d - 100d) * (1500d - 40d) ) / ( (1500d * 1500d * 1499d) ), tol); 
-        
-        dist = new HypergeometricDistributionImpl(3000, 55, 200);
+        Assert.assertEquals(dist.getNumericalVariance(), ( 100d * 40d * (1500d - 100d) * (1500d - 40d) ) / ( (1500d * 1500d * 1499d) ), tol);
+
+        dist = new HypergeometricDistribution(3000, 55, 200);
         Assert.assertEquals(dist.getNumericalMean(), 55d * 200d / 3000d, tol);
         Assert.assertEquals(dist.getNumericalVariance(), ( 200d * 55d * (3000d - 200d) * (3000d - 55d) ) / ( (3000d * 3000d * 2999d) ), tol);
     }

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomDataTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomDataTest.java?rev=1206406&r1=1206405&r2=1206406&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomDataTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/RandomDataTest.java Sat Nov 26 07:25:26 2011
@@ -33,7 +33,7 @@ import org.apache.commons.math.distribut
 import org.apache.commons.math.distribution.ExponentialDistribution;
 import org.apache.commons.math.distribution.FDistribution;
 import org.apache.commons.math.distribution.GammaDistribution;
-import org.apache.commons.math.distribution.HypergeometricDistributionImpl;
+import org.apache.commons.math.distribution.HypergeometricDistribution;
 import org.apache.commons.math.distribution.HypergeometricDistributionTest;
 import org.apache.commons.math.distribution.PascalDistributionImpl;
 import org.apache.commons.math.distribution.PascalDistributionTest;
@@ -989,7 +989,7 @@ public class RandomDataTest {
         double[] densityValues = testInstance.makeDensityTestValues();
         int sampleSize = 1000;
         int length = TestUtils.eliminateZeroMassPoints(densityPoints, densityValues);
-        HypergeometricDistributionImpl distribution = (HypergeometricDistributionImpl) testInstance.makeDistribution();
+        HypergeometricDistribution distribution = (HypergeometricDistribution) testInstance.makeDistribution();
         double[] expectedCounts = new double[length];
         long[] observedCounts = new long[length];
         for (int i = 0; i < length; i++) {