| 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.Likelihood; |
| 29 |
|
| 30 |
public abstract class SimpleMCMCOperator implements MCMCOperator { |
| 31 |
|
| 32 |
public abstract String getOperatorName(); |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public final double getWeight() { |
| 38 |
return weight; |
| 39 |
} |
| 40 |
|
| 41 |
public void setPathParameter(double beta) { |
| 42 |
throw new IllegalArgumentException("Path parameter has no effect on Metropolis-Hastings kernels." + |
| 43 |
"\nGibbs samplers need an implementation for use in power-posteriors"); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public final void setWeight(double w) { |
| 50 |
if( w > 0 ) { |
| 51 |
weight = w; |
| 52 |
} else { |
| 53 |
throw new IllegalArgumentException( |
| 54 |
"Weight must be a positive real, but tried to set weight to " |
| 55 |
+ w); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
public void accept(double deviation) { |
| 60 |
lastDeviation = deviation; |
| 61 |
|
| 62 |
if( !operateAllowed ) { |
| 63 |
operateAllowed = true; |
| 64 |
acceptCount += 1; |
| 65 |
sumDeviation += deviation; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
} else { |
| 71 |
throw new RuntimeException( |
| 72 |
"Accept/reject methods called twice without operate called in between!"); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
public void reject() { |
| 77 |
if( !operateAllowed ) { |
| 78 |
operateAllowed = true; |
| 79 |
rejectCount += 1; |
| 80 |
} else { |
| 81 |
throw new RuntimeException( |
| 82 |
"Accept/reject methods called twice without operate called in between!"); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
public void reset() { |
| 87 |
operateAllowed = true; |
| 88 |
acceptCount = 0; |
| 89 |
rejectCount = 0; |
| 90 |
lastDeviation = 0.0; |
| 91 |
sumDeviation = 0.0; |
| 92 |
} |
| 93 |
|
| 94 |
public final long getCount() { |
| 95 |
return acceptCount + rejectCount; |
| 96 |
} |
| 97 |
|
| 98 |
public final long getAcceptCount() { |
| 99 |
return acceptCount; |
| 100 |
} |
| 101 |
|
| 102 |
public final void setAcceptCount(long acceptCount) { |
| 103 |
this.acceptCount = acceptCount; |
| 104 |
} |
| 105 |
|
| 106 |
public final long getRejectCount() { |
| 107 |
return rejectCount; |
| 108 |
} |
| 109 |
|
| 110 |
public final void setRejectCount(long rejectCount) { |
| 111 |
this.rejectCount = rejectCount; |
| 112 |
} |
| 113 |
|
| 114 |
public final double getMeanDeviation() { |
| 115 |
return sumDeviation / acceptCount; |
| 116 |
} |
| 117 |
|
| 118 |
public final double getDeviation() { |
| 119 |
return lastDeviation; |
| 120 |
} |
| 121 |
|
| 122 |
public final double getSumDeviation() { |
| 123 |
return sumDeviation; |
| 124 |
} |
| 125 |
|
| 126 |
public final void setSumDeviation(double sumDeviation) { |
| 127 |
this.sumDeviation = sumDeviation; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public final double operate() { |
| 145 |
if( operateAllowed ) { |
| 146 |
operateAllowed = false; |
| 147 |
return doOperation(); |
| 148 |
} else { |
| 149 |
throw new RuntimeException( |
| 150 |
"Operate called twice without accept/reject in between!"); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
public final double operate(Likelihood likelihood) { |
| 155 |
if( operateAllowed ) { |
| 156 |
operateAllowed = false; |
| 157 |
return doOperation(likelihood); |
| 158 |
} else { |
| 159 |
throw new RuntimeException( |
| 160 |
"Operate called twice without accept/reject in between!"); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
public final double getAcceptanceProbability() { |
| 165 |
return (double) acceptCount / (double) (acceptCount + rejectCount); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public double doOperation(Likelihood likelihood) { |
| 174 |
return 0.0; |
| 175 |
} |
| 176 |
|
| 177 |
public double getMeanEvaluationTime() { |
| 178 |
return (double) sumEvaluationTime / (double) (acceptCount + rejectCount); |
| 179 |
} |
| 180 |
|
| 181 |
public long getTotalEvaluationTime() { |
| 182 |
return sumEvaluationTime; |
| 183 |
} |
| 184 |
|
| 185 |
public void addEvaluationTime(long time) { |
| 186 |
sumEvaluationTime += time; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
public abstract double doOperation(); |
| 195 |
|
| 196 |
private double weight = 1.0; |
| 197 |
private long acceptCount = 0; |
| 198 |
private long rejectCount = 0; |
| 199 |
|
| 200 |
private double sumDeviation = 0.0; |
| 201 |
private double lastDeviation = 0.0; |
| 202 |
|
| 203 |
private boolean operateAllowed = true; |
| 204 |
|
| 205 |
private long sumEvaluationTime = 0; |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
} |