Debian Codegraph @

src/dr/evomodel/epidemiology/LogisticGrowthN0.java

raw symbols SCIP
1 /*
2 * LogisticGrowthN0.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 /*
27 * LogisticGrowthN0.java
28 *
29 * Daniel Wilson 4th October 2011
30 *
31 */
32
33 package dr.evomodel.epidemiology;
34
35 import dr.evolution.coalescent.*;
36
37 /**
38 * This class models logistic growth.
39 *
40 * @author Daniel Wilson
41 * @author Alexei Drummond
42 * @author Andrew Rambaut
43 * @author Matthew Hall
44 * @version $Id: LogisticGrowth.java,v 1.15 2008/03/21 20:25:56 rambaut Exp $
45 */
46 public class LogisticGrowthN0 extends ExponentialGrowth {
47
48 /**
49 * Construct demographic model with default settings
50 */
51 public LogisticGrowthN0(Type units) {
52 super(units);
53 }
54
55 public void setT50(double value) {
56 t50 = value;
57 }
58
59 public double getT50() {
60 return t50;
61 }
62
63 // Implementation of abstract methods
64
65 /**
66 * Gets the value of the demographic function N(t) at time t.
67 *
68 * @param t the time
69 * @return the value of the demographic function N(t) at time t.
70 */
71 public double getDemographic(double t) {
72 double N0 = getN0();
73 double r = getGrowthRate();
74 double T50 = getT50();
75
76 return N0 * (1 + Math.exp(-r * T50)) / (1 + Math.exp(-r * (T50-t)));
77 }
78
79 public double getLogDemographic(double t) {
80 return Math.log(getDemographic(t));
81 }
82
83 /**
84 * Returns value of demographic intensity function at time t
85 * (= integral 1/N(x) dx from 0 to t).
86 */
87 public double getIntensity(double t) {
88 double N0 = getN0();
89 double r = getGrowthRate();
90 double T50 = getT50();
91 double exp_rT50 = Math.exp(-r*T50);
92
93 return (t + exp_rT50 * (Math.exp(r * t) - 1)/r) / (N0 * (1 + exp_rT50));
94 }
95
96 /**
97 * Returns the inverse function of getIntensity
98 *
99 * If exp(-qt) = a(t-k) then t = k + (1/q) * W(q*exp(-q*k)/a) where W(x) is the Lambert W function.
100 *
101 * for our purposes:
102 *
103 * q = -r
104 * a = (q/exp(q*T50))
105 * k = N0*(1+exp(q*T50))*x - (1/q)exp(q*T50)
106 *
107 * For large x, W0(x) is approximately equal to ln(x) - ln(ln(x)); if q*exp(-q*k)/a rounds to infinity, we log it
108 * and use this instead
109 */
110 public double getInverseIntensity(double x) {
111
112 double q = -getGrowthRate();
113 double T50 = getT50();
114 double N0 = getN0();
115 double a = (q/Math.exp(q*T50));
116 double k = N0*(1+Math.exp(q*T50))*x - (1/q)*Math.exp(q*T50);
117
118 double lambertInput = q*Math.exp(-q*k)/a;
119
120 double lambertResult;
121
122 if(lambertInput==Double.POSITIVE_INFINITY){
123
124 //use the asymptote; note q/a = exp(q*T50)
125
126 double logInput = q*T50-q*k;
127 lambertResult = logInput - Math.log(logInput);
128
129 } else {
130 lambertResult = LambertW.branch0(lambertInput);
131 }
132
133 return k + (1/q)*lambertResult;
134
135 }
136
137 public double getIntegral(double start, double finish) {
138 return getIntensity(finish) - getIntensity(start);
139 }
140
141 public int getNumArguments() {
142 return 3;
143 }
144
145 public String getArgumentName(int n) {
146 switch (n) {
147 case 0:
148 return "N0";
149 case 1:
150 return "r";
151 case 2:
152 return "t50";
153 }
154 throw new IllegalArgumentException("Argument " + n + " does not exist");
155 }
156
157 public double getArgument(int n) {
158 switch (n) {
159 case 0:
160 return getN0();
161 case 1:
162 return getGrowthRate();
163 case 2:
164 return getT50();
165 }
166 throw new IllegalArgumentException("Argument " + n + " does not exist");
167 }
168
169 public void setArgument(int n, double value) {
170 switch (n) {
171 case 0:
172 setN0(value);
173 break;
174 case 1:
175 setGrowthRate(value);
176 break;
177 case 2:
178 setT50(value);
179 break;
180 default:
181 throw new IllegalArgumentException("Argument " + n + " does not exist");
182
183 }
184 }
185
186 public double getLowerBound(int n) {
187 return 0.0;
188 }
189
190 public double getUpperBound(int n) {
191 return Double.POSITIVE_INFINITY;
192 }
193
194 public DemographicFunction getCopy() {
195 LogisticGrowthN0 df = new LogisticGrowthN0(getUnits());
196 df.setN0(getN0());
197 df.setGrowthRate(getGrowthRate());
198 df.setT50(getT50());
199 return df;
200 }
201
202 //
203 // private stuff
204 //
205
206 private double t50;
207 }