| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.evolution.coalescent; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class ConstExpConst extends DemographicFunction.Abstract { |
| 39 |
public enum Parameterization { |
| 40 |
GROWTH_RATE, |
| 41 |
ANCESTRAL_POPULATION_SIZE |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 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 |
|
| 59 |
|
| 60 |
public double getN0() { return N0; } |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public void setN0(double N0) { this.N0 = N0; } |
| 67 |
|
| 68 |
|
| 69 |
|
| 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 |
|
| 81 |
|
| 82 |
|
| 83 |
public void setN1(double N1) { |
| 84 |
assert(parameterization == Parameterization.ANCESTRAL_POPULATION_SIZE); |
| 85 |
this.N1 = N1; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public double getTime1() { |
| 93 |
return time1; |
| 94 |
} |
| 95 |
|
| 96 |
public void setTime1(double time1) { |
| 97 |
this.time1 = time1; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public double getEpochTime() { |
| 105 |
return epochTime; |
| 106 |
} |
| 107 |
|
| 108 |
public void setEpochTime(double epochTime) { |
| 109 |
this.epochTime = epochTime; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public double getTime2() { |
| 117 |
return time1 + epochTime; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public final void setGrowthRate(double growthRate) { |
| 125 |
assert(parameterization == Parameterization.GROWTH_RATE); |
| 126 |
this.growthRate = growthRate; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 172 |
|
| 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 |
|
| 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 |
|
| 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 |
} |