| 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.inference.model.Variable; |
| 30 |
import dr.inferencexml.operators.SelectorOperatorParser; |
| 31 |
import dr.math.MathUtils; |
| 32 |
|
| 33 |
import java.util.ArrayList; |
| 34 |
import java.util.List; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public class SelectorOperator extends SimpleMCMCOperator { |
| 44 |
private final Parameter selector; |
| 45 |
|
| 46 |
private final int[] np_m1; |
| 47 |
|
| 48 |
private final int[] np_m2; |
| 49 |
|
| 50 |
public SelectorOperator(Parameter selector) { |
| 51 |
this.selector = selector; |
| 52 |
final int len = selector.getSize(); |
| 53 |
np_m1 = new int[len +1]; |
| 54 |
|
| 55 |
for(int l = 0; l < np_m1.length; ++l) { |
| 56 |
np_m1[l] = npos(len, l); |
| 57 |
} |
| 58 |
|
| 59 |
np_m2 = new int[len + 1]; |
| 60 |
np_m2[0] = 1; |
| 61 |
for(int mx = 0; mx < len; ++mx) { |
| 62 |
np_m2[mx+1] = 0; |
| 63 |
for(int l = 1; l < len+1; ++l) { |
| 64 |
np_m2[mx+1] += npos(l, mx); |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
public String getOperatorName() { |
| 70 |
return SelectorOperatorParser.SELECTOR_OPERATOR + "(" + selector.getParameterName() + ")"; |
| 71 |
} |
| 72 |
|
| 73 |
public double doOperation() { |
| 74 |
|
| 75 |
final int[] s = vals(); |
| 76 |
final List<Integer> poss = movesFrom_m2(s); |
| 77 |
final int i = MathUtils.nextInt(poss.size()/2); |
| 78 |
|
| 79 |
final int[] y = new int[s.length]; |
| 80 |
System.arraycopy(s, 0, y, 0, s.length); |
| 81 |
final Integer p = poss.get(2 * i); |
| 82 |
y[p] = poss.get(2*i+1); |
| 83 |
|
| 84 |
double hr = count_sr_m2(s, y); |
| 85 |
hr *= (double)(poss.size()* np_m2[max(s)+1])/(movesFrom_m2(y).size() * np_m2[max(y)+1]); |
| 86 |
|
| 87 |
selector.setParameterValue(p, y[p]); |
| 88 |
|
| 89 |
return Math.log(hr); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public String getPerformanceSuggestion() { |
| 112 |
return null; |
| 113 |
} |
| 114 |
|
| 115 |
private int[] vals() { |
| 116 |
return intVals(selector); |
| 117 |
} |
| 118 |
|
| 119 |
static int[] intVals(Variable<Double> var) { |
| 120 |
int[] v = new int[var.getSize()]; |
| 121 |
for(int k = 0; k < v.length; ++k) { |
| 122 |
final double vk = var.getValue(k); |
| 123 |
v[k] = (int)(vk + ((vk>= 0) ? 0.5 : -0.5)); |
| 124 |
} |
| 125 |
return v; |
| 126 |
} |
| 127 |
|
| 128 |
private List<Integer> movesFrom_m2(int[] s) { |
| 129 |
final int mx = max(s); |
| 130 |
|
| 131 |
final int[] counts = counts_used_m2(s, mx); |
| 132 |
final List<Integer> opt = new ArrayList<Integer>(5); |
| 133 |
|
| 134 |
for(int k = 0; k < s.length; ++k) { |
| 135 |
final int si = s[k]; |
| 136 |
if( si < 0 ) { |
| 137 |
opt.add(k); opt.add(0); |
| 138 |
if( mx >= 0 ) { |
| 139 |
opt.add(k); opt.add(mx+1); |
| 140 |
} |
| 141 |
for(int x = 1; x < mx+1; ++x) { |
| 142 |
if( counts[x]+1 <= counts[x-1]) { |
| 143 |
opt.add(k); opt.add(x); |
| 144 |
} |
| 145 |
} |
| 146 |
} else { |
| 147 |
if( si < mx && ((counts[si] == 1) || counts[si] == counts[si+1]) ) { |
| 148 |
|
| 149 |
} else { |
| 150 |
for(int x = 0; x < mx+1; ++x) { |
| 151 |
if(x == si) { |
| 152 |
continue; |
| 153 |
} |
| 154 |
if( (x > si && counts[si] - 1 >= counts[x] + 1 && counts[x-1] >= counts[x]+1) |
| 155 |
|| |
| 156 |
(x < si && (x > 0 && counts[x]+1 <= counts[x-1] || x == 0)) ) { |
| 157 |
opt.add(k); opt.add(x); |
| 158 |
} |
| 159 |
} |
| 160 |
if( counts[si] > 1) { |
| 161 |
opt.add(k); opt.add(mx+1); |
| 162 |
} |
| 163 |
opt.add(k); opt.add(-1); |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
return opt; |
| 168 |
} |
| 169 |
|
| 170 |
private List<Integer> movesFrom_m1(int[] s) { |
| 171 |
final int mx = max(s); |
| 172 |
|
| 173 |
final int[] counts = counts_m1(s, mx); |
| 174 |
final List<Integer> opt = new ArrayList<Integer>(5); |
| 175 |
|
| 176 |
for(int k = 0; k < s.length; ++k) { |
| 177 |
final int si = s[k]; |
| 178 |
if( si<mx && ((counts[si] == 1) || counts[si] == counts[si+1]) ) { |
| 179 |
|
| 180 |
} else { |
| 181 |
for(int x = 0; x < mx+1; ++x) { |
| 182 |
if(x == si) { |
| 183 |
continue; |
| 184 |
} |
| 185 |
if( (x > si && counts[si] - 1 >= counts[x] + 1 && counts[x-1] >= counts[x]+1) |
| 186 |
|| |
| 187 |
(x < si && (x > 0 && counts[x]+1 <= counts[x-1] || x == 0)) ) { |
| 188 |
opt.add(k); |
| 189 |
opt.add(x); |
| 190 |
} |
| 191 |
} |
| 192 |
if( counts[si] > 1) { |
| 193 |
opt.add(k); |
| 194 |
opt.add(mx+1); |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
return opt; |
| 199 |
} |
| 200 |
|
| 201 |
private static int npos(int s, int m) { |
| 202 |
return npos(s, m, 1); |
| 203 |
} |
| 204 |
|
| 205 |
private static int npos(int s, int m, int mn) { |
| 206 |
if( m == 0 || s == 0 ) { |
| 207 |
return 1; |
| 208 |
} |
| 209 |
|
| 210 |
int tot = 0; |
| 211 |
for(int k = mn; k < 1+s/m; ++k) { |
| 212 |
final int r = s - k*(m+1); |
| 213 |
if(r < 0 ) { |
| 214 |
break; |
| 215 |
} |
| 216 |
tot += npos(r, m-1, 0); |
| 217 |
} |
| 218 |
return tot; |
| 219 |
} |
| 220 |
|
| 221 |
private static int sum(int[] s) { |
| 222 |
int sum = 0; |
| 223 |
for(int si : s) { |
| 224 |
sum += si; |
| 225 |
} |
| 226 |
return sum; |
| 227 |
} |
| 228 |
|
| 229 |
private static int max(int[] s) { |
| 230 |
int mx = s[0]; |
| 231 |
|
| 232 |
for(int k = 1; k < s.length; ++k) { |
| 233 |
if( mx < s[k] ) { |
| 234 |
mx = s[k]; |
| 235 |
} |
| 236 |
} |
| 237 |
return mx; |
| 238 |
} |
| 239 |
|
| 240 |
private static int[] counts_m1(int[] s, int mx) { |
| 241 |
int[] c = new int[mx+1]; |
| 242 |
for(int si : s) { |
| 243 |
c[si]++; |
| 244 |
} |
| 245 |
return c; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
private static int[] counts_m2(int[] s, int mx) { |
| 250 |
int[] c = new int[mx+2]; |
| 251 |
for(int si : s) { |
| 252 |
c[si+1]++; |
| 253 |
} |
| 254 |
return c; |
| 255 |
} |
| 256 |
|
| 257 |
static int[] counts_m2(int[] s) { |
| 258 |
return counts_m2(s, max(s)); |
| 259 |
} |
| 260 |
|
| 261 |
private static int[] counts_used_m2(int[] s, int mx) { |
| 262 |
int[] c = new int[mx+1]; |
| 263 |
for(int si : s) { |
| 264 |
if( si >= 0 ) { |
| 265 |
c[si]++; |
| 266 |
} |
| 267 |
} |
| 268 |
return c; |
| 269 |
} |
| 270 |
|
| 271 |
static int[] counts_used_m2(int[] s) { |
| 272 |
return counts_used_m2(s, max(s)); |
| 273 |
} |
| 274 |
|
| 275 |
private static long choose(int n, int k) { |
| 276 |
double r = 1; |
| 277 |
while( n > k ) { |
| 278 |
r *= n; |
| 279 |
r /= (n-k); |
| 280 |
--n; |
| 281 |
} |
| 282 |
return (long)(r+0.5); |
| 283 |
} |
| 284 |
|
| 285 |
private static long[] countl_m1(int[] ls) { |
| 286 |
int l = sum(ls); |
| 287 |
int i = 0; |
| 288 |
long[] r = new long[ls.length]; |
| 289 |
|
| 290 |
while(l > 0) { |
| 291 |
r[i] = choose(l, ls[i]); |
| 292 |
|
| 293 |
l -= ls[i]; |
| 294 |
i += 1; |
| 295 |
} |
| 296 |
return r; |
| 297 |
} |
| 298 |
|
| 299 |
private static double count_sr_m1(int[] x, int[] y) { |
| 300 |
long[] r1 = countl_m1(counts_m1(x, max(x))); |
| 301 |
long[] r2 = countl_m1(counts_m1(y, max(y))); |
| 302 |
|
| 303 |
int k = Math.min(r1.length, r2.length); |
| 304 |
double r = 1; |
| 305 |
for(int i=0; i < k; ++i) { |
| 306 |
r *= r1[i]; |
| 307 |
r /= r2[i]; |
| 308 |
} |
| 309 |
for(int i=k; i < r1.length; ++i) { |
| 310 |
r *= r1[i]; |
| 311 |
} |
| 312 |
for(int i=k; i < r2.length; ++i) { |
| 313 |
r /= r2[i]; |
| 314 |
} |
| 315 |
return r; |
| 316 |
} |
| 317 |
|
| 318 |
private static long[] countl_m2(int[] ls) { |
| 319 |
if( ls.length == 1 ) { |
| 320 |
return new long[]{1}; |
| 321 |
} |
| 322 |
|
| 323 |
int l = sum(ls); |
| 324 |
|
| 325 |
long[] r = new long[ls.length]; |
| 326 |
r[0] = choose(l, ls[0]); |
| 327 |
l -= ls[0]; |
| 328 |
int i = 1; |
| 329 |
|
| 330 |
while( l > 0 ) { |
| 331 |
r[i] = choose(l, ls[i]); |
| 332 |
|
| 333 |
l -= ls[i]; |
| 334 |
i += 1; |
| 335 |
} |
| 336 |
return r; |
| 337 |
} |
| 338 |
|
| 339 |
private static double count_sr_m2(int[] x, int[] y) { |
| 340 |
long[] r1 = countl_m2(counts_m2(x)); |
| 341 |
long[] r2 = countl_m2(counts_m2(y)); |
| 342 |
|
| 343 |
int k = Math.min(r1.length, r2.length); |
| 344 |
double r = 1; |
| 345 |
for(int i=0; i < k; ++i) { |
| 346 |
r *= r1[i]; |
| 347 |
r /= r2[i]; |
| 348 |
} |
| 349 |
for(int i=k; i < r1.length; ++i) { |
| 350 |
r *= r1[i]; |
| 351 |
} |
| 352 |
for(int i=k; i < r2.length; ++i) { |
| 353 |
r /= r2[i]; |
| 354 |
} |
| 355 |
return r; |
| 356 |
} |
| 357 |
|
| 358 |
} |