Debian Codegraph @

src/dr/inference/operators/RandomWalkIntegerOperator.java

raw symbols SCIP
1 /*
2 * RandomWalkIntegerOperator.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.Parameter;
29 import dr.inference.model.Variable;
30 import dr.inferencexml.operators.RandomWalkIntegerOperatorParser;
31 import dr.math.MathUtils;
32
33 /**
34 * A generic random walk operator for use with a multi-dimensional Integer parameters.
35 *
36 * @author Michael Defoin Platel
37 * @version $Id: RandomWalkIntegerOperator.java$
38 */
39 public class RandomWalkIntegerOperator extends SimpleMCMCOperator {
40
41 public RandomWalkIntegerOperator(Variable parameter, int windowSize, double weight) {
42 this.parameter = parameter;
43 this.windowSize = windowSize;
44 setWeight(weight);
45 }
46
47
48 /**
49 * @return the parameter this operator acts on.
50 */
51 public Parameter getParameter() {
52 return (Parameter) parameter;
53 }
54
55 public final int getWindowSize() {
56 return windowSize;
57 }
58
59 /**
60 * change the parameter and return the hastings ratio.
61 */
62 double logq;
63 public double doOperation() {
64 logq = 0.0;
65 // a random dimension to perturb
66 int index = MathUtils.nextInt(parameter.getSize()); // use getSize(), which = getDimension()
67
68 if (parameter instanceof Parameter) {
69 int newValue = calculateNewValue(index);
70 ((Parameter) parameter).setParameterValue(index, newValue);
71 //System.out.println("newValue: "+newValue);
72 } else if (parameter instanceof Variable) { // todo this code is improper if we are going to use Variable<Double>
73
74 int newValue = calculateNewValue(index);
75 ((Variable<Integer>) parameter).setValue(index, newValue);
76
77 }
78
79 return logq;
80 }
81
82 protected int calculateNewValue(int index) {
83 // a random non zero integer around old value within windowSize * 2
84 int oldValue;
85 int upper;
86 int lower;
87
88 if (parameter instanceof Parameter) {
89 oldValue = (int) ((Parameter) parameter).getParameterValue(index);
90 upper = (int) (double) ((Parameter) parameter).getBounds().getUpperLimit(index);
91 lower = (int) (double) ((Parameter) parameter).getBounds().getLowerLimit(index);
92 } else if (parameter instanceof Variable) { // todo this code is improper if we are going to use Variable<Double>
93 oldValue = ((Variable<Integer>) parameter).getValue(index);
94 upper = ((Variable<Integer>) parameter).getBounds().getUpperLimit(index);
95 lower = ((Variable<Integer>) parameter).getBounds().getLowerLimit(index);
96 } else {
97 throw new RuntimeException("The parameter (" + parameter.getId() + ") uses invalid class!");
98 }
99
100 if (upper == lower) return upper;
101
102 int maxWindowSize = upper - lower;
103 if(windowSize> maxWindowSize){
104 windowSize = maxWindowSize;
105 System.err.println("The maximum window size should be smaller than the total number of possible integer values.");
106 }
107
108 int newValue;
109 int roll = MathUtils.nextInt(2 * windowSize); // windowSize="1"; roll = {0, 1}
110 if (roll >= windowSize) { // roll = 1
111 //roll - window is the positive step size
112 int step = 1 + (roll - windowSize);
113 newValue = oldValue + step;
114
115 if (newValue > upper){
116 newValue = 2 * upper - newValue; //reflect down
117 }
118
119 } else { // roll = 0
120 newValue = oldValue - 1 - roll;
121
122 if (newValue < lower){
123 newValue = 2 * lower - newValue; //reflect up
124 }
125
126 }
127
128
129 //New and seemingly correct (accoding to the running MCMC with uniform prior)
130 //calculation of the hastings ratio --CHW
131 int newToOldCount = 0;
132 int oldToNewCount = 0;
133 if(newValue != oldValue){
134 oldToNewCount = oldToNewCount +1;
135 newToOldCount = newToOldCount +1;
136 }
137 int temp = oldValue + windowSize;
138 if(temp > upper){
139 if((2*upper - temp) <= newValue && newValue != upper){
140 oldToNewCount = oldToNewCount+1;
141 }
142 }
143
144 temp = oldValue - windowSize;
145 if(temp < lower){
146 if((2*lower - temp) >= newValue && newValue != lower){
147 oldToNewCount = oldToNewCount+1;
148 }
149 }
150
151 temp = newValue + windowSize;
152 if( temp > upper){
153 if((2*upper - temp) <= oldValue && oldValue != upper){
154 newToOldCount = newToOldCount+1;
155 }
156 }
157
158 temp = newValue - windowSize;
159 if( temp < lower){
160 if((2*lower - temp) >= oldValue && oldValue != lower){
161 newToOldCount = newToOldCount+1;
162 }
163 }
164
165
166 logq = Math.log(newToOldCount)- Math.log(oldToNewCount);
167
168 return newValue;
169 }
170
171 //MCMCOperator INTERFACE
172 public String getOperatorName() {
173 return "randomWalkInteger(" + parameter.getId() + ")";
174 }
175
176 public double getTargetAcceptanceProbability() {
177 return 0.234;
178 }
179
180 public double getMinimumAcceptanceLevel() {
181 return 0.1;
182 }
183
184 public double getMaximumAcceptanceLevel() {
185 return 0.4;
186 }
187
188 public double getMinimumGoodAcceptanceLevel() {
189 return 0.20;
190 }
191
192 public double getMaximumGoodAcceptanceLevel() {
193 return 0.30;
194 }
195
196 public final String getPerformanceSuggestion() {
197
198 double prob = Utils.getAcceptanceProbability(this);
199 double targetProb = getTargetAcceptanceProbability();
200
201 double maxDelta = 0;
202 if (parameter instanceof Parameter) {
203 maxDelta = ((Parameter) parameter).getParameterValue(0) * 2.0;
204 } else if (parameter instanceof Variable) {
205 maxDelta = ((Variable<Integer>) parameter).getValue(0) * 2.0;
206 }
207 long ws = Math.round(OperatorUtils.optimizeWindowSize(windowSize, maxDelta * 2.0, prob, targetProb));
208
209 if (prob < getMinimumGoodAcceptanceLevel()) {
210 if(ws <= 1){
211 return "";
212 }
213 return "Try decreasing windowSize to about " + ws;
214 } else if (prob > getMaximumGoodAcceptanceLevel()) {
215 return "Try increasing windowSize to about " + ws;
216 } else return "";
217 }
218
219 public String toString() {
220 return RandomWalkIntegerOperatorParser.RANDOM_WALK_INTEGER_OPERATOR + "(" + parameter.getId() + ", " + windowSize + ", " + getWeight() + ")";
221 }
222
223 //PRIVATE STUFF
224
225 protected Variable parameter = null;
226 protected int windowSize = 1;
227 }