Debian Codegraph @

src/dr/inference/operators/TransformedRandomWalkOperator.java

raw symbols SCIP
1 /*
2 * TransformedRandomWalkOperator.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 java.util.ArrayList;
29 import java.util.List;
30
31 import dr.inference.model.Bounds;
32 import dr.inference.model.Parameter;
33 import dr.inferencexml.operators.RandomWalkOperatorParser;
34 import dr.math.MathUtils;
35 import dr.util.Transform;
36
37 /**
38 * A random walk operator that takes a (list of) parameter(s) and corresponding transformations.
39 *
40 * @author Guy Baele
41 */
42 public class TransformedRandomWalkOperator extends AbstractAdaptableOperator {
43
44 private Parameter parameter = null;
45 private final Transform[] transformations;
46 private double windowSize = 0.01;
47 private List<Integer> updateMap = null;
48 private int updateMapSize;
49 private final BoundaryCondition condition;
50
51 private final Double lowerOperatorBound;
52 private final Double upperOperatorBound;
53
54 public enum BoundaryCondition {
55 reflecting,
56 absorbing
57 }
58
59 public TransformedRandomWalkOperator(Parameter parameter, Transform[] transformations, double windowSize, BoundaryCondition bc, double weight, AdaptationMode mode) {
60 this(parameter, transformations, null, windowSize, bc, weight, mode);
61 }
62
63 public TransformedRandomWalkOperator(Parameter parameter, Transform[] transformations, Parameter updateIndex, double windowSize, BoundaryCondition bc,
64 double weight, AdaptationMode mode) {
65 this(parameter, transformations, updateIndex, windowSize, bc, weight, mode, null, null);
66 }
67
68 public TransformedRandomWalkOperator(Parameter parameter, Transform[] transformations, Parameter updateIndex, double windowSize, BoundaryCondition bc,
69 double weight, AdaptationMode mode, Double lowerOperatorBound, Double upperOperatorBound) {
70 super(mode);
71 this.parameter = parameter;
72 this.transformations = transformations;
73 this.windowSize = windowSize;
74 this.condition = bc;
75
76 setWeight(weight);
77 if (updateIndex != null) {
78 updateMap = new ArrayList<Integer>();
79 for (int i = 0; i < updateIndex.getDimension(); i++) {
80 if (updateIndex.getParameterValue(i) == 1.0)
81 updateMap.add(i);
82 }
83 updateMapSize=updateMap.size();
84 }
85
86 this.lowerOperatorBound = lowerOperatorBound;
87 this.upperOperatorBound = upperOperatorBound;
88 }
89
90 /**
91 * @return the parameter this operator acts on.
92 */
93 public Parameter getParameter() {
94 return parameter;
95 }
96
97 public final double getWindowSize() {
98 return windowSize;
99 }
100
101 /**
102 * change the parameter and return the hastings ratio.
103 */
104 public final double doOperation() {
105
106 //store MH-ratio in logq
107 double logJacobian = 0.0;
108
109 // a random dimension to perturb
110 int index;
111 if (updateMap == null) {
112 index = MathUtils.nextInt(parameter.getDimension());
113 } else {
114 index = updateMap.get(MathUtils.nextInt(updateMapSize));
115 }
116
117 //System.err.println("index: " + index);
118
119 // a random point around old value within windowSize * 2
120 double draw = (2.0 * MathUtils.nextDouble() - 1.0) * windowSize;
121
122 //System.err.println("draw: " + draw);
123
124 //transform parameter values first
125 double[] x = parameter.getParameterValues();
126 int dim = parameter.getDimension();
127
128 //System.err.println("parameter " + index + ": " + x[index]);
129
130 double[] transformedX = new double[dim];
131 for (int i = 0; i < dim; i++) {
132 transformedX[i] = transformations[i].transform(x[i]);
133 }
134
135 //System.err.println("transformed parameter " + index + ": " + transformedX[index]);
136
137 //double newValue = parameter.getParameterValue(index) + draw;
138 double newValue = transformedX[index] + draw;
139
140 //System.err.println("new value: " + newValue);
141
142 final Bounds<Double> bounds = parameter.getBounds();
143 final double lower = (lowerOperatorBound == null ? bounds.getLowerLimit(index) : Math.max(bounds.getLowerLimit(index), lowerOperatorBound));
144 final double upper = (upperOperatorBound == null ? bounds.getUpperLimit(index) : Math.min(bounds.getUpperLimit(index), upperOperatorBound));
145
146 if (condition == BoundaryCondition.reflecting) {
147 newValue = RandomWalkOperator.reflectValue(newValue, lower, upper);
148 } else if (newValue < lower || newValue > upper) {
149 // throw new OperatorFailedException("proposed value outside boundaries");
150 return Double.NEGATIVE_INFINITY;
151 }
152
153 //parameter.setParameterValue(index, newValue);
154 parameter.setParameterValue(index, transformations[index].inverse(newValue));
155
156 //System.err.println("set parameter to: " + parameter.getValue(index));
157
158 //this should be correct
159 //logJacobian += transformations[index].getLogJacobian(parameter.getParameterValue(index)) - transformations[index].getLogJacobian(x[index]);
160
161 logJacobian += transformations[index].getLogJacobian(x[index]) - transformations[index].getLogJacobian(parameter.getParameterValue(index));
162
163 //return 0.0;
164 return logJacobian;
165 }
166
167 // public double reflectValueLoop(double value, double lower, double upper) {
168 // double newValue = value;
169 //
170 // while (newValue < lower || newValue > upper) {
171 // if (newValue < lower) {
172 // newValue = lower + (lower - newValue);
173 // }
174 // if (newValue > upper) {
175 // newValue = upper - (newValue - upper);
176 //
177 // }
178 // }
179 //
180 // return newValue;
181 // }
182
183 //MCMCOperator INTERFACE
184 public final String getOperatorName() {
185 return parameter.getParameterName();
186 }
187
188 public double getAdaptableParameter() {
189 return Math.log(windowSize);
190 }
191
192 public void setAdaptableParameter(double value) {
193 windowSize = Math.exp(value);
194 }
195
196 public double getRawParameter() {
197 return windowSize;
198 }
199
200 public String getAdaptableParameterName() {
201 return "windowSize";
202 }
203
204 public String toString() {
205 return RandomWalkOperatorParser.RANDOM_WALK_OPERATOR + "(" + parameter.getParameterName() + ", " + windowSize + ", " + getWeight() + ")";
206 }
207
208 }