| 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.math.MathUtils; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class TeamOperator extends SimpleMCMCOperator { |
| 40 |
|
| 41 |
private final MCMCOperator[] operators; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
private final int nPick; |
| 47 |
private final boolean unequalWeights; |
| 48 |
|
| 49 |
private final MCMCOperator[] currentRound; |
| 50 |
private int nToReject; |
| 51 |
private int[][] binomial; |
| 52 |
|
| 53 |
public TeamOperator(MCMCOperator[] operators, int nPick, double weight) { |
| 54 |
setWeight(weight); |
| 55 |
|
| 56 |
this.operators = operators; |
| 57 |
|
| 58 |
final int N = operators.length; |
| 59 |
assert 0 < nPick && nPick <= N; |
| 60 |
this.nPick = nPick; |
| 61 |
currentRound = new MCMCOperator[N]; |
| 62 |
|
| 63 |
|
| 64 |
{ |
| 65 |
boolean b = false; |
| 66 |
double w = operators[0].getWeight(); |
| 67 |
for( MCMCOperator o : operators ) { |
| 68 |
if( o.getWeight() != w ) { |
| 69 |
b = true; |
| 70 |
break; |
| 71 |
} |
| 72 |
} |
| 73 |
unequalWeights = b; |
| 74 |
} |
| 75 |
|
| 76 |
if( unequalWeights ) { |
| 77 |
final int M = N+1; |
| 78 |
binomial = new int[M][M]; |
| 79 |
for(int n = 0; n < M; ++n) { |
| 80 |
|
| 81 |
binomial[n][0] = 1; |
| 82 |
} |
| 83 |
|
| 84 |
for(int n = 1; n < M; ++n) { |
| 85 |
for(int k = 1; k <= n; ++k) { |
| 86 |
binomial[n][k] = binomial[n-1][k] + binomial[n-1][k-1]; |
| 87 |
} |
| 88 |
} |
| 89 |
} else { |
| 90 |
for(int k = 0; k < N; ++k) { |
| 91 |
currentRound[k] = operators[k]; |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
private void choose() { |
| 109 |
final int n = operators.length; |
| 110 |
if( nPick < n ) { |
| 111 |
if( unequalWeights ) { |
| 112 |
chooseUsingWeights(); |
| 113 |
} else { |
| 114 |
|
| 115 |
for(int k = 0; k < nPick; ++k) { |
| 116 |
final int which = k + MathUtils.nextInt(n - k); |
| 117 |
final MCMCOperator tmp = currentRound[k]; |
| 118 |
currentRound[k] = currentRound[which]; |
| 119 |
currentRound[which] = tmp; |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
private void chooseUsingWeights() { |
| 126 |
|
| 127 |
double inSumWeights = 0.0; |
| 128 |
|
| 129 |
double sumWeightsRemaining = 0; |
| 130 |
for( MCMCOperator o : operators ) { |
| 131 |
sumWeightsRemaining += o.getWeight(); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
int k = nPick; |
| 136 |
|
| 137 |
int j = 0; |
| 138 |
while( k > 0 ) { |
| 139 |
|
| 140 |
final int n = operators.length - j; |
| 141 |
|
| 142 |
if( k == n ) { |
| 143 |
|
| 144 |
for( ; k > 0; k--, j++ ) { |
| 145 |
currentRound[k-1] = operators[j]; |
| 146 |
} |
| 147 |
} else { |
| 148 |
final int cnk = binomial[n][k]; |
| 149 |
final int cnk1 = binomial[n-1][k-1]; |
| 150 |
final int cnk2 = k >= 2 ? binomial[n-2][k-2] : 0; |
| 151 |
|
| 152 |
final double tot = cnk1 * sumWeightsRemaining + cnk * inSumWeights; |
| 153 |
final double we0 = operators[j].getWeight(); |
| 154 |
final double has = cnk2 * (sumWeightsRemaining-we0) + cnk1 * (we0+inSumWeights); |
| 155 |
|
| 156 |
final double r = MathUtils.nextDouble(); |
| 157 |
if( r < has/tot ) { |
| 158 |
currentRound[k-1] = operators[j]; |
| 159 |
k -= 1; |
| 160 |
inSumWeights += we0; |
| 161 |
} |
| 162 |
j += 1; |
| 163 |
sumWeightsRemaining -= we0; |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
public final double doOperation() { |
| 169 |
|
| 170 |
choose(); |
| 171 |
|
| 172 |
double logP = 0; |
| 173 |
|
| 174 |
for(int k = 0; k < nPick; ++k) { |
| 175 |
MCMCOperator operation = currentRound[k]; |
| 176 |
logP += operation.operate(); |
| 177 |
} |
| 178 |
nToReject = nPick; |
| 179 |
|
| 180 |
return logP; |
| 181 |
} |
| 182 |
|
| 183 |
public void accept(double deviation) { |
| 184 |
super.accept(deviation); |
| 185 |
for(int k = 0; k < nPick; ++k) { |
| 186 |
currentRound[k].accept(deviation); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
public void reject() { |
| 191 |
super.reject(); |
| 192 |
for(int k = 0; k < nToReject; ++k) { |
| 193 |
currentRound[k].reject(); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
public void reset() { |
| 198 |
for( MCMCOperator op : operators ) { |
| 199 |
op.reset(); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
public String getOperatorName() { |
| 204 |
StringBuffer sb = new StringBuffer("Team " + nPick + " ("); |
| 205 |
for( MCMCOperator operation : operators ) { |
| 206 |
sb.append(operation.getOperatorName()+","); |
| 207 |
} |
| 208 |
return sb.substring(0, sb.length()-1) + ")"; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public String getPerformanceSuggestion() { |
| 216 |
return ""; |
| 217 |
} |
| 218 |
|
| 219 |
} |