1   package baseCode.math;
2   
3   import junit.framework.TestCase;
4   import baseCode.math.Stats;
5   import baseCode.util.RegressionTesting;
6   import cern.colt.list.DoubleArrayList;
7   
8   /***
9    * 
10   * 
11   * <hr>
12   * <p>
13   * Copyright (c) 2004 Columbia University
14   * 
15   * @author pavlidis
16   * @version $Id: TestStats.java,v 1.1 2005/03/17 13:58:42 pavlidis Exp $
17   */
18  public class TestStats extends TestCase {
19  
20     private DoubleArrayList data1missing;
21     private DoubleArrayList data1Nomissing;
22     private DoubleArrayList data2missing;
23     private DoubleArrayList data2Nomissing;
24     private DoubleArrayList data3shortmissing;
25     private DoubleArrayList data3shortNomissing;
26     private DoubleArrayList longtest;
27  
28     protected void setUp() throws Exception {
29        super.setUp();
30        data1missing = new DoubleArrayList( new double[] { 1.0, Double.NaN, 3.0,
31              4.0, 5.0, 6.0 } );
32        data2missing = new DoubleArrayList( new double[] { Double.NaN,
33              Double.NaN, 3.0, Double.NaN, 3.5, 4.0 } );
34        data3shortmissing = new DoubleArrayList( new double[] { Double.NaN,
35              Double.NaN, 3.0 } );
36  
37        /* versions of the above, but without the NaNs */
38        data1Nomissing = new DoubleArrayList( new double[] { 1.0, 3.0, 4.0, 5.0,
39              6.0 } );
40        data2Nomissing = new DoubleArrayList( new double[] { 3.0, 3.5, 4.0 } );
41        data3shortNomissing = new DoubleArrayList( new double[] { 3.0 } );
42  
43        longtest = new DoubleArrayList( new double[] { 0.944582576545844,
44              0.779251841525345, 0.243662620408789, 0.221922034715125,
45              0.707629937237768, 0.844940612116396, 0.37307859512114,
46              0.216209684719754, 0.606640632705043, 0.0289807962282107,
47              0.831726757257319, 0.599810024125913, 0.945573940587206,
48              0.692997506949111, 0.139842747221286, 0.606947460245238,
49              0.0423340684872135, 0.974213612924272, 0.963219529277481,
50              0.990324034140914, 0.18366629791934, 0.998038865833498,
51              0.95102835078158, 0.260760025894898, 0.135541313794304,
52              0.362569773855302, 0.00907603291856485, 0.347046293225362,
53              0.552972766279235, 0.956864628364488, 0.578842167640368,
54              0.0202553960552123, 0.139571658240153, 0.838049580661271,
55              0.696258674341387, 0.167767706757327, 0.857975063707331,
56              0.384427669696752, 0.675471091800467, 0.012919572385802,
57              0.0944474009613609, 0.420980230040455, 0.658166227311119,
58              0.0618454224981813, 0.595221354638128, 0.880250901050482,
59              0.155426216787906, 0.652202074026153, 0.487896010852323,
60              0.714714352227883, } );
61  
62     } /*
63        * @see TestCase#tearDown()
64        */
65  
66     protected void tearDown() throws Exception {
67        super.tearDown();
68     }
69  
70     public final void testIsValidFraction() {
71        boolean actualReturn = Stats.isValidFraction( 12 );
72        assertEquals( false, actualReturn );
73     }
74  
75     public final void testIsValidFraction2() {
76        boolean actualReturn = Stats.isValidFraction( 0.5 );
77        assertEquals( true, actualReturn );
78     }
79  
80     public final void testCv() {
81        double expectedReturn = 1.975525931;
82        double actualReturn = Stats.cv( data1Nomissing );
83        assertEquals( expectedReturn, actualReturn, 0.000001 );
84     }
85  
86     public final void testCumulate() {
87        DoubleArrayList expectedReturn = new DoubleArrayList( new double[] { 1,
88              4, 8, 13, 19 } );
89        DoubleArrayList actualReturn = Stats.cumulate( data1Nomissing );
90        assertEquals( true, RegressionTesting.closeEnough( actualReturn,
91              expectedReturn, 0.0001 ) );
92     }
93  
94     public final void testCumulateRight() {
95        DoubleArrayList expectedReturn = new DoubleArrayList( new double[] { 19,
96              18, 15, 11, 6 } );
97        DoubleArrayList actualReturn = Stats.cumulateRight( data1Nomissing );
98        assertEquals( true, RegressionTesting.closeEnough( actualReturn,
99              expectedReturn, 0.0001 ) );
100    }
101 
102    public final void testCdf() {
103       DoubleArrayList expectedReturn = new DoubleArrayList( new double[] { 1,
104             0.947368421052632, 0.789473684210526, 0.578947368421053,
105             0.315789473684211 } );
106       DoubleArrayList actualReturn = Stats.cdf( data1Nomissing );
107       assertEquals( true, RegressionTesting.closeEnough( actualReturn,
108             expectedReturn, 0.0001 ) );
109    }
110 
111    /*
112     * Class under test for DoubleArrayList normalize(DoubleArrayList, double)
113     */
114    public final void testNormalizeDoubleArrayListdouble() {
115       DoubleArrayList expectedReturn = new DoubleArrayList( new double[] {
116             0.263157894736842, 0.789473684210526, 1.05263157894737,
117             1.31578947368421, 1.57894736842105, } );
118 
119       DoubleArrayList actualReturn = Stats.normalize( data1Nomissing, 3.8 );
120       assertEquals( true, RegressionTesting.closeEnough( actualReturn,
121             expectedReturn, 0.0001 ) );
122    }
123 
124    /*
125     * Class under test for DoubleArrayList normalize(DoubleArrayList)
126     */
127    public final void testNormalizeDoubleArrayList() {
128       DoubleArrayList expectedReturn = new DoubleArrayList( new double[] {
129             0.0526315789473684, 0.157894736842105, 0.210526315789474,
130             0.263157894736842, 0.315789473684211 } );
131 
132       DoubleArrayList actualReturn = Stats.normalize( data1Nomissing );
133       assertEquals( true, RegressionTesting.closeEnough( actualReturn,
134             expectedReturn, 0.0001 ) );
135    }
136 
137    public final void testMeanAboveQuantile() {
138       double expectedReturn = 0.806953261;
139       double actualReturn = Stats.meanAboveQuantile(25, longtest.elements(), 50);
140       assertEquals( expectedReturn, actualReturn, 1e-5 );
141    }
142 
143    public final void testRange() {
144       double expectedReturn = 5;
145       double actualReturn = Stats.range( data1Nomissing );
146       assertEquals( expectedReturn, actualReturn, 1e-5 );
147    }
148 
149    public final void testQuantile() {
150       double expectedReturn = 0.595221355;
151       double actualReturn = Stats.quantile(25, longtest.elements(), 50);
152       assertEquals( expectedReturn, actualReturn, 1e-5 );
153    }
154    
155    public final void testQuantile75() {
156       double expectedReturn = 0.831726757;
157       double actualReturn = Stats.quantile(25, longtest.elements(), 37);
158       assertEquals( expectedReturn, actualReturn, 1e-5 );
159    }
160 
161 }