Debian Codegraph @

src/dr/evolution/coalescent/LogisticGrowth.java

raw symbols SCIP
1 /*
2 * LogisticGrowth.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 logistic growth.
30 *
31 * @author Alexei Drummond
32 * @author Andrew Rambaut
33 * @version $Id: LogisticGrowth.java,v 1.15 2005/05/24 20:25:56 rambaut Exp $
34 */
35 public class LogisticGrowth extends ExponentialGrowth {
36
37 /**
38 * Construct demographic model with default settings
39 */
40 public LogisticGrowth(Type units) {
41
42 super(units);
43 }
44
45 public void setShape(double value) {
46 c = value;
47 }
48
49 public double getShape() {
50 return c;
51 }
52
53 double lowLimit = 0; // 1e-6;
54
55 /**
56 * An alternative parameterization of this model. This
57 * function sets the time at which there is a 0.5 proportion
58 * of N0.
59 *
60 * The general form for any k where tk is the time at which Nt = N0/k:
61 * c = (k - 1.0) / (exp(r * tk) - k);
62 */
63 public void setTime50(double time50) {
64 c = 1.0 / (Math.exp(getGrowthRate() * time50) - 2.0);
65 }
66
67 public void setShapeFromTimeAtAlpha(double time, double alpha) {
68
69 // New parameterization of logistic shape to be the time at which the
70 // population reached some proportion alpha:
71 double ert = Math.exp(- getGrowthRate() * time);
72 c = ((1.0 - alpha) * ert) / (ert - alpha);
73 }
74 // Implementation of abstract methods
75
76 /**
77 * Gets the value of the demographic function N(t) at time t.
78 *
79 * @param t the time
80 * @return the value of the demographic function N(t) at time t.
81 */
82 public double getDemographic(double t) {
83
84 double nZero = getN0();
85 double r = getGrowthRate();
86 double c = getShape();
87
88 // return nZero * (1 + c) / (1 + (c * Math.exp(r*t)));
89 // AER rearranging this to use exp(-rt) may help
90 // with some overflow situations...
91
92 double expOfMRT = Math.exp(-r * t);
93 return lowLimit + (nZero * (1 + c) * expOfMRT) / (c + expOfMRT);
94 }
95
96 public double getLogDemographic(double t) {
97 final double d = getDemographic(t);
98 if( d == 0.0 && lowLimit == 0.0 ) {
99 double nZero = getN0();
100 double r = getGrowthRate();
101 double c = getShape();
102 int sign = c > 0 ? 1 : -1;
103
104 final double v1 = Math.log(c * sign) + r * t;
105 double ld = Math.log(nZero);
106 if( v1 < 600 ) {
107 double v = sign * Math.exp(v1);
108
109 if( c > -1 ) {
110 ld += Math.log1p(c) - Math.log1p(v);
111 } else {
112 ld += Math.log((1+c)/(1+v));
113 }
114 } else {
115 ld += Math.log1p(c) - sign * v1;
116 }
117 return ld;
118 }
119 // if( ! (Math.abs(Math.log(d) - ld) < 1e-12) ) {
120 // return Math.log(d);
121 // }
122 return Math.log(d);
123 }
124 /**
125 * Returns value of demographic intensity function at time t
126 * (= integral 1/N(x) dx from 0 to t).
127 */
128 public double getIntensity(double t) {
129
130 double nZero = getN0();
131 double r = getGrowthRate();
132 double c = getShape();
133
134 double ert = Math.exp(r * t);
135 if( lowLimit == 0 ) {
136 // double emrt = Math.exp(-r * t);
137 return (c * (ert - 1)/r + t) / ((1+c) * nZero);
138 }
139 double z = lowLimit;
140 return (r*t*z + (1 + c)*nZero*Math.log(nZero + c*nZero + z + c*ert*z))/(r*z*(nZero + c*nZero + z));
141 }
142
143 /**
144 * Returns value of demographic intensity function at time t
145 * (= integral 1/N(x) dx from 0 to t).
146 */
147 public double getInverseIntensity(double x) {
148
149 throw new RuntimeException("Not implemented!");
150 }
151
152 public double getIntegral(double start, double finish) {
153 if( lowLimit > 0 ) {
154 double v1 = getNumericalIntegral(start, finish);
155 final double v2 = getIntensity(finish) - getIntensity(start);
156 return v2;
157 }
158 double intervalLength = finish - start;
159
160 double nZero = getN0();
161 double r = getGrowthRate();
162 double c = getShape();
163 double expOfMinusRT = Math.exp(-r * start);
164 double expOfMinusRG = Math.exp(-r * intervalLength);
165
166 double term1 = nZero * (1.0 + c);
167 if (term1 == 0.0) {
168 return Double.POSITIVE_INFINITY;
169 }
170
171 double term2 = c * (1.0 - expOfMinusRG);
172
173 double term3 = (term1 * expOfMinusRT) * r * expOfMinusRG;
174 double term2over3;
175 if (term3 == 0.0) {
176 double l1 = expOfMinusRG < 1e-8 ? -r * intervalLength : Math.log1p(expOfMinusRG);
177 final int sign = c > 0 ? 1 : -1;
178 term2over3 = (sign/term1) * Math.exp(l1 + r * start + Math.log(c*sign) - Math.log(r));
179
180 // throw new RuntimeException("Infinite integral!");
181 } else {
182 // if (term3 != 0.0 && term2 == 0.0) {
183 // term2over3 = 0.0;
184 // } else if (term3 == 0.0 && term2 == 0.0) {
185 // throw new RuntimeException("term3 and term2 are both zeros. N0=" + getN0() + " growthRate=" + getGrowthRate() + "c=" + c);
186 // } else {
187 term2over3 = term2 / term3;
188 // }
189 }
190
191 final double term5 = intervalLength / term1;
192
193 // double v0 = 1/term1 * (finish + c * (Math.exp(r*finish) - 1) /r);
194 // double v1 = 1/term1 * (start + c * (Math.exp(r*start) - 1) /r);
195 // double v = 1/term1 * ((finish + c * (Math.exp(r*finish) - 1) /r) - (start + c * (Math.exp(r*start) - 1) /r));
196 // double v2 = 1/term1 * ((finish-start) + (c * (Math.exp(r*finish) - 1) /r) - (c * (Math.exp(r*start) - 1) /r));
197 // double v3 = 1/term1 * ((finish-start) + (c/r)* ( Math.exp(r*finish) - 1) - (c/r) * (Math.exp(r*start) - 1) );
198 // double v4 = 1/term1 * ((finish-start) + (c/r) * (Math.exp(r*finish) - Math.exp(r*start) ) );
199 // double v = ( (c * (Math.exp(r*finish) - Math.exp(r*start)) / r) + (start - finish)) / term1;
200
201 return term5 + term2over3;
202 }
203
204 public int getNumArguments() {
205 return 3;
206 }
207
208 public String getArgumentName(int n) {
209 switch (n) {
210 case 0:
211 return "N0";
212 case 1:
213 return "r";
214 case 2:
215 return "c";
216 }
217 throw new IllegalArgumentException("Argument " + n + " does not exist");
218 }
219
220 public double getArgument(int n) {
221 switch (n) {
222 case 0:
223 return getN0();
224 case 1:
225 return getGrowthRate();
226 case 2:
227 return getShape();
228 }
229 throw new IllegalArgumentException("Argument " + n + " does not exist");
230 }
231
232 public void setArgument(int n, double value) {
233 switch (n) {
234 case 0:
235 setN0(value);
236 break;
237 case 1:
238 setGrowthRate(value);
239 break;
240 case 2:
241 setShape(value);
242 break;
243 default:
244 throw new IllegalArgumentException("Argument " + n + " does not exist");
245
246 }
247 }
248
249 public double getLowerBound(int n) {
250 return 0.0;
251 }
252
253 public double getUpperBound(int n) {
254 return Double.POSITIVE_INFINITY;
255 }
256
257 public DemographicFunction getCopy() {
258 LogisticGrowth df = new LogisticGrowth(getUnits());
259 df.setN0(getN0());
260 df.setGrowthRate(getGrowthRate());
261 df.c = c;
262
263 return df;
264 }
265
266 //
267 // private stuff
268 //
269
270 private double c;
271 }