Debian Codegraph @

src/dr/evomodel/alloppnet/speciation/BirthDeathCollapseModel.java

raw symbols SCIP
1 /*
2 * BirthDeathCollapseModel.java
3 *
4 * Copyright (c) 2002-2017 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 /*
27 This file is part of BEAST.
28
29 BEAST is free software: you can redistribute it and/or modify
30 it under the terms of the GNU General Public License as published by
31 the Free Software Foundation, either version 3 of the License, or
32 (at your option) any later version.
33
34 BEAST is distributed in the hope that it will be useful,
35 but WITHOUT ANY WARRANTY; without even the implied warranty of
36 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 GNU General Public License for more details.
38
39 You should have received a copy of the GNU General Public License
40 along with DISSECT. If not, see <http://www.gnu.org/licenses/>.
41 */
42
43 package dr.evomodel.alloppnet.speciation;
44
45
46 /**
47 *
48 * Model for prior for species delimitation in multispecies coalescent model.
49 *
50 * @author Graham Jones
51 * Date: 01/09/2013
52 */
53
54 import dr.evolution.util.Taxon;
55 import dr.evolution.util.Units;
56 import dr.evolution.tree.Tree;
57 import dr.evomodel.speciation.SpeciationModel;
58 import dr.inference.model.Parameter;
59 import dr.util.Author;
60 import dr.util.Citable;
61 import dr.util.Citation;
62
63 import java.util.*;
64 import java.util.logging.Logger;
65
66
67 public class BirthDeathCollapseModel extends SpeciationModel implements Citable {
68 private Parameter birthDiffRate; // lambda - mu
69 private Parameter relativeDeathRate; // mu/lambda
70 private Parameter originHeight;
71 private Parameter collapseWeight;
72 private final double collapseHeight;
73
74 public BirthDeathCollapseModel(String modelName, Tree tree, Units.Type units,
75 Parameter birthDiffRate, Parameter relativeDeathRate,
76 Parameter originHeight, Parameter collapseWeight, double collH) {
77 super(modelName, units);
78 this.collapseHeight = collH;
79
80 this.birthDiffRate = birthDiffRate;
81 addVariable(birthDiffRate);
82 birthDiffRate.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
83
84 this.relativeDeathRate = relativeDeathRate;
85 addVariable(relativeDeathRate);
86 relativeDeathRate.addBounds(new Parameter.DefaultBounds(1.0, 0.0, 1));
87
88 this.originHeight = originHeight;
89 originHeight.setParameterValue(0, 1.05 * tree.getNodeHeight(tree.getRoot()));
90 addVariable(originHeight);
91 originHeight.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0, 1));
92
93 this.collapseWeight = collapseWeight;
94 addVariable(collapseWeight);
95 collapseWeight.addBounds(new Parameter.DefaultBounds(1.0, 0.0, 1));
96
97 Logger.getLogger("dr.evomodel.speciation").info("\tConstructing a birth-death-collapse model, please cite:\n"
98 + Citable.Utils.getCitationString(this));
99 }
100
101
102 public double getCollapseHeight() {
103 return collapseHeight;
104 }
105
106 // provided to help avoid inconsistent treatment of h == collapseHeight
107 public static boolean belowCollapseHeight(double h, double collapseHeight) {
108 return (h < collapseHeight);
109 }
110
111
112 @Override
113 public double calculateTreeLogLikelihood(Tree tree) {
114 double logpt = 0.0;
115 int ninodes = tree.getInternalNodeCount();
116 int ntips = tree.getExternalNodeCount();
117 double alpha = birthDiffRate.getParameterValue(0);
118 double beta = relativeDeathRate.getParameterValue(0);
119 double tor = originHeight.getParameterValue(0);
120 double w = collapseWeight.getParameterValue(0);
121
122 double rooth = tree.getNodeHeight(tree.getRoot());
123 if (rooth > tor) {
124 return Double.NEGATIVE_INFINITY;
125 }
126
127 logpt += originHeightLogLikelihood(tor, alpha, beta, w, ntips);
128
129 for (int n = 0; n < ninodes; n++) {
130 final double height = tree.getNodeHeight(tree.getInternalNode(n));
131 double usualpn = nodeHeightLikelihood(height, tor, alpha, beta);
132 double collapsedpn = (height < collapseHeight) ? 1.0 / collapseHeight : 0.0;
133 logpt += Math.log((1.0 - w) * usualpn + w * collapsedpn);
134 }
135
136 return logpt;
137 }
138
139
140 private double originHeightLogLikelihood(double t, double a, double b, double w, int n) {
141 double E = Math.exp(-a * t);
142 double B = (1 - E) / (1-b*E);
143 double z = 0.0;
144 z += Math.log(a);
145 z += Math.log(1 - b);
146 z -= a * t;
147 z -= 2 * Math.log(1 - b * E);
148 z += (n-2) * Math.log(w + (1 - w) * B);
149 z += Math.log(w + n * (1 - w) * B);
150 return z;
151 }
152
153
154 private double nodeHeightLikelihood(double s, double t, double a, double b) {
155 double Es = Math.exp(-a * s);
156 double Et = Math.exp(-a * t);
157 double z = 0.0;
158 if (s < t) {
159 z = a;
160 z *= (1 - b);
161 z *= Es;
162 z /= (1 - b * Es) * (1 - b * Es);
163 z *= (1 - b * Et);
164 z /= (1 - Et);
165 }
166 return z;
167 }
168
169
170 @Override
171 public double calculateTreeLogLikelihood(Tree tree, Set<Taxon> exclude) {
172 // not implemented.
173 return Double.NEGATIVE_INFINITY;
174 }
175
176 @Override
177 public Citation.Category getCategory() {
178 return Citation.Category.SPECIES_MODELS;
179 }
180
181 @Override
182 public String getDescription() {
183 return "DISSECT species delimitation model";
184 }
185
186 @Override
187 public List<Citation> getCitations() {
188 return Collections.singletonList(
189 new Citation(
190 new Author[]{
191 new Author("Graham", "Jones"),
192 new Author("Bengt", "Oxelman")
193 },
194 "DISSECT: an assignment-free Bayesian discovery method for species delimitation under the multispecies coalescent",
195 2014,
196 "BIORXIV/2014/003178",
197 -1,
198 -1,
199 -1,
200 Citation.Status.IN_SUBMISSION
201 ));
202 }
203 }