Clover coverage report - baseCode - 0.2.5
Coverage timestamp: Tue Apr 12 2005 11:31:58 EDT
file stats: LOC: 234   Methods: 11
NCLOC: 113   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
Stats.java 86.1% 94.1% 90.9% 91.3%
coverage coverage
 1   
 package baseCode.math;
 2   
 
 3   
 import cern.colt.list.DoubleArrayList;
 4   
 import cern.jet.stat.Descriptive;
 5   
 
 6   
 /**
 7   
  * Miscellaneous functions used for statistical analysis. Some are optimized or specialized versions of methods that can
 8   
  * be found elsewhere.
 9   
  * 
 10   
  * @see <a href="http://hoschek.home.cern.ch/hoschek/colt/V1.0.3/doc/cern/jet/math/package-summary.html">cern.jet.math
 11   
  *      </a>
 12   
  * @see <a href="http://hoschek.home.cern.ch/hoschek/colt/V1.0.3/doc/cern/jet/stat/package-summary.html">cern.jet.stat
 13   
  *      </a>
 14   
  *      <p>
 15   
  *      Copyright (c) 2004
 16   
  *      </p>
 17   
  *      <p>
 18   
  *      Columbia University
 19   
  *      </p>
 20   
  * @author Paul Pavlidis
 21   
  * @version $Id: Stats.java,v 1.10 2004/07/27 03:18:58 pavlidis Exp $
 22   
  */
 23   
 public class Stats {
 24   
 
 25  0
    private Stats() { /* block instantiation */
 26   
    };
 27   
 
 28   
    /**
 29   
     * Test whether a value is a valid fractional or probability value.
 30   
     * 
 31   
     * @param value
 32   
     * @return true if the value is in the interval 0 to 1.
 33   
     */
 34  23
    public static boolean isValidFraction( double value ) {
 35  23
       if ( value > 1.0 || value < 0.0 ) {
 36  2
          return false;
 37   
       }
 38  21
       return true;
 39   
    }
 40   
 
 41   
    /**
 42   
     * Compute the coefficient of variation of an array (standard deviation / mean)
 43   
     * 
 44   
     * @param data DoubleArrayList
 45   
     * @return the cv
 46   
     * @todo offer a regularized version of this function.
 47   
     */
 48  1
    public static double cv( DoubleArrayList data ) {
 49  1
       double mean = DescriptiveWithMissing.mean( data );
 50  1
       return mean
 51   
             / Math.sqrt( DescriptiveWithMissing.sampleVariance( data, mean ) );
 52   
    }
 53   
 
 54   
    /**
 55   
     * Convert an array into a cumulative array. Summing is from the left hand side. Use this to make CDFs where the
 56   
     * concern is the left tail.
 57   
     * 
 58   
     * @param x DoubleArrayList
 59   
     * @return cern.colt.list.DoubleArrayList
 60   
     */
 61  1
    public static DoubleArrayList cumulate( DoubleArrayList x ) {
 62  1
       if ( x.size() == 0 ) {
 63  0
          return new DoubleArrayList( 0 );
 64   
       }
 65   
 
 66  1
       DoubleArrayList r = new DoubleArrayList();
 67   
 
 68  1
       double sum = 0.0;
 69  1
       for ( int i = 0; i < x.size(); i++ ) {
 70  5
          sum += x.get( i );
 71  5
          r.add( sum );
 72   
       }
 73  1
       return r;
 74   
    }
 75   
 
 76   
    /**
 77   
     * Convert an array into a cumulative array. Summing is from the right hand side. This is useful for creating
 78   
     * upper-tail cumulative density histograms from count histograms, where the upper tail is expected to have very
 79   
     * small numbers that could be lost to rounding.
 80   
     * 
 81   
     * @param x the array of data to be cumulated.
 82   
     * @return cern.colt.list.DoubleArrayList
 83   
     */
 84  2
    public static DoubleArrayList cumulateRight( DoubleArrayList x ) {
 85  2
       if ( x.size() == 0 ) {
 86  0
          return new DoubleArrayList( 0 );
 87   
       }
 88   
 
 89  2
       DoubleArrayList r = new DoubleArrayList( new double[x.size()] );
 90   
 
 91  2
       double sum = 0.0;
 92  2
       for ( int i = x.size() - 1; i >= 0; i-- ) {
 93  10
          sum += x.get( i );
 94  10
          r.set( i, sum );
 95   
       }
 96  2
       return r;
 97   
    }
 98   
 
 99   
    /**
 100   
     * Convert an array into a cumulative density function (CDF). This assumes that the input contains counts
 101   
     * representing the distribution in question.
 102   
     * 
 103   
     * @param x The input of counts (i.e. a histogram).
 104   
     * @return DoubleArrayList the CDF.
 105   
     */
 106  1
    public static DoubleArrayList cdf( DoubleArrayList x ) {
 107  1
       return cumulateRight( normalize( x ) );
 108   
    }
 109   
 
 110   
    /**
 111   
     * Divide the elements of an array by a given factor.
 112   
     * 
 113   
     * @param x Input array.
 114   
     * @param normfactor double
 115   
     * @return Normalized array.
 116   
     */
 117  3
    public static DoubleArrayList normalize( DoubleArrayList x, double normfactor ) {
 118  3
       if ( x.size() == 0 ) {
 119  0
          return new DoubleArrayList( 0 );
 120   
       }
 121   
 
 122  3
       DoubleArrayList r = new DoubleArrayList();
 123   
 
 124  3
       for ( int i = 0; i < x.size(); i++ ) {
 125  15
          r.add( x.get( i ) / normfactor );
 126   
       }
 127  3
       return r;
 128   
 
 129   
    }
 130   
 
 131   
    /**
 132   
     * Adjust the elements of an array so they total to 1.0.
 133   
     * 
 134   
     * @param x Input array.
 135   
     * @return Normalized array.
 136   
     */
 137  2
    public static DoubleArrayList normalize( DoubleArrayList x ) {
 138  2
       return normalize( x, Descriptive.sum( x ) );
 139   
    }
 140   
 
 141   
    /**
 142   
     * calculate the mean of the values above (NOT greater or equal to) a particular index rank of an array. Quantile
 143   
     * must be a value from 0 to 100.
 144   
     * 
 145   
     * @see DescriptiveWithMissing#meanAboveQuantile
 146   
     * @param index the rank of the value we wish to average above.
 147   
     * @param array Array for which we want to get the quantile.
 148   
     * @param effectiveSize The size of the array, not including NaNs.
 149   
     * @return double
 150   
     */
 151  1
    public static double meanAboveQuantile( int index, double[] array,
 152   
          int effectiveSize ) {
 153   
 
 154  1
       double[] temp = new double[effectiveSize];
 155  1
       double median;
 156  1
       double returnvalue = 0.0;
 157  1
       int k = 0;
 158   
 
 159  1
       temp = array;
 160  1
       median = quantile( index, array, effectiveSize );
 161   
 
 162  1
       for ( int i = 0; i < effectiveSize; i++ ) {
 163  50
          if ( temp[i] > median ) {
 164  24
             returnvalue += temp[i];
 165  24
             k++;
 166   
          }
 167   
       }
 168  1
       return ( returnvalue / k );
 169   
    }
 170   
 
 171   
    /**
 172   
     * Compute the range of an array.
 173   
     * 
 174   
     * @param data DoubleArrayList
 175   
     * @return double
 176   
     */
 177  1
    public static double range( DoubleArrayList data ) {
 178  1
       return Descriptive.max( data ) - Descriptive.min( data );
 179   
    }
 180   
 
 181   
    /**
 182   
     * Given a double array, calculate the quantile requested. Note that no interpolation is done.
 183   
     * 
 184   
     * @see DescriptiveWithMissing#quantile
 185   
     * @param index - the rank of the value we wish to get. Thus if we have 200 items in the array, and want the median,
 186   
     *        we should enter 100.
 187   
     * @param values double[] - array of data we want quantile of
 188   
     * @param effectiveSize int the effective size of the array
 189   
     * @return double the value at the requested quantile
 190   
     */
 191  23
    public static double quantile( int index, double[] values, int effectiveSize ) {
 192  23
       double pivot = -1.0;
 193  23
       if ( index == 0 ) {
 194  3
          double ans = values[0];
 195  3
          for ( int i = 1; i < effectiveSize; i++ ) {
 196  3
             if ( ans > values[i] ) {
 197  1
                ans = values[i];
 198   
             }
 199   
          }
 200  3
          return ans;
 201   
       }
 202   
 
 203  20
       double[] temp = new double[effectiveSize];
 204   
 
 205  20
       for ( int i = 0; i < effectiveSize; i++ ) {
 206  456
          temp[i] = values[i];
 207   
       }
 208   
 
 209  20
       pivot = temp[0];
 210   
 
 211  20
       double[] smaller = new double[effectiveSize];
 212  20
       double[] bigger = new double[effectiveSize];
 213  20
       int itrSm = 0;
 214  20
       int itrBg = 0;
 215  20
       for ( int i = 1; i < effectiveSize; i++ ) {
 216  436
          if ( temp[i] <= pivot ) {
 217  313
             smaller[itrSm] = temp[i];
 218  313
             itrSm++;
 219  123
          } else if ( temp[i] > pivot ) {
 220  123
             bigger[itrBg] = temp[i];
 221  123
             itrBg++;
 222   
          }
 223   
       }
 224  20
       if ( itrSm > index ) { // quantile must be in the 'smaller' array
 225  11
          return quantile( index, smaller, itrSm );
 226  9
       } else if ( itrSm < index ) { // quantile is in the 'bigger' array
 227  9
          return quantile( index - itrSm - 1, bigger, itrBg );
 228   
       } else {
 229  0
          return pivot;
 230   
       }
 231   
 
 232   
    }
 233   
 
 234   
 }