Debian Codegraph @

src/dr/evolution/tree/LeastSquaresClockTree.java

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