| 1 |
package dr.inference.operators; |
| 2 |
|
| 3 |
import dr.evomodel.continuous.FullyConjugateMultivariateTraitLikelihood; |
| 4 |
import dr.evomodel.continuous.GaussianProcessFromTree; |
| 5 |
import dr.inference.model.AdaptableSizeFastMatrixParameter; |
| 6 |
import dr.math.MathUtils; |
| 7 |
import dr.math.distributions.GammaDistribution; |
| 8 |
import dr.math.distributions.NormalDistribution; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
public class LFMSplitMergeOperator extends AbstractAdaptableOperator { |
| 14 |
|
| 15 |
NormalDistribution drawDistribution; |
| 16 |
AdaptableSizeFastMatrixParameter factors; |
| 17 |
AdaptableSizeFastMatrixParameter sparseLoadings; |
| 18 |
AdaptableSizeFastMatrixParameter denseLoadings; |
| 19 |
AdaptableSizeFastMatrixParameter cutoffs; |
| 20 |
FullyConjugateMultivariateTraitLikelihood tree; |
| 21 |
GaussianProcessFromTree treeDraw; |
| 22 |
NormalDistribution standardNormal = new NormalDistribution(0, 1); |
| 23 |
GammaDistribution gamma = new GammaDistribution(1, 1); |
| 24 |
|
| 25 |
private String lastCall; |
| 26 |
|
| 27 |
|
| 28 |
final boolean upperTriangular = true; |
| 29 |
|
| 30 |
public LFMSplitMergeOperator(double weight, double initialVariance, AdaptableSizeFastMatrixParameter factors, |
| 31 |
AdaptableSizeFastMatrixParameter denseLoadings, AdaptableSizeFastMatrixParameter sparseLoadings, |
| 32 |
AdaptableSizeFastMatrixParameter cutoffs, FullyConjugateMultivariateTraitLikelihood tree){ |
| 33 |
|
| 34 |
super(AdaptationMode.DEFAULT); |
| 35 |
setWeight(weight); |
| 36 |
|
| 37 |
drawDistribution = new NormalDistribution(0, Math.sqrt(initialVariance)); |
| 38 |
this.factors = factors; |
| 39 |
this.sparseLoadings = sparseLoadings; |
| 40 |
this.denseLoadings = denseLoadings; |
| 41 |
this.cutoffs = cutoffs; |
| 42 |
this.tree = tree; |
| 43 |
GaussianProcessFromTree treeDraw = new GaussianProcessFromTree(tree); |
| 44 |
this.treeDraw = treeDraw; |
| 45 |
} |
| 46 |
|
| 47 |
@Override |
| 48 |
public String getOperatorName() { |
| 49 |
return "Latent Factor Model Split-Merge Operator"; |
| 50 |
} |
| 51 |
|
| 52 |
@Override |
| 53 |
public double doOperation() { |
| 54 |
double randNum = MathUtils.nextDouble(); |
| 55 |
double from1 = 0; |
| 56 |
double hastingsRatio; |
| 57 |
if((randNum < .5 || |
| 58 |
denseLoadings.getColumnDimension() == denseLoadings.getMaxColumnDimension()) && denseLoadings.getColumnDimension() != 1){ |
| 59 |
lastCall = "decrement"; |
| 60 |
if( |
| 61 |
denseLoadings.getColumnDimension() == 2 |
| 62 |
) |
| 63 |
from1 += Math.log(2); |
| 64 |
if( |
| 65 |
denseLoadings.getColumnDimension() == denseLoadings.getMaxColumnDimension() |
| 66 |
) |
| 67 |
from1 += -Math.log(2); |
| 68 |
hastingsRatio = decrement(); |
| 69 |
|
| 70 |
} |
| 71 |
else{ |
| 72 |
lastCall = "increment"; |
| 73 |
if( |
| 74 |
denseLoadings.getColumnDimension() == 1 |
| 75 |
) |
| 76 |
from1 += -Math.log(2); |
| 77 |
if( |
| 78 |
denseLoadings.getColumnDimension() == denseLoadings.getMaxColumnDimension() - 1 |
| 79 |
) |
| 80 |
from1 += Math.log(2); |
| 81 |
hastingsRatio = increment(); |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
denseLoadings.fireParameterChangedEvent(); |
| 86 |
sparseLoadings.fireParameterChangedEvent(); |
| 87 |
cutoffs.fireParameterChangedEvent(); |
| 88 |
factors.fireParameterChangedEvent(); |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
return hastingsRatio + from1; |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
public double getAdaptableParameter() { |
| 98 |
|
| 99 |
return 0; |
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
public void setAdaptableParameter(double value) { |
| 104 |
|
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
@Override |
| 109 |
public double getRawParameter() { |
| 110 |
|
| 111 |
return 0; |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
public String getAdaptableParameterName() { |
| 116 |
return ""; |
| 117 |
} |
| 118 |
|
| 119 |
private double increment(){ |
| 120 |
double hastings = 0; |
| 121 |
int splitFac = MathUtils.nextInt(denseLoadings.getColumnDimension()); |
| 122 |
int lastColumn = denseLoadings.getColumnDimension(); |
| 123 |
denseLoadings.setColumnDimension(denseLoadings.getColumnDimension() + 1); |
| 124 |
sparseLoadings.setColumnDimension(sparseLoadings.getColumnDimension() + 1); |
| 125 |
cutoffs.setColumnDimension(cutoffs.getColumnDimension() + 1); |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
for (int i = 0; i < denseLoadings.getRowDimension(); i++) { |
| 133 |
if(i >= lastColumn || !upperTriangular) { |
| 134 |
|
| 135 |
|
| 136 |
if (sparseLoadings.getParameterValue(i, splitFac) == 0) { |
| 137 |
hastings += sparseIncrement(i, lastColumn); |
| 138 |
} else { |
| 139 |
|
| 140 |
int intDraw = MathUtils.nextInt(3); |
| 141 |
intDraw = 2; |
| 142 |
if (intDraw == 0) { |
| 143 |
hastings += sparseIncrement(i, lastColumn); |
| 144 |
} else if (intDraw == 1) { |
| 145 |
|
| 146 |
sparseLoadings.setParameterValueQuietly(i, lastColumn, sparseLoadings.getParameterValue(i, splitFac)); |
| 147 |
cutoffs.setParameterValueQuietly(i, lastColumn, cutoffs.getParameterValue(i, splitFac)); |
| 148 |
denseLoadings.setParameterValueQuietly(i, lastColumn, denseLoadings.getParameterValue(i, splitFac)); |
| 149 |
|
| 150 |
|
| 151 |
hastings += sparseIncrement(i, splitFac); |
| 152 |
} else { |
| 153 |
|
| 154 |
|
| 155 |
hastings += -Math.log(Math.pow(denseLoadings.getParameterValue(i, splitFac), 2)); |
| 156 |
|
| 157 |
sparseLoadings.setParameterValueQuietly(i, lastColumn, 1); |
| 158 |
|
| 159 |
|
| 160 |
double draw = (Double) drawDistribution.nextRandom(); |
| 161 |
double reset1 = denseLoadings.getParameterValue(i, splitFac) + draw; |
| 162 |
double reset2 = denseLoadings.getParameterValue(i, splitFac) - draw; |
| 163 |
denseLoadings.setParameterValueQuietly(i, lastColumn, reset1); |
| 164 |
denseLoadings.setParameterValueQuietly(i, splitFac, reset2); |
| 165 |
|
| 166 |
hastings += -drawDistribution.logPdf(draw); |
| 167 |
|
| 168 |
hastings += Math.log(2); |
| 169 |
|
| 170 |
|
| 171 |
double cutoffDraw1 = MathUtils.nextDouble() * Math.pow(reset1, 2); |
| 172 |
double cutoffDraw2 = MathUtils.nextDouble() * Math.pow(reset2, 2); |
| 173 |
hastings += Math.log(Math.pow(reset1, 2)); |
| 174 |
hastings += Math.log(Math.pow(reset2, 2)); |
| 175 |
cutoffs.setParameterValueQuietly(i, lastColumn, cutoffDraw1); |
| 176 |
cutoffs.setParameterValueQuietly(i, splitFac, cutoffDraw2); |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
else{ |
| 181 |
|
| 182 |
double cutoffDraw = gamma.nextGamma(); |
| 183 |
hastings += -gamma.logPdf(cutoffDraw); |
| 184 |
cutoffs.setParameterValueQuietly(i, lastColumn, cutoffDraw); |
| 185 |
|
| 186 |
|
| 187 |
} |
| 188 |
|
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
return hastings; |
| 217 |
} |
| 218 |
|
| 219 |
double sparseIncrement(int i, int column){ |
| 220 |
|
| 221 |
double draw = 0; |
| 222 |
double cutoffDraw = 0; |
| 223 |
double hastings = 0; |
| 224 |
|
| 225 |
|
| 226 |
sparseLoadings.setParameterValueQuietly(i, column, 0); |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
draw = (Double) standardNormal.nextRandom(); |
| 231 |
denseLoadings.setParameterValueQuietly(i, column, draw); |
| 232 |
hastings += -standardNormal.logPdf(draw); |
| 233 |
|
| 234 |
|
| 235 |
cutoffDraw = MathUtils.nextDouble() * Math.pow(draw, 2); |
| 236 |
hastings += Math.log(Math.pow(draw, 2)); |
| 237 |
cutoffs.setParameterValueQuietly(i, column, cutoffDraw); |
| 238 |
|
| 239 |
return hastings; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
private double decrement(){ |
| 244 |
double hastings = 0; |
| 245 |
int combineFac = MathUtils.nextInt(denseLoadings.getColumnDimension() - 1); |
| 246 |
int lastColumn = denseLoadings.getColumnDimension() - 1; |
| 247 |
for (int i = 0; i < denseLoadings.getRowDimension(); i++) { |
| 248 |
if(i >= lastColumn || !upperTriangular){ |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
if(sparseLoadings.getParameterValue(i, combineFac) == 1 && sparseLoadings.getParameterValue(i, lastColumn) == 1){ |
| 253 |
hastings += -Math.log(Math.pow(denseLoadings.getParameterValue(i, combineFac), 2)); |
| 254 |
hastings += -Math.log(Math.pow(denseLoadings.getParameterValue(i, lastColumn), 2)); |
| 255 |
hastings += drawDistribution.logPdf(( denseLoadings.getParameterValue(i, lastColumn) - denseLoadings.getParameterValue(i, combineFac)) / 2); |
| 256 |
|
| 257 |
hastings += -Math.log(2); |
| 258 |
double sum = (denseLoadings.getParameterValue(i, combineFac) + denseLoadings.getParameterValue(i, lastColumn)) / 2; |
| 259 |
denseLoadings.setParameterValueQuietly(i, combineFac, sum); |
| 260 |
double draw = MathUtils.nextDouble() * Math.pow(sum, 2); |
| 261 |
cutoffs.setParameterValueQuietly(i, combineFac, draw); |
| 262 |
} |
| 263 |
else if(sparseLoadings.getParameterValue(i, combineFac) == 1 && sparseLoadings.getParameterValue(i, lastColumn) == 0){ |
| 264 |
hastings += standardNormal.logPdf(denseLoadings.getParameterValue(i, lastColumn)); |
| 265 |
hastings += -Math.log(Math.pow(denseLoadings.getParameterValue(i, lastColumn), 2)); |
| 266 |
} |
| 267 |
else if(sparseLoadings.getParameterValue(i, combineFac) == 0 && sparseLoadings.getParameterValue(i, lastColumn) == 1){ |
| 268 |
hastings += standardNormal.logPdf(denseLoadings.getParameterValue(i, combineFac)); |
| 269 |
hastings += -Math.log(Math.pow(denseLoadings.getParameterValue(i, combineFac), 2)); |
| 270 |
denseLoadings.setParameterValueQuietly(i, combineFac, denseLoadings.getParameterValue(i, lastColumn)); |
| 271 |
cutoffs.setParameterValueQuietly(i, combineFac, cutoffs.getParameterValue(i, lastColumn)); |
| 272 |
sparseLoadings.setParameterValueQuietly(i, combineFac, 1); |
| 273 |
} |
| 274 |
else{ |
| 275 |
hastings += standardNormal.logPdf(denseLoadings.getParameterValue(i, lastColumn)); |
| 276 |
hastings += -Math.log(Math.pow(denseLoadings.getParameterValue(i, lastColumn), 2)); |
| 277 |
} |
| 278 |
} |
| 279 |
else{ |
| 280 |
|
| 281 |
hastings += gamma.logPdf(cutoffs.getParameterValue(i, lastColumn)); |
| 282 |
|
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
denseLoadings.setColumnDimension(denseLoadings.getColumnDimension() - 1); |
| 302 |
sparseLoadings.setColumnDimension(sparseLoadings.getColumnDimension() - 1); |
| 303 |
cutoffs.setColumnDimension(cutoffs.getColumnDimension() - 1); |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
return hastings; |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
} |