Clover coverage report - baseCode - 0.2.5 Coverage timestamp: Tue Apr 12 2005 11:31:58 EDT FRAMES NO FRAMES |
file stats: | LOC: | 161 |  | Methods: | 5
| NCLOC: | 89 |  | 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
Distance.java | 0% | 0% | 0% | 0% |

1 |  |
     package baseCode.math;
2 |  |
3 |  |
     import cern.colt.list.DoubleArrayList;
4 |  |
     import cern.colt.list.IntArrayList;
5 |  |
6 |  |
     /**
7 |  |
      * Alternative distance and similarity metrics for vectors.
8 |  |
      * <p>
9 |  |
      * Copyright (c) 2004
10 |  |
      * </p>
11 |  |
      * <p>
12 |  |
      * Institution:: Columbia University
13 |  |
      * </p>
14 |  |
      *
15 |  |
      * @author Paul Pavlidis
16 |  |
      * @version $Id: Distance.java,v 1.7 2004/08/14 20:38:35 pavlidis Exp $
17 |  |
      */
18 |  |
     public class Distance {
19 |  |
20 |  |
        /**
21 |  |
         * Calculate the Manhattan distance between two vectors.
22 |  |
         *
23 |  |
         * @param x DoubleArrayList
24 |  |
         * @param y DoubleArrayList
25 |  |
         * @return Manhattan distance between x and y
26 |  |
         */
27 | 0 |
        public double manhattanDistance( DoubleArrayList x, DoubleArrayList y ) {
28 | 0 |
           int j;
29 | 0 |
           double sum = 0.0;
30 | 0 |
           int numused = 0;
31 |  |
32 | 0 |
           if ( x.size() != y.size() ) {
33 | 0 |
              throw new ArithmeticException();
34 |  |
           }
35 |  |
36 | 0 |
           int length = x.size();
37 | 0 |
           for ( j = 0; j < length; j++ ) {
38 | 0 |
              if ( !Double.isNaN( x.elements()[j] )
39 |  |
                    && !Double.isNaN( y.elements()[j] ) ) {
40 | 0 |
                 sum += Math.abs( x.elements()[j] - y.elements()[j] );
41 | 0 |
                 numused++;
42 |  |
              }
43 |  |
           }
44 | 0 |
           return sum;
45 |  |
        }
46 |  |
47 |  |
        /**
48 |  |
         * Calculate the Euclidean distance between two vectors.
49 |  |
         *
50 |  |
         * @param x DoubleArrayList
51 |  |
         * @param y DoubleArrayList
52 |  |
         * @return Euclidean distance between x and y
53 |  |
         */
54 | 0 |
        public double euclDistance( DoubleArrayList x, DoubleArrayList y ) {
55 | 0 |
           int j;
56 | 0 |
           double sum;
57 | 0 |
           int numused;
58 | 0 |
           sum = 0.0;
59 | 0 |
           numused = 0;
60 |  |
61 | 0 |
           if ( x.size() != y.size() ) {
62 | 0 |
              throw new ArithmeticException();
63 |  |
           }
64 |  |
65 | 0 |
           int length = x.size();
66 |  |
67 | 0 |
           for ( j = 0; j < length; j++ ) {
68 | 0 |
              if ( !Double.isNaN( x.elements()[j] )
69 |  |
                    && !Double.isNaN( y.elements()[j] ) ) {
70 | 0 |
                 sum += Math.pow( ( x.elements()[j] - y.elements()[j] ), 2 );
71 | 0 |
                 numused++;
72 |  |
              }
73 |  |
           }
74 | 0 |
           if ( sum == 0.0 ) {
75 | 0 |
              return 0.0;
76 |  |
           }
77 | 0 |
           return Math.sqrt( sum );
78 |  |
        }
79 |  |
80 |  |
        /**
81 |  |
         * Spearman Rank Correlation. This does the rank transformation of the data.
82 |  |
         *
83 |  |
         * @param x DoubleArrayList
84 |  |
         * @param y DoubleArrayList
85 |  |
         * @return Spearman's rank correlation between x and y.
86 |  |
         */
87 | 0 |
        public static double spearmanRankCorrelation( DoubleArrayList x,
88 |  |
              DoubleArrayList y ) {
89 | 0 |
           double sum = 0.0;
90 |  |
91 | 0 |
           if ( x.size() != y.size() ) {
92 | 0 |
              throw new ArithmeticException();
93 |  |
           }
94 |  |
95 | 0 |
           IntArrayList rx = Rank.rankTransform( x );
96 | 0 |
           IntArrayList ry = Rank.rankTransform( y );
97 |  |
98 | 0 |
           for ( int j = 0; j < x.size(); j++ ) {
99 | 0 |
              sum += ( rx.elements()[j] - ry.elements()[j]
100 |  |
                    * ( rx.elements()[j] - ry.elements()[j] ) );
101 |  |
           }
102 |  |
103 | 0 |
           return 1.0 - 6.0 * sum / ( Math.pow( x.size(), 3 ) - x.size() );
104 |  |
        }
105 |  |
106 |  |
        /**
107 |  |
         * Highly optimized implementation of the Pearson correlation. The inputs must be standardized - mean zero, variance
108 |  |
         * one, without any missing values.
109 |  |
         *
110 |  |
         * @param xe A standardized vector
111 |  |
         * @param ye A standardized vector
112 |  |
         * @return Pearson correlation coefficient.
113 |  |
         */
114 | 0 |
        public static double correlationOfStandardized( double[] xe, double[] ye ) {
115 | 0 |
           double sxy = 0.0;
116 | 0 |
           for ( int i = 0, n = xe.length; i < n; i++ ) {
117 | 0 |
              double xj = xe[i];
118 | 0 |
              double yj = ye[i];
119 | 0 |
              sxy += xj * yj;
120 |  |
           }
121 |  |
122 | 0 |
           return sxy / xe.length;
123 |  |
        }
124 |  |
125 |  |
        /**
126 |  |
         * Like correlationofNormedFast, but takes DoubleArrayLists as inputs, handles missing values correctly, and does
127 |  |
         * more error checking. Assumes the data has been converted to z scores already.
128 |  |
         *
129 |  |
         * @param x A standardized vector
130 |  |
         * @param y A standardized vector
131 |  |
         * @return The Pearson correlation between x and y.
132 |  |
         */
133 | 0 |
        public static double correlationOfStandardized( DoubleArrayList x,
134 |  |
              DoubleArrayList y ) {
135 |  |
136 | 0 |
           if ( x.size() != y.size() ) {
137 | 0 |
              throw new IllegalArgumentException( "Array lengths must be the same" );
138 |  |
           }
139 |  |
140 | 0 |
           double[] xe = x.elements();
141 | 0 |
           double[] ye = y.elements();
142 | 0 |
           double sxy = 0.0;
143 | 0 |
           int length = 0;
144 | 0 |
           for ( int i = 0, n = x.size(); i < n; i++ ) {
145 | 0 |
              double xj = xe[i];
146 | 0 |
              double yj = ye[i];
147 |  |
148 | 0 |
              if ( Double.isNaN( xj ) || Double.isNaN( yj ) ) {
149 | 0 |
                 continue;
150 |  |
              }
151 |  |
152 | 0 |
              sxy += xj * yj;
153 | 0 |
              length++;
154 |  |
           }
155 |  |
156 | 0 |
           if ( length == 0 ) {
157 | 0 |
              return -2.0; // flag of illegal value.
158 |  |
           }
159 | 0 |
           return sxy / length;
160 |  |
        }
161 |  |
     }