Debian Codegraph @

src/dr/evomodel/operators/NodeHeightScaleOperator.java

raw symbols SCIP
1 /*
2 * NodeHeightScaleOperator.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.operators;
27
28 import dr.evolution.tree.NodeRef;
29 import dr.evolution.tree.Tree;
30 import dr.evomodel.tree.TreeModel;
31 import dr.inference.model.Parameter;
32 import dr.inference.operators.*;
33 import dr.inferencexml.operators.ScaleOperatorParser;
34 import dr.math.MathUtils;
35 import dr.xml.*;
36
37 import java.util.*;
38
39 /**
40 * An operator for scaling
41 *
42 * @author Andrew Rambaut
43 */
44 // Cleaning out untouched stuff. Can be resurrected if needed
45 @Deprecated
46 public class NodeHeightScaleOperator extends AbstractAdaptableOperator {
47
48 public static final String NODE_HEIGHT_SCALE_OPERATOR = "nodeHeightScaleOperator";
49 public static final String SCALE_FACTOR = ScaleOperatorParser.SCALE_FACTOR;
50 public static final String SCALE_ALL = ScaleOperatorParser.SCALE_ALL;
51
52 private final TreeModel tree;
53 private final boolean scaleAll;
54 Set<Double> tipDates = new TreeSet<Double>();
55 private final Map<Double, Integer> intervals = new TreeMap<Double, Integer>();
56 private Parameter nodeHeightParameter;
57
58 public NodeHeightScaleOperator(TreeModel tree, double scale, boolean scaleAll, AdaptationMode mode) {
59
60 super(mode);
61
62 this.scaleFactor = scale;
63 this.scaleAll = scaleAll;
64 this.tree = tree;
65 // nodeHeightParameter = tree.get
66
67 for (int i = 0; i < tree.getExternalNodeCount(); i++) {
68 double h = tree.getNodeHeight(tree.getExternalNode(i));
69 tipDates.add(h);
70 }
71 }
72
73
74 /**
75 * scale the rates of a subtree and return the hastings ratio.
76 */
77 public final double doOperation() {
78
79 final double scale = (scaleFactor + (MathUtils.nextDouble() * ((1.0 / scaleFactor) - scaleFactor)));
80
81 List<NodeRef> listNode = new ArrayList<NodeRef>();
82
83 double logq = 0;
84 if (scaleAll) {
85
86 } else {
87 int r = MathUtils.nextInt(tree.getInternalNodeCount());
88 NodeRef node = tree.getNode(r);
89
90 double oldValue, newValue;
91
92 oldValue = tree.getNodeHeight(node);
93 newValue = oldValue * scale;
94
95 tree.setNodeHeight(node, newValue);
96
97 logq = getJacobian(node);
98 }
99
100 // According to the hastings ratio defined in the original scale Operator
101 logq = (listNode.size() - 2) * Math.log(scale);
102
103 return logq;
104 }
105
106 private double getJacobian(NodeRef node) {
107
108 intervals.clear();
109 for (Double date : tipDates) {
110 intervals.put(date, 0);
111 }
112
113 traverse(tree, node);
114
115 double logq = 0.0;
116 for (Double date : intervals.keySet()) {
117 double s = tree.getNodeHeight(tree.getRoot()) - date;
118 int k = intervals.get(date);
119
120 logq += logFactorial(k) - (double) k * Math.log(s);
121 }
122
123 return logq;
124 }
125
126 private double logFactorial(int n) {
127 if (n == 0) {
128 return 0;
129 }
130
131 double rValue = 0;
132
133 for (int i = n; i > 0; i--) {
134 rValue += Math.log(i);
135 }
136 return rValue;
137 }
138
139
140 private Double traverse(Tree tree, NodeRef node) {
141 Double date;
142 if (tree.isExternal(node)) {
143 date = tree.getNodeHeight(node);
144 if (!intervals.keySet().contains(date)) {
145 throw new RuntimeException("Tip date not found");
146 }
147
148 } else {
149 Double date1 = traverse(tree, tree.getChild(node, 0));
150 Double date2 = traverse(tree, tree.getChild(node, 1));
151 date = (date1 > date2 ? date1 : date2);
152 if (!tree.isRoot(node)) {
153 intervals.put(date, intervals.get(date) + 1);
154 }
155 }
156
157 return date;
158 }
159
160 /**
161 * This method should be overridden by operators that need to do something just before the return of doOperation.
162 *
163 * @param newValue the proposed parameter value
164 * @param oldValue the old parameter value
165 */
166 void cleanupOperation(double newValue, double oldValue) {
167 // DO NOTHING
168 }
169
170 //MCMCOperator INTERFACE
171 public final String getOperatorName() {
172 return "nodeHeightScale";
173 }
174
175 public double getAdaptableParameter() {
176 return Math.log(1.0 / scaleFactor - 1.0);
177 }
178
179 public void setAdaptableParameter(double value) {
180 scaleFactor = 1.0 / (Math.exp(value) + 1.0);
181 }
182
183 public double getRawParameter() {
184 return scaleFactor;
185 }
186
187 public double getScaleFactor() {
188 return scaleFactor;
189 }
190
191 @Override
192 public String getAdaptableParameterName() {
193 return "scaleFactor";
194 }
195
196
197 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
198
199 public String getParserName() {
200 return NODE_HEIGHT_SCALE_OPERATOR;
201 }
202
203 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
204
205 AdaptationMode mode = AdaptationMode.parseMode(xo);
206
207 final double weight = xo.getDoubleAttribute(WEIGHT);
208 final double scaleFactor = xo.getDoubleAttribute(SCALE_FACTOR);
209 final boolean scaleAll = xo.getBooleanAttribute(SCALE_ALL);
210
211 if (scaleFactor <= 0.0 || scaleFactor >= 1.0) {
212 throw new XMLParseException("scaleFactor must be between 0.0 and 1.0");
213 }
214
215 TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
216
217 NodeHeightScaleOperator operator = new NodeHeightScaleOperator(treeModel, scaleFactor, scaleAll, mode);
218 operator.setWeight(weight);
219 return operator;
220 }
221
222 //************************************************************************
223 // AbstractXMLObjectParser implementation
224 //************************************************************************
225
226 public String getParserDescription() {
227 return "This element returns a nodeHeightScale operator on a given tree.";
228 }
229
230 public Class getReturnType() {
231 return MCMCOperator.class;
232 }
233
234 public XMLSyntaxRule[] getSyntaxRules() {
235 return rules;
236 }
237
238 private final XMLSyntaxRule[] rules = {
239 AttributeRule.newDoubleRule(SCALE_FACTOR),
240 AttributeRule.newDoubleRule(WEIGHT),
241 AttributeRule.newBooleanRule(SCALE_ALL, true),
242 AttributeRule.newBooleanRule(AUTO_OPTIMIZE, true),
243 new ElementRule(TreeModel.class),
244 };
245
246 };
247
248 public String toString() {
249 return "nodeHeightScaleOperator(" + " [" + scaleFactor + ", " + (1.0 / scaleFactor) + "]";
250 }
251
252 //PRIVATE STUFF
253
254 private double scaleFactor = 0.5;
255 }