| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.inference.operators; |
| 27 |
|
| 28 |
import dr.inference.model.Parameter; |
| 29 |
import dr.math.MathUtils; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 161 |
|
| 162 |
|
| 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 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
return hastingsRatio; |
| 181 |
} |
| 182 |
} |