| 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 |
import dr.math.matrixAlgebra.Vector; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public class MultiEpochExponential extends ConstantPopulation { |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public MultiEpochExponential(Type units, int numEpoch) { |
| 42 |
|
| 43 |
super(units); |
| 44 |
transitionTime = new double[numEpoch - 1]; |
| 45 |
rate = new double[numEpoch]; |
| 46 |
} |
| 47 |
|
| 48 |
public void setTransitionTime(int index, double transitionTime) { |
| 49 |
this.transitionTime[index] = transitionTime; |
| 50 |
} |
| 51 |
|
| 52 |
public void setGrowthRate(int index, double rate) { |
| 53 |
this.rate[index] = rate; |
| 54 |
} |
| 55 |
|
| 56 |
public double getDemographic(double t) { |
| 57 |
|
| 58 |
double logDemographic = 0.0; |
| 59 |
double lastTransitionTime = 0.0; |
| 60 |
int currentEpoch = 0; |
| 61 |
|
| 62 |
|
| 63 |
while (currentEpoch < transitionTime.length && t > transitionTime[currentEpoch]) { |
| 64 |
logDemographic += -rate[currentEpoch] * (transitionTime[currentEpoch] - lastTransitionTime); |
| 65 |
lastTransitionTime = transitionTime[currentEpoch]; |
| 66 |
++currentEpoch; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
logDemographic += -rate[currentEpoch] * (t - lastTransitionTime); |
| 71 |
|
| 72 |
return getN0() * Math.exp(logDemographic); |
| 73 |
} |
| 74 |
|
| 75 |
private double integrateConstant(double start, double finish, double logDemographic) { |
| 76 |
double integral = (finish - start) / Math.exp(logDemographic); |
| 77 |
return integral; |
| 78 |
} |
| 79 |
|
| 80 |
private double integrateExponential(double start, double finish, double logDemographic, double rate) { |
| 81 |
double integral = (Math.exp(finish * rate) - Math.exp(start * rate)) / Math.exp(logDemographic) / rate; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
return integral; |
| 86 |
} |
| 87 |
|
| 88 |
public double getAnalyticIntegral(double start, double finish) { |
| 89 |
|
| 90 |
if (start == finish) { |
| 91 |
return 0.0; |
| 92 |
} |
| 93 |
|
| 94 |
double integral = 0.0; |
| 95 |
double logDemographic = 0.0; |
| 96 |
double lastTransitionTime = 0.0; |
| 97 |
int currentEpoch = 0; |
| 98 |
|
| 99 |
|
| 100 |
while (currentEpoch < transitionTime.length && start > transitionTime[currentEpoch]) { |
| 101 |
logDemographic += -rate[currentEpoch] * (transitionTime[currentEpoch] - lastTransitionTime); |
| 102 |
lastTransitionTime = transitionTime[currentEpoch]; |
| 103 |
++currentEpoch; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
while (currentEpoch < transitionTime.length && finish > transitionTime[currentEpoch]) { |
| 108 |
|
| 109 |
|
| 110 |
double incr = 0.0; |
| 111 |
if (rate[currentEpoch] == 0.0) { |
| 112 |
integral += incr = integrateConstant(start, transitionTime[currentEpoch], logDemographic); |
| 113 |
} else { |
| 114 |
integral += incr = integrateExponential( |
| 115 |
start - lastTransitionTime, |
| 116 |
transitionTime[currentEpoch] - lastTransitionTime, |
| 117 |
logDemographic, rate[currentEpoch]); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
logDemographic += -rate[currentEpoch] * (transitionTime[currentEpoch] - lastTransitionTime); |
| 124 |
lastTransitionTime = transitionTime[currentEpoch]; |
| 125 |
start = lastTransitionTime; |
| 126 |
++currentEpoch; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
double incr = 0.0; |
| 131 |
if (rate[currentEpoch] == 0.0) { |
| 132 |
integral += incr = integrateConstant(start, finish, logDemographic); |
| 133 |
} else { |
| 134 |
integral += incr = integrateExponential( |
| 135 |
start - lastTransitionTime, |
| 136 |
finish - lastTransitionTime, |
| 137 |
logDemographic, rate[currentEpoch]); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
if (Double.isNaN(integral) || Double.isInfinite(integral)) { |
| 144 |
System.err.println(integral + " " + start + " " + finish + new Vector(rate) + "\n"); |
| 145 |
} |
| 146 |
|
| 147 |
return integral / getN0(); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public double getIntegral(double start, double finish) { |
| 154 |
double analytic = getAnalyticIntegral(start, finish); |
| 155 |
|
| 156 |
if (DEBUG) { |
| 157 |
double numeric = getNumericalIntegral(start, finish); |
| 158 |
|
| 159 |
if (Math.abs(analytic - numeric) > 1E-10) { |
| 160 |
System.err.println(analytic); |
| 161 |
System.err.println(numeric); |
| 162 |
throw new RuntimeException("Error in analytic calculation"); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
return analytic; |
| 167 |
} |
| 168 |
|
| 169 |
public double getIntensity(double t) { throw new RuntimeException("Not implemented!"); } |
| 170 |
|
| 171 |
public double getInverseIntensity(double x) { |
| 172 |
throw new RuntimeException("Not implemented!"); |
| 173 |
} |
| 174 |
|
| 175 |
public int getNumArguments() { |
| 176 |
throw new RuntimeException("Not implemented!"); |
| 177 |
} |
| 178 |
|
| 179 |
public String getArgumentName(int n) { |
| 180 |
throw new RuntimeException("Not implemented!"); |
| 181 |
} |
| 182 |
|
| 183 |
public double getArgument(int n) { |
| 184 |
throw new RuntimeException("Not implemented!"); |
| 185 |
} |
| 186 |
|
| 187 |
public void setArgument(int n, double value) { |
| 188 |
throw new RuntimeException("Not implemented!"); |
| 189 |
} |
| 190 |
|
| 191 |
public double getLowerBound(int n) { |
| 192 |
throw new RuntimeException("Not implemented!"); |
| 193 |
} |
| 194 |
|
| 195 |
public double getUpperBound(int n) { |
| 196 |
throw new RuntimeException("Not implemented!"); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
final private double[] transitionTime; |
| 204 |
final private double[] rate; |
| 205 |
|
| 206 |
static final private boolean DEBUG = false; |
| 207 |
} |