Debian Codegraph @

src/dr/evomodel/continuous/DiffusionRateCovarianceStatistic.java

raw symbols SCIP
1 /*
2 * DiffusionRateCovarianceStatistic.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.evomodel.continuous;
27
28 import dr.evolution.tree.MutableTreeModel;
29 import dr.evolution.tree.NodeRef;
30 import dr.evolution.tree.Tree;
31 import dr.evomodel.tree.TreeModel;
32 import dr.evomodel.tree.TreeStatistic;
33 import dr.geo.math.SphericalPolarCoordinates;
34 import dr.inference.model.Statistic;
35 import dr.stats.DiscreteStatistics;
36 import dr.xml.*;
37
38 import java.util.ArrayList;
39 import java.util.List;
40
41 /**
42 * @author Marc Suchard
43 * @author Philippe Lemey
44 * @author Andrew Rambaut
45 */
46 public class DiffusionRateCovarianceStatistic extends Statistic.Abstract {
47
48 public static final String DIFFUSION_RATE_COVARIANCE_STATISTIC = "diffusionRateCovarianceStatistic";
49 public static final String TREE_DISPERSION_COVARIANCE_STATISTIC = "treeDispersionCovarianceStatistic";
50 public static final String BOOLEAN_DIS_OPTION = "greatCircleDistance";
51 public static final String BOOLEAN_DC_OPTION = "diffusionCoefficient";
52
53 public DiffusionRateCovarianceStatistic(String name, TreeModel tree, List<AbstractMultivariateTraitLikelihood> traitLikelihoods,
54 boolean option, boolean diffusionCoefficient) {
55 super(name);
56 this.traitLikelihoods = traitLikelihoods;
57 useGreatCircleDistances = option;
58 this.diffusionCoefficient = diffusionCoefficient;
59 int n = tree.getExternalNodeCount();
60 childRate = new double[2 * n - 4];
61 parentRate = new double[childRate.length];
62 }
63
64 public int getDimension() {
65 return 1;
66 }
67
68 /**
69 * @return whatever Philippe wants
70 */
71 public double getStatisticValue(int dim) {
72
73 String traitName = traitLikelihoods.get(0).getTraitName();
74
75 for (AbstractMultivariateTraitLikelihood traitLikelihood : traitLikelihoods) {
76 MutableTreeModel tree = traitLikelihood.getTreeModel();
77
78 int counter = 0;
79 int index = 0;
80
81 for (int i = 0; i < tree.getNodeCount(); i++) {
82
83 NodeRef child = tree.getNode(i);
84 NodeRef parent = tree.getParent(child);
85 if (parent != null & !tree.isRoot(parent)) {
86 double[] childTrait = traitLikelihood.getTraitForNode(tree, child, traitName);
87 double[] parentTrait = traitLikelihood.getTraitForNode(tree, parent, traitName);
88 double childTime = tree.getBranchLength(child);
89 double parentTime = tree.getBranchLength(parent);
90
91 NodeRef grandParent = tree.getParent(parent);
92 double[] grandParentTrait = traitLikelihood.getTraitForNode(tree, grandParent, traitName);
93
94 if (useGreatCircleDistances && (childTrait.length == 2)) { // Great Circle distance
95 SphericalPolarCoordinates coord1 = new SphericalPolarCoordinates(childTrait[0], childTrait[1]);
96 SphericalPolarCoordinates coord2 = new SphericalPolarCoordinates(parentTrait[0], parentTrait[1]);
97 double childDistance = coord1.distance(coord2);
98 SphericalPolarCoordinates coord3 = new SphericalPolarCoordinates(grandParentTrait[0], grandParentTrait[1]);
99 double parentDistance = coord2.distance(coord3);
100
101 if (!diffusionCoefficient){
102 childRate[index] = childDistance/childTime;
103 parentRate[index] = parentDistance/parentTime;
104 } else {
105 childRate[index] = Math.pow(childDistance,2)/(4*childTime);
106 parentRate[index] = Math.pow(parentDistance,2)/(4*parentTime);
107 }
108
109 } else {
110 double childDistance = getNativeDistance(childTrait, parentTrait);
111 double parentDistance = getNativeDistance(parentTrait, grandParentTrait);
112
113 if (!diffusionCoefficient){
114 childRate[index] = childDistance/childTime;
115 parentRate[index] = parentDistance/parentTime;
116 } else {
117 childRate[index] = Math.pow(childDistance,2)/(4*childTime);
118 parentRate[index] = Math.pow(parentDistance,2)/(4*parentTime);
119 }
120
121 }
122 index += 1;
123 }
124
125 }
126 }
127
128 return DiscreteStatistics.covariance(childRate, parentRate);
129 }
130
131 // private double getNativeDistance(double[] location1, double[] location2) {
132 // return Math.sqrt(Math.pow((location2[0] - location1[0]), 2.0) + Math.pow((location2[1] - location1[1]), 2.0));
133 // }
134
135 private double getNativeDistance(double[] location1, double[] location2) {
136 int traitDimension = location1.length;
137 double sum = 0;
138 for (int i = 0; i < traitDimension; i++) {
139 sum += Math.pow((location2[i] - location1[i]),2);
140 }
141 return Math.sqrt(sum);
142 }
143
144 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
145
146 public String getParserName() {
147 return DIFFUSION_RATE_COVARIANCE_STATISTIC;
148 }
149
150 @Override
151 public String[] getParserNames() {
152 return new String[]{getParserName(), TREE_DISPERSION_COVARIANCE_STATISTIC};
153 }
154
155 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
156
157 String name = xo.getAttribute(NAME, xo.getId());
158 TreeModel tree = (TreeModel) xo.getChild(Tree.class);
159
160 boolean option = xo.getAttribute(BOOLEAN_DIS_OPTION, false); // Default value is false
161
162 boolean diffCoeff = xo.getAttribute(BOOLEAN_DC_OPTION, false); // Default value is false
163
164 List<AbstractMultivariateTraitLikelihood> traitLikelihoods = new ArrayList<AbstractMultivariateTraitLikelihood>();
165
166 for (int i = 0; i < xo.getChildCount(); i++) {
167 if (xo.getChild(i) instanceof AbstractMultivariateTraitLikelihood) {
168 traitLikelihoods.add((AbstractMultivariateTraitLikelihood) xo.getChild(i));
169 }
170 }
171
172 return new DiffusionRateCovarianceStatistic(name, tree, traitLikelihoods, option, diffCoeff);
173 }
174
175 //************************************************************************
176 // AbstractXMLObjectParser implementation
177 //************************************************************************
178
179 public String getParserDescription() {
180 return "A statistic that returns the average of the branch rates";
181 }
182
183 public Class getReturnType() {
184 return TreeStatistic.class;
185 }
186
187 public XMLSyntaxRule[] getSyntaxRules() {
188 return rules;
189 }
190
191 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
192 AttributeRule.newStringRule(NAME, true),
193 AttributeRule.newBooleanRule(BOOLEAN_DIS_OPTION, true),
194 AttributeRule.newBooleanRule(BOOLEAN_DC_OPTION, true),
195 new ElementRule(MutableTreeModel.class),
196 new ElementRule(AbstractMultivariateTraitLikelihood.class, 1, Integer.MAX_VALUE),
197 };
198 };
199
200 private boolean useGreatCircleDistances;
201 private List<AbstractMultivariateTraitLikelihood> traitLikelihoods;
202 private double[] childRate = null;
203 private double[] parentRate = null;
204 private boolean diffusionCoefficient;
205 }