Debian Codegraph @

src/dr/evomodel/operators/BitFlipInSubstitutionModelOperator.java

raw symbols SCIP
1 /*
2 * BitFlipInSubstitutionModelOperator.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.inference.model.BayesianStochasticSearchVariableSelection;
29 import dr.inference.model.Parameter;
30 import dr.inference.operators.*;
31 import dr.math.MathUtils;
32 import dr.xml.*;
33
34 /**
35 * A generic operator that flips bits.
36 *
37 * @author Marc Suchard
38 * @version $Id$
39 */
40 public class BitFlipInSubstitutionModelOperator extends AbstractAdaptableOperator {
41
42 public static final String BIT_FLIP_OPERATOR = "bitFlipInSubstitutionModelOperator";
43 public static final String SCALE_FACTOR = "scaleFactor";
44
45 public BitFlipInSubstitutionModelOperator(BayesianStochasticSearchVariableSelection subModel, Parameter rateParameter, double weight, double scaleFactor, AdaptationMode mode) {
46 super(mode);
47 this.model = subModel;
48 this.indicatorParameter = subModel.getIndicators();
49 this.rateParameter = rateParameter;
50 this.scaleFactor = scaleFactor;
51 setWeight(weight);
52
53 }
54
55 /**
56 * @return the indicatorParameter this operator acts on.
57 */
58 public Parameter getIndicatorParameter() {
59 return indicatorParameter;
60 }
61
62 /**
63 * Change the indicatorParameter and return the hastings ratio.
64 * Flip (Switch a 0 to 1 or 1 to 0) for a random bit in a bit vector.
65 * Return the hastings ratio which makes all subsets of vectors with the same number of 1 bits
66 * equiprobable.
67 */
68 public final double doOperation() {
69 final int dim = indicatorParameter.getDimension();
70 double sum = 0.0;
71
72 for (int i = 0; i < dim; i++) {
73 sum += indicatorParameter.getParameterValue(i);
74 }
75
76 final int pos = MathUtils.nextInt(dim);
77
78 final int value = (int) indicatorParameter.getParameterValue(pos);
79
80 double rand = 0;
81 if (rateParameter != null)
82 rand = MathUtils.nextDouble();
83
84 double logq;
85 if (value == 0) {
86 indicatorParameter.setParameterValue(pos, 1.0);
87
88 logq = -Math.log((dim - sum) / (sum + 1));
89 // rand = 0.5 - rand;
90
91 } else if (value == 1) {
92 indicatorParameter.setParameterValue(pos, 0.0);
93
94 logq = -Math.log(sum / (dim - sum + 1));
95 // rand = 0.5 + rand;
96 rand *= -1;
97
98 } else {
99 throw new RuntimeException("expected 1 or 0");
100 }
101
102 if (rateParameter != null) {
103 final double scale = Math.exp((rand) * scaleFactor);
104 logq += Math.log(scale);
105
106 final double oldValue = rateParameter.getParameterValue(0);
107 final double newValue = scale * oldValue;
108
109 rateParameter.setParameterValue(0, newValue);
110 }
111
112 // System.err.println("Operator ISM");
113 // if (!model.validState()) {
114 // System.err.println("invalid model");
115 // throw new OperatorFailedException("Out of bounds");
116 // } // else System.err.println("valid");
117
118 // hastings ratio is designed to make move symmetric on sum of 1's
119 return logq;
120 }
121
122 // Interface MCMCOperator
123 public final String getOperatorName() {
124 return "bitflip(" + indicatorParameter.getParameterName() + ")";
125 }
126
127 public double getAdaptableParameter() {
128 // return Math.log(1.0 / scaleFactor - 1.0);
129 return Math.log(scaleFactor);
130 }
131
132 public void setAdaptableParameter(double value) {
133 // scaleFactor = 1.0 / (Math.exp(value) + 1.0);
134 scaleFactor = Math.exp(value);
135 }
136
137 public double getRawParameter() {
138 return scaleFactor;
139 }
140
141 public double getScaleFactor() {
142 return scaleFactor;
143 }
144
145 public String getAdaptableParameterName() {
146 return "scaleFactor";
147 }
148
149
150 public String toString() {
151 return getOperatorName();
152 }
153
154 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
155
156 public String getParserName() {
157 return BIT_FLIP_OPERATOR;
158 }
159
160 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
161
162 double weight = xo.getDoubleAttribute(WEIGHT);
163
164 AdaptationMode mode = AdaptationMode.parseMode(xo);
165 final double scaleFactor = xo.getDoubleAttribute(SCALE_FACTOR);
166
167 if (scaleFactor <= 0.0 || scaleFactor >= 1.0) {
168 throw new XMLParseException("scaleFactor must be between 0.0 and 1.0");
169 }
170
171
172 Parameter rateParameter = (Parameter) xo.getChild(Parameter.class);
173 BayesianStochasticSearchVariableSelection subModel = (BayesianStochasticSearchVariableSelection) xo.getChild(BayesianStochasticSearchVariableSelection.class);
174
175 // if (xo.hasAttribute(MIN) && xo.hasAttribute(MAX)) {
176 // int min = xo.getIntegerAttribute(MIN);
177 // int max = xo.getIntegerAttribute(MAX);
178 // return new BitFlipOperator(indicatorParameter, weight, min, max);
179 // }
180
181 return new BitFlipInSubstitutionModelOperator(subModel, rateParameter, weight, scaleFactor, mode);
182 }
183
184 //************************************************************************
185 // AbstractXMLObjectParser implementation
186 //************************************************************************
187
188 public String getParserDescription() {
189 return "This element returns a bit-flip operator on a given indicatorParameter.";
190 }
191
192 public Class getReturnType() {
193 return MCMCOperator.class;
194 }
195
196 public XMLSyntaxRule[] getSyntaxRules() {
197 return rules;
198 }
199
200 private final XMLSyntaxRule[] rules = {
201 AttributeRule.newDoubleRule(WEIGHT),
202 AttributeRule.newDoubleRule(SCALE_FACTOR),
203 AttributeRule.newBooleanRule(AUTO_OPTIMIZE, true),
204 new ElementRule(Parameter.class,true),
205 new ElementRule(BayesianStochasticSearchVariableSelection.class)
206 };
207
208 };
209 // Private instance variables
210
211 private Parameter indicatorParameter = null;
212 private Parameter rateParameter = null;
213 private BayesianStochasticSearchVariableSelection model;
214 private double scaleFactor;
215 }