Debian Codegraph @

uk/ac/babraham/FastQC/Statistics/PearsonCorrelation.java

raw symbols SCIP
1 /**
2 * Copyright Copyright 2010-17 Simon Andrews
3 *
4 * This file is part of FastQC.
5 *
6 * FastQC is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * FastQC is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with FastQC; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 package uk.ac.babraham.FastQC.Statistics;
21
22
23 /**
24 * A Class to calculate the Pearson Correlation.
25 */
26 public class PearsonCorrelation {
27
28
29 /**
30 * Calculate correlation.
31 *
32 * @param data1 the first dataset
33 * @param data2 the second dataset
34 * @return the Pearson r-value
35 * @throws SeqMonkException if the two datasets don't have the same number of points in them.
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 * Calculate correlation.
50 *
51 * @param data1 the first dataset
52 * @param data2 the second dataset
53 * @return the Pearson r-value
54 * @throws SeqMonkException if the two datasets don't have the same number of points in them.
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 * Calculate correlation.
70 *
71 * @param data1 the first dataset
72 * @param data2 the second dataset
73 * @return the Pearson r-value
74 * @throws SeqMonkException if the two datasets don't have the same number of points in them.
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 }