| 1 | /* |
| 2 | * GammaProductLikelihood.java |
| 3 | * |
| 4 | * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard |
| 5 | * |
| 6 | * This file is part of BEAST. |
| 7 | * See the NOTICE file distributed with this work for additional |
| 8 | * information regarding copyright ownership and licensing. |
| 9 | * |
| 10 | * BEAST is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU Lesser General Public License as |
| 12 | * published by the Free Software Foundation; either version 2 |
| 13 | * of the License, or (at your option) any later version. |
| 14 | * |
| 15 | * BEAST is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU Lesser General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Lesser General Public |
| 21 | * License along with BEAST; if not, write to the |
| 22 | * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 23 | * Boston, MA 02110-1301 USA |
| 24 | */ |
| 25 | |
| 26 | package dr.evomodel.coalescent; |
| 27 | |
| 28 | import dr.evomodel.tree.TreeModel; |
| 29 | import dr.inference.model.Likelihood; |
| 30 | import dr.math.distributions.GammaDistribution; |
| 31 | |
| 32 | /** |
| 33 | * Calculates a product of gamma densities and gamma tail probabilities. |
| 34 | * |
| 35 | * @author Guy Baele |
| 36 | */ |
| 37 | |
| 38 | public class GammaProductLikelihood extends Likelihood.Abstract { |
| 39 | |
| 40 | public final static boolean USE_EXPONENTIAL = false; |
| 41 | public final static boolean REDUCE_TO_EXPONENTIAL = false; |
| 42 | |
| 43 | public final static boolean DEBUG = false; |
| 44 | |
| 45 | //let's forget about this for the moment |
| 46 | public final static boolean FIXED_TREE = false; |
| 47 | |
| 48 | private TreeModel treeModel; |
| 49 | private double popSize; |
| 50 | private double[] means; |
| 51 | private double[] variances; |
| 52 | |
| 53 | private double[] alphas; |
| 54 | private double[] betas; |
| 55 | |
| 56 | public GammaProductLikelihood(TreeModel treeModel, double popSize, double[] means, double[] variances) { |
| 57 | |
| 58 | super(treeModel); |
| 59 | this.treeModel = treeModel; |
| 60 | this.popSize = popSize; |
| 61 | this.means = means; |
| 62 | this.variances = variances; |
| 63 | |
| 64 | System.err.println("\nProvided empirical means and variances: "); |
| 65 | for (int i = 0; i < means.length; i++) { |
| 66 | System.err.println(means[i] + " " + variances[i]); |
| 67 | } |
| 68 | |
| 69 | System.err.println("Ratio of mean squared and variance:"); |
| 70 | for (int i = 0; i < means.length; i++) { |
| 71 | System.err.println(means[i]*means[i]/variances[i]); |
| 72 | } |
| 73 | |
| 74 | //calculate alpha and beta for the gamma distributions |
| 75 | alphas = new double[means.length]; |
| 76 | betas = new double[means.length]; |
| 77 | |
| 78 | //approach 1 |
| 79 | for (int i = 0; i < alphas.length; i++) { |
| 80 | alphas[i] = means[i]*means[i]/variances[i]; |
| 81 | } |
| 82 | for (int i = 0; i < betas.length; i++) { |
| 83 | betas[i] = means[i]/variances[i]; |
| 84 | } |
| 85 | |
| 86 | /*for (int i = 0; i < alphas.length; i++) { |
| 87 | alphas[i] = 2.0*means[i]*means[i]/variances[i]; |
| 88 | } |
| 89 | for (int i = 0; i < betas.length; i++) { |
| 90 | betas[i] = 2.0*means[i]/variances[i]; |
| 91 | }*/ |
| 92 | |
| 93 | /*for (int i = 0; i < alphas.length; i++) { |
| 94 | alphas[i] = 4.0*means[i]*means[i]/variances[i]; |
| 95 | } |
| 96 | for (int i = 0; i < betas.length; i++) { |
| 97 | betas[i] = 4.0*means[i]/variances[i]; |
| 98 | }*/ |
| 99 | |
| 100 | //approach 2 - DEFINITELY NOT |
| 101 | /*for (int i = 0; i < alphas.length; i++) { |
| 102 | alphas[i] = 1.0/variances[i]; |
| 103 | } |
| 104 | for (int i = 0; i < betas.length; i++) { |
| 105 | betas[i] = 1.0/(means[i]*variances[i]); |
| 106 | }*/ |
| 107 | |
| 108 | //approach 3 |
| 109 | /*for (int i = 0; i < alphas.length; i++) { |
| 110 | alphas[i] = means[i]/variances[i]; |
| 111 | } |
| 112 | for (int i = 0; i < betas.length; i++) { |
| 113 | betas[i] = 1.0/variances[i]; |
| 114 | }*/ |
| 115 | |
| 116 | //approach 4 - reduce approach 1 to exponential |
| 117 | /*for (int i = 0; i < alphas.length; i++) { |
| 118 | alphas[i] = 1.0; |
| 119 | } |
| 120 | for (int i = 0; i < betas.length; i++) { |
| 121 | betas[i] = 1.0/means[i]; |
| 122 | }*/ |
| 123 | |
| 124 | /*for (int i = 0; i < alphas.length; i++) { |
| 125 | alphas[i] = 2.0; |
| 126 | } |
| 127 | for (int i = 0; i < betas.length; i++) { |
| 128 | betas[i] = 2.0/means[i]; |
| 129 | }*/ |
| 130 | |
| 131 | //approach 5 |
| 132 | /*for (int i = 0; i < alphas.length; i++) { |
| 133 | alphas[i] = (means[i]*means[i]*means[i])/variances[i]; |
| 134 | } |
| 135 | for (int i = 0; i < betas.length; i++) { |
| 136 | betas[i] = (means[i]*means[i])/variances[i]; |
| 137 | }*/ |
| 138 | |
| 139 | //approach 6 - still run 50 replicates for this approach |
| 140 | /*for (int i = 0; i < alphas.length; i++) { |
| 141 | alphas[i] = means[i]; |
| 142 | } |
| 143 | for (int i = 0; i < betas.length; i++) { |
| 144 | betas[i] = 1.0; |
| 145 | }*/ |
| 146 | |
| 147 | //approach 7 - still run 50 replicates for this approach |
| 148 | /*for (int i = 0; i < alphas.length; i++) { |
| 149 | alphas[i] = 1.0/means[i]; |
| 150 | } |
| 151 | for (int i = 0; i < betas.length; i++) { |
| 152 | betas[i] = 1.0/(means[i]*means[i]); |
| 153 | }*/ |
| 154 | |
| 155 | //approach 8 |
| 156 | /*for (int i = 0; i < alphas.length; i++) { |
| 157 | alphas[i] = means[i]*variances[i]; |
| 158 | } |
| 159 | for (int i = 0; i < betas.length; i++) { |
| 160 | betas[i] = variances[i]; |
| 161 | }*/ |
| 162 | |
| 163 | //approach 9 |
| 164 | /*for (int i = 0; i < alphas.length; i++) { |
| 165 | alphas[i] = variances[i]; |
| 166 | } |
| 167 | for (int i = 0; i < betas.length; i++) { |
| 168 | betas[i] = variances[i]/means[i]; |
| 169 | }*/ |
| 170 | |
| 171 | //approach 10 |
| 172 | /*for (int i = 0; i < alphas.length; i++) { |
| 173 | alphas[i] = means[i]; |
| 174 | } |
| 175 | for (int i = 0; i < betas.length; i++) { |
| 176 | betas[i] = 1.0; |
| 177 | }*/ |
| 178 | |
| 179 | if (REDUCE_TO_EXPONENTIAL) { |
| 180 | for (int i = 0; i < alphas.length; i++) { |
| 181 | alphas[i] = 1.0; |
| 182 | } |
| 183 | for (int i = 0; i < betas.length; i++) { |
| 184 | betas[i] = 1.0/popSize; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | System.err.println("\nResulting alphas and betas: "); |
| 189 | for (int i = 0; i < alphas.length; i++) { |
| 190 | System.err.println(alphas[i] + " " + betas[i] + " --> mean = " + (alphas[i]/betas[i]) + " variance = " + (alphas[i]/(betas[i]*betas[i]))); |
| 191 | } |
| 192 | |
| 193 | } |
| 194 | |
| 195 | public GammaProductLikelihood(TreeModel treeModel, double popSize, int dim) { |
| 196 | |
| 197 | super(treeModel); |
| 198 | this.treeModel = treeModel; |
| 199 | this.popSize = popSize; |
| 200 | |
| 201 | int dimension = dim; |
| 202 | |
| 203 | System.err.println("Number of intervals: " + dimension); |
| 204 | |
| 205 | //calculate alpha and beta for the gamma distributions |
| 206 | alphas = new double[dimension]; |
| 207 | betas = new double[dimension]; |
| 208 | |
| 209 | for (int i = 0; i < alphas.length; i++) { |
| 210 | alphas[i] = 0.5; |
| 211 | } |
| 212 | for (int i = 0; i < betas.length; i++) { |
| 213 | betas[i] = 0.5/this.popSize; |
| 214 | } |
| 215 | |
| 216 | System.err.println("\nResulting alphas and betas: "); |
| 217 | for (int i = 0; i < alphas.length; i++) { |
| 218 | System.err.println(alphas[i] + " " + betas[i] + " --> mean = " + (alphas[i]/betas[i]) + " variance = " + (alphas[i]/(betas[i]*betas[i]))); |
| 219 | } |
| 220 | |
| 221 | } |
| 222 | |
| 223 | public double calculateLogLikelihood() { |
| 224 | |
| 225 | double logPDF = 0.0; |
| 226 | |
| 227 | if (USE_EXPONENTIAL) { |
| 228 | |
| 229 | CoalescentTreeIntervalStatistic ctis = new CoalescentTreeIntervalStatistic(treeModel); |
| 230 | |
| 231 | for (int i = 0; i < ctis.getDimension(); i++) { |
| 232 | |
| 233 | int combinations = (int)ctis.getLineageCount(i)*((int)ctis.getLineageCount(i)-1)/2; |
| 234 | double branchLength = ctis.getStatisticValue(i); |
| 235 | |
| 236 | if (ctis.getLineageCount(i) != 1) { |
| 237 | |
| 238 | //GammaDistribution is not parameterized in terms of alpha and beta, but in terms of shape and scale! |
| 239 | if (i == ctis.getDimension()-1) { |
| 240 | //coalescent event at root: exponential density |
| 241 | logPDF += GammaDistribution.logPdf(branchLength, 1.0, 1.0/(combinations*(1.0/popSize))) - 1.0*Math.log(combinations); |
| 242 | } else if (ctis.getLineageCount(i) > ctis.getLineageCount(i+1)) { |
| 243 | //coalescent event: exponential density |
| 244 | logPDF += GammaDistribution.logPdf(branchLength, 1.0, 1.0/(combinations*(1.0/popSize))) - 1.0*Math.log(combinations); |
| 245 | } else { |
| 246 | //sampling event: exponential tail probability |
| 247 | logPDF += Math.log(1-GammaDistribution.cdf(branchLength, 1.0, 1.0/(combinations*(1.0/popSize)))); |
| 248 | } |
| 249 | |
| 250 | } |
| 251 | |
| 252 | } |
| 253 | |
| 254 | } else { |
| 255 | |
| 256 | CoalescentTreeIntervalStatistic ctis = new CoalescentTreeIntervalStatistic(treeModel); |
| 257 | |
| 258 | if (DEBUG) { |
| 259 | System.err.println(treeModel); |
| 260 | System.err.println("CoalescentTreeIntervalStatistic dimension = " + ctis.getDimension() + "\n"); |
| 261 | } |
| 262 | |
| 263 | int coalescentIntervalCounter = 0; |
| 264 | for (int i = 0; i < ctis.getDimension(); i++) { |
| 265 | |
| 266 | int combinations = (int)ctis.getLineageCount(i)*((int)ctis.getLineageCount(i)-1)/2; |
| 267 | double branchLength = ctis.getStatisticValue(i); |
| 268 | |
| 269 | if (DEBUG) { |
| 270 | System.err.println("\nIteration: " + i); |
| 271 | System.err.println("combinations = " + combinations); |
| 272 | System.err.println("branchLength = " + branchLength); |
| 273 | } |
| 274 | |
| 275 | if (ctis.getLineageCount(i) != 1) { |
| 276 | |
| 277 | //GammaDistribution is not parameterized in terms of alpha and beta, but in terms of shape and scale! |
| 278 | if (i == ctis.getDimension()-1) { |
| 279 | //coalescent event at root: gamma density |
| 280 | |
| 281 | //GammaDistribution.logPdf uses shape and scale, rather than shape and rate |
| 282 | logPDF += GammaDistribution.logPdf(branchLength, alphas[coalescentIntervalCounter], 1.0/(combinations*betas[coalescentIntervalCounter])) - Math.log(combinations); |
| 283 | |
| 284 | if (DEBUG) { |
| 285 | System.err.print("coalescent event at root: "); |
| 286 | System.err.println("branchLength = " + branchLength); |
| 287 | System.err.println("alpha = " + alphas[coalescentIntervalCounter]); |
| 288 | System.err.println("beta = " + combinations*betas[coalescentIntervalCounter]); |
| 289 | System.err.println("mean = " + (alphas[coalescentIntervalCounter]/(combinations*betas[coalescentIntervalCounter]))); |
| 290 | System.err.println("variance = " + (alphas[coalescentIntervalCounter]/((combinations*betas[coalescentIntervalCounter])*(combinations*betas[coalescentIntervalCounter])))); |
| 291 | System.err.println("combinations = " + combinations); |
| 292 | } |
| 293 | |
| 294 | } else if (ctis.getLineageCount(i) > ctis.getLineageCount(i+1)) { |
| 295 | |
| 296 | //GammaDistribution.logPdf uses shape and scale, rather than shape and rate |
| 297 | logPDF += GammaDistribution.logPdf(branchLength, alphas[coalescentIntervalCounter], 1.0/(combinations*betas[coalescentIntervalCounter])) - Math.log(combinations); |
| 298 | |
| 299 | if (DEBUG) { |
| 300 | System.err.print("coalescent event (not at root): "); |
| 301 | System.err.println(GammaDistribution.logPdf(branchLength, combinations*alphas[coalescentIntervalCounter], 1.0/(combinations*betas[coalescentIntervalCounter])) - Math.log(combinations)); |
| 302 | System.err.println("coalescentIntervalCounter = " + coalescentIntervalCounter); |
| 303 | } |
| 304 | |
| 305 | //coalesent event: move towards next empirical mean/variance |
| 306 | coalescentIntervalCounter++; |
| 307 | |
| 308 | } else { |
| 309 | //sampling event: gamma tail probability |
| 310 | |
| 311 | //GammaDistribution.cdf also uses shape and scale, rather than shape and rate |
| 312 | logPDF += Math.log(1-GammaDistribution.cdf(branchLength, alphas[coalescentIntervalCounter], 1.0/(combinations*betas[coalescentIntervalCounter]))); |
| 313 | |
| 314 | if (DEBUG) { |
| 315 | System.err.print("sampling event: "); |
| 316 | System.err.println("branchLength = " + branchLength); |
| 317 | System.err.println("alpha = " + alphas[coalescentIntervalCounter]); |
| 318 | System.err.println("beta = " + combinations*betas[coalescentIntervalCounter]); |
| 319 | System.err.println("mean = " + (alphas[coalescentIntervalCounter]/(combinations*betas[coalescentIntervalCounter]))); |
| 320 | System.err.println("variance = " + (alphas[coalescentIntervalCounter]/((combinations*betas[coalescentIntervalCounter])*(combinations*betas[coalescentIntervalCounter])))); |
| 321 | System.err.println("combinations = " + combinations); |
| 322 | System.err.println(GammaDistribution.cdf(branchLength, alphas[coalescentIntervalCounter], 1.0/(combinations*betas[coalescentIntervalCounter]))); |
| 323 | System.err.println(Math.log(1-GammaDistribution.cdf(branchLength, alphas[coalescentIntervalCounter], 1.0/(combinations*betas[coalescentIntervalCounter])))); |
| 324 | } |
| 325 | |
| 326 | } |
| 327 | |
| 328 | } |
| 329 | |
| 330 | } |
| 331 | |
| 332 | } |
| 333 | |
| 334 | if (DEBUG) { |
| 335 | System.err.println("logPDF = " + logPDF); |
| 336 | } |
| 337 | |
| 338 | return logPDF; |
| 339 | |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Overridden to always return false. |
| 344 | */ |
| 345 | protected boolean getLikelihoodKnown() { |
| 346 | return false; |
| 347 | } |
| 348 | |
| 349 | } |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | /*public class GammaProductLikelihood extends Likelihood.Abstract { |
| 356 | |
| 357 | public final static boolean USE_EXPONENTIAL = false; |
| 358 | public final static boolean REDUCE_TO_EXPONENTIAL = false; |
| 359 | |
| 360 | private TreeModel treeModel; |
| 361 | private double popSize; |
| 362 | private double[] means; |
| 363 | private double[] variances; |
| 364 | |
| 365 | public GammaProductLikelihood(TreeModel treeModel, double popSize, double[] means, double[] variances) { |
| 366 | super(treeModel); |
| 367 | this.treeModel = treeModel; |
| 368 | this.popSize = popSize; |
| 369 | this.means = means; |
| 370 | this.variances = variances; |
| 371 | } |
| 372 | |
| 373 | //make sure to provide a log(popSize) |
| 374 | //public GammaProductLikelihood(TreeModel treeModel, double popSize) { |
| 375 | // super(treeModel); |
| 376 | // this.treeModel = treeModel; |
| 377 | // this.popSize = popSize; |
| 378 | //} |
| 379 | |
| 380 | public double calculateLogLikelihood() { |
| 381 | |
| 382 | double logPDF = 0.0; |
| 383 | |
| 384 | if (USE_EXPONENTIAL) { |
| 385 | |
| 386 | CoalescentTreeIntervalStatistic ctis = new CoalescentTreeIntervalStatistic(treeModel); |
| 387 | |
| 388 | for (int i = 0; i < ctis.getDimension(); i++) { |
| 389 | |
| 390 | int combinations = (int)ctis.getLineageCount(i)*((int)ctis.getLineageCount(i)-1)/2; |
| 391 | double branchLength = ctis.getStatisticValue(i); |
| 392 | |
| 393 | if (ctis.getLineageCount(i) != 1) { |
| 394 | |
| 395 | //GammaDistribution is not parameterized in terms of alpha and beta, but in terms of shape and scale! |
| 396 | if (i == ctis.getDimension()-1) { |
| 397 | //coalescent event at root: exponential density |
| 398 | logPDF += GammaDistribution.logPdf(branchLength, 1.0, 1.0/(combinations*(1.0/popSize))) - 1.0*Math.log(combinations); |
| 399 | } else if (ctis.getLineageCount(i) > ctis.getLineageCount(i+1)) { |
| 400 | //coalescent event: exponential density |
| 401 | logPDF += GammaDistribution.logPdf(branchLength, 1.0, 1.0/(combinations*(1.0/popSize))) - 1.0*Math.log(combinations); |
| 402 | } else { |
| 403 | //sampling event: exponential tail probability |
| 404 | logPDF += Math.log(1-GammaDistribution.cdf(branchLength, 1.0, 1.0/(combinations*(1.0/popSize)))); |
| 405 | } |
| 406 | |
| 407 | } |
| 408 | |
| 409 | } |
| 410 | |
| 411 | } else { |
| 412 | |
| 413 | //System.err.println("\nProvided empirical means and variances: "); |
| 414 | //for (int i = 0; i < means.length; i++) { |
| 415 | // System.err.println(means[i] + " " + variances[i]); |
| 416 | //} |
| 417 | |
| 418 | //calculate alpha and beta for the gamma distributions |
| 419 | double[] alphas = new double[means.length]; |
| 420 | for (int i = 0; i < alphas.length; i++) { |
| 421 | alphas[i] = means[i]*means[i]/variances[i]; |
| 422 | } |
| 423 | double[] betas = new double[means.length]; |
| 424 | for (int i = 0; i < betas.length; i++) { |
| 425 | betas[i] = means[i]/variances[i]; |
| 426 | } |
| 427 | |
| 428 | if (REDUCE_TO_EXPONENTIAL) { |
| 429 | for (int i = 0; i < alphas.length; i++) { |
| 430 | alphas[i] = 1.0; |
| 431 | } |
| 432 | for (int i = 0; i < betas.length; i++) { |
| 433 | betas[i] = 1.0/popSize; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | //System.err.println("\nResulting alphas and betas: "); |
| 438 | //for (int i = 0; i < alphas.length; i++) { |
| 439 | // System.err.println(alphas[i] + " " + betas[i]); |
| 440 | //} |
| 441 | |
| 442 | CoalescentTreeIntervalStatistic ctis = new CoalescentTreeIntervalStatistic(treeModel); |
| 443 | |
| 444 | //System.err.println(treeModel); |
| 445 | |
| 446 | //System.err.println("CoalescentTreeIntervalStatistic dimension = " + ctis.getDimension()); |
| 447 | |
| 448 | int coalescentIntervalCounter = 0; |
| 449 | for (int i = 0; i < ctis.getDimension(); i++) { |
| 450 | |
| 451 | int combinations = (int)ctis.getLineageCount(i)*((int)ctis.getLineageCount(i)-1)/2; |
| 452 | //System.err.println("combinations = " + combinations); |
| 453 | double branchLength = ctis.getStatisticValue(i); |
| 454 | //System.err.println("branchLength = " + branchLength); |
| 455 | |
| 456 | if (ctis.getLineageCount(i) != 1) { |
| 457 | |
| 458 | //GammaDistribution is not parameterized in terms of alpha and beta, but in terms of shape and scale! |
| 459 | if (i == ctis.getDimension()-1) { |
| 460 | //coalescent event at root: gamma density |
| 461 | //System.err.print("coalescent event at root: "); |
| 462 | logPDF += GammaDistribution.logPdf(branchLength, alphas[coalescentIntervalCounter], 1.0/(combinations*betas[coalescentIntervalCounter])) - Math.log(combinations); |
| 463 | |
| 464 | //logPDF += GammaDistribution.logPdf(branchLength, alphas[coalescentIntervalCounter], 1.0/(combinations*betas[coalescentIntervalCounter])) - alphas[coalescentIntervalCounter]*Math.log(combinations); |
| 465 | //System.err.println(GammaDistribution.logPdf(branchLength, alphas[coalescentIntervalCounter], 1.0/(combinations*betas[coalescentIntervalCounter])) - Math.log(combinations)); |
| 466 | //coalesent event: move towards next empirical mean/variance but nowhere else to go |
| 467 | //coalescentIntervalCounter++; |
| 468 | } else if (ctis.getLineageCount(i) > ctis.getLineageCount(i+1)) { |
| 469 | //coalescent event: gamma density |
| 470 | //System.err.print("coalescent event (not at root): "); |
| 471 | logPDF += GammaDistribution.logPdf(branchLength, alphas[coalescentIntervalCounter], 1.0/(combinations*betas[coalescentIntervalCounter])) - Math.log(combinations); |
| 472 | |
| 473 | //logPDF += GammaDistribution.logPdf(branchLength, alphas[coalescentIntervalCounter], 1.0/(combinations*betas[coalescentIntervalCounter])) - alphas[coalescentIntervalCounter]*Math.log(combinations); |
| 474 | //System.err.println(GammaDistribution.logPdf(branchLength, alphas[coalescentIntervalCounter], 1.0/(combinations*betas[coalescentIntervalCounter])) - Math.log(combinations)); |
| 475 | //coalesent event: move towards next empirical mean/variance |
| 476 | coalescentIntervalCounter++; |
| 477 | } else { |
| 478 | //sampling event: gamma tail probability |
| 479 | //System.err.print("sampling event: "); |
| 480 | logPDF += Math.log(1-GammaDistribution.cdf(branchLength, alphas[coalescentIntervalCounter], 1.0/(combinations*betas[coalescentIntervalCounter]))); |
| 481 | //System.err.println(Math.log(1-GammaDistribution.cdf(branchLength, alphas[coalescentIntervalCounter], 1.0/(combinations*betas[coalescentIntervalCounter])))); |
| 482 | //System.err.println(alphas[coalescentIntervalCounter]); |
| 483 | //System.err.println(betas[coalescentIntervalCounter]); |
| 484 | //System.err.println("coalescentIntervalCounter = " + coalescentIntervalCounter); |
| 485 | //System.err.println(1-GammaDistribution.cdf(branchLength, alphas[coalescentIntervalCounter], 1.0/(combinations*betas[coalescentIntervalCounter]))); |
| 486 | } |
| 487 | |
| 488 | } |
| 489 | |
| 490 | } |
| 491 | |
| 492 | } |
| 493 | |
| 494 | //System.err.println("logPDF = " + logPDF); |
| 495 | //System.exit(0); |
| 496 | |
| 497 | return logPDF; |
| 498 | |
| 499 | } |
| 500 | |
| 501 | |
| 502 | |
| 503 | public double calculateOldLogLikelihood() { |
| 504 | |
| 505 | double logPDF = 0.0; |
| 506 | |
| 507 | //Should not be used |
| 508 | double logPopSize = 0.0; |
| 509 | |
| 510 | //means and variances are probably in the reverse order |
| 511 | //System.err.println("\nProvided empirical means and variances: "); |
| 512 | //for (int i = 0; i < means.length; i++) { |
| 513 | //System.err.println(means[i] + " " + variances[i]); |
| 514 | //} |
| 515 | |
| 516 | CoalescentTreeIntervalStatistic ctis = new CoalescentTreeIntervalStatistic(treeModel); |
| 517 | |
| 518 | //System.err.println("\nDimension = " + ctis.getDimension() + "\nLineage info: "); |
| 519 | //for (int i = ctis.getDimension()-1; i >= 0; i--) { |
| 520 | //System.err.println(ctis.getLineageCount(i)); |
| 521 | //} |
| 522 | //System.err.println("\nStatistic values: "); |
| 523 | //for (int i = ctis.getDimension()-1; i >= 0; i--) { |
| 524 | //System.err.println(ctis.getStatisticValue(i)); |
| 525 | //} |
| 526 | |
| 527 | //ignore possibility of more than 1 dimension for now |
| 528 | //System.err.println("\nPopulation size = " + popSize); |
| 529 | |
| 530 | //System.err.println("\nTree: " + treeModel); |
| 531 | |
| 532 | //calculate alpha and beta for the gamma distributions |
| 533 | double[] alphas = new double[means.length]; |
| 534 | for (int i = 0; i < alphas.length; i++) { |
| 535 | alphas[i] = means[i]*means[i]/variances[i]; |
| 536 | } |
| 537 | double[] betas = new double[means.length]; |
| 538 | for (int i = 0; i < betas.length; i++) { |
| 539 | betas[i] = means[i]/variances[i]; |
| 540 | } |
| 541 | |
| 542 | if (REDUCE_TO_EXPONENTIAL) { |
| 543 | for (int i = 0; i < alphas.length; i++) { |
| 544 | alphas[i] = 1.0; |
| 545 | } |
| 546 | for (int i = 0; i < betas.length; i++) { |
| 547 | betas[i] = 1.0/logPopSize; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | int indicator = 0; |
| 552 | for (int i = ctis.getDimension()-1; i > 0; i--) { |
| 553 | //System.err.println("\nInterval " + i); |
| 554 | if (i == ctis.getDimension()-1) { |
| 555 | //coalescent event: gamma density |
| 556 | //System.err.println("Coalescent event at root"); |
| 557 | //System.err.println("Interval length = " + ctis.getStatisticValue(i)); |
| 558 | //System.err.println("Lineage count = " + ctis.getLineageCount(i)); |
| 559 | int combinations = (int)ctis.getLineageCount(i)*((int)ctis.getLineageCount(i)-1)/2; |
| 560 | //System.err.println("Combinations = " + combinations); |
| 561 | logPDF += GammaDistribution.logPdf(ctis.getStatisticValue(i), alphas[indicator], 1.0/(combinations*betas[indicator])) - alphas[indicator]*Math.log(combinations); |
| 562 | //System.err.println(GammaDistribution.logPdf(ctis.getStatisticValue(i), alphas[indicator], 1.0/(combinations*betas[indicator])) - alphas[indicator]*Math.log(combinations)); |
| 563 | if (indicator < (means.length-1)) { |
| 564 | indicator++; |
| 565 | } |
| 566 | } else if (ctis.getLineageCount(i) - ctis.getLineageCount(i+1) > 0) { |
| 567 | //coalescent event: gamma density |
| 568 | //System.err.println("Coalescent event"); |
| 569 | //System.err.println("Interval length = " + ctis.getStatisticValue(i)); |
| 570 | //System.err.println("Lineage count = " + ctis.getLineageCount(i)); |
| 571 | int combinations = (int)ctis.getLineageCount(i)*((int)ctis.getLineageCount(i)-1)/2; |
| 572 | //System.err.println("Combinations = " + combinations); |
| 573 | logPDF += GammaDistribution.logPdf(ctis.getStatisticValue(i), alphas[indicator], 1.0/(combinations*betas[indicator])) - alphas[indicator]*Math.log(combinations); |
| 574 | //System.err.println(GammaDistribution.logPdf(ctis.getStatisticValue(i), alphas[indicator], 1.0/(combinations*betas[indicator])) - alphas[indicator]*Math.log(combinations)); |
| 575 | if (indicator < (means.length-1)) { |
| 576 | indicator++; |
| 577 | } |
| 578 | } else { |
| 579 | //new sample: gamma tail probability |
| 580 | //System.err.println("New sample"); |
| 581 | //System.err.println("Interval length = " + ctis.getStatisticValue(i)); |
| 582 | //System.err.println("Lineage count = " + ctis.getLineageCount(i)); |
| 583 | int combinations = (int)ctis.getLineageCount(i)*((int)ctis.getLineageCount(i)-1)/2; |
| 584 | //System.err.println("Combinations = " + combinations); |
| 585 | logPDF += Math.log(1-GammaDistribution.cdf(ctis.getStatisticValue(i), alphas[indicator], 1.0/(combinations*betas[indicator]))); |
| 586 | //System.err.println(Math.log(1-GammaDistribution.cdf(ctis.getStatisticValue(i), alphas[indicator], 1.0/(combinations*betas[indicator])))); |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | //System.err.println("\nlogPDF = " + logPDF); |
| 591 | //System.exit(0); |
| 592 | |
| 593 | return logPDF; |
| 594 | } |
| 595 | |
| 596 | protected boolean getLikelihoodKnown() { |
| 597 | return false; |
| 598 | } |
| 599 | |
| 600 | }*/ |