Debian Codegraph @

src/dr/evolution/coalescent/MultiEpochExponential.java

raw symbols SCIP
1 /*
2 * MultiEpochExponential.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.math.matrixAlgebra.Vector;
29
30 /**
31 * This class models a multi-phase exponential growth
32 *
33 * @author Marc A. Suchard
34 */
35
36 public class MultiEpochExponential extends ConstantPopulation {
37
38 /**
39 * Construct demographic model with default settings
40 */
41 public MultiEpochExponential(Type units, int numEpoch) {
42
43 super(units);
44 transitionTime = new double[numEpoch - 1];
45 rate = new double[numEpoch];
46 }
47
48 public void setTransitionTime(int index, double transitionTime) {
49 this.transitionTime[index] = transitionTime;
50 }
51
52 public void setGrowthRate(int index, double rate) {
53 this.rate[index] = rate;
54 }
55
56 public double getDemographic(double t) {
57
58 double logDemographic = 0.0;
59 double lastTransitionTime = 0.0;
60 int currentEpoch = 0;
61
62 // Account for all epochs before t
63 while (currentEpoch < transitionTime.length && t > transitionTime[currentEpoch]) {
64 logDemographic += -rate[currentEpoch] * (transitionTime[currentEpoch] - lastTransitionTime);
65 lastTransitionTime = transitionTime[currentEpoch];
66 ++currentEpoch;
67 }
68
69 // Account for epoch that holds t
70 logDemographic += -rate[currentEpoch] * (t - lastTransitionTime);
71
72 return getN0() * Math.exp(logDemographic);
73 }
74
75 private double integrateConstant(double start, double finish, double logDemographic) {
76 double integral = (finish - start) / Math.exp(logDemographic);
77 return integral;
78 }
79
80 private double integrateExponential(double start, double finish, double logDemographic, double rate) {
81 double integral = (Math.exp(finish * rate) - Math.exp(start * rate)) / Math.exp(logDemographic) / rate;
82 // System.err.println("\tint: " + integral + " " + start + " " + finish + " " + logDemographic + " " + rate);
83 // System.err.println("\t\t" + Math.exp(finish * rate) + " - " + Math.exp(start * rate));
84 // System.err.println("\t\t" + Math.exp(finish * rate - logDemographic - Math.log(rate)) + " - " + Math.exp(start * rate - logDemographic - Math.log(rate)));
85 return integral;
86 }
87
88 public double getAnalyticIntegral(double start, double finish) {
89
90 if (start == finish) {
91 return 0.0;
92 }
93
94 double integral = 0.0;
95 double logDemographic = 0.0;
96 double lastTransitionTime = 0.0;
97 int currentEpoch = 0;
98
99 // Account for all epochs before start
100 while (currentEpoch < transitionTime.length && start > transitionTime[currentEpoch]) {
101 logDemographic += -rate[currentEpoch] * (transitionTime[currentEpoch] - lastTransitionTime);
102 lastTransitionTime = transitionTime[currentEpoch];
103 ++currentEpoch;
104 }
105
106 // Account for all epochs before finish
107 while (currentEpoch < transitionTime.length && finish > transitionTime[currentEpoch]) {
108
109 // Add to integral
110 double incr = 0.0;
111 if (rate[currentEpoch] == 0.0) {
112 integral += incr = integrateConstant(start, transitionTime[currentEpoch], logDemographic);
113 } else {
114 integral += incr = integrateExponential(
115 start - lastTransitionTime,
116 transitionTime[currentEpoch] - lastTransitionTime,
117 logDemographic, rate[currentEpoch]);
118 }
119 // System.err.println("begin incr = " + incr + " for " + start + " -> " + transitionTime[currentEpoch] + " or " +
120 // (start - lastTransitionTime) + " -> " + (transitionTime[currentEpoch] - lastTransitionTime) + " @ " + rate[currentEpoch] + " & " + Math.exp(logDemographic));
121
122 // Update demographic function
123 logDemographic += -rate[currentEpoch] * (transitionTime[currentEpoch] - lastTransitionTime);
124 lastTransitionTime = transitionTime[currentEpoch];
125 start = lastTransitionTime;
126 ++currentEpoch;
127 }
128
129 // End of integral
130 double incr = 0.0;
131 if (rate[currentEpoch] == 0.0) {
132 integral += incr = integrateConstant(start, finish, logDemographic);
133 } else {
134 integral += incr = integrateExponential(
135 start - lastTransitionTime,
136 finish - lastTransitionTime,
137 logDemographic, rate[currentEpoch]);
138 }
139 // System.err.println("final incr = " + incr + " for " + start + " -> " + finish + " or " +
140 // (start - lastTransitionTime) + " -> " + (finish - lastTransitionTime) + " @ " + rate[currentEpoch] + " & " + Math.exp(logDemographic));
141
142
143 if (Double.isNaN(integral) || Double.isInfinite(integral)) {
144 System.err.println(integral + " " + start + " " + finish + new Vector(rate) + "\n");
145 }
146
147 return integral / getN0();
148 }
149
150 /**
151 * Calculates the integral 1/N(x) dx between start and finish.
152 */
153 public double getIntegral(double start, double finish) {
154 double analytic = getAnalyticIntegral(start, finish);
155
156 if (DEBUG) {
157 double numeric = getNumericalIntegral(start, finish);
158
159 if (Math.abs(analytic - numeric) > 1E-10) {
160 System.err.println(analytic);
161 System.err.println(numeric);
162 throw new RuntimeException("Error in analytic calculation");
163 }
164 }
165
166 return analytic;
167 }
168
169 public double getIntensity(double t) { throw new RuntimeException("Not implemented!"); }
170
171 public double getInverseIntensity(double x) {
172 throw new RuntimeException("Not implemented!");
173 }
174
175 public int getNumArguments() {
176 throw new RuntimeException("Not implemented!");
177 }
178
179 public String getArgumentName(int n) {
180 throw new RuntimeException("Not implemented!");
181 }
182
183 public double getArgument(int n) {
184 throw new RuntimeException("Not implemented!");
185 }
186
187 public void setArgument(int n, double value) {
188 throw new RuntimeException("Not implemented!");
189 }
190
191 public double getLowerBound(int n) {
192 throw new RuntimeException("Not implemented!");
193 }
194
195 public double getUpperBound(int n) {
196 throw new RuntimeException("Not implemented!");
197 }
198
199 //
200 // private stuff
201 //
202
203 final private double[] transitionTime;
204 final private double[] rate;
205
206 static final private boolean DEBUG = false;
207 }