Debian Codegraph @

src/dr/evolution/coalescent/CataclysmicDemographic.java

raw symbols SCIP
1 /*
2 * CataclysmicDemographic.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 an exponentially growing (or shrinking) population
30 * (Parameters: N0=present-day population size; r=growth rate).
31 * This model is nested with the constant-population size model (r=0).
32 *
33 * @version $Id: CataclysmicDemographic.java,v 1.5 2005/05/24 20:25:55 rambaut Exp $
34 *
35 * @author Alexei Drummond
36 * @author Andrew Rambaut
37 */
38 public class CataclysmicDemographic extends ExponentialGrowth {
39
40 //
41 // Public stuff
42 //
43
44 /**
45 * Construct demographic model with default settings
46 */
47 public CataclysmicDemographic(Type units) {
48
49 super(units);
50 }
51
52 /**
53 * returns the positive-valued decline rate
54 */
55 public final double getDeclineRate() { return -d; }
56
57 /**
58 * sets the decline rate.
59 */
60 public void setDeclineRate(double d) {
61 // if (d <= 0) throw new IllegalArgumentException();
62 this.d = d;
63 }
64
65 public final double getCataclysmTime() { return catTime; }
66
67 public final void setCataclysmTime(double t) {
68 if (t <= 0) throw new IllegalArgumentException();
69 catTime = t;
70 }
71
72 /**
73 * An alternative parameterization of this model. This
74 * function sets the decline rate using N0 & t which must
75 * already have been set.
76 */
77 public final void setSpikeFactor(double f) {
78 setDeclineRate( Math.log(f) / catTime );
79 }
80
81 // Implementation of abstract methods
82
83 public double getDemographic(double t) {
84
85 double d = getDeclineRate();
86
87 if (t < catTime) {
88 return getN0() * Math.exp(t * d);
89 } else {
90 double spikeHeight = getN0() * Math.exp(catTime * d);
91 //System.out.println("Spike height = " + spikeHeight);
92 t -= catTime;
93
94 double r = getGrowthRate();
95 if (r == 0) {
96 return spikeHeight;
97 } else {
98 return spikeHeight * Math.exp(-t * r);
99 }
100 }
101 }
102
103 public double getIntensity(double t) {
104
105 double d = getDeclineRate();
106 double r = getGrowthRate();
107 if (t < catTime) {
108 return (Math.exp(t*-d)-1.0)/getN0()/-d;
109 } else {
110
111 double intensityUpToSpike = (Math.exp(catTime*-d)-1.0)/getN0()/-d;
112
113 double spikeHeight = getN0() * Math.exp(catTime * d);
114 t -= catTime;
115 //System.out.println("Spike height = " + spikeHeight);
116
117 if (r == 0) {
118 return t/spikeHeight + intensityUpToSpike;
119 } else {
120 return (Math.exp(t*r)-1.0)/spikeHeight/r + intensityUpToSpike;
121 }
122 }
123 }
124
125 public double getInverseIntensity(double x) {
126 double d = getDeclineRate();
127 double r = getGrowthRate();
128 double intensityUpToSpike = (Math.exp(catTime*-d)-1.0)/getN0()/-d;
129
130 if(x < intensityUpToSpike){
131 return -Math.log(1.0 - getN0() * d * x)/d;
132 }
133
134
135 double spikeHeight = getN0() * Math.exp(catTime * d);
136 x -= intensityUpToSpike;
137
138 if(r == 0){
139 return spikeHeight*x + catTime;
140 }
141
142 return catTime + Math.log(1.0 + spikeHeight * x * r)/r;
143 //throw new UnsupportedOperationException();
144 }
145
146 public int getNumArguments() {
147 return 4;
148 }
149
150 public String getArgumentName(int n) {
151
152 switch (n) {
153 case 0: return "N0";
154 case 1: return "r";
155 case 2: return "d";
156 case 3: return "t";
157 default: throw new IllegalArgumentException();
158 }
159
160 }
161
162 public double getArgument(int n) {
163 switch (n) {
164 case 0: return getN0();
165 case 1: return getGrowthRate();
166 case 2: return getDeclineRate();
167 case 3: return getCataclysmTime();
168 default: throw new IllegalArgumentException();
169 }
170 }
171
172 public void setArgument(int n, double value) {
173 switch (n) {
174 case 0: setN0(value); break;
175 case 1: setGrowthRate(value); break;
176 case 2: setDeclineRate(value); break;
177 case 3: setCataclysmTime(value); break;
178 default: throw new IllegalArgumentException();
179 }
180 }
181
182 public DemographicFunction getCopy() {
183 CataclysmicDemographic df = new CataclysmicDemographic(getUnits());
184 df.setN0(getN0());
185 df.setGrowthRate(getGrowthRate());
186 df.d = d;
187 df.catTime = catTime;
188
189 return df;
190 }
191
192 //
193 // private stuff
194 //
195
196 private double d;
197 private double catTime;
198 }