Debian Codegraph @

src/dr/evomodel/operators/FunkyPriorMixerOperator.java

raw symbols SCIP
1 /*
2 * FunkyPriorMixerOperator.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.evomodelxml.operators.FunkyPriorMixerOperatorParser;
32 import dr.inference.model.Bounds;
33 import dr.inference.model.Parameter;
34 import dr.inference.operators.*;
35 import dr.inferencexml.operators.RandomWalkOperatorParser;
36 import dr.math.MathUtils;
37
38 /**
39 * @author Marc A. Suchard
40 * @author John J. Welch
41 */
42 // Cleaning out untouched stuff. Can be resurrected if needed
43 @Deprecated
44 public class FunkyPriorMixerOperator extends
45 SimpleMCMCOperator {
46 // AbstractAdaptableOperator {
47 // implements GibbsOperator {
48
49 public FunkyPriorMixerOperator(TreeModel treeModel, Parameter parameter, double windowSize, RandomWalkOperator.BoundaryCondition bc,
50 double weight, AdaptationMode mode) {
51
52 // super(mode);
53 this.treeModel = treeModel;
54 // this.parameter = treeModel.getRootHeightParameter();
55 // this.parameter = null;
56 this.parameter = parameter;
57 this.windowSize = windowSize;
58 this.condition = bc;
59
60 setWeight(weight);
61 }
62
63 public final double doOperation() {
64
65 // // a random dimension to perturb
66 // int index;
67 // index = MathUtils.nextInt(parameter.getDimension());
68 //
69 // double newValue = parameter.getParameterValue(index) + ((2.0 * MathUtils.nextDouble() - 1.0) * windowSize);
70 //
71 //// treeModel.setNodeHeight(node, 0.0);
72 //// treeModel.getNodeHeightLower(node);
73 //
74 // double lower = parameter.getBounds().getLowerLimit(index);
75 // double upper = parameter.getBounds().getUpperLimit(index);
76 //
77 // while (newValue < lower || newValue > upper) {
78 // if (newValue < lower) {
79 // if (condition == RandomWalkOperator.BoundaryCondition.reflecting) {
80 // newValue = lower + (lower - newValue);
81 // } else {
82 // throw new OperatorFailedException("proposed value outside boundaries");
83 // }
84 //
85 // }
86 // if (newValue > upper) {
87 // if (condition == RandomWalkOperator.BoundaryCondition.reflecting) {
88 // newValue = upper - (newValue - upper);
89 // } else {
90 // throw new OperatorFailedException("proposed value outside boundaries");
91 // }
92 //
93 // }
94 // }
95 //
96 // parameter.setParameterValue(index, newValue);
97
98 // double[] minNodeHeights = new double[treeModel.getNodeCount()];
99 // recursivelyFindNodeMinHeights(treeModel, treeModel.getRoot(), minNodeHeights);
100 //
101 // logForwardDensity = new Double(0.0);
102 // logBackwardDensity = new Double(0.0);
103 //
104 // try {
105 //
106 // recursivelyDrawNodeHeights(treeModel, treeModel.getRoot(), 0.0, 0.0, minNodeHeights); //,
107 // //logForwardDensity, logBackwardDensity);
108 //
109 // } catch (Exception e) {
110 // System.err.println("Got exception: " + e.getMessage());
111 // }
112
113
114 int iterations = 100;
115
116 for (int i = 0; i < iterations; i++) {
117
118 // NodeRef node = treeModel.getNode(MathUtils.nextInt(treeModel.getNodeCount()));
119 // if (node != treeModel.getRoot()) {
120 // treeModel.getN
121 // }
122
123 final int index = MathUtils.nextInt(parameter.getDimension());
124 final Bounds<Double> bounds = parameter.getBounds();
125 final double lower = bounds.getLowerLimit(index);
126 final double upper = bounds.getUpperLimit(index);
127 final double newValue = (MathUtils.nextDouble() * (upper - lower)) + lower;
128
129 // parameter.setParameterValueQuietly(index, newValue);
130 parameter.setParameterValue(index, newValue);
131 }
132 // ((Parameter.Default)parameter).fireParameterChangedEvent(-1, Parameter.ChangeType.VALUE_CHANGED);
133
134 // System.err.println("logFD = " + logForwardDensity);
135 // System.err.println("logBD = " + logBackwardDensity);
136
137 return
138 0.0;
139 // -1 *
140 // (logBackwardDensity - logForwardDensity);
141 }
142
143
144 private double recursivelyFindNodeMinHeights(Tree tree, NodeRef node, double[] minNodeHeights) {
145
146 // Post-order traversal
147
148 double minHeight;
149
150 if (tree.isExternal(node))
151 minHeight = tree.getNodeHeight(node);
152 else {
153 double minHeightChild0 = recursivelyFindNodeMinHeights(tree, tree.getChild(node, 0), minNodeHeights);
154 double minHeightChild1 = recursivelyFindNodeMinHeights(tree, tree.getChild(node, 1), minNodeHeights);
155 minHeight = (minHeightChild0 > minHeightChild1) ? minHeightChild0 : minHeightChild1;
156 }
157
158 minNodeHeights[node.getNumber()] = minHeight;
159 return minHeight;
160 }
161
162 private void recursivelyDrawNodeHeights(TreeModel tree, NodeRef node,
163 double oldParentHeight, double newParentHeight, double[] minNodeHeights) {//,
164 // Double logForwardDensity, Double logBackwardDensity) {
165 // Pre-order traversal
166
167 if (tree.isExternal(node))
168 return;
169
170 final double oldNodeHeight = tree.getNodeHeight(node);
171 double newNodeHeight = oldNodeHeight;
172
173 // System.err.println("old: " + oldNodeHeight);
174 //
175 if (!tree.isRoot(node)) {
176
177 double minHeight = minNodeHeights[node.getNumber()];
178
179 double oldDiff = oldParentHeight - minHeight;
180 double newDiff = newParentHeight - minHeight;
181
182 newNodeHeight = MathUtils.nextDouble() * newDiff + minHeight; // Currently uniform
183 logForwardDensity -= Math.log(newDiff);
184 logBackwardDensity -= Math.log(oldDiff);
185
186 // System.err.println("inner logFD = " + logForwardDensity);
187
188 tree.setNodeHeight(node, newNodeHeight);
189 }
190
191 // System.err.println("new: " + newNodeHeight + "\n");
192 //
193 recursivelyDrawNodeHeights(tree, tree.getChild(node, 0), oldNodeHeight, newNodeHeight, minNodeHeights); //,
194 // logForwardDensity, logBackwardDensity);
195 recursivelyDrawNodeHeights(tree, tree.getChild(node, 1), oldNodeHeight, newNodeHeight, minNodeHeights); //,
196 // logForwardDensity, logBackwardDensity);
197
198 }
199
200 //MCMCOperator INTERFACE
201
202 public final String getOperatorName() {
203 return FunkyPriorMixerOperatorParser.FUNKY_OPERATOR;
204 }
205
206 /* This is not an adaptable operator so these will not be called
207 public double getCoercableParameter() {
208 return Math.log(windowSize);
209 }
210
211 public void setCoercableParameter(double value) {
212 windowSize = Math.exp(value);
213 }
214 */
215
216 public double getRawParameter() {
217 return windowSize;
218 }
219
220 public double getTargetAcceptanceProbability() {
221 return 0.234;
222 }
223
224 public double getMinimumAcceptanceLevel() {
225 return 0.1;
226 }
227
228 public double getMaximumAcceptanceLevel() {
229 return 0.4;
230 }
231
232 public double getMinimumGoodAcceptanceLevel() {
233 return 0.20;
234 }
235
236 public double getMaximumGoodAcceptanceLevel() {
237 return 0.30;
238 }
239
240 public final String getPerformanceSuggestion() {
241
242 double prob = MCMCOperator.Utils.getAcceptanceProbability(this);
243 double targetProb = getTargetAcceptanceProbability();
244
245 // double ws = OperatorUtils.optimizeWindowSize(windowSize, parameter.getParameterValue(0) * 2.0, prob, targetProb);
246 double ws = 2;
247
248 if (prob < getMinimumGoodAcceptanceLevel()) {
249 return "Try decreasing windowSize to about " + ws;
250 } else if (prob > getMaximumGoodAcceptanceLevel()) {
251 return "Try increasing windowSize to about " + ws;
252 } else return "";
253 }
254
255 public String toString() {
256 return RandomWalkOperatorParser.RANDOM_WALK_OPERATOR + "(" + parameter.getParameterName() + ", " + windowSize + ", " + getWeight() + ")";
257 }
258
259 public int getStepCount() {
260 return 0;
261 }
262
263 private final TreeModel treeModel;
264 private final Parameter parameter;
265 private double windowSize;
266 private final RandomWalkOperator.BoundaryCondition condition;
267
268 private Double logForwardDensity;
269 private Double logBackwardDensity;
270
271 /**
272 * @return the number of steps the operator performs in one go.
273 */
274 }