| 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 |
public class EmpiricalPiecewiseConstant extends DemographicFunction.Abstract { |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public EmpiricalPiecewiseConstant(double[] intervals, double[] popSizes, double lag, Type units) { |
| 47 |
super(units); |
| 48 |
if (popSizes == null || intervals == null) { throw new IllegalArgumentException(); } |
| 49 |
if (popSizes.length != intervals.length + 1) { throw new IllegalArgumentException(); } |
| 50 |
if (lag < 0.0) throw new IllegalArgumentException("Lag must be greater than 1."); |
| 51 |
|
| 52 |
this.intervals = intervals; |
| 53 |
this.popSizes = popSizes; |
| 54 |
this.lag = lag; |
| 55 |
} |
| 56 |
|
| 57 |
public void setLag(double lag) { |
| 58 |
this.lag = lag; |
| 59 |
} |
| 60 |
|
| 61 |
public void setPopulationSizes(double[] popSizes) { |
| 62 |
this.popSizes = popSizes; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public double getDemographic(double t) { |
| 70 |
int epoch = 0; |
| 71 |
double t1 = t+lag; |
| 72 |
while (t1 > getEpochDuration(epoch)) { |
| 73 |
t1 -= getEpochDuration(epoch); |
| 74 |
epoch += 1; |
| 75 |
} |
| 76 |
return getDemographic(epoch, t1); |
| 77 |
} |
| 78 |
|
| 79 |
public double getIntensity(double t) { |
| 80 |
|
| 81 |
|
| 82 |
double t2 = lag; |
| 83 |
int epoch = 0; |
| 84 |
while (t2 > getEpochDuration(epoch)) { |
| 85 |
t2 -= getEpochDuration(epoch); |
| 86 |
epoch += 1; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
double intensity = getIntensity(epoch)-getIntensity(epoch, t2); |
| 91 |
|
| 92 |
double t1 = t-(getEpochDuration(epoch)-t2); |
| 93 |
epoch += 1; |
| 94 |
while (t1 > getEpochDuration(epoch)) { |
| 95 |
t1 -= getEpochDuration(epoch); |
| 96 |
intensity += getIntensity(epoch); |
| 97 |
epoch += 1; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
intensity += t1 >= 0 ? getIntensity(epoch, t1) : getIntensity(epoch-1, t1); |
| 103 |
|
| 104 |
return intensity; |
| 105 |
} |
| 106 |
|
| 107 |
public double getInverseIntensity(double x) { |
| 108 |
throw new RuntimeException("Not implemented!"); |
| 109 |
} |
| 110 |
|
| 111 |
public double getUpperBound(int i) { return 1e9;} |
| 112 |
public double getLowerBound(int i) { return Double.MIN_VALUE;} |
| 113 |
|
| 114 |
public int getNumArguments() { return 1; } |
| 115 |
|
| 116 |
public String getArgumentName(int i) { |
| 117 |
return "lag"; |
| 118 |
} |
| 119 |
|
| 120 |
public double getArgument(int i) { |
| 121 |
return lag; |
| 122 |
} |
| 123 |
|
| 124 |
public void setArgument(int i, double value) { |
| 125 |
lag = value; |
| 126 |
} |
| 127 |
|
| 128 |
public DemographicFunction getCopy() { |
| 129 |
EmpiricalPiecewiseConstant df = new EmpiricalPiecewiseConstant(new double[intervals.length], new double[popSizes.length], lag, getUnits()); |
| 130 |
System.arraycopy(intervals, 0, df.intervals, 0, intervals.length); |
| 131 |
System.arraycopy(popSizes, 0, df.popSizes, 0, popSizes.length); |
| 132 |
|
| 133 |
return df; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
protected double getDemographic(int epoch, double t) { |
| 141 |
return getEpochDemographic(epoch); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
protected double getIntensity(int epoch) { |
| 148 |
return getEpochDuration(epoch) / getEpochDemographic(epoch); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
protected double getIntensity(int epoch, double relativeTime) { |
| 155 |
return relativeTime / getEpochDemographic(epoch); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public double getEpochDuration(int epoch) { |
| 162 |
if (epoch < intervals.length) { |
| 163 |
return intervals[epoch]; |
| 164 |
} |
| 165 |
return Double.POSITIVE_INFINITY; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public double getEpochDemographic(int epoch) { |
| 172 |
if (epoch >= popSizes.length) { throw new IllegalArgumentException(); } |
| 173 |
return popSizes[epoch]; |
| 174 |
} |
| 175 |
|
| 176 |
public String toString() { |
| 177 |
StringBuffer buffer = new StringBuffer(); |
| 178 |
buffer.append(popSizes[0]); |
| 179 |
for (int i =1; i < popSizes.length; i++) { |
| 180 |
buffer.append("\t").append(popSizes[i]); |
| 181 |
} |
| 182 |
return buffer.toString(); |
| 183 |
} |
| 184 |
|
| 185 |
double[] intervals; |
| 186 |
double[] popSizes; |
| 187 |
double lag; |
| 188 |
} |