commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From celes...@apache.org
Subject svn commit: r1206425 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/distribution/ test/java/org/apache/commons/math/distribution/
Date Sat, 26 Nov 2011 10:53:27 GMT
Author: celestin
Date: Sat Nov 26 10:53:26 2011
New Revision: 1206425

URL: http://svn.apache.org/viewvc?rev=1206425&view=rev
Log:
Merged KolmogorovSmirnovDistribution and KolmogorovSmirnovDistributionImpl (MATH-711).

Added:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/KolmogorovSmirnovDistribution.java
      - copied, changed from r1206052, commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/KolmogorovSmirnovDistributionImpl.java
Removed:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/KolmogorovSmirnovDistributionImpl.java
Modified:
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/KolmogorovSmirnovDistributionTest.java

Copied: commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/KolmogorovSmirnovDistribution.java
(from r1206052, commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/KolmogorovSmirnovDistributionImpl.java)
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/KolmogorovSmirnovDistribution.java?p2=commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/KolmogorovSmirnovDistribution.java&p1=commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/KolmogorovSmirnovDistributionImpl.java&r1=1206052&r2=1206425&rev=1206425&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/KolmogorovSmirnovDistributionImpl.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/distribution/KolmogorovSmirnovDistribution.java
Sat Nov 26 10:53:26 2011
@@ -33,27 +33,26 @@ import org.apache.commons.math.linear.Fi
 import org.apache.commons.math.linear.RealMatrix;
 
 /**
- * The default implementation of {@link KolmogorovSmirnovDistribution}.
+ * Implementation of the Kolmogorov-Smirnov distribution.
  *
- * <p>Treats the distribution of the two-sided
- * {@code P(D}<sub>{@code n}</sub>{@code < d)}
- * where {@code D}<sub>{@code n}</sub>{@code = sup_x | G(x) - Gn (x) |} for the
- * theoretical cdf G and the emperical cdf Gn.</p>
- *
- * <p>This implementation is based on [1] with certain quick
- * decisions for extreme values given in [2].</p>
- *
- * <p>In short, when wanting to evaluate {@code P(D}<sub>{@code n}</sub>{@code
< d)},
- * the method in [1] is to write {@code d = (k - h) / n} for positive
- * integer {@code k} and {@code 0 <= h < 1}. Then
- * {@code P(D}<sub>{@code n}</sub>{@code < d) = (n!/n}<sup>{@code n}</sup>{@code
) * t_kk}
- * where {@code t_kk} is the {@code (k, k)}'th entry in the special
- * matrix {@code H}<sup>{@code n}</sup>, i.e. {@code H} to the {@code n}'th power.</p>
- *
- * <p>See also <a href="http://en.wikipedia.org/wiki/Kolmogorov-Smirnov_test">
- * Kolmogorov-Smirnov test on Wikipedia</a> for details.</p>
- *
- * <p>References:
+ * <p>
+ * Treats the distribution of the two-sided {@code P(D_n < d)} where
+ * {@code D_n = sup_x |G(x) - G_n (x)|} for the theoretical cdf {@code G} and
+ * the empirical cdf {@code G_n}.
+ * </p>
+ * <p>
+ * This implementation is based on [1] with certain quick decisions for extreme
+ * values given in [2].
+ * </p>
+ * <p>
+ * In short, when wanting to evaluate {@code P(D_n < d)}, the method in [1] is
+ * to write {@code d = (k - h) / n} for positive integer {@code k} and
+ * {@code 0 <= h < 1}. Then {@code P(D_n < d) = (n! / n^n) * t_kk}, where
+ * {@code t_kk} is the {@code (k, k)}'th entry in the special matrix
+ * {@code H^n}, i.e. {@code H} to the {@code n}'th power.
+ * </p>
+ * <p>
+ * References:
  * <ul>
  * <li>[1] <a href="http://www.jstatsoft.org/v08/i18/">
  * Evaluating Kolmogorov's Distribution</a> by George Marsaglia, Wai
@@ -66,9 +65,11 @@ import org.apache.commons.math.linear.Re
  * <a href="https://issues.apache.org/jira/browse/MATH-437">MATH-437</a> for
details.
  * </p>
  *
+ * @see <a href="http://en.wikipedia.org/wiki/Kolmogorov-Smirnov_test">
+ * Kolmogorov-Smirnov test (Wikipedia)</a>
  * @version $Id$
  */
