Debian Codegraph @

src/dr/evolution/coalescent/ConstExpConst.java

raw symbols SCIP
1 /*
2 * ConstExpConst.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 /**
29 * This class models exponential growth from an initial population size which
30 * then transitions back to a constant population size.
31 *
32 * @author Andrew Rambaut
33 * @author Alexei Drummond
34 *
35 * @version $ID$
36 *
37 */
38 public class ConstExpConst extends DemographicFunction.Abstract {
39 public enum Parameterization {
40 GROWTH_RATE,
41 ANCESTRAL_POPULATION_SIZE
42 }
43
44 /**
45 * Construct demographic model with default settings
46 */
47 public ConstExpConst(Type units) {
48 this(Parameterization.ANCESTRAL_POPULATION_SIZE, false, units);
49 }
50
51 public ConstExpConst(Parameterization parameterization, boolean useNumericalIntegrator, Type units) {
52 super(units);
53 this.parameterization = parameterization;
54 this.useNumericalIntegrator = useNumericalIntegrator;
55 }
56
57 /**
58 * @return initial population size.
59 */
60 public double getN0() { return N0; }
61
62 /**
63 * sets initial population size.
64 * @param N0 new size
65 */
66 public void setN0(double N0) { this.N0 = N0; }
67
68 /**
69 * @return ancestral population size.
70 */
71 public double getN1() {
72 if (parameterization == Parameterization.ANCESTRAL_POPULATION_SIZE) {
73 return N1;
74 }
75
76 return N0 * Math.exp(-epochTime * growthRate);
77 }
78
79 /**
80 * sets ancestral population size. Can only be set if the parameterization is ANCESTRAL_POPULATION_SIZE
81 * @param N1 new size
82 */
83 public void setN1(double N1) {
84 assert(parameterization == Parameterization.ANCESTRAL_POPULATION_SIZE);
85 this.N1 = N1;
86 }
87
88 /**
89 * return the first transition time to exponential growth
90 * @return
91 */
92 public double getTime1() {
93 return time1;
94 }
95
96 public void setTime1(double time1) {
97 this.time1 = time1;
98 }
99
100 /**
101 * Get the time period of exponential growth phase
102 * @return
103 */
104 public double getEpochTime() {
105 return epochTime;
106 }
107
108 public void setEpochTime(double epochTime) {
109 this.epochTime = epochTime;
110 }
111
112 /**
113 * Return the second transition time to constant size N1
114 * @return
115 */
116 public double getTime2() {
117 return time1 + epochTime;
118 }
119
120 /**
121 * sets growth rate for exponential phase. Can only be set if the parameterization is GROWTH_RATE
122 * @param growthRate new growthRate
123 */
124 public final void setGrowthRate(double growthRate) {
125 assert(parameterization == Parameterization.GROWTH_RATE);
126 this.growthRate = growthRate;
127 }
128
129 /**
130 * @return growth rate.
131 */
132 public final double getGrowthRate() {
133 if (parameterization == Parameterization.GROWTH_RATE) {
134 return growthRate;
135 }
136
137 return (Math.log(N0) - Math.log(N1)) / epochTime;
138 }
139
140
141 // Implementation of abstract methods
142
143 public double getDemographic(double t) {
144
145 double r = getGrowthRate();
146
147 if (t < getTime1()) {
148 return getN0();
149 }
150
151 if (t >= getTime2()) {
152 return getN1();
153 }
154
155 return getN0() * Math.exp(-r*(t - getTime1()));
156 }
157
158 /**
159 * Calculates the integral 1/N(x) dx between start and finish.
160 */
161 public double getIntegral(double start, double finish)
162 {
163 if (useNumericalIntegrator) {
164 return getNumericalIntegral(start, finish);
165 } else {
166 return getIntensity(finish) - getIntensity(start);
167 }
168 }
169
170 /**
171 * Returns value of demographic intensity function at time t
172 * (= integral 1/N(x) dx from 0 to t).
173 */
174 public double getIntensity(double t) {
175 double time2 = getTime2();
176 double oneOverN0 = 1.0 / getN0();
177 double r = getGrowthRate();
178
179 if (t < time1) {
180 return (t * oneOverN0);
181 }
182 if (t > time1 && t < time2) {
183 return (time1 * oneOverN0) + (( (Math.exp(t*r) - Math.exp(time1*r)) * oneOverN0) / r);
184 }
185
186 double oneOverN1 = 1.0 / getN1();
187 // if (t >= time2) {
188 return (time1 * oneOverN0) + (( (Math.exp(time2*r) - Math.exp(time1*r)) * oneOverN0) / r) + (oneOverN1 * (t-time2));
189 }
190
191 public double getInverseIntensity(double x) {
192
193 throw new RuntimeException("Not implemented!");
194 }
195
196 public int getNumArguments() {
197 return 4;
198 }
199
200 public String getArgumentName(int n) {
201 switch (n) {
202 case 0: return "N0";
203 case 1: return (parameterization == Parameterization.ANCESTRAL_POPULATION_SIZE ? "N1": "r");
204 case 2: return "epochTime";
205 case 3: return "time1";
206 }
207 throw new IllegalArgumentException("Argument " + n + " does not exist");
208 }
209
210 public double getArgument(int n) {
211 switch (n) {
212 case 0: return getN0();
213 case 1: return (parameterization == Parameterization.ANCESTRAL_POPULATION_SIZE ? getN1(): getGrowthRate());
214 case 2: return getEpochTime();
215 case 3: return getTime1();
216 }
217 throw new IllegalArgumentException("Argument " + n + " does not exist");
218 }
219
220 public void setArgument(int n, double value) {
221 switch (n) {
222 case 0: setN0(value); break;
223 case 1:
224 if (parameterization == Parameterization.ANCESTRAL_POPULATION_SIZE) {
225 setN1(value);
226 } else {
227 setGrowthRate(value);
228 }
229 break;
230 case 2: setEpochTime(value); break;
231 case 3: setTime1(value); break;
232 default: throw new IllegalArgumentException("Argument " + n + " does not exist");
233
234 }
235 }
236
237 public double getLowerBound(int n) {
238 if (n == 1 && parameterization == Parameterization.GROWTH_RATE) {
239 return Double.NEGATIVE_INFINITY;
240 }
241 return 0.0;
242 }
243
244 public double getUpperBound(int n) {
245 return Double.POSITIVE_INFINITY;
246 }
247
248 //
249 // private stuff
250 //
251
252 private double N0;
253 private double N1;
254 private double time1;
255 private double epochTime;
256 private double growthRate;
257 private final boolean useNumericalIntegrator;
258 private final Parameterization parameterization;
259 }