Debian Codegraph @

src/dr/evolution/coalescent/Coalescent.java

raw symbols SCIP
1 /*
2 * Coalescent.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.evolution.coalescent;
27
28 import dr.evolution.tree.Tree;
29 import dr.evolution.util.Units;
30 import dr.math.Binomial;
31 import dr.math.MultivariateFunction;
32
33 /**
34 * A likelihood function for the coalescent. Takes a tree and a demographic model.
35 *
36 * Parts of this class were derived from C++ code provided by Oliver Pybus.
37 *
38 * @version $Id: Coalescent.java,v 1.12 2005/05/24 20:25:55 rambaut Exp $
39 *
40 * @author Andrew Rambaut
41 * @author Alexei Drummond
42 */
43 public class Coalescent implements MultivariateFunction, Units {
44
45 // PUBLIC STUFF
46
47 public Coalescent(Tree tree, DemographicFunction demographicFunction) {
48 this(new TreeIntervals(tree), demographicFunction);
49 }
50
51 public Coalescent(IntervalList intervals, DemographicFunction demographicFunction) {
52
53 this.intervals = intervals;
54 this.demographicFunction = demographicFunction;
55 }
56
57
58 /**
59 * Calculates the log likelihood of this set of coalescent intervals,
60 * given a demographic model.
61 */
62 public double calculateLogLikelihood() {
63
64 return calculateLogLikelihood(intervals, demographicFunction);
65 }
66
67 /**
68 * Calculates the log likelihood of this set of coalescent intervals,
69 * given a demographic model.
70 */
71 public static double calculateLogLikelihood(IntervalList intervals, DemographicFunction demographicFunction) {
72 return calculateLogLikelihood(intervals, demographicFunction, 0.0);
73 }
74
75 /**
76 * Calculates the log likelihood of this set of coalescent intervals,
77 * given a demographic model.
78 */
79 public static double calculateLogLikelihood(IntervalList intervals, DemographicFunction demographicFunction, double threshold) {
80
81 double logL = 0.0;
82
83 double startTime = 0.0;
84 final int n = intervals.getIntervalCount();
85 for (int i = 0; i < n; i++) {
86
87 final double duration = intervals.getInterval(i);
88 final double finishTime = startTime + duration;
89
90 final double intervalArea = demographicFunction.getIntegral(startTime, finishTime);
91 if( intervalArea == 0 && duration != 0 ) {
92 return Double.NEGATIVE_INFINITY;
93 }
94 final int lineageCount = intervals.getLineageCount(i);
95
96 final double kChoose2 = Binomial.choose2(lineageCount);
97 // common part
98 logL += -kChoose2 * intervalArea;
99
100 if (intervals.getIntervalType(i) == IntervalType.COALESCENT) {
101
102 final double demographicAtCoalPoint = demographicFunction.getDemographic(finishTime);
103
104 // if value at end is many orders of magnitude different than mean over interval reject the interval
105 // This is protection against cases where ridiculous infinitesimal population size at the end of a
106 // linear interval drive coalescent values to infinity.
107
108 if( duration == 0.0 || demographicAtCoalPoint * (intervalArea/duration) >= threshold ) {
109 // if( duration == 0.0 || demographicAtCoalPoint >= threshold * (duration/intervalArea) ) {
110 logL -= Math.log(demographicAtCoalPoint);
111 } else {
112 // remove this at some stage
113 // System.err.println("Warning: " + i + " " + demographicAtCoalPoint + " " + (intervalArea/duration) );
114 return Double.NEGATIVE_INFINITY;
115 }
116
117 }
118
119 startTime = finishTime;
120 }
121
122 return logL;
123 }
124
125 /**
126 * Calculates the log likelihood of this set of coalescent intervals,
127 * using an analytical integration over theta.
128 */
129 public static double calculateAnalyticalLogLikelihood(IntervalList intervals) {
130
131 if (!intervals.isCoalescentOnly()) {
132 throw new IllegalArgumentException("Can only calculate analytical likelihood for pure coalescent intervals");
133 }
134
135 final double lambda = getLambda(intervals);
136 final int n = intervals.getSampleCount();
137
138 // assumes a 1/theta prior
139 //logLikelihood = Math.log(1.0/Math.pow(lambda,n));
140
141 // assumes a flat prior
142 return (1-n) * Math.log(lambda); // Math.log(1.0/Math.pow(lambda,n-1));
143 }
144
145 /**
146 * Returns a factor lambda such that the likelihood can be expressed as
147 * 1/theta^(n-1) * exp(-lambda/theta). This allows theta to be integrated
148 * out analytically. :-)
149 */
150 private static double getLambda(IntervalList intervals) {
151 double lambda = 0.0;
152 for (int i= 0; i < intervals.getIntervalCount(); i++) {
153 lambda += (intervals.getInterval(i) * intervals.getLineageCount(i));
154 }
155 lambda /= 2;
156
157 return lambda;
158 }
159
160 // **************************************************************
161 // MultivariateFunction IMPLEMENTATION
162 // **************************************************************
163
164 public double evaluate(double[] argument) {
165 for (int i = 0; i < argument.length; i++) {
166 demographicFunction.setArgument(i, argument[i]);
167 }
168
169 return calculateLogLikelihood();
170 }
171
172 public int getNumArguments() {
173 return demographicFunction.getNumArguments();
174 }
175
176 public double getLowerBound(int n) {
177 return demographicFunction.getLowerBound(n);
178 }
179
180 public double getUpperBound(int n) {
181 return demographicFunction.getUpperBound(n);
182 }
183
184 // **************************************************************
185 // Units IMPLEMENTATION
186 // **************************************************************
187
188 /**
189 * Sets the units these coalescent intervals are
190 * measured in.
191 */
192 public final void setUnits(Type u)
193 {
194 demographicFunction.setUnits(u);
195 }
196
197 /**
198 * Returns the units these coalescent intervals are
199 * measured in.
200 */
201 public final Type getUnits()
202 {
203 return demographicFunction.getUnits();
204 }
205
206 public DemographicFunction getDemographicFunction(){
207 return demographicFunction;
208 }
209
210 public IntervalList getIntervals(){
211 return intervals;
212 }
213
214 /** The demographic function. */
215 DemographicFunction demographicFunction = null;
216
217 /** The intervals. */
218 IntervalList intervals = null;
219 }