| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
package uk.ac.babraham.FastQC.Statistics; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public class PearsonCorrelation { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public static float calculateCorrelation (long [] data1, long [] data2) { |
| 38 |
|
| 39 |
float [] d1 = new float[data1.length]; |
| 40 |
float [] d2 = new float[data2.length]; |
| 41 |
for (int i=0;i<data1.length;i++)d1[i] = data1[i]; |
| 42 |
for (int i=0;i<data2.length;i++)d2[i] = data2[i]; |
| 43 |
|
| 44 |
return calculateCorrelation(d1, d2); |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public static float calculateCorrelation (long [] data1, long [] data2, int offset) { |
| 57 |
|
| 58 |
float [] d1 = new float[data1.length-offset]; |
| 59 |
float [] d2 = new float[data2.length-offset]; |
| 60 |
for (int i=0;i<d1.length;i++)d1[i] = data1[i]; |
| 61 |
for (int i=0;i<d2.length;i++)d2[i] = data2[i+offset]; |
| 62 |
|
| 63 |
return calculateCorrelation(d1, d2); |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public static float calculateCorrelation (float [] data1, float [] data2) { |
| 77 |
|
| 78 |
if (data1.length != data2.length) { |
| 79 |
throw new IllegalArgumentException("Data sets must be the same length when calculating correlation"); |
| 80 |
} |
| 81 |
|
| 82 |
float sum12 = 0; |
| 83 |
float sum1 = 0; |
| 84 |
float sum2 = 0; |
| 85 |
float sum1square = 0; |
| 86 |
float sum2square =0; |
| 87 |
|
| 88 |
for (int i=0;i<data1.length;i++) { |
| 89 |
sum12 += data1[i]*data2[i]; |
| 90 |
sum1 += data1[i]; |
| 91 |
sum2 += data2[i]; |
| 92 |
sum1square += data1[i]*data1[i]; |
| 93 |
sum2square += data2[i]*data2[i]; |
| 94 |
} |
| 95 |
|
| 96 |
float top = sum12 - ((sum1*sum2)/data1.length); |
| 97 |
float bottomRight = sum2square - ((sum2*sum2)/data1.length); |
| 98 |
float bottomLeft = sum1square - ((sum1*sum1)/data1.length); |
| 99 |
float bottom = (float)Math.sqrt(bottomLeft * bottomRight); |
| 100 |
|
| 101 |
|
| 102 |
return top/bottom; |
| 103 |
} |
| 104 |
|
| 105 |
} |