Debian Codegraph @

src/dr/evomodel/speciation/SpeciesTreeSimplePrior.java

raw symbols SCIP
1 /*
2 * SpeciesTreeSimplePrior.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.coalescent.DemographicFunction;
29 import dr.evolution.tree.NodeRef;
30 import dr.evomodel.coalescent.VDdemographicFunction;
31 import dr.inference.distribution.ParametricDistributionModel;
32 import dr.inference.model.CompoundModel;
33 import dr.inference.model.Likelihood;
34 import dr.inference.model.Parameter;
35
36 /**
37 * @author Joseph Heled
38 * Date: 8/12/2008
39 */
40 public class SpeciesTreeSimplePrior extends Likelihood.Abstract {
41
42 private final SpeciesTreeModel sTree;
43 //private final ParametricDistributionModel dist;
44 private final ParametricDistributionModel tips;
45 private final Parameter sigma;
46 private static final double d1 = (1 - Math.exp(-1));
47 private static final double f2 = Math.log(Math.sqrt(2*Math.PI));
48
49 // public SpeciesTreeSimplePrior(SpeciesTreeModel sTree, ParametricDistributionModel dist, ParametricDistributionModel tipsPrior) {
50 // super(new CompoundModel("STprior"));
51 // this.sTree = sTree;
52 // this.dist = dist;
53 // this.tips = tipsPrior;
54 //
55 // final CompoundModel cm = (CompoundModel)this.getModel();
56 // cm.addModel(tipsPrior);
57 // cm.addModel(dist);
58 // cm.addModel(sTree);
59 // }
60
61 public SpeciesTreeSimplePrior(SpeciesTreeModel sTree, Parameter sigma, ParametricDistributionModel tipsPrior) {
62 super(new CompoundModel("STprior"));
63 this.sTree = sTree;
64 this.sigma = sigma;
65 this.tips = tipsPrior;
66
67 final CompoundModel cm = (CompoundModel)this.getModel();
68 cm.addModel(tipsPrior);
69 cm.addModel(sTree);
70 }
71
72 protected double calculateLogLikelihood() {
73 double ll = 0;
74 final NodeRef root = sTree.getRoot();
75 double l = sTree.getNodeHeight(root) + ((VDdemographicFunction)sTree.getNodeDemographic(root)).naturalLimit() ;
76
77 for(int nn = 0; nn < sTree.getNodeCount(); ++nn) {
78 final NodeRef n = sTree.getNode(nn);
79 final DemographicFunction demog = sTree.getNodeDemographic(n);
80
81 if( sTree.isExternal(n) ) {
82 ll += tips.logPdf(demog.getDemographic(0));
83 }
84
85 final double branch = sTree.isRoot(n) ? ((VDdemographicFunction)demog).naturalLimit()
86 : sTree.getBranchLength(n);
87 final double avg = branch/demog.getIntegral(0, branch);
88
89 final double p = demog.getDemographic(branch);
90 final double s = sigma.getParameterValue(0) * Math.sqrt((1 - Math.exp(-branch/l)) / d1);
91 //final double vx = (new LogNormalDistribution(-0.5 * s * s, s)).logPdf(p / avg);
92
93 double z = p/avg;
94 final double v2 = Math.log(z);
95 final double v1 = v2 / s + s / 2;
96 final double v = -Math.log(s) - f2 - v2 - v1*v1/2;
97 ll += v;
98 //ll += dist.logPdf(p/avg);
99 //
100 // double[] pa = {demog.getDemographic(0), demog.getDemographic(branch)};
101 // for( double p : pa ) {
102 // ll += dist.logPdf(p/avg);
103 // }
104 }
105
106 return ll;
107 }
108
109 protected boolean getLikelihoodKnown() {
110 return false;
111 }
112
113 }