View Javadoc

1   package baseCode.math;
2   
3   import baseCode.dataStructure.matrix.DenseDoubleMatrix2DNamed;
4   import cern.colt.list.DoubleArrayList;
5   
6   /***
7    * Convenience functions for getting row statistics from matrices.
8    * <p>
9    * Copyright (c) 2004
10   * </p>
11   * <p>
12   * Institution:: Columbia University
13   * </p>
14   * 
15   * @author Paul Pavlidis
16   * @version $Id: MatrixRowStats.java,v 1.11 2004/07/27 03:18:58 pavlidis Exp $
17   * @todo Have min() and max() throw an EmptyMatrixException -- this exception class does not yet exist and needs to be
18   *       defined somewhere.
19   */
20  public class MatrixRowStats {
21  
22     private MatrixRowStats() { /* keep us from instantiating this */
23     }
24  
25     /***
26      * Calculates the sum of squares for each row of a matrix
27      * 
28      * @param M DenseDoubleMatrix2DNamed
29      * @return DoubleArrayList
30      */
31     public static DoubleArrayList sumOfSquares( DenseDoubleMatrix2DNamed M ) {
32        DoubleArrayList r = new DoubleArrayList();
33  
34        for ( int i = 0; i < M.rows(); i++ ) {
35           DoubleArrayList row = new DoubleArrayList( M.getRow( i ) );
36           r.add( DescriptiveWithMissing.sumOfSquares( row ) );
37        }
38  
39        return r;
40     }
41  
42     /***
43      * Calculates the means of a matrix's rows.
44      * 
45      * @param M DenseDoubleMatrix2DNamed
46      * @return DoubleArrayList
47      */
48     public static DoubleArrayList means( DenseDoubleMatrix2DNamed M ) {
49        DoubleArrayList r = new DoubleArrayList();
50        for ( int i = 0; i < M.rows(); i++ ) {
51           r.add( DescriptiveWithMissing
52                 .mean( new DoubleArrayList( M.getRow( i ) ) ) );
53        }
54        return r;
55     }
56  
57     /***
58      * Calculate the sums of a matrix's rows.
59      * 
60      * @param M DenseDoubleMatrix2DNamed
61      * @return DoubleArrayList
62      * @todo calls new a lot.
63      */
64     public static DoubleArrayList sums( DenseDoubleMatrix2DNamed M ) {
65        DoubleArrayList r = new DoubleArrayList();
66        for ( int i = 0; i < M.rows(); i++ ) {
67           r.add( DescriptiveWithMissing
68                 .sum( new DoubleArrayList( M.getRow( i ) ) ) );
69        }
70        return r;
71     }
72  
73     /***
74      * Calculates the sample standard deviation of each row of a matrix
75      * 
76      * @param M DenseDoubleMatrix2DNamed
77      * @return DoubleArrayList
78      */
79     public static DoubleArrayList sampleStandardDeviations(
80           DenseDoubleMatrix2DNamed M ) {
81        DoubleArrayList r = new DoubleArrayList();
82        for ( int i = 0; i < M.rows(); i++ ) {
83           DoubleArrayList row = new DoubleArrayList( M.getRow( i ) );
84           double mean = DescriptiveWithMissing.mean( row );
85           r
86                 .add( Math.sqrt( DescriptiveWithMissing.sampleVariance( row,
87                       mean ) ) );
88        }
89        return r;
90     }
91  
92  }