Debian Codegraph @

src/dr/inference/operators/RandomWalkOperator.java

raw symbols SCIP
1 /*
2 * RandomWalkOperator.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.inference.operators;
27
28 import dr.inference.model.Bounds;
29 import dr.inference.model.Parameter;
30 import dr.inferencexml.operators.RandomWalkOperatorParser;
31 import dr.math.MathUtils;
32 import dr.util.Transform;
33
34 import java.util.ArrayList;
35 import java.util.List;
36
37 /**
38 * A generic random walk operator for use with a multi-dimensional parameters.
39 *
40 * @author Alexei Drummond
41 * @author Andrew Rambaut
42 * @version $Id: RandomWalkOperator.java,v 1.16 2005/06/14 10:40:34 rambaut Exp $
43 */
44 public class RandomWalkOperator extends AbstractAdaptableOperator {
45
46 public enum BoundaryCondition {
47 rejecting,
48 reflecting,
49 absorbing,
50 log,
51 logit
52 }
53
54 public RandomWalkOperator(Parameter parameter, double windowSize, BoundaryCondition bc, double weight, AdaptationMode mode) {
55 this(parameter, null, windowSize, bc, weight, mode);
56 }
57
58 public RandomWalkOperator(Parameter parameter, Parameter updateIndex, double windowSize, BoundaryCondition boundaryCondition,
59 double weight, AdaptationMode mode) {
60 super(mode);
61 this.parameter = parameter;
62 this.windowSize = windowSize;
63 this.boundaryCondition = boundaryCondition;
64
65 setWeight(weight);
66 if (updateIndex != null) {
67 updateMap = new ArrayList<Integer>();
68 for (int i = 0; i < updateIndex.getDimension(); i++) {
69 if (updateIndex.getParameterValue(i) == 1.0)
70 updateMap.add(i);
71 }
72 updateMapSize=updateMap.size();
73 }
74 }
75
76 /**
77 * @return the parameter this operator acts on.
78 */
79 public Parameter getParameter() {
80 return parameter;
81 }
82
83 public final double getWindowSize() {
84 return windowSize;
85 }
86
87 /**
88 * change the parameter and return the hastings ratio.
89 */
90 public final double doOperation() {
91
92 // a random dimension to perturb
93 if (parameter.getDimension() <= 0) {
94 throw new RuntimeException("Illegal Dimension");
95 }
96
97 int dim;
98 if (updateMap == null) {
99 dim = MathUtils.nextInt(parameter.getDimension());
100 } else {
101 dim = updateMap.get(MathUtils.nextInt(updateMapSize));
102 }
103
104 // a random point around old value within windowSize * 2
105 double draw = (2.0 * MathUtils.nextDouble() - 1.0) * windowSize;
106
107 double oldValue = parameter.getParameterValue(dim);
108
109 final Bounds<Double> bounds = parameter.getBounds();
110 final double lower = bounds.getLowerLimit(dim);
111 final double upper = bounds.getUpperLimit(dim);
112
113 if (boundaryCondition == BoundaryCondition.logit) {
114 // scale oldValue to [0,1]
115 double x1 = (oldValue - lower) / (upper - lower);
116 // logit transform it, add the draw, inverse transform it
117 double x2 = Transform.LOGIT.inverse(Transform.LOGIT.transform(x1) + draw);
118
119 // parameter takes new value scaled back into interval [lower, upper]
120 parameter.setParameterValue(dim, (x2 * (upper - lower)) + lower);
121
122 // HR is the ratio of Jacobians for the before and after values in interval [0,1]
123 return Transform.LOGIT.getLogJacobian(x1) - Transform.LOGIT.getLogJacobian(x2);
124
125 } else if (boundaryCondition == BoundaryCondition.log) {
126 // offset oldValue to [0,+Inf]
127 double x1 = oldValue - lower;
128 // logit transform it, add the draw, inverse transform it
129 double x2 = Transform.LOG.inverse(Transform.LOG.transform(x1) + draw);
130
131 // parameter takes new value tranlated back into interval [lower, +Inf]
132 parameter.setParameterValue(dim, x2 + lower);
133
134 // HR is the ratio of Jacobians for the before and after values
135 return Transform.LOG.getLogJacobian(x1) - Transform.LOG.getLogJacobian(x2);
136
137 } else {
138 double newValue = oldValue + draw;
139 if (boundaryCondition == BoundaryCondition.reflecting) {
140 newValue = reflectValue(newValue, lower, upper);
141 } else if (boundaryCondition == BoundaryCondition.absorbing && (newValue < lower || newValue > upper)) {
142 return 0.0;
143 } else if (boundaryCondition == BoundaryCondition.rejecting && (newValue < lower || newValue > upper)) {
144 return Double.NEGATIVE_INFINITY;
145 }
146
147 parameter.setParameterValue(dim, newValue);
148
149 return 0.0;
150 }
151
152 }
153
154 public static double reflectValue(double value, double lower, double upper) {
155
156 double newValue = value;
157
158 if (value < lower) {
159 if (Double.isInfinite(upper)) {
160 // we are only going to reflect once as the upper bound is at infinity...
161 newValue = lower + (lower - value);
162 } else {
163 double remainder = lower - value;
164
165 double widths = Math.floor(remainder / (upper - lower));
166 remainder -= (upper - lower) * widths;
167
168 // even reflections
169 if (widths % 2 == 0) {
170 newValue = lower + remainder;
171 // odd reflections
172 } else {
173 newValue = upper - remainder;
174 }
175 }
176 } else if (value > upper) {
177 if (Double.isInfinite(lower)) {
178 // we are only going to reflect once as the lower bound is at -infinity...
179 newValue = upper - (newValue - upper);
180 } else {
181
182 double remainder = value - upper;
183
184 double widths = Math.floor(remainder / (upper - lower));
185 remainder -= (upper - lower) * widths;
186
187 // even reflections
188 if (widths % 2 == 0) {
189 newValue = upper - remainder;
190 // odd reflections
191 } else {
192 newValue = lower + remainder;
193 }
194 }
195 }
196
197 return newValue;
198 }
199
200 public double reflectValueLoop(double value, double lower, double upper) {
201 double newValue = value;
202
203 while (newValue < lower || newValue > upper) {
204 if (newValue < lower) {
205 newValue = lower + (lower - newValue);
206 }
207 if (newValue > upper) {
208 newValue = upper - (newValue - upper);
209
210 }
211 }
212
213 return newValue;
214 }
215
216 //MCMCOperator INTERFACE
217 public final String getOperatorName() {
218 return parameter.getParameterName();
219 }
220
221 public double getAdaptableParameter() {
222 return Math.log(windowSize);
223 }
224
225 public void setAdaptableParameter(double value) {
226 windowSize = Math.exp(value);
227 }
228
229 public double getRawParameter() {
230 return windowSize;
231 }
232
233 public String getAdaptableParameterName() {
234 return "windowSize";
235 }
236
237 public String toString() {
238 return RandomWalkOperatorParser.RANDOM_WALK_OPERATOR + "(" + parameter.getParameterName() + ", " + windowSize + ", " + getWeight() + ")";
239 }
240
241 //PRIVATE STUFF
242
243 private Parameter parameter = null;
244 private double windowSize = 0.01;
245 private List<Integer> updateMap = null;
246 private int updateMapSize;
247 private final BoundaryCondition boundaryCondition;
248 }