Debian Codegraph @

src/dr/inference/model/PearsonCorrelation.java

raw symbols SCIP
1 /*
2 * PearsonCorrelation.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.inference.model;
27
28 import dr.stats.DiscreteStatistics;
29
30 /**
31 * @author Simon Greenhill
32 * @author Alexei Drummond
33 */
34 public class PearsonCorrelation extends Statistic.Abstract {
35
36 Parameter X, Y;
37 boolean log = false;
38
39
40 public PearsonCorrelation(Parameter X, Parameter Y, boolean log) {
41
42 if (X.getDimension() != Y.getDimension()) throw new IllegalArgumentException();
43 this.X = X;
44 this.Y = Y;
45 this.log = log;
46 }
47
48 public int getDimension() {
49 return 1;
50 }
51
52 public double getStatisticValue(int dim) {
53
54 double[] xvalues = X.getParameterValues();
55 double[] yvalues = Y.getParameterValues();
56
57 if (log) {
58 for (int i = 0; i < xvalues.length; i++) {
59 xvalues[i] = Math.log(xvalues[i]);
60 yvalues[i] = Math.log(yvalues[i]);
61 }
62 }
63
64 double meanX = DiscreteStatistics.mean(xvalues);
65 double meanY = DiscreteStatistics.mean(yvalues);
66 double stdevX = DiscreteStatistics.stdev(xvalues);
67 double stdevY = DiscreteStatistics.stdev(yvalues);
68
69 double corr = 0;
70 for (int i = 0; i < xvalues.length; i++) {
71 double deviateX = xvalues[i] - meanX;
72 double deviateY = yvalues[i] - meanY;
73 corr += deviateX*deviateY;
74 }
75 corr /= X.getDimension();
76
77 corr /= (stdevX*stdevY);
78 return corr;
79
80
81 }
82 }