Debian Codegraph @

src/dr/evolution/tree/RateSmoothingTree.java

raw symbols SCIP
1 /*
2 * RateSmoothingTree.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.evolution.tree;
27
28 import dr.math.ConjugateDirectionSearch;
29 import dr.math.MultivariateFunction;
30 import dr.math.MultivariateMinimum;
31
32 /**
33 * An extension of simple tree that provides non-parametric rate smoothed
34 * node heights in the manner described by Mike Sanderson. This takes a
35 * tree with branch lengths as data and uses multidimensional optimization
36 * to find the set of node heights that requires the smallest differences
37 * in rates across nodes.
38 *
39 * @version $Id: RateSmoothingTree.java,v 1.8 2005/05/24 20:25:56 rambaut Exp $
40 *
41 * @author Andrew Rambaut
42 * @author Alexei Drummond
43 */
44 public class RateSmoothingTree extends SimpleTree {
45
46 /**
47 * constructor
48 */
49 public RateSmoothingTree(Tree sourceTree) {
50
51 super(sourceTree);
52 this.sourceTree = sourceTree;
53 this.mu = 1.0;
54 this.optimizeMu = false;
55 }
56
57 /**
58 * constructor with a specific mutation rate
59 */
60 public RateSmoothingTree(Tree sourceTree, double mu) {
61
62 this.sourceTree = sourceTree;
63 this.mu = mu;
64 this.optimizeMu = false;
65 }
66
67 public double getMu() { return mu; }
68
69 public void smoothRates() {
70 nodeCount = getInternalNodeCount();
71 int argumentCount = nodeCount;
72
73 if (optimizeMu) {
74 argumentCount++;
75 muIndex = nodeCount;
76 }
77
78 MultivariateMinimum optimizer = new ConjugateDirectionSearch();
79
80 nodeValues = new double[nodeCount];
81
82 double[] xvec = new double[argumentCount];
83 for (int i = 0; i < nodeCount; i++) {
84 xvec[i] = 1.0;
85 }
86 if (optimizeMu) {
87 xvec[muIndex] = mu;
88 }
89
90 optimizer.optimize(nonParametricRateSmoothing, xvec, 1E-8, 1E-8);
91 }
92
93 public double getSumOfRates() {
94
95 double[] score = new double[] { 0.0 };
96
97 NodeRef root = getRoot();
98
99 if (getChildCount(root) != 2) {
100 throw new IllegalArgumentException("The tree must have a bifurcating root node");
101 }
102
103 sumDist = 0;
104 sumTime = 0;
105
106 double rate1 = sumScoreAtNode(getChild(root, 0), score);
107 double rate2 = sumScoreAtNode(getChild(root, 1), score);
108
109 mu = sumDist / sumTime;
110
111 double diff = rate2 - rate1;
112 score[0] += diff * diff;
113
114 return score[0];
115 }
116
117 //
118 // Private stuff
119 //
120
121 private double sumScoreAtNode(NodeRef node, double[] score) {
122
123 double rate0 = getRateAtNode(node);
124
125 if (!isExternal(node)) {
126
127 for (int i = 0; i < getChildCount(node); i++) {
128 double rate1 = sumScoreAtNode(getChild(node, i), score);
129 double diff = rate0 - rate1;
130 score[0] += diff * diff;
131 }
132 }
133
134 return rate0;
135 }
136
137 /**
138 * returns the rate on the branch to the node above.
139 */
140 private double getRateAtNode(NodeRef node) {
141 double time = getNodeHeight(getParent(node)) - getNodeHeight(node);
142 double dist = sourceTree.getBranchLength(sourceTree.getNode(node.getNumber()));
143
144 double rate;
145
146 if (time == 0.0) {
147 if (dist == 0.0) {
148 rate = 1.0;
149 } else {
150 rate = Double.MIN_VALUE;
151 }
152 } else {
153 rate = (dist / time);
154 }
155 sumDist += dist;
156 sumTime += time;
157
158 setNodeRate(node, rate);
159 return rate;
160 }
161
162
163 private double setNodeHeightsFromValues(NodeRef node) {
164
165 if (!isExternal(node)) {
166 double maxHeight = setNodeHeightsFromValues(getChild(node, 0));
167
168 for (int i = 1; i < getChildCount(node); i++) {
169 double height = setNodeHeightsFromValues(getChild(node, i));
170 if (height > maxHeight) maxHeight = height;
171 }
172
173 setNodeHeight(node, maxHeight + nodeValues[node.getNumber() - getExternalNodeCount()]);
174 }
175
176 return getNodeHeight(node);
177 }
178
179 private MultivariateFunction nonParametricRateSmoothing = new MultivariateFunction() {
180
181 public double evaluate(double[] argument) {
182 for (int i = 0; i < getInternalNodeCount(); i++) {
183 nodeValues[i] = argument[i];
184 }
185
186 setNodeHeightsFromValues(getRoot());
187
188 if (optimizeMu) {
189 mu = argument[muIndex];
190 }
191
192 double score = getSumOfRates();
193 return score;
194 }
195
196 public int getNumArguments() {
197 if (optimizeMu) {
198 return getInternalNodeCount() + 1;
199 } else {
200 return getInternalNodeCount();
201 }
202 }
203
204 public double getLowerBound(int n) {
205
206 if (optimizeMu && n == muIndex) {
207 return Double.MIN_VALUE;
208 } else {
209 return 0.0;
210 }
211 }
212
213 public double getUpperBound(int n) {
214
215 if (optimizeMu && n == muIndex) {
216 return Double.MAX_VALUE;
217 } else {
218 return Double.MAX_VALUE;
219 }
220 }
221 };
222
223 private int nodeCount;
224 private double[] nodeValues;
225 private Tree sourceTree;
226 private double mu, sumDist, sumTime;
227 private boolean optimizeMu;
228 private int muIndex;
229 }