Debian Codegraph @

src/dr/evomodel/speciation/SpeciesTreeBMPrior.java

raw symbols SCIP
1 /*
2 * SpeciesTreeBMPrior.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.speciation;
27
28 import dr.evolution.tree.NodeRef;
29 import dr.inference.distribution.ParametricDistributionModel;
30 import dr.inference.model.CompoundModel;
31 import dr.inference.model.Likelihood;
32 import dr.inference.model.Parameter;
33
34 /**
35 * @author Joseph Heled
36 * Date: 8/12/2008
37 */
38 public class SpeciesTreeBMPrior extends Likelihood.Abstract {
39
40 private final SpeciesTreeModel sTree;
41 //private final ParametricDistributionModel dist;
42 private final ParametricDistributionModel tips;
43 private final Parameter popSigma;
44 private final Parameter stSigma;
45
46 private static final double d1 = (1 - Math.exp(-1));
47 private static final double f2 = -0.5 * Math.log(2*Math.PI);
48 private final boolean logRoot;
49
50 // public SpeciesTreeSimplePrior(SpeciesTreeModel sTree, ParametricDistributionModel dist, ParametricDistributionModel tipsPrior) {
51 // super(new CompoundModel("STprior"));
52 // this.sTree = sTree;
53 // this.dist = dist;
54 // this.tips = tipsPrior;
55 //
56 // final CompoundModel cm = (CompoundModel)this.getModel();
57 // cm.addModel(tipsPrior);
58 // cm.addModel(dist);
59 // cm.addModel(sTree);
60 // }
61
62 public SpeciesTreeBMPrior(SpeciesTreeModel sTree, Parameter popSigma, Parameter stSigma,
63 ParametricDistributionModel tipsPrior, boolean logRoot) {
64 super(new CompoundModel("STBMprior"));
65
66 this.sTree = sTree;
67 this.popSigma = popSigma;
68 this.stSigma = stSigma;
69 this.tips = tipsPrior;
70 this.logRoot = logRoot;
71
72 final CompoundModel cm = (CompoundModel)this.getModel();
73
74 cm.addModel(tipsPrior);
75 cm.addModel(sTree);
76 }
77
78 protected double calculateLogLikelihood() {
79 double logLike = 0;
80
81 //if( true ) {
82 final NodeRef root = sTree.getRoot();
83 final double stRootHeight = sTree.getNodeHeight(root);
84
85 final SpeciesTreeModel.RawPopulationHelper helper = sTree.getPopulationHelper();
86 final double lim = stRootHeight ;//helper.geneTreesRootHeight() ;
87
88 {
89 final double sigma = stSigma.getParameterValue(0);
90 final double s2 = 2 * sigma * sigma;
91 int count = 0;
92 double[] pops = new double[2];
93
94 for(int nn = 0; nn < sTree.getNodeCount(); ++nn) {
95 final NodeRef n = sTree.getNode(nn);
96
97 if( sTree.isExternal(n) ) {
98 logLike += tips.logPdf(helper.tipPopulation(n));
99 } else {
100 for(int nc = 0; nc < 2; ++nc) {
101 final NodeRef child = sTree.getChild(n, nc);
102 helper.getPopulations(n, nc, pops);
103 final double dt = sTree.getBranchLength(child) / lim;
104 final double pDiff = (pops[1] - pops[0]) / lim;
105 logLike -= pDiff * pDiff / (s2 * dt);
106 //need to adjest for dt!
107 logLike -= .5 * Math.log(dt);
108 count += 1;
109 }
110 }
111 }
112
113 if( ! helper.perSpeciesPopulation() ) {
114 helper.getRootPopulations(pops);
115 final double dt = helper.geneTreesRootHeight()/lim - 1;
116 //final double dt = (lim - stRootHeight) / lim;
117 // log(p1/lim) - log(p0/lim) = log(p1/p0)
118 final double pDiff = logRoot ? Math.log(pops[1]/pops[0]) : (pops[1] - pops[0])/lim;
119
120 logLike -= pDiff * pDiff / (s2 * dt);
121 //need to adjust for dt!
122 logLike -= .5 * Math.log(dt);
123 count += 1;
124 }
125
126 logLike += count * (f2 - Math.log(sigma));
127 }
128
129 if( helper.perSpeciesPopulation() ) {
130 final double sigma = (popSigma != null ? popSigma : stSigma).getParameterValue(0);
131 final double s2 = 2 * sigma * sigma;
132
133 for(int ns = 0; ns < helper.nSpecies(); ++ns) {
134 double[] times = helper.getTimes(ns);
135 double[] pops = helper.getPops(ns);
136 final double tMax = times[times.length-1];
137
138 double ll = 0.0;
139 double x = pops[0]/tMax;
140 double t0 = 0.0;
141 for(int k = 1; k < times.length; ++k) {
142 final double y = pops[k]/tMax;
143 final double dt = (times[k-1] - t0) / tMax;
144 ll -= (y - x)*(y-x) / (s2*dt);
145 // need to adjest for dt!
146 ll -= .5 * Math.log(dt);
147 x = y;
148 t0 = times[k-1];
149 }
150 ll += (times.length-1) * (f2 - Math.log(sigma));
151
152 logLike += ll;
153 }
154 }
155 //}
156 return logLike;
157 }
158
159 protected boolean getLikelihoodKnown() {
160 return false;
161 }
162
163 }