| 1 | /* |
| 2 | * BitFlipOperator.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.evomodel.tree.TreeModel; |
| 29 | import dr.evomodel.tree.TreeParameterModel; |
| 30 | import dr.inference.model.Parameter; |
| 31 | import dr.math.MathUtils; |
| 32 | |
| 33 | /** |
| 34 | * A generic operator that flips bits. |
| 35 | * |
| 36 | * @author Alexei Drummond |
| 37 | * @version $Id$ |
| 38 | */ |
| 39 | public class BitFlipOperator extends SimpleMCMCOperator { |
| 40 | |
| 41 | public BitFlipOperator(Parameter parameter, double weight, boolean usesPriorOnSum, TreeModel treeModel) { |
| 42 | |
| 43 | this.parameter = parameter; |
| 44 | this.usesPriorOnSum = usesPriorOnSum; |
| 45 | |
| 46 | if (treeModel != null) { |
| 47 | indicators = new TreeParameterModel(treeModel, parameter, true); |
| 48 | bitFlipHelper = new DriftBitFlipHelper(); |
| 49 | tree = treeModel; |
| 50 | } else { |
| 51 | bitFlipHelper = new BitFlipHelper(); |
| 52 | } |
| 53 | |
| 54 | setWeight(weight); |
| 55 | } |
| 56 | |
| 57 | public BitFlipOperator(Parameter parameter, double weight, boolean usesPriorOnSum) { |
| 58 | this(parameter, weight, usesPriorOnSum, null); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @return the parameter this operator acts on. |
| 63 | */ |
| 64 | public Parameter getParameter() { |
| 65 | return parameter; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Change the parameter and return the hastings ratio. |
| 70 | * Flip (Switch a 0 to 1 or 1 to 0) for a random bit in a bit vector. |
| 71 | * Return the hastings ratio which makes all subsets of vectors with the same number of 1 bits |
| 72 | * equiprobable, unless usesPriorOnSum = false then all configurations are equiprobable |
| 73 | */ |
| 74 | public final double doOperation() { |
| 75 | final int dim = parameter.getDimension(); |
| 76 | // final int dim = bitFlipHelper.getDim(parameter.getDimension()); |
| 77 | double sum = 0.0; |
| 78 | // double sumNeg = 0.0; |
| 79 | // double temp; |
| 80 | |
| 81 | if(usesPriorOnSum) { |
| 82 | for (int i = 0; i < dim; i++) { |
| 83 | // if(parameter.getParameterValue(i)<0) { |
| 84 | // System.err.println(parameter.getParameterValue(i)); |
| 85 | // } |
| 86 | sum += Math.abs(parameter.getParameterValue(i)); |
| 87 | } |
| 88 | // AR - removed a debugging printf |
| 89 | // if (sum > 103) { |
| 90 | // System.err.println("sum: " + sum); |
| 91 | // } |
| 92 | |
| 93 | /* |
| 94 | for (int i = 0; i < dim; i++) { |
| 95 | temp = parameter.getParameterValue(i); |
| 96 | if (temp < 0) { |
| 97 | sumNeg++; |
| 98 | } else { |
| 99 | // sum += parameter.getParameterValue(i); |
| 100 | sum += temp; |
| 101 | } |
| 102 | } |
| 103 | */ |
| 104 | } |
| 105 | // make it so pos never corresponds to external nodes when used for drift diffusion model |
| 106 | final int pos = MathUtils.nextInt(dim); |
| 107 | |
| 108 | final int value = (int) parameter.getParameterValue(pos); |
| 109 | double logq = 0.0; |
| 110 | if (value == 0) { |
| 111 | logq = bitFlipHelper.flipZero(pos, dim, sum); |
| 112 | //parameter.setParameterValue(pos, 1.0); |
| 113 | // if(sum==103){ |
| 114 | // System.err.println("value: " + value); |
| 115 | // System.err.println("parameter.getParameterValue(pos): " + parameter.getParameterValue(pos)); |
| 116 | // } |
| 117 | |
| 118 | // if(usesPriorOnSum) |
| 119 | // logq = bitFlipHelper.getLogQFlipZero(dim, sum); |
| 120 | // logq = -Math.log((dim - sum) / (sum + 1)); |
| 121 | |
| 122 | } else if (value == 1) { |
| 123 | // parameter.setParameterValue(pos, 0.0); |
| 124 | logq = bitFlipHelper.flipOne(pos, dim, sum); |
| 125 | // if(usesPriorOnSum) |
| 126 | // logq = bitFlipHelper.getLogQFlipOne(dim, sum); |
| 127 | // logq = -Math.log(sum / (dim - sum + 1)); |
| 128 | |
| 129 | } else if (value == -1) { |
| 130 | logq = bitFlipHelper.flipNegOne(pos, dim, sum); |
| 131 | // logq = bitFlipHelper.getLogQFlipNegOne(dim,sum); |
| 132 | } else { |
| 133 | throw new RuntimeException("expected 1 or 0 or -1"); |
| 134 | } |
| 135 | |
| 136 | if (!usesPriorOnSum) { |
| 137 | logq = 0; |
| 138 | } |
| 139 | |
| 140 | // hastings ratio is designed to make move symmetric on sum of 1's |
| 141 | return logq; |
| 142 | } |
| 143 | |
| 144 | class BitFlipHelper { |
| 145 | |
| 146 | public BitFlipHelper() { |
| 147 | } |
| 148 | |
| 149 | // public int getDim (int paramDim){ |
| 150 | // return paramDim; |
| 151 | // } |
| 152 | /* |
| 153 | public double getLogQFlipZero(int dim, double sum, double sumNeg){ |
| 154 | // System.err.println("I am NOT in DriftBitFlipHelper"); |
| 155 | return -Math.log((dim - sum) / (sum + 1)); |
| 156 | } |
| 157 | |
| 158 | public double getLogQFlipOne(int dim, double sum){ |
| 159 | return -Math.log(sum / (dim - sum + 1)); |
| 160 | } |
| 161 | */ |
| 162 | public double flipOne(int pos, int dim, double sum) { |
| 163 | parameter.setParameterValue(pos, 0.0); |
| 164 | return -Math.log(sum / (dim - sum + 1)); |
| 165 | } |
| 166 | |
| 167 | public double flipZero(int pos, int dim, double sum) { |
| 168 | parameter.setParameterValue(pos, 1.0); |
| 169 | return -Math.log((dim - sum) / (sum + 1)); |
| 170 | } |
| 171 | |
| 172 | public double flipNegOne(int pos, int dim, double sum) { |
| 173 | throw new RuntimeException("expected 1 or 0"); |
| 174 | } |
| 175 | |
| 176 | } |
| 177 | |
| 178 | class DriftBitFlipHelper extends BitFlipHelper { |
| 179 | |
| 180 | public DriftBitFlipHelper() { |
| 181 | } |
| 182 | |
| 183 | // public int getDim (int paramDim){ |
| 184 | // return (int) (0.5*(paramDim+1)-1); |
| 185 | // } |
| 186 | /* |
| 187 | public double getLogQFlipZero(int dim, double sum){ |
| 188 | // System.err.println("I am in DriftBitFlipHelper"); |
| 189 | if(isZeroPair){ |
| 190 | return Math.log((sum + 1)/(dim - 2*sum)); |
| 191 | }else{ |
| 192 | return 0; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | public double getLogQFlipOne(int dim, double sum){ |
| 197 | return Math.log((dim - 2*sum + 2)/sum); |
| 198 | } |
| 199 | |
| 200 | public double getLogQFlipNegOne(int dim, double sum){ |
| 201 | return Math.log((dim - 2*sum + 2)/sum); |
| 202 | } |
| 203 | */ |
| 204 | |
| 205 | public double flipOne(int pos, int dim, double sum) { |
| 206 | // draw random number from [0,1] |
| 207 | double rand = MathUtils.nextDouble(); |
| 208 | double internalDim = 0.5 * (dim + 1) - 1; |
| 209 | |
| 210 | if (rand < 0.5) { |
| 211 | parameter.setParameterValue(pos, 0.0); |
| 212 | return Math.log(2 * (internalDim + 1 - sum) / sum); |
| 213 | } else { |
| 214 | parameter.setParameterValue(pos, -1.0); |
| 215 | return 0; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | public double flipNegOne(int pos, int dim, double sum) { |
| 220 | double rand = MathUtils.nextDouble(); |
| 221 | double internalDim = 0.5 * (dim + 1) - 1; |
| 222 | |
| 223 | if (rand < 0.5) { |
| 224 | parameter.setParameterValue(pos, 0.0); |
| 225 | return Math.log(2 * (internalDim + 1 - sum) / sum); |
| 226 | } else { |
| 227 | parameter.setParameterValue(pos, 1.0); |
| 228 | return 0; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | public double flipZero(int pos, int dim, double sum) { |
| 233 | |
| 234 | int nodeNum = indicators.getNodeNumberFromParameterIndex(pos); |
| 235 | |
| 236 | // if indicator corresponds to external node, keep default value of zero |
| 237 | // would be unnecessary if pos never matches up with external nodes |
| 238 | if (tree.isExternal(tree.getNode(nodeNum))) { |
| 239 | // if(nodeNum > 104) { |
| 240 | // System.err.println("external node num: " + nodeNum); |
| 241 | // } |
| 242 | parameter.setParameterValue(pos, 0.0); |
| 243 | return 0; |
| 244 | } else { |
| 245 | |
| 246 | double rand = MathUtils.nextDouble(); |
| 247 | double internalDim = 0.5 * (dim + 1) - 1; |
| 248 | |
| 249 | if (rand < 0.5) { |
| 250 | parameter.setParameterValue(pos, 1.0); |
| 251 | return Math.log((sum + 1) / (2 * (internalDim - sum))); |
| 252 | } else { |
| 253 | parameter.setParameterValue(pos, -1.0); |
| 254 | return Math.log((sum + 1) / (2 * (internalDim - sum))); |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | /* |
| 259 | public void flipZero(int pos){ |
| 260 | int nodeNumber = indicators.getNodeNumberFromParameterIndex(pos); |
| 261 | NodeRef chosenNode = tree.getNode(nodeNumber); |
| 262 | NodeRef parentNode = tree.getParent(chosenNode); |
| 263 | NodeRef pairedNode = tree.getChild(parentNode, 0); |
| 264 | int parentNumber = parentNode.getNumber(); |
| 265 | |
| 266 | // check this |
| 267 | if(pairedNode.equals(chosenNode)){ |
| 268 | // System.err.println("I get HERE!!!!!"); |
| 269 | pairedNode = tree.getChild(parentNode, 1); |
| 270 | } |
| 271 | |
| 272 | // System.err.println("chosen value before: " + indicators.getNodeValue(tree,chosenNode)); |
| 273 | // System.err.println("paired value after:" + indicators.getNodeValue(tree,pairedNode)); |
| 274 | |
| 275 | |
| 276 | if(indicators.getNodeValue(tree, pairedNode) == 0.0){ |
| 277 | // we have (0,0) pair |
| 278 | isZeroPair = true; |
| 279 | // flip (0,0) to (1,0) |
| 280 | indicators.setNodeValue(tree, chosenNode, 1.0); |
| 281 | // parameter.setParameterValue(pos,1.0); |
| 282 | }else if (indicators.getNodeValue(tree, pairedNode) == 1.0){ |
| 283 | // we have (0,1) pair |
| 284 | isZeroPair = false; |
| 285 | // flip (0,1) to (1,0) |
| 286 | indicators.setNodeValue(tree, chosenNode, 1.0); |
| 287 | indicators.setNodeValue(tree, pairedNode, 0.0); |
| 288 | // parameter.setParameterValue(pos,1.0); |
| 289 | // parameter.setParameterValue(indicators.getParameterIndexFromNodeNumber(pairedNode.getNumber()),0.0); |
| 290 | }else { |
| 291 | throw new RuntimeException("expected 1 or 0"); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | protected boolean isZeroPair = false; |
| 296 | */ |
| 297 | } |
| 298 | |
| 299 | |
| 300 | // Interface MCMCOperator |
| 301 | public final String getOperatorName() { |
| 302 | return "bitFlip(" + parameter.getParameterName() + ")"; |
| 303 | } |
| 304 | |
| 305 | public final String getPerformanceSuggestion() { |
| 306 | return "no performance suggestion"; |
| 307 | } |
| 308 | |
| 309 | public String toString() { |
| 310 | return getOperatorName(); |
| 311 | } |
| 312 | |
| 313 | // Private instance variables |
| 314 | |
| 315 | private BitFlipHelper bitFlipHelper; |
| 316 | private TreeModel tree; |
| 317 | private TreeParameterModel indicators = null; |
| 318 | private Parameter parameter = null; |
| 319 | // private int bits; |
| 320 | private boolean usesPriorOnSum = true; |
| 321 | } |