commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject svn commit: r1408173 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math3/stat/inference/TestUtils.java test/java/org/apache/commons/math3/stat/inference/TestUtilsTest.java
Date Mon, 12 Nov 2012 04:32:03 GMT
Author: psteitz
Date: Mon Nov 12 04:32:02 2012
New Revision: 1408173

URL: http://svn.apache.org/viewvc?rev=1408173&view=rev
Log:
Added G-test to TestUtils. JIRA: MATH-878

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/inference/TestUtils.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/inference/TestUtilsTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/inference/TestUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/inference/TestUtils.java?rev=1408173&r1=1408172&r2=1408173&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/inference/TestUtils.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/stat/inference/TestUtils.java
Mon Nov 12 04:32:02 2012
@@ -47,6 +47,9 @@ public class TestUtils  {
     /** Singleton OneWayAnova instance. */
     private static final OneWayAnova ONE_WAY_ANANOVA = new OneWayAnova();
 
+    /** Singleton G-Test instance. */
+    private static final GTest G_TEST = new GTest();
+
     /**
      * Prevent instantiation.
      */
@@ -362,6 +365,83 @@ public class TestUtils  {
         return ONE_WAY_ANANOVA.anovaTest(categoryData, alpha);
     }
 
+     /**
+     * @see org.apache.commons.math3.stat.inference.GTest#g(double[], long[])
+     */
+    public static double g(final double[] expected, final long[] observed)
+        throws NotPositiveException, NotStrictlyPositiveException,
+        DimensionMismatchException {
+        return G_TEST.g(expected, observed);
+    }
+
+    /**
+     * @see org.apache.commons.math3.stat.inference.GTest#gTest( double[],  long[] )
+     */
+    public static double gTest(final double[] expected, final long[] observed)
+        throws NotPositiveException, NotStrictlyPositiveException,
+        DimensionMismatchException, MaxCountExceededException {
+        return G_TEST.gTest(expected, observed);
+    }
+
+    /**
+     * @see org.apache.commons.math3.stat.inference.GTest#gTestIntrinsic(double[], long[]
)
+     */
+    public static double gTestIntrinsic(final double[] expected, final long[] observed)
+        throws NotPositiveException, NotStrictlyPositiveException,
+        DimensionMismatchException, MaxCountExceededException {
+        return G_TEST.gTestIntrinsic(expected, observed);
+    }
+
+     /**
+     * @see org.apache.commons.math3.stat.inference.GTest#gTest( double[],long[],double)
+     */
+    public static boolean gTest(final double[] expected, final long[] observed,
+                                final double alpha)
+        throws NotPositiveException, NotStrictlyPositiveException,
+        DimensionMismatchException, OutOfRangeException, MaxCountExceededException {
+        return G_TEST.gTest(expected, observed, alpha);
+    }
+
+    /**
+     * @see org.apache.commons.math3.stat.inference.GTest#gDataSetsComparison(long[], long[])
+     */
+    public static double gDataSetsComparison(final long[] observed1,
+                                                  final long[] observed2)
+        throws DimensionMismatchException, NotPositiveException, ZeroException {
+        return G_TEST.gDataSetsComparison(observed1, observed2);
+    }
+
+    /**
+     * @see org.apache.commons.math3.stat.inference.GTest#rootLogLikelihoodRatio(long, long,
long, long)
+     */
+    public static double rootLogLikelihoodRatio(final long k11, final long k12, final long
k21, final long k22)
+        throws DimensionMismatchException, NotPositiveException, ZeroException {
+        return G_TEST.rootLogLikelihoodRatio(k11, k12, k21, k22);
+    }
+
+
+    /**
+     * @see org.apache.commons.math3.stat.inference.GTest#gTestDataSetsComparison(long[],
long[])
+     *
+     */
+    public static double gTestDataSetsComparison(final long[] observed1,
+                                                        final long[] observed2)
+        throws DimensionMismatchException, NotPositiveException, ZeroException,
+        MaxCountExceededException {
+        return G_TEST.gTestDataSetsComparison(observed1, observed2);
+    }
+
+    /**
+     * @see org.apache.commons.math3.stat.inference.GTest#gTestDataSetsComparison(double[],long[],double)
+     */
+    public static boolean gTestDataSetsComparison(final long[] observed1,
+                                                  final long[] observed2,
+                                                  final double alpha)
+        throws DimensionMismatchException, NotPositiveException,
+        ZeroException, OutOfRangeException, MaxCountExceededException {
+        return G_TEST.gTestDataSetsComparison(observed1, observed2, alpha);
+    }
+
     // CHECKSTYLE: resume JavadocMethodCheck
 
 }

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/inference/TestUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/inference/TestUtilsTest.java?rev=1408173&r1=1408172&r2=1408173&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/inference/TestUtilsTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/inference/TestUtilsTest.java
Mon Nov 12 04:32:02 2012
@@ -469,4 +469,62 @@ public class TestUtilsTest {
         Assert.assertEquals(oneWayAnova.anovaTest(classes, 0.01),
                 TestUtils.oneWayAnovaTest(classes, 0.01));
     }
