Debian Codegraph @

src/dr/evomodel/operators/RateSampleOperator.java

raw symbols SCIP
1 /*
2 * RateSampleOperator.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.oldevomodel.clock.RateEvolutionLikelihood;
30 import dr.evomodel.tree.TreeModel;
31 import dr.evomodelxml.operators.RateSampleOperatorParser;
32 import dr.inference.operators.SimpleMCMCOperator;
33 import dr.math.MathUtils;
34
35 /**
36 * A special operator for sampling rates in a subtree
37 * according to a RateEvolutionLikelihood such as the autocorrelated clock model
38 *
39 * @author Michael Defoin Platel
40 */
41 public class RateSampleOperator extends SimpleMCMCOperator {
42
43 private TreeModel tree;
44
45 private boolean sampleAll;
46
47 RateEvolutionLikelihood rateEvolution;
48
49 public RateSampleOperator(TreeModel tree, boolean sampleAll, RateEvolutionLikelihood rateEvolution) {
50
51 this.tree = tree;
52 this.sampleAll = sampleAll;
53 this.rateEvolution = rateEvolution;
54 }
55
56 /**
57 * sample the rates of a subtree and return the hastings ratio.
58 */
59 public final double doOperation() {
60
61 int index;
62 if (sampleAll) {
63 index = tree.getRoot().getNumber();
64 } else {
65 do {
66 index = MathUtils.nextInt(tree.getNodeCount());
67 } while (tree.isExternal(tree.getNode(index)));
68 }
69
70
71 double logBackward = rateEvolution.getLogLikelihood();
72
73 //sampleOne(tree.getNode(index));
74
75 //sampleSubtree(tree.getNode(index));
76 sampleNode(tree.getNode(index));
77
78 //sampleSister(tree.getNode(index));
79
80 double logForward = rateEvolution.getLogLikelihood();
81
82 return logBackward - logForward;
83 }
84
85 void sampleSubtree(NodeRef parent) {
86
87 int nbChildren = tree.getChildCount(parent);
88 for (int c = 0; c < nbChildren; c++) {
89 final NodeRef node = tree.getChild(parent, c);
90 rateEvolution.sampleRate(node);
91 sampleSubtree(node);
92 }
93 }
94
95 void sampleSister(NodeRef parent) {
96
97 int nbChildren = tree.getChildCount(parent);
98 for (int c = 0; c < nbChildren; c++) {
99 final NodeRef node = tree.getChild(parent, c);
100 rateEvolution.sampleRate(node);
101 }
102
103 }
104
105 void sampleNode(NodeRef parent) {
106
107 int nbChildren = tree.getChildCount(parent);
108
109 if (nbChildren > 0) {
110 final int c = MathUtils.nextInt(nbChildren);
111
112 final NodeRef node = tree.getChild(parent, c);
113 rateEvolution.sampleRate(node);
114 }
115
116 }
117
118 void sampleOne(NodeRef parent) {
119
120 int nbChildren = tree.getChildCount(parent);
121
122 if (nbChildren > 0) {
123
124 final NodeRef node = tree.getChild(parent, 0);
125 rateEvolution.sampleRate(node);
126 }
127
128 }
129
130 /**
131 * This method should be overridden by operators that need to do something just before the return of doOperation.
132 *
133 * @param newValue the proposed parameter value
134 * @param oldValue the old parameter value
135 */
136 void cleanupOperation(double newValue, double oldValue) {
137 // DO NOTHING
138 }
139
140 //MCMCOperator INTERFACE
141 public final String getOperatorName() {
142 return "rateSample";
143 }
144
145
146 public double getTargetAcceptanceProbability() {
147 return 0.234;
148 }
149
150 public double getMinimumAcceptanceLevel() {
151 return 0.1;
152 }
153
154 public double getMaximumAcceptanceLevel() {
155 return 0.4;
156 }
157
158 public double getMinimumGoodAcceptanceLevel() {
159 return 0.20;
160 }
161
162 public double getMaximumGoodAcceptanceLevel() {
163 return 0.30;
164 }
165
166 public final String getPerformanceSuggestion() {
167
168 return "No suggestions";
169 }
170
171 public String toString() {
172 return RateSampleOperatorParser.SAMPLE_OPERATOR + "(";
173 }
174
175 //PRIVATE STUFF
176
177 }