Debian Codegraph @

src/dr/oldevomodel/clock/ACLikelihood.java

raw symbols SCIP
1 /*
2 * ACLikelihood.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.InverseGaussianDistribution;
32 import dr.math.distributions.LogNormalDistribution;
33 import dr.math.distributions.NormalDistribution;
34
35 /**
36 * Calculates the likelihood of a set of rate changes in a tree, assuming a (log)normal or inverse gaussian distributed
37 * change in rate at each node, with a mean of the previous (log) rate and a variance proportional to branch length.
38 * cf Yang and Rannala 2006
39 *
40 * @author Michael Defoin Platel
41 * @author Wai Lok Sibon Li
42 */
43 public class ACLikelihood extends RateEvolutionLikelihood {
44 public static final String LOGNORMAL = "logNormal";
45 public static final String NORMAL = "normal";
46 public static final String INVERSEGAUSSIAN = "inverseGaussian";
47
48 public ACLikelihood(TreeModel tree, Parameter ratesParameter, Parameter variance, Parameter rootRate,
49 boolean isEpisodic, String distribution) {
50
51 //super((isLogSpace) ? "LogNormally Distributed" : "Normally Distributed", tree, ratesParameter, rootRate, isEpisodic);
52 super(distribution, tree, ratesParameter, rootRate, isEpisodic);
53
54 //this.isLogSpace = isLogSpace;
55 this.variance = variance;
56 this.distribution = distribution;
57
58 addVariable(variance);
59 }
60
61 /**
62 * @return the log likelihood of the rate change from the parent to the child.
63 */
64 double branchRateChangeLogLikelihood(double parentRate, double childRate, double time) {
65 double var = variance.getParameterValue(0);
66
67 if (!isEpisodic())
68 var *= time;
69
70 //if (isLogSpace) {
71 // double logParentRate = Math.log(parentRate);
72 // double logChildRate = Math.log(childRate);
73
74 // return NormalDistribution.logPdf(logChildRate, logParentRate - (var / 2.), Math.sqrt(var)) - logChildRate;
75
76 //} else {
77 // return NormalDistribution.logPdf(childRate, parentRate, Math.sqrt(var));
78 //}
79 if(distribution.equals(LOGNORMAL)) {
80 return LogNormalDistribution.logPdf(childRate, Math.log(parentRate) - (var / 2.), Math.sqrt(var));
81 }
82 else if(distribution.equals(NORMAL)) {
83 return NormalDistribution.logPdf(childRate, parentRate, Math.sqrt(var));
84 }
85 else if(distribution.equals(INVERSEGAUSSIAN)) { /* Inverse Gaussian */
86 double shape = (parentRate * parentRate * parentRate) / var;
87 return InverseGaussianDistribution.logPdf(childRate, parentRate, shape);
88 }
89 else {
90 throw new RuntimeException ("Parameter for distribution is not recognised");
91 }
92 }
93
94 double branchRateSample(double parentRate, double time) {
95
96 double var = variance.getParameterValue(0);
97
98 if (!isEpisodic())
99 var *= time;
100
101 //if (isLogSpace) {
102 // final double logParentRate = Math.log(parentRate);
103
104 // return Math.exp(MathUtils.nextGaussian() * Math.sqrt(var) + logParentRate - (var / 2.));
105 //} else {
106 // return MathUtils.nextGaussian() * Math.sqrt(var) + parentRate;
107 //}
108
109 if(distribution.equals(LOGNORMAL)) {
110 final double logParentRate = Math.log(parentRate);
111
112 return Math.exp(MathUtils.nextGaussian() * Math.sqrt(var) + logParentRate - (var / 2.));
113 }
114 else if(distribution.equals(NORMAL)) {
115 return MathUtils.nextGaussian() * Math.sqrt(var) + parentRate;
116 }
117 else { /* Inverse Gaussian */
118 //return Math.random()
119 //Random rand = new Random();
120 double lambda = (parentRate * parentRate * parentRate) / var;
121 return MathUtils.nextInverseGaussian(parentRate, lambda);
122 }
123 }
124
125 private Parameter variance;
126 //boolean isLogSpace = false;
127 String distribution;
128 }