View Javadoc
1   /*
2    * The baseCode project
3    *
4    * Copyright (c) 2004-2011 University of British Columbia
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
12   * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
13   * specific language governing permissions and limitations under the License.
14   */
15  package ubic.basecode.math;
16  
17  import static org.junit.Assert.assertEquals;
18  
19  import org.junit.Test;
20  
21  import ubic.basecode.math.distribution.ProbabilityComputer;
22  import ubic.basecode.math.distribution.UniformProbabilityComputer;
23  import cern.colt.list.DoubleArrayList;
24  
25  /**
26   * @author pavlidis
27   * 
28   */
29  public class TestKSTest {
30  
31      // uniform (r)
32      DoubleArrayList x = new DoubleArrayList( new double[] { 0.42084388, 0.08428030, 0.51525081, 0.02165163, 0.99627802,
33              0.79237273, 0.52478154, 0.21394388, 0.19654006, 0.88131869 } );
34  
35      // normal (n)
36      DoubleArrayList y = new DoubleArrayList( new double[] { -0.09503411, 2.33677197, 0.61934707, 0.83549049,
37              0.09643316, -0.57449861, -1.40573974, 0.51279445, -0.09593008, 1.48125008 } );
38  
39      @Test
40      public void testKSTestOneSample() {
41          // ks.test(n, punif, 0, 1)
42  
43          // x<-punif(sort(n)) - (0 : (10-1)) / 10
44          // max(c(x, 1/10 - x))
45  
46          ProbabilityComputer ps = new UniformProbabilityComputer();
47          double actualReturn = KSTest.oneSample( y, ps );
48          double expectedReturn = 0.07698; // D is 0.4036
49  
50          assertEquals( 0.4036, KSTest.oneSampleStatistic( y, ps ), 0.001 );
51  
52          assertEquals( expectedReturn, actualReturn, 0.0001 );
53      }
54  
55      /**
56       *
57       */
58      @Test
59      public void testKSTestTwoSample() {
60          double actualReturn = KSTest.twoSample( x, y );
61          double expectedReturn = 0.4175; // from R ks.test(x,y); D = 0.4.
62  
63          assertEquals( 0.40, KSTest.twoSampleStatistic( x, y ), 0.001 );
64  
65          assertEquals( expectedReturn, actualReturn, 0.0001 );
66      }
67  
68  }