+    @Test
+    public void testGTestGoodnesOfFit() throws Exception {
+        double[] exp = new double[]{
+            0.54d, 0.40d, 0.05d, 0.01d
+        };
+
+        long[] obs = new long[]{
+            70, 79, 3, 4
+        };
+        Assert.assertEquals("G test statistic",
+                13.144799, TestUtils.g(exp, obs), 1E-5);
+        double p_gtgf = TestUtils.gTest(exp, obs);
+        Assert.assertEquals("g-Test p-value", 0.004333, p_gtgf, 1E-5);
+
+        Assert.assertTrue(TestUtils.gTest(exp, obs, 0.05));
+}
+
+    @Test
+    public void testGTestIndependance() throws Exception {
+        long[] obs1 = new long[]{
+            268, 199, 42
+        };
+
+        long[] obs2 = new long[]{
+            807, 759, 184
+        };
+
+        double g = TestUtils.gDataSetsComparison(obs1, obs2);
+
+        Assert.assertEquals("G test statistic",
+                7.3008170, g, 1E-4);
+        double p_gti = TestUtils.gTestDataSetsComparison(obs1, obs2);
+
+        Assert.assertEquals("g-Test p-value", 0.0259805, p_gti, 1E-4);
+        Assert.assertTrue(TestUtils.gTestDataSetsComparison(obs1, obs2, 0.05));
+    }
+
+    @Test
+    public void testRootLogLikelihood() {
+        // positive where k11 is bigger than expected.
+        Assert.assertTrue(TestUtils.rootLogLikelihoodRatio(904, 21060, 1144, 283012) >
0.0);
+
+        // negative because k11 is lower than expected
+        Assert.assertTrue(TestUtils.rootLogLikelihoodRatio(36, 21928, 60280, 623876) <
0.0);
+
+        Assert.assertEquals(Math.sqrt(2.772589), TestUtils.rootLogLikelihoodRatio(1, 0, 0,
1), 0.000001);
+        Assert.assertEquals(-Math.sqrt(2.772589), TestUtils.rootLogLikelihoodRatio(0, 1,
1, 0), 0.000001);
+        Assert.assertEquals(Math.sqrt(27.72589), TestUtils.rootLogLikelihoodRatio(10, 0,
0, 10), 0.00001);
+
+        Assert.assertEquals(Math.sqrt(39.33052), TestUtils.rootLogLikelihoodRatio(5, 1995,
0, 100000), 0.00001);
+        Assert.assertEquals(-Math.sqrt(39.33052), TestUtils.rootLogLikelihoodRatio(0, 100000,
5, 1995), 0.00001);
+
+        Assert.assertEquals(Math.sqrt(4730.737), TestUtils.rootLogLikelihoodRatio(1000, 1995,
1000, 100000), 0.001);
+        Assert.assertEquals(-Math.sqrt(4730.737), TestUtils.rootLogLikelihoodRatio(1000,
100000, 1000, 1995), 0.001);
+
+        Assert.assertEquals(Math.sqrt(5734.343), TestUtils.rootLogLikelihoodRatio(1000, 1000,
1000, 100000), 0.001);
+        Assert.assertEquals(Math.sqrt(5714.932), TestUtils.rootLogLikelihoodRatio(1000, 1000,
1000, 99000), 0.001);
+    }
 }



Mime
View raw message