Debian Codegraph @

src/dr/evomodel/operators/RateVarianceScaleOperator.java

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