Debian Codegraph @

src/dr/evomodel/operators/RateScaleOperator.java

raw symbols SCIP
1 /*
2 * RateScaleOperator.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.evomodel.tree.TreeModel;
30 import dr.evomodelxml.operators.RateScaleOperatorParser;
31 import dr.inference.operators.AbstractAdaptableOperator;
32 import dr.inference.operators.AdaptationMode;
33 import dr.inference.operators.OperatorUtils;
34 import dr.math.MathUtils;
35
36 import java.util.ArrayList;
37 import java.util.List;
38
39 /**
40 * A special operator for scaling rates in a subtree.
41 *
42 * @author Michael Defoin Platel
43 */
44 public class RateScaleOperator extends AbstractAdaptableOperator {
45
46 private TreeModel tree;
47
48 private boolean noRoot;
49
50 public RateScaleOperator(TreeModel tree, double scale, boolean noRoot, AdaptationMode mode) {
51
52 super(mode);
53
54 this.scaleFactor = scale;
55 this.tree = tree;
56
57 this.noRoot = noRoot;
58 }
59
60
61 /**
62 * scale the rates of a subtree and return the hastings ratio.
63 */
64 public final double doOperation() {
65
66 final double scale = (scaleFactor + (MathUtils.nextDouble() * ((1.0 / scaleFactor) - scaleFactor)));
67
68 int index;
69 if (noRoot) {
70 do {
71 index = MathUtils.nextInt(tree.getNodeCount());
72 } while (index == tree.getRoot().getNumber());
73 } else {
74 index = MathUtils.nextInt(tree.getNodeCount());
75 }
76
77 List<NodeRef> listNode = new ArrayList<NodeRef>();
78 getSubtree(listNode, tree.getNode(index));
79
80 double oldValue, newValue;
81 double logq = 0;
82 for (NodeRef node : listNode) {
83
84 oldValue = tree.getNodeRate(node);
85 newValue = oldValue * scale;
86
87 tree.setNodeRate(node, newValue);
88 }
89
90 // According to the hastings ratio defined in the original scale Operator
91 logq = (listNode.size() - 2) * Math.log(scale);
92
93 return logq;
94 }
95
96 void getSubtree(List<NodeRef> listNode, NodeRef parent) {
97
98 listNode.add(parent);
99 int nbChildren = tree.getChildCount(parent);
100 for (int c = 0; c < nbChildren; c++) {
101 getSubtree(listNode, tree.getChild(parent, c));
102 }
103 }
104
105 /**
106 * This method should be overridden by operators that need to do something just before the return of doOperation.
107 *
108 * @param newValue the proposed parameter value
109 * @param oldValue the old parameter value
110 */
111 void cleanupOperation(double newValue, double oldValue) {
112 // DO NOTHING
113 }
114
115 //MCMCOperator INTERFACE
116 public final String getOperatorName() {
117 return "rateScale";
118 }
119
120 public double getAdaptableParameter() {
121 return Math.log(1.0 / scaleFactor - 1.0);
122 }
123
124 public void setAdaptableParameter(double value) {
125 scaleFactor = 1.0 / (Math.exp(value) + 1.0);
126 }
127
128 public double getRawParameter() {
129 return scaleFactor;
130 }
131
132 public double getScaleFactor() {
133 return scaleFactor;
134 }
135
136 public String getAdaptableParameterName() {
137 return "scaleFactor";
138 }
139
140 public String toString() {
141 return RateScaleOperatorParser.SCALE_OPERATOR + "(" + " [" + scaleFactor + ", " + (1.0 / scaleFactor) + "]";
142 }
143
144 //PRIVATE STUFF
145
146 private double scaleFactor = 0.5;
147 }