Debian Codegraph @

src/dr/inference/operators/ScaleOperator.java

raw symbols SCIP
1 /*
2 * ScaleOperator.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.inference.model.Variable;
31 import dr.inferencexml.operators.ScaleOperatorParser;
32 import dr.math.MathUtils;
33
34 import java.util.logging.Logger;
35
36 /**
37 * A generic scale operator for use with a multi-dimensional parameters.
38 * Either scale all dimentions at once or scale one dimention at a time.
39 * An optional bit vector and a threshold is used to vary the rate of the individual dimentions according
40 * to their on/off status. For example a threshold of 1 means pick only "on" dimentions.
41 *
42 * @author Alexei Drummond
43 * @author Andrew Rambaut
44 * @version $Id: ScaleOperator.java,v 1.20 2005/06/14 10:40:34 rambaut Exp $
45 */
46 public class ScaleOperator extends AbstractAdaptableOperator {
47
48 private Parameter indicator;
49 private double indicatorOnProb;
50
51 public ScaleOperator(Variable variable, double scale) {
52
53 this(variable, scale, AdaptationMode.ADAPTATION_ON, 1.0);
54 }
55
56 public ScaleOperator(Variable<Double> variable, double scale, AdaptationMode mode, double weight) {
57
58 this(variable, false, 0, scale, mode, null, 1.0, false);
59 setWeight(weight);
60 }
61
62 public ScaleOperator(Variable<Double> variable, boolean scaleAll, int degreesOfFreedom, double scale,
63 AdaptationMode mode, Parameter indicator, double indicatorOnProb, boolean scaleAllInd) {
64
65 super(mode);
66
67 this.variable = variable;
68 this.indicator = indicator;
69 this.indicatorOnProb = indicatorOnProb;
70 this.scaleAll = scaleAll;
71 this.scaleAllIndependently = scaleAllInd;
72 this.scaleFactor = scale;
73 this.degreesOfFreedom = degreesOfFreedom;
74 }
75
76
77 /**
78 * @return the parameter this operator acts on.
79 */
80 public Variable getVariable() {
81 return variable;
82 }
83
84 /**
85 * change the parameter and return the hastings ratio.
86 */
87 public final double doOperation() {
88
89 final double scale = (scaleFactor + (MathUtils.nextDouble() * ((1.0 / scaleFactor) - scaleFactor)));
90
91 double logq;
92
93 final Bounds<Double> bounds = variable.getBounds();
94 final int dim = variable.getSize();
95
96 if (scaleAllIndependently) {
97 // update all dimensions independently.
98 logq = 0;
99 for (int i = 0; i < dim; i++) {
100
101 final double scaleOne = (scaleFactor + (MathUtils.nextDouble() * ((1.0 / scaleFactor) - scaleFactor)));
102 final double offset = bounds.getLowerLimit(i);
103
104 // scale offset by the lower bound
105 final double value = ((variable.getValue(i) - offset) * scaleOne) + offset;
106
107 logq -= Math.log(scaleOne);
108
109 if (value > bounds.getUpperLimit(i)) {
110 // if bounded then perhaps this could be reflected.
111 throw new RuntimeException("proposed value greater than upper bound");
112 }
113
114 variable.setValue(i, value);
115
116 }
117 } else if (scaleAll) {
118 // update all dimensions
119 // hasting ratio is dim-2 times of 1dim case. would be nice to have a reference here
120 // for the proof. It is supposed to be somewhere in an Alexei/Nicholes article.
121 if (degreesOfFreedom > 0)
122 // For parameters with non-uniform prior on only one dimension
123 logq = -degreesOfFreedom * Math.log(scale);
124 else
125 logq = (dim - 2) * Math.log(scale);
126
127 // Must first set all parameters first and check for boundaries later for the operator to work
128 // correctly with dependent parameters such as tree node heights.
129 for (int i = 0; i < dim; i++) {
130 // For scale all we scale by the same factor (i.e., not relative to their individual
131 // origins).
132 variable.setValue(i, variable.getValue(i) * scale);
133 }
134
135 for (int i = 0; i < dim; i++) {
136 if (variable.getValue(i) > variable.getBounds().getUpperLimit(i)) {
137 throw new RuntimeException("proposed value greater than upper bound");
138 }
139 }
140 } else {
141 logq = -Math.log(scale);
142
143 // which bit to scale
144 int index;
145 if (indicator != null) {
146 final int idim = indicator.getDimension();
147 final boolean impliedOne = idim == (dim - 1);
148 // available bit locations
149 int[] loc = new int[idim + 1];
150 int nLoc = 0;
151 // choose active or non active ones?
152 final boolean takeOne = indicatorOnProb >= 1.0 || MathUtils.nextDouble() < indicatorOnProb;
153
154 if (impliedOne && takeOne) {
155 loc[nLoc] = 0;
156 ++nLoc;
157 }
158 for (int i = 0; i < idim; i++) {
159 final double value = indicator.getStatisticValue(i);
160 if (takeOne == (value > 0)) {
161 loc[nLoc] = i + (impliedOne ? 1 : 0);
162 ++nLoc;
163 }
164 }
165
166 if (nLoc > 0) {
167 final int rand = MathUtils.nextInt(nLoc);
168 index = loc[rand];
169 } else {
170 // this used to throw an exception
171 return Double.NEGATIVE_INFINITY;
172 }
173 } else {
174 // any is good
175 index = MathUtils.nextInt(dim);
176 }
177
178 final double oldValue = variable.getValue(index);
179 final double offset = bounds.getLowerLimit(index);
180
181 if (oldValue == 0) {
182 Logger.getLogger("dr.inference").severe("The " + ScaleOperatorParser.SCALE_OPERATOR +
183 " for " +
184 variable.getVariableName()
185 + " has failed since the parameter has a value of 0.0." +
186 "\nTo fix this problem, initalize the value of " +
187 variable.getVariableName() + " to be a positive real number"
188 );
189 }
190 final double newValue = ((oldValue - offset) * scale) + offset;
191
192 if (newValue > bounds.getUpperLimit(index)) {
193 // if bounded then perhaps this could be reflected.
194 throw new RuntimeException("proposed value greater than upper bound: " + newValue + " (" + variable.getId() + ")");
195 }
196
197 variable.setValue(index, newValue);
198
199 // provides a hook for subclasses
200 cleanupOperation(newValue, oldValue);
201 }
202
203 return logq;
204 }
205
206 /**
207 * This method should be overridden by operators that need to do something just before the return of doOperation.
208 *
209 * @param newValue the proposed parameter value
210 * @param oldValue the old parameter value
211 */
212 void cleanupOperation(double newValue, double oldValue) {
213 // DO NOTHING
214 }
215
216 //MCMCOperator INTERFACE
217 public final String getOperatorName() {
218 return "scale(" + variable.getVariableName() + ")";
219 }
220
221 public double getAdaptableParameter() {
222 return Math.log(1.0 / scaleFactor - 1.0);
223 }
224
225 public void setAdaptableParameter(double value) {
226 scaleFactor = 1.0 / (Math.exp(value) + 1.0);
227 }
228
229 public double getRawParameter() {
230 return scaleFactor;
231 }
232
233 public double getScaleFactor() {
234 return scaleFactor;
235 }
236
237 public String getAdaptableParameterName() {
238 return "scaleFactor";
239 }
240
241 public String toString() {
242 return ScaleOperatorParser.SCALE_OPERATOR + "(" + variable.getVariableName() + " [" + scaleFactor + ", " + (1.0 / scaleFactor) + "]";
243 }
244
245 //PRIVATE STUFF
246
247 private Variable<Double> variable = null;
248 private boolean scaleAll = false;
249 private boolean scaleAllIndependently = false;
250 private int degreesOfFreedom = 0;
251 private double scaleFactor = 0.5;
252 }