| 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 |
|
| 29 |
import dr.inference.model.Parameter; |
| 30 |
import dr.math.MathUtils; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class HierarchicalBitFlipOperator extends SimpleMCMCOperator { |
| 39 |
|
| 40 |
public HierarchicalBitFlipOperator(Parameter hParameter, Parameter[] strataParameters, int NStrata, double weight, boolean usesPriorOnSum) { |
| 41 |
|
| 42 |
this.Nstrata = NStrata; |
| 43 |
this.hParameter = hParameter; |
| 44 |
this.strataParameters = strataParameters; |
| 45 |
this.usesPriorOnSum = usesPriorOnSum; |
| 46 |
setWeight(weight); |
| 47 |
|
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public final double doOperation() { |
| 60 |
final int dim = hParameter.getDimension(); |
| 61 |
double logq = 0.0; |
| 62 |
|
| 63 |
|
| 64 |
int rep= MathUtils.nextInt(4); |
| 65 |
|
| 66 |
for (int repete=0; repete<rep+1;repete++ ){ |
| 67 |
|
| 68 |
|
| 69 |
if(usesPriorOnSum) { |
| 70 |
for (int i = 0; i < dim; i++) { |
| 71 |
|
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
final int pos = MathUtils.nextInt(dim); |
| 78 |
|
| 79 |
int value = (int) hParameter.getParameterValue(pos); |
| 80 |
|
| 81 |
if (value == 0) { |
| 82 |
hParameter.setParameterValue(pos, 1.0); |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
} else if (value == 1) { |
| 88 |
hParameter.setParameterValue(pos, 0.0); |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
} else { |
| 93 |
throw new RuntimeException("expected 1 or 0"); |
| 94 |
} |
| 95 |
|
| 96 |
for (int j=0; j<Nstrata ; j++){ |
| 97 |
|
| 98 |
value = (int) strataParameters[j].getParameterValue(pos); |
| 99 |
|
| 100 |
if (value == 0) { |
| 101 |
strataParameters[j].setParameterValue(pos, 1.0); |
| 102 |
|
| 103 |
} else if (value == 1) { |
| 104 |
strataParameters[j].setParameterValue(pos, 0.0); |
| 105 |
|
| 106 |
} else { |
| 107 |
throw new RuntimeException("expected 1 or 0"); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
return logq; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
public final String getOperatorName() { |
| 119 |
return "bitFlip(" + hParameter.getParameterName() + ")"; |
| 120 |
} |
| 121 |
|
| 122 |
public final String getPerformanceSuggestion() { |
| 123 |
return "no performance suggestion"; |
| 124 |
} |
| 125 |
|
| 126 |
public String toString() { |
| 127 |
return getOperatorName(); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
private Parameter hParameter = null; |
| 133 |
private boolean usesPriorOnSum = false; |
| 134 |
private Parameter[] strataParameters = null ; |
| 135 |
int Nstrata; |
| 136 |
} |
| 137 |
|
| 138 |
|