Debian Codegraph @

src/dr/oldevomodel/clock/UCLikelihood.java

raw symbols SCIP
1 /*
2 * UCLikelihood.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.oldevomodel.clock;
27
28 import dr.evomodel.tree.TreeModel;
29 import dr.inference.model.Parameter;
30 import dr.math.MathUtils;
31 import dr.math.distributions.NormalDistribution;
32
33 /**
34 * Calculates the likelihood of a set of rate changes in a tree, assuming that rates are lognormally distributed
35 * cf Yang and Rannala 2006
36 *
37 * @author Michael Defoin Platel
38 */
39 public class UCLikelihood extends RateEvolutionLikelihood {
40
41 public UCLikelihood(TreeModel tree, Parameter ratesParameter, Parameter variance, Parameter rootRate, boolean isLogSpace) {
42
43 super((isLogSpace) ? "LogNormally Distributed" : "Normally Distributed", tree, ratesParameter, rootRate, false);
44
45 this.isLogSpace = isLogSpace;
46 this.variance = variance;
47
48 addVariable(variance);
49 }
50
51 /**
52 * @return the log likelihood of the rate.
53 */
54 double branchRateChangeLogLikelihood(double foo1, double rate, double foo2) {
55 double var = variance.getParameterValue(0);
56 double meanRate = rootRateParameter.getParameterValue(0);
57
58
59 if (isLogSpace) {
60 final double logmeanRate = Math.log(meanRate);
61 final double logRate = Math.log(rate);
62
63 return NormalDistribution.logPdf(logRate, logmeanRate - (var / 2.), Math.sqrt(var)) - logRate;
64
65 } else {
66 return NormalDistribution.logPdf(rate, meanRate, Math.sqrt(var));
67 }
68 }
69
70 double branchRateSample(double foo1, double foo2) {
71 double meanRate = rootRateParameter.getParameterValue(0);
72
73 double var = variance.getParameterValue(0);
74
75 if (isLogSpace) {
76 final double logMeanRate = Math.log(meanRate);
77
78 return Math.exp(MathUtils.nextGaussian() * Math.sqrt(var) + logMeanRate - (var / 2.));
79 } else {
80 return MathUtils.nextGaussian() * Math.sqrt(var) + meanRate;
81 }
82 }
83
84 private final Parameter variance;
85
86 boolean isLogSpace = false;
87 }