-public class KolmogorovSmirnovDistributionImpl implements KolmogorovSmirnovDistribution,
Serializable {
+public class KolmogorovSmirnovDistribution implements Serializable {
 
     /** Serializable version identifier. */
     private static final long serialVersionUID = -4670676796862967187L;
@@ -78,10 +79,10 @@ public class KolmogorovSmirnovDistributi
 
     /**
      * @param n Number of observations
-     * @throws NotStrictlyPositiveException
-     *             if {@code n <= 0}
+     * @throws NotStrictlyPositiveException if {@code n <= 0}
      */
-    public KolmogorovSmirnovDistributionImpl(int n) {
+    public KolmogorovSmirnovDistribution(int n)
+        throws NotStrictlyPositiveException {
         if (n <= 0) {
             throw new NotStrictlyPositiveException(LocalizedFormats.NOT_POSITIVE_NUMBER_OF_SAMPLES,
n);
         }
@@ -90,67 +91,62 @@ public class KolmogorovSmirnovDistributi
     }
 
     /**
-     * Calculates {@code P(D}<sub>n</sub> {@code < d)} using method described
in
-     * [1] with quick decisions for extreme values given in [2] (see above). The
-     * result is not exact as with
-     * {@link KolmogorovSmirnovDistributionImpl#cdfExact(double)} because
-     * calculations are based on double rather than
+     * Calculates {@code P(D_n < d)} using method described in [1] with quick
+     * decisions for extreme values given in [2] (see above). The result is not
+     * exact as with
+     * {@link KolmogorovSmirnovDistribution#cdfExact(double)} because
+     * calculations are based on {@code double} rather than
      * {@link org.apache.commons.math.fraction.BigFraction}.
      *
      * @param d statistic
-     * @return the two-sided probability of {@code P(D}<sub>n</sub> {@code <
d)}
-     * @throws MathArithmeticException
-     *             if algorithm fails to convert {@code h} to a
-     *             {@link org.apache.commons.math.fraction.BigFraction} in
-     *             expressing {@code d} as {@code (k - h) / m} for integer
-     *             {@code k, m} and {@code 0 <= h < 1}.
+     * @return the two-sided probability of {@code P(D_n < d)}
+     * @throws MathArithmeticException if algorithm fails to convert {@code h}
+     * to a {@link org.apache.commons.math.fraction.BigFraction} in expressing
+     * {@code d} as {@code (k - h) / m} for integer {@code k, m} and
+     * {@code 0 <= h < 1}.
      */
     public double cdf(double d) throws MathArithmeticException {
         return this.cdf(d, false);
     }
 
     /**
-     * Calculates {@code P(D}<sub>n</sub> {@code < d)} using method described
in
-     * [1] with quick decisions for extreme values given in [2] (see above).
-     * The result is exact in the sense that BigFraction/BigReal is used everywhere
-     * at the expense of very slow execution time. Almost never choose this in
-     * real applications unless you are very sure; this is almost solely for
+     * Calculates {@code P(D_n < d)} using method described in [1] with quick
+     * decisions for extreme values given in [2] (see above). The result is
+     * exact in the sense that BigFraction/BigReal is used everywhere at the
+     * expense of very slow execution time. Almost never choose this in real
+     * applications unless you are very sure; this is almost solely for
      * verification purposes. Normally, you would choose
-     * {@link KolmogorovSmirnovDistributionImpl#cdf(double)}
+     * {@link KolmogorovSmirnovDistribution#cdf(double)}
      *
      * @param d statistic
-     * @return the two-sided probability of {@code P(D}<sub>n</sub> {@code <
d)}
-     * @throws MathArithmeticException
-     *             if algorithm fails to convert {@code h} to a
-     *             {@link org.apache.commons.math.fraction.BigFraction} in
-     *             expressing {@code d} as {@code (k - h) / m} for integer
-     *             {@code k, m} and {@code 0 <= h < 1}.
+     * @return the two-sided probability of {@code P(D_n < d)}
+     * @throws MathArithmeticException if algorithm fails to convert {@code h}
+     * to a {@link org.apache.commons.math.fraction.BigFraction} in expressing
+     * {@code d} as {@code (k - h) / m} for integer {@code k, m} and
+     * {@code 0 <= h < 1}.
      */
     public double cdfExact(double d) throws MathArithmeticException {
         return this.cdf(d, true);
     }
 
     /**
-     * Calculates {@code P(D}<sub>n</sub> {@code < d)} using method described
in
-     * [1] with quick decisions for extreme values given in [2] (see above).
+     * Calculates {@code P(D_n < d)} using method described in [1] with quick
+     * decisions for extreme values given in [2] (see above).
      *
      * @param d statistic
-     * @param exact
-     *            whether the probability should be calculated exact using
-     *            BigFraction everywhere at the expense of very
-     *            slow execution time, or if double should be used convenient
-     *            places to gain speed. Almost never choose {@code true} in
-     *            real applications unless you are very sure; {@code true} is
-     *            almost solely for verification purposes.
-     * @return the two-sided probability of {@code P(D}<sub>n</sub> {@code <
d)}
-     * @throws MathArithmeticException
-     *             if algorithm fails to convert {@code h} to a
-     *             {@link org.apache.commons.math.fraction.BigFraction} in
-     *             expressing {@code d} as {@code (k - h) / m} for integer
-     *             {@code k, m} and {@code 0 <= h < 1}.
+     * @param exact whether the probability should be calculated exact using
+     * {@link org.apache.commons.math.fraction.BigFraction} everywhere at the
+     * expense of very slow execution time, or if {@code double} should be used
+     * convenient places to gain speed. Almost never choose {@code true} in real
+     * applications unless you are very sure; {@code true} is almost solely for
+     * verification purposes.
+     * @return the two-sided probability of {@code P(D_n < d)}
+     * @throws MathArithmeticException if algorithm fails to convert {@code h}
+     * to a {@link org.apache.commons.math.fraction.BigFraction} in expressing
+     * {@code d} as {@code (k - h) / m} for integer {@code k, m} and
+     * {@code 0 <= h < 1}.
      */
-    public double cdf(double d, boolean exact)
-            throws MathArithmeticException {
+    public double cdf(double d, boolean exact) throws MathArithmeticException {
 
         final double ninv = 1 / ((double) n);
         final double ninvhalf = 0.5 * ninv;
@@ -184,19 +180,18 @@ public class KolmogorovSmirnovDistributi
     }
 
     /**
-     * Calculates {@code P(D}<sub>n</sub> {@code < d)} exact using method
-     * described in [1] and BigFraction (see above).
+     * Calculates the exact value of {@code P(D_n < d)} using method described
+     * in [1] and {@link org.apache.commons.math.fraction.BigFraction} (see
+     * above).
      *
      * @param d statistic
-     * @return the two-sided probability of {@code P(D}<sub>n</sub> {@code <
d)}
-     * @throws MathArithmeticException
-     *             if algorithm fails to convert {@code h} to a
-     *             {@link org.apache.commons.math.fraction.BigFraction} in
-     *             expressing {@code d} as {@code (k - h) / m} for integer
-     *             {@code k, m} and {@code 0 <= h < 1}.
+     * @return the two-sided probability of {@code P(D_n < d)}
+     * @throws MathArithmeticException if algorithm fails to convert {@code h}
+     * to a {@link org.apache.commons.math.fraction.BigFraction} in expressing
+     * {@code d} as {@code (k - h) / m} for integer {@code k, m} and
+     * {@code 0 <= h < 1}.
      */
-    private double exactK(double d)
-            throws MathArithmeticException {
+    private double exactK(double d) throws MathArithmeticException {
 
         final int k = (int) Math.ceil(n * d);
 
@@ -218,19 +213,17 @@ public class KolmogorovSmirnovDistributi
     }
 
     /**
-     * Calculates {@code P(D}<sub>n</sub> {@code < d)} using method described
in
-     * [1] and doubles (see above).
+     * Calculates {@code P(D_n < d)} using method described in [1] and doubles
+     * (see above).
      *
      * @param d statistic
-     * @return the two-sided probability of {@code P(D}<sub>n</sub> {@code <
d)}
-     * @throws MathArithmeticException
-     *             if algorithm fails to convert {@code h} to a
-     *             {@link org.apache.commons.math.fraction.BigFraction} in
-     *             expressing {@code d} as {@code (k - h) / m} for integer
-     *             {@code k, m} and {@code 0 <= h < 1}.
+     * @return the two-sided probability of {@code P(D_n < d)}
+     * @throws MathArithmeticException if algorithm fails to convert {@code h}
+     * to a {@link org.apache.commons.math.fraction.BigFraction} in expressing
+     * {@code d} as {@code (k - h) / m} for integer {@code k, m} and
+     * {@code 0 <= h < 1}.
      */
-    private double roundedK(double d)
-            throws MathArithmeticException {
+    private double roundedK(double d) throws MathArithmeticException {
 
         final int k = (int) Math.ceil(n * d);
         final FieldMatrix<BigFraction> HBigFraction = this.createH(d);
@@ -265,11 +258,10 @@ public class KolmogorovSmirnovDistributi
      * @param d statistic
      * @return H matrix
      * @throws NumberIsTooLargeException if fractional part is greater than 1
-     * @throws FractionConversionException
-     *             if algorithm fails to convert {@code h} to a
-     *             {@link org.apache.commons.math.fraction.BigFraction} in
-     *             expressing {@code d} as {@code (k - h) / m} for integer
-     *             {@code k, m} and {@code 0 <= h < 1}.
+     * @throws FractionConversionException if algorithm fails to convert
+     * {@code h} to a {@link org.apache.commons.math.fraction.BigFraction} in
+     * expressing {@code d} as {@code (k - h) / m} for integer {@code k, m} and
+     * {@code 0 <= h < 1}.
      */
     private FieldMatrix<BigFraction> createH(double d)
             throws NumberIsTooLargeException, FractionConversionException {

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/KolmogorovSmirnovDistributionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/KolmogorovSmirnovDistributionTest.java?rev=1206425&r1=1206424&r2=1206425&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/KolmogorovSmirnovDistributionTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/KolmogorovSmirnovDistributionTest.java
Sat Nov 26 10:53:26 2011
@@ -21,7 +21,7 @@ import org.junit.Assert;
 import org.junit.Test;
 
 /**
- * Test cases for {@link KolmogorovSmirnovDistributionImpl}.
+ * Test cases for {@link KolmogorovSmirnovDistribution}.
  *
  * @version $Id$
  */
@@ -32,7 +32,7 @@ public class KolmogorovSmirnovDistributi
     @Test
     public void testCumulativeDensityFunction() throws Exception {
         
-        KolmogorovSmirnovDistributionImpl dist;
+        KolmogorovSmirnovDistribution dist;
         
         /* The code below is generated using the R-script located in
          * /src/test/R/KolmogorovSmirnovDistributionTestCases.R
@@ -44,73 +44,73 @@ public class KolmogorovSmirnovDistributi
         /* formatC(.C("pkolmogorov2x", p = as.double(0.005), n = as.integer(200), PACKAGE
= "stats")$p, 40) gives
          * 4.907829957616471622388047046469198862537e-86
          */
-        dist = new KolmogorovSmirnovDistributionImpl(200);
+        dist = new KolmogorovSmirnovDistribution(200);
         Assert.assertEquals(4.907829957616471622388047046469198862537e-86, dist.cdf(0.005,
false), TOLERANCE);
 
         /* formatC(.C("pkolmogorov2x", p = as.double(0.02), n = as.integer(200), PACKAGE
= "stats")$p, 40) gives
          * 5.151982014280041957199687829849210629618e-06
          */
-        dist = new KolmogorovSmirnovDistributionImpl(200);
+        dist = new KolmogorovSmirnovDistribution(200);
         Assert.assertEquals(5.151982014280041957199687829849210629618e-06, dist.cdf(0.02,
false), TOLERANCE);
 
         /* formatC(.C("pkolmogorov2x", p = as.double(0.031111), n = as.integer(200), PACKAGE
= "stats")$p, 40) gives
          * 0.01291614648162886340443389343590752105229
          */
-        dist = new KolmogorovSmirnovDistributionImpl(200);
+        dist = new KolmogorovSmirnovDistribution(200);
         Assert.assertEquals(0.01291614648162886340443389343590752105229, dist.cdf(0.031111,
false), TOLERANCE);
 
         /* formatC(.C("pkolmogorov2x", p = as.double(0.04), n = as.integer(200), PACKAGE
= "stats")$p, 40) gives
          * 0.1067137011362679355208626930107129737735
          */
-        dist = new KolmogorovSmirnovDistributionImpl(200);
+        dist = new KolmogorovSmirnovDistribution(200);
         Assert.assertEquals(0.1067137011362679355208626930107129737735, dist.cdf(0.04, false),
TOLERANCE);
 
         /* formatC(.C("pkolmogorov2x", p = as.double(0.005), n = as.integer(341), PACKAGE
= "stats")$p, 40) gives
          * 1.914734701559404553985102395145063418825e-53
          */
-        dist = new KolmogorovSmirnovDistributionImpl(341);
+        dist = new KolmogorovSmirnovDistribution(341);
         Assert.assertEquals(1.914734701559404553985102395145063418825e-53, dist.cdf(0.005,
false), TOLERANCE);
 
         /* formatC(.C("pkolmogorov2x", p = as.double(0.02), n = as.integer(341), PACKAGE
= "stats")$p, 40) gives
          * 0.001171328985781981343872182321774744195864
          */
-        dist = new KolmogorovSmirnovDistributionImpl(341);
+        dist = new KolmogorovSmirnovDistribution(341);
         Assert.assertEquals(0.001171328985781981343872182321774744195864, dist.cdf(0.02,
false), TOLERANCE);
 
         /* formatC(.C("pkolmogorov2x", p = as.double(0.031111), n = as.integer(341), PACKAGE
= "stats")$p, 40) gives
          * 0.1142955196267499418105728636874118819833
          */
-        dist = new KolmogorovSmirnovDistributionImpl(341);
+        dist = new KolmogorovSmirnovDistribution(341);
         Assert.assertEquals(0.1142955196267499418105728636874118819833, dist.cdf(0.031111,
false), TOLERANCE);
 
         /* formatC(.C("pkolmogorov2x", p = as.double(0.04), n = as.integer(341), PACKAGE
= "stats")$p, 40) gives
          * 0.3685529520496805266915885113121476024389
          */
-        dist = new KolmogorovSmirnovDistributionImpl(341);
+        dist = new KolmogorovSmirnovDistribution(341);
         Assert.assertEquals(0.3685529520496805266915885113121476024389, dist.cdf(0.04, false),
TOLERANCE);
 
         /* formatC(.C("pkolmogorov2x", p = as.double(0.005), n = as.integer(389), PACKAGE
= "stats")$p, 40) gives
          * 1.810657144595055888918455512707637574637e-47
          */
-        dist = new KolmogorovSmirnovDistributionImpl(389);
+        dist = new KolmogorovSmirnovDistribution(389);
         Assert.assertEquals(1.810657144595055888918455512707637574637e-47, dist.cdf(0.005,
false), TOLERANCE);
 
         /* formatC(.C("pkolmogorov2x", p = as.double(0.02), n = as.integer(389), PACKAGE
= "stats")$p, 40) gives
          * 0.003068542559702356568168690742481885536108
          */
-        dist = new KolmogorovSmirnovDistributionImpl(389);
+        dist = new KolmogorovSmirnovDistribution(389);
         Assert.assertEquals(0.003068542559702356568168690742481885536108, dist.cdf(0.02,
false), TOLERANCE);
 
         /* formatC(.C("pkolmogorov2x", p = as.double(0.031111), n = as.integer(389), PACKAGE
= "stats")$p, 40) gives
          * 0.1658291700122746237244797384846606291831
          */
-        dist = new KolmogorovSmirnovDistributionImpl(389);
+        dist = new KolmogorovSmirnovDistribution(389);
         Assert.assertEquals(0.1658291700122746237244797384846606291831, dist.cdf(0.031111,
false), TOLERANCE);
 
         /* formatC(.C("pkolmogorov2x", p = as.double(0.04), n = as.integer(389), PACKAGE
= "stats")$p, 40) gives
          * 0.4513143712128902529379104180407011881471
          */
-        dist = new KolmogorovSmirnovDistributionImpl(389);
+        dist = new KolmogorovSmirnovDistribution(389);
         Assert.assertEquals(0.4513143712128902529379104180407011881471, dist.cdf(0.04, false),
TOLERANCE);
 
     }



Mime
View raw message