Clover coverage report - baseCode - 0.2.5
Coverage timestamp: Tue Apr 12 2005 11:31:58 EDT
file stats: LOC: 194   Methods: 8
NCLOC: 114   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
MatrixStats.java 64.7% 62.5% 62.5% 63.2%
coverage coverage
 1   
 package baseCode.math;
 2   
 
 3   
 import baseCode.dataStructure.matrix.AbstractNamedDoubleMatrix;
 4   
 import baseCode.dataStructure.matrix.DenseDoubleMatrix2DNamed;
 5   
 import baseCode.dataStructure.matrix.SparseDoubleMatrix2DNamed;
 6   
 import cern.colt.function.DoubleFunction;
 7   
 import cern.colt.list.DoubleArrayList;
 8   
 import cern.colt.matrix.DoubleMatrix1D;
 9   
 import cern.jet.math.Functions;
 10   
 
 11   
 /**
 12   
  * <hr>
 13   
  * <p>
 14   
  * Copyright (c) 2004 Columbia University
 15   
  * 
 16   
  * @author pavlidis
 17   
  * @version $Id: MatrixStats.java,v 1.10 2004/08/17 21:17:40 pavlidis Exp $
 18   
  */
 19   
 public class MatrixStats {
 20   
 
 21   
    /**
 22   
     * @todo this is pretty inefficient - calls new
 23   
     * @param data DenseDoubleMatrix2DNamed
 24   
     * @return DenseDoubleMatrix2DNamed
 25   
     */
 26  1
    public static DenseDoubleMatrix2DNamed correlationMatrix(
 27   
          AbstractNamedDoubleMatrix data ) {
 28  1
       DenseDoubleMatrix2DNamed result = new DenseDoubleMatrix2DNamed( data
 29   
             .rows(), data.rows() );
 30   
 
 31  1
       for ( int i = 0; i < data.rows(); i++ ) {
 32  30
          DoubleArrayList irow = new DoubleArrayList( data.getRow( i ) );
 33  30
          for ( int j = i + 1; j < data.rows(); j++ ) {
 34  435
             DoubleArrayList jrow = new DoubleArrayList( data.getRow( j ) );
 35  435
             double c = DescriptiveWithMissing.correlation( irow, jrow );
 36  435
             result.setQuick( i, j, c );
 37  435
             result.setQuick( j, i, c );
 38   
          }
 39   
       }
 40  1
       result.setRowNames( data.getRowNames() );
 41  1
       result.setColumnNames( data.getRowNames() );
 42   
 
 43  1
       return result;
 44   
    }
 45   
 
 46   
    /**
 47   
     * @param data DenseDoubleMatrix2DNamed
 48   
     * @param threshold only correlations with absolute values above this level are stored.
 49   
     * @return SparseDoubleMatrix2DNamed
 50   
     */
 51  0
    public static SparseDoubleMatrix2DNamed correlationMatrix(
 52   
          AbstractNamedDoubleMatrix data, double threshold ) {
 53  0
       SparseDoubleMatrix2DNamed result = new SparseDoubleMatrix2DNamed( data
 54   
             .rows(), data.rows() );
 55   
 
 56  0
       for ( int i = 0; i < data.rows(); i++ ) {
 57  0
          DoubleArrayList irow = new DoubleArrayList( data.getRow( i ) );
 58  0
          for ( int j = i + 1; j < data.rows(); j++ ) {
 59  0
             DoubleArrayList jrow = new DoubleArrayList( data.getRow( j ) );
 60  0
             double c = DescriptiveWithMissing.correlation( irow, jrow );
 61  0
             if ( Math.abs( c ) > threshold ) {
 62  0
                result.setQuick( i, j, c );
 63  0
                result.setQuick( j, i, c );
 64   
             }
 65   
          }
 66   
       }
 67  0
       result.setRowNames( data.getRowNames() );
 68  0
       result.setColumnNames( data.getRowNames() );
 69   
 
 70  0
       return result;
 71   
    }
 72   
 
 73   
    /**
 74   
     * Find the minimum of the entire matrix.
 75   
     * 
 76   
     * @param matrix DenseDoubleMatrix2DNamed
 77   
     * @return the smallest value in the matrix
 78   
     */
 79  1
    public static double min( AbstractNamedDoubleMatrix matrix ) {
 80   
 
 81  1
       int totalRows = matrix.rows();
 82  1
       int totalColumns = matrix.columns();
 83   
 
 84  1
       double min = Double.MAX_VALUE;
 85   
 
 86  1
       for ( int i = 0; i < totalRows; i++ ) {
 87  30
          for ( int j = 0; j < totalColumns; j++ ) {
 88  360
             double val = matrix.getQuick( i, j );
 89  360
             if ( Double.isNaN( val ) ) {
 90  0
                continue;
 91   
             }
 92   
 
 93  360
             if ( val < min ) {
 94  12
                min = val;
 95   
             }
 96   
 
 97   
          }
 98   
       }
 99  1
       if ( min == Double.MAX_VALUE ) {
 100  0
          return Double.NaN;
 101   
       }
 102  1
       return min; // might be NaN if all values are missing
 103   
 
 104   
    } // end min
 105   
 
 106   
    /**
 107   
     * Compute the maximum value in the matrix.
 108   
     * 
 109   
     * @param matrix DenseDoubleMatrix2DNamed
 110   
     * @return the largest value in the matrix
 111   
     */
 112  1
    public static double max( AbstractNamedDoubleMatrix matrix ) {
 113   
 
 114  1
       int totalRows = matrix.rows();
 115  1
       int totalColumns = matrix.columns();
 116   
 
 117  1
       double max = -Double.MAX_VALUE;
 118   
 
 119  1
       for ( int i = 0; i < totalRows; i++ ) {
 120  30
          for ( int j = 0; j < totalColumns; j++ ) {
 121  360
             double val = matrix.getQuick( i, j );
 122  360
             if ( Double.isNaN( val ) ) {
 123  0
                continue;
 124   
             }
 125   
 
 126  360
             if ( val > max ) {
 127  14
                max = val;
 128   
             }
 129   
          }
 130   
       }
 131   
 
 132  1
       if ( max == -Double.MAX_VALUE ) {
 133  0
          return Double.NaN;
 134   
       }
 135   
 
 136  1
       return max; // might be NaN if all values are missing
 137   
 
 138   
    }
 139   
 
 140   
    /**
 141   
     * Normalize a matrix in place to be a transition matrix. Assumes that values operate such that small values like p
 142   
     * values represent closer distances, and the values are probabilities.
 143   
     * <p>
 144   
     * Each point is first transformed via v' = exp(-v/sigma). Then the values for each node's edges are adjusted to sum
 145   
     * to 1.
 146   
     * 
 147   
     * @param matrixToNormalize
 148   
     * @param sigma a scaling factor for the input values.
 149   
     */
 150  1
    public static void rbfNormalize(
 151   
          AbstractNamedDoubleMatrix matrixToNormalize, final double sigma ) {
 152   
 
 153   
       // define the function we will use.
 154  1
       DoubleFunction f = new DoubleFunction() {
 155  360
          public double apply( double value ) {
 156  360
             return Math.exp( -value / sigma );
 157   
          }
 158   
       };
 159   
 
 160  1
       for ( int j = 0; j < matrixToNormalize.rows(); j++ ) { // do each row in turn ...
 161   
 
 162  30
          DoubleMatrix1D row = matrixToNormalize.viewRow( j );
 163  30
          row.assign( f );
 164  30
          double sum = row.zSum();
 165  30
          row.assign( Functions.div( sum ) );
 166   
 
 167   
       }
 168   
    }
 169   
 
 170   
    /**
 171   
     * Normalize a count matrix in place to be a transition matrix. Assumes that the values are defined as "bigger is better"
 172   
     * 
 173   
     * @param matrixToNormalize
 174   
     * @param sigma
 175   
     */
 176  0
    public static void countsNormalize (
 177   
          AbstractNamedDoubleMatrix matrixToNormalize, final double sigma ) {
 178   
 
 179  0
       final double min = MatrixStats.min( matrixToNormalize );
 180  0
       DoubleFunction f = new DoubleFunction() {
 181  0
          public double apply( double value ) {
 182  0
             return value - min + 1;
 183   
          }
 184   
       };
 185   
 
 186  0
       for ( int j = 0; j < matrixToNormalize.rows(); j++ ) { // do each row in turn ...
 187  0
          DoubleMatrix1D row = matrixToNormalize.viewRow( j );
 188  0
          row.assign( f );
 189  0
          double sum = row.zSum();
 190  0
          row.assign( Functions.div( sum ) );
 191   
       }
 192   
    }
 193   
 
 194   
 }