SpearmanMetrics extends PearsonMetrics { protected double spearman( double[] vectorA, double[] vectorB, boolean[] usedA, boolean[] usedB, int i, int j ) { int numused = 0; for ( int k = 0; k < vectorA.length; k++ ) { if ( usedA[k] && usedB[k] ) { numused++; } } if ( numused < minNumUsed ) { this.setCorrel( i, j, Double.NaN, 0 ); return Double.NaN; } double[] xjc; double[] yjc; if ( numused == vectorA.length ) { xjc = vectorA; yjc = vectorB; } else { xjc = new double[numused]; yjc = new double[numused]; int v = 0; for ( int k = 0; k < vectorA.length; k++ ) { if ( usedA[k] && usedB[k] ) { xjc[v] = vectorA[k]; yjc[v] = vectorB[k]; v++; } } xjc = Rank.rankTransform( new DoubleArrayList( xjc ) ).elements(); yjc = Rank.rankTransform( new DoubleArrayList( yjc ) ).elements(); } double correl; double sxy = 0.0; double sxx = 0.0; double syy = 0.0; double sx = 0.0; double sy = 0.0; numused = 0; for ( int k = 0; k < xjc.length; k++ ) { double xj = xjc[k]; double yj = yjc[k]; sx += xj; sy += yj; sxy += xj * yj; sxx += xj * xj; syy += yj * yj; numused++; } double denom = this.correlationNorm( numused, sxx, sx, syy, sy ); if ( denom <= 0.0 ) { this.setCorrel( i, j, 0.0, numused ); return 0.0; } correl = ( sxy - sx * sy / numused ) / Math.sqrt( denom ); assert correl < 1.0001 && correl > -1.0001; if ( correl < -1.0 ) correl = -1.0; else if ( correl > 1.0 ) correl = 1.0; this.setCorrel( i, j, correl, numused ); return correl; } SpearmanMetrics( ExpressionDataDoubleMatrix dataMatrix );  SpearmanMetrics( ExpressionDataDoubleMatrix dataMatrix, double tmts ); protected  SpearmanMetrics( int size ); @Override void calculateMetrics(); @Override QuantitationType getMetricType(); @SuppressWarnings("LoopStatementThatDoesntLoop") // Simplifying can have side effects @Override double correctedPvalue( int i, int j, double correl, int numused );  }
@Test public void testCorrel() { double[] a = new double[] { 49.0, 43.0, 310.0, 20.0, 20.0, 688.0, 498.0, 533.0, 723.0, 1409.0, 279.0 }; double[] b = new double[] { 1545.0, 1287.0, 2072.0, 1113.0, 676.0, 2648.0, 2478.0, 2574.0, 3554.0, 5155.0, 1624.0 }; boolean[] usedA = new boolean[] { true, true, true, true, true, true, true, true, true, true, true }; boolean[] usedB = new boolean[] { true, true, true, true, true, true, true, true, true, true, true }; assertEquals( a.length, usedA.length ); assertEquals( b.length, usedB.length ); DoubleArrayList ranksIA = Rank.rankTransform( new DoubleArrayList( a ) ); DoubleArrayList ranksIB = Rank.rankTransform( new DoubleArrayList( b ) ); SpearmanMetrics test = new SpearmanMetrics( 10 ); double actualValue = test.spearman( ranksIA.elements(), ranksIB.elements(), usedA, usedB, 0, 1 ); double expectedValue = 0.9977247; assertEquals( expectedValue, actualValue, 0.0001 ); }
@Test public void testCorrelB() { boolean[] usedA = new boolean[] { true, true, true, true, true, true, true, true, true }; boolean[] usedB = new boolean[] { true, true, true, true, true, true, true, true, true }; double[] a = new double[] { 400, 310, 20, 20, 688, 498, 533, 1409, 1500 }; double[] b = new double[] { 1545, 2072, 1113, 676, 2648, 2478, 2574, 5155, 1624 }; assertEquals( a.length, b.length ); assertEquals( a.length, usedA.length ); assertEquals( b.length, usedB.length ); DoubleArrayList ranksIA = Rank.rankTransform( new DoubleArrayList( a ) ); DoubleArrayList ranksIB = Rank.rankTransform( new DoubleArrayList( b ) ); SpearmanMetrics test = new SpearmanMetrics( 10 ); double actualValue = test.spearman( ranksIA.elements(), ranksIB.elements(), usedA, usedB, 0, 1 ); double expectedValue = 0.7113033; assertEquals( expectedValue, actualValue, 0.0001 ); }
@Test public void testCorrelFromRanks() { double[] a = new double[] { 4.0, 3.0, 6.0, 1.5, 1.5, 9.0, 7.0, 8.0, 10.0, 11.0, 5.0 }; double[] b = new double[] { 4.0, 3.0, 6.0, 2, 1, 9, 7, 8, 10, 11, 5 }; boolean[] usedA = new boolean[] { true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true }; boolean[] usedB = new boolean[] { true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true }; SpearmanMetrics test = new SpearmanMetrics( 10 ); double actualValue = test.spearman( a, b, usedA, usedB, 0, 1 ); double expectedValue = 0.9977247; assertEquals( expectedValue, actualValue, 0.00001 ); }
@Test public void testCorrelWithMissing() { double[] a = new double[] { 400, 43, 310, 20, 20, 688, 498, 533, 723, 1409, 1500 }; double[] b = new double[] { 1545, 1287, 2072, 1113, 676, 2648, 2478, 2574, 3554, 5155, 1624 }; boolean[] usedA = new boolean[] { true, false, true, true, true, true, true, true, false, true, true }; boolean[] usedB = new boolean[] { true, true, true, true, true, true, true, true, false, true, true }; assertEquals( a.length, b.length ); assertEquals( a.length, usedA.length ); assertEquals( b.length, usedB.length ); DoubleArrayList ranksIA = Rank.rankTransform( new DoubleArrayList( a ) ); DoubleArrayList ranksIB = Rank.rankTransform( new DoubleArrayList( b ) ); SpearmanMetrics test = new SpearmanMetrics( 10 ); double actualValue = test.spearman( ranksIA.elements(), ranksIB.elements(), usedA, usedB, 0, 1 ); double expectedValue = 0.7113033; assertEquals( expectedValue, actualValue, 0.0001 ); }