1   package baseCode.math;
2   
3   import junit.framework.TestCase;
4   import baseCode.dataFilter.AbstractTestFilter;
5   import baseCode.dataStructure.matrix.DenseDoubleMatrix2DNamed;
6   import baseCode.io.reader.DoubleMatrixReader;
7   import baseCode.math.MatrixRowStats;
8   import baseCode.util.RegressionTesting;
9   import cern.colt.list.DoubleArrayList;
10  
11  /***
12   * 
13   * 
14   * <hr>
15   * <p>
16   * Copyright (c) 2004 Columbia University
17   * 
18   * @author pavlidis
19   * @version $Id: TestMatrixRowStats.java,v 1.1 2005/03/17 13:58:41 pavlidis Exp $
20   */
21  public class TestMatrixRowStats extends TestCase {
22  
23     protected DenseDoubleMatrix2DNamed testdata = null;
24  
25     /*
26      * @see TestCase#setUp()
27      */
28     protected void setUp() throws Exception {
29        super.setUp();
30        DoubleMatrixReader f = new DoubleMatrixReader();
31  
32        testdata = ( DenseDoubleMatrix2DNamed ) f.read( AbstractTestFilter.class
33              .getResourceAsStream( "/data/testdata.txt" ) );
34     }
35  
36     /*
37      * @see TestCase#tearDown()
38      */
39     protected void tearDown() throws Exception {
40        super.tearDown();
41     }
42  
43     /*
44      * Class under test for DoubleArrayList
45      * sumOfSquares(DenseDoubleMatrix2DNamed)
46      */
47     public final void testSumOfSquaresDenseDoubleMatrix2DNamed() {
48        DoubleArrayList actualReturn = MatrixRowStats.sumOfSquares( testdata );
49        DoubleArrayList expectedReturn = new DoubleArrayList( new double[] {
50              293847.67, 500060.93, 11146426060.47, 17712065.42, 18082011688.08,
51              723701.66, 286139.3, 11612833039.53, 2918638887.99, 20682978339.68,
52              34465.26, 5331217.87, 1160638.14, 11110284.05, 466405.43,
53              1238996.89, 8761486.94, 10293417.38, 8606490.67, 84399.81,
54              25917.75, 10492284.77, 2110962.48, 195657.72, 14831407.56,
55              2248759.62, 66003411.25, 159539.01, 19204218029.07, 1413519.59 } );
56        assertEquals( true, RegressionTesting.closeEnough( expectedReturn,
57              actualReturn, 0.0001 ) );
58     }
59  
60     /*
61      * Class under test for DoubleArrayList
62      * sumOfSquares(DenseDoubleMatrix2DNamed, DoubleArrayList)
63      */
64     public final void testSumOfSquaresDenseDoubleMatrix2DNamedDoubleArrayList() {
65        DoubleArrayList actualReturn = MatrixRowStats.sumOfSquares( testdata  );
66        DoubleArrayList expectedReturn = new DoubleArrayList( new double[] {
67              293847.67, 500060.93, 11146426060.47, 17712065.42, 18082011688.08,
68              723701.66, 286139.3, 11612833039.53, 2918638887.99, 20682978339.68,
69              34465.26, 5331217.87, 1160638.14, 11110284.05, 466405.43,
70              1238996.89, 8761486.94, 10293417.38, 8606490.67, 84399.81,
71              25917.75, 10492284.77, 2110962.48, 195657.72, 14831407.56,
72              2248759.62, 66003411.25, 159539.01, 19204218029.07, 1413519.59 } );
73  
74        assertEquals( true, RegressionTesting.closeEnough( expectedReturn,
75              actualReturn, 0.0001 ) );
76     }
77  
78     public final void testMeans() {
79        DoubleArrayList actualReturn = MatrixRowStats.means( testdata );
80        DoubleArrayList expectedReturn = new DoubleArrayList( new double[] {
81              134.708333333333, 168.641666666667, 30408.875, 1042.28333333333,
82              38778.8833333333, 199.35, -44.7666666666667, 31072.8416666667,
83              15550.7083333333, 41462.5166666667, 9.33333333333333,
84              -644.091666666667, 258.466666666667, 934.708333333333,
85              -166.791666666667, -172.158333333333, 811.633333333333,
86              920.366666666667, 825.375, -11.1916666666667, 21.7916666666667,
87              927.058333333333, 396.6, 107.933333333333, 1080.13333333333,
88              416.183333333333, 2265.75833333333, 12.3916666666667,
89              39963.4916666667, -333.291666666667 } );
90        assertEquals( true, RegressionTesting.closeEnough( expectedReturn,
91              actualReturn, 0.0001 ) );
92     }
93  
94     public final void testSums() {
95  
96        DoubleArrayList actualReturn = MatrixRowStats.sums( testdata );
97        DoubleArrayList expectedReturn = new DoubleArrayList(
98              new double[] { 1616.5, 2023.7, 364906.5, 12507.4, 465346.6, 2392.2,
99                    -537.2, 372874.1, 186608.5, 497550.2, 112, -7729.1, 3101.6,
100                   11216.5, -2001.5, -2065.9, 9739.6, 11044.4, 9904.5, -134.3,
101                   261.5, 11124.7, 4759.2, 1295.2, 12961.6, 4994.2, 27189.1,
102                   148.7, 479561.9, -3999.5 } );
103       assertEquals( true, RegressionTesting.closeEnough( expectedReturn,
104             actualReturn, 0.0001 ) );
105    }
106 
107    public final void testStandardDeviations() {
108 
109       DoubleArrayList actualReturn = MatrixRowStats
110             .sampleStandardDeviations( testdata );
111       DoubleArrayList expectedReturn = new DoubleArrayList( new double[] {
112             83.1710445498621, 120.144151738525, 2132.64486415128,
113             651.97677431211, 1818.84521598115, 149.792762903225,
114             154.358098019645, 1554.32442586264, 1233.78692609993,
115             2201.13358556793, 55.11964397123, 179.131482732826,
116             180.64981862625, 238.580657062224, 109.781563763795,
117             283.378158709053, 279.041040752507, 108.09077273993,
118             198.073444252838, 86.8105403982859, 42.8732054134542,
119             127.578471098688, 142.53030299809, 71.2629360930776,
120             274.880361854632, 124.410178137653, 632.418255527338,
121             119.733176524329, 1888.96399621998, 85.5567696522802, } );
122       assertEquals( true, RegressionTesting.closeEnough( expectedReturn,
123             actualReturn, 0.00001 ) );
124    }
125 
126 
127 
128 }