Debian Codegraph @

src/dr/inference/operators/BitSwapOperator.java

raw symbols SCIP
1 /*
2 * BitSwapOperator.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.math.MathUtils;
30
31 /**
32 * Given a values vector (data) and an indicators vector (boolean vector indicating whether the corrosponding value
33 * is used or ignored), this operator explores all possible positions for the used data points while preserving their
34 * order.
35 * The distribition is uniform on all possible data positions.
36 * <p/>
37 * For example, if data values A and B are used in a vector of dimension 4, each of the following states is visited 1/6
38 * of the time.
39 * <p/>
40 * ABcd 1100
41 * AcBd 1010
42 * AcdB 1001
43 * cABd 0110
44 * cAdB 0101
45 * cdAB 0011
46 * <p/>
47 * The operator works by picking a 1 bit in the indicators and swapping it with a neighbour 0, with the appropriate
48 * adjustment to the hastings ratio since a pair of 1,1 and 0,0 are never swapped, and the ends can be swapped in one
49 * direction only.
50 *
51 * @author Joseph Heled
52 * @version $Id$
53 */
54 public class BitSwapOperator extends SimpleMCMCOperator {
55
56 private final Parameter data;
57 private final Parameter indicators;
58 private final boolean impliedOne;
59 private final int radius;
60
61 public BitSwapOperator(Parameter data, Parameter indicators, int radius, double weight) {
62 this.data = data;
63 this.indicators = indicators;
64 this.radius = radius;
65 setWeight(weight);
66
67 final int iDim = indicators.getDimension();
68 final int dDim = data.getDimension();
69 if (iDim == dDim - 1) {
70 impliedOne = true;
71 } else if (iDim == dDim) {
72 impliedOne = false;
73 } else {
74 throw new IllegalArgumentException();
75 }
76 }
77
78
79 public String getPerformanceSuggestion() {
80 return "";
81 }
82
83 public String getOperatorName() {
84 return "bitSwap(" + data.getParameterName() + ")";
85 }
86
87 public double doOperation() {
88 final int dim = indicators.getDimension();
89 if (dim < 2) {
90 throw new RuntimeException("no swaps possible");
91 }
92 int nLoc = 0;
93 int[] loc = new int[2 * dim];
94 double hastingsRatio;
95 int pos;
96 int direction;
97
98 int nOnes = 0;
99 if (radius > 0) {
100 for (int i = 0; i < dim; i++) {
101 final double value = indicators.getStatisticValue(i);
102 if (value > 0) {
103 ++nOnes;
104 loc[nLoc] = i;
105 ++nLoc;
106 }
107 }
108
109 if (nOnes == 0 || nOnes == dim) {
110 throw new RuntimeException("no swaps possible"); //??
111 //return 0;
112 }
113
114 hastingsRatio = 0.0;
115 final int rand = MathUtils.nextInt(nLoc);
116 pos = loc[rand];
117 direction = MathUtils.nextInt(2 * radius);
118 direction -= radius - (direction < radius ? 0 : 1);
119 for (int i = direction > 0 ? pos + 1 : pos + direction; i < (direction > 0 ? pos + direction + 1 : pos); i++) {
120 if (i < 0 || i >= dim || indicators.getStatisticValue(i) > 0) {
121 throw new RuntimeException("swap faild");
122 }
123 }
124 } else {
125 double prev = -1;
126 for (int i = 0; i < dim; i++) {
127 final double value = indicators.getStatisticValue(i);
128 if (value > 0) {
129 ++nOnes;
130 if (i > 0 && prev == 0) {
131 loc[nLoc] = -(i + 1);
132 ++nLoc;
133 }
134 if (i < dim - 1 && indicators.getStatisticValue(i + 1) == 0) {
135 loc[nLoc] = (i + 1);
136 ++nLoc;
137 }
138 }
139 prev = value;
140 }
141
142 if (nOnes == 0 || nOnes == dim) {
143 return 0;
144 }
145
146 if (!(nLoc > 0)) {
147 // System.out.println(indicators);
148 assert false : indicators;
149 }
150
151 final int rand = MathUtils.nextInt(nLoc);
152 pos = loc[rand];
153 direction = pos < 0 ? -1 : 1;
154 pos = (pos < 0 ? -pos : pos) - 1;
155 final int maxOut = 2 * nOnes;
156
157 hastingsRatio = (maxOut == nLoc) ? 0.0 : Math.log((double) nLoc / maxOut);
158 }
159
160 // System.out.println("swap " + pos + "<->" + nto + " " +
161 // indicators.getParameterValue(pos) + "<->" + indicators.getParameterValue(nto) +
162 // " " + data.getParameterValue(pos) + "<->" + data.getParameterValue(nto));
163 final int nto = pos + direction;
164 double vto = indicators.getStatisticValue(nto);
165
166 indicators.setParameterValue(nto, indicators.getParameterValue(pos));
167 indicators.setParameterValue(pos, vto);
168
169 final int dataOffset = impliedOne ? 1 : 0;
170 final int ntodata = nto + dataOffset;
171 final int posdata = pos + dataOffset;
172 vto = data.getStatisticValue(ntodata);
173 data.setParameterValue(ntodata, data.getParameterValue(posdata));
174 data.setParameterValue(posdata, vto);
175
176 // System.out.println("after " + pos + "<->" + nto + " " +
177 // indicators.getParameterValue(pos) + "<->" + indicators.getParameterValue(nto) +
178 // " " + data.getParameterValue(pos) + "<->" + data.getParameterValue(nto));
179
180 return hastingsRatio;
181 }
182 }