| 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 test.dr.evomodel.substmodel; |
| 27 |
|
| 28 |
import dr.evomodel.branchratemodel.LatentStateBranchRateModel; |
| 29 |
import dr.inference.markovjumps.MarkovReward; |
| 30 |
import dr.inference.markovjumps.SericolaSeriesMarkovReward; |
| 31 |
import dr.inference.markovjumps.TwoStateOccupancyMarkovReward; |
| 32 |
import dr.inference.model.Parameter; |
| 33 |
import dr.math.matrixAlgebra.Matrix; |
| 34 |
import dr.math.matrixAlgebra.Vector; |
| 35 |
import org.apache.commons.math.FunctionEvaluationException; |
| 36 |
import org.apache.commons.math.MaxIterationsExceededException; |
| 37 |
import org.apache.commons.math.analysis.UnivariateRealFunction; |
| 38 |
import org.apache.commons.math.analysis.integration.TrapezoidIntegrator; |
| 39 |
import test.dr.math.MathTestCase; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public class TwoStateOccupancyMarkovRewardsTest extends MathTestCase { |
| 46 |
|
| 47 |
private static final double tolerance = 10E-3; |
| 48 |
|
| 49 |
private double sum(double[] v) { |
| 50 |
double sum = 0.0; |
| 51 |
for (double x : v) { |
| 52 |
sum += x; |
| 53 |
} |
| 54 |
return sum; |
| 55 |
} |
| 56 |
|
| 57 |
public void testNew() { |
| 58 |
|
| 59 |
|
| 60 |
double rate = 2.1; |
| 61 |
double prop = 0.5; |
| 62 |
double eps = 0.3; |
| 63 |
double branchLength = 2.4; |
| 64 |
|
| 65 |
MarkovReward markovReward1 = createMarkovReward(rate, prop); |
| 66 |
MarkovReward markovReward2 = createSericolaMarkovReward(rate, prop); |
| 67 |
|
| 68 |
double r1 = markovReward1.computePdf(eps * branchLength, branchLength, 0, 0); |
| 69 |
double r2 = markovReward2.computePdf(eps * branchLength, branchLength, 0, 0); |
| 70 |
|
| 71 |
assertEquals(r1, r2, tolerance); |
| 72 |
|
| 73 |
|
| 74 |
prop = 0.501; |
| 75 |
MarkovReward markovReward3 = createMarkovReward(rate, prop); |
| 76 |
MarkovReward markovReward4 = createSericolaMarkovReward(rate, prop); |
| 77 |
|
| 78 |
double r3 = markovReward3.computePdf(eps * branchLength, branchLength, 0, 0); |
| 79 |
double r4 = markovReward4.computePdf(eps * branchLength, branchLength, 0, 0); |
| 80 |
|
| 81 |
assertEquals(r3, r4, tolerance); |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
System.exit(-1); |
| 86 |
} |
| 87 |
|
| 88 |
public void testTwoStateSericolaRewards1() { |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
final double rate = 1; |
| 95 |
final double prop = 0.5; |
| 96 |
final double eps = 0.1; |
| 97 |
final double branchLength = 1.2; |
| 98 |
|
| 99 |
final boolean print = false; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
private void run(MarkovReward markovReward, double rate, double prop, double branchLength, |
| 170 |
boolean print, int length) { |
| 171 |
DensityFunction densityFunction = new DensityFunction(markovReward, branchLength, rate, prop); |
| 172 |
|
| 173 |
final double step = branchLength / length; |
| 174 |
int i = 0; |
| 175 |
double sum = 0.0; |
| 176 |
double modeY = 0.0; |
| 177 |
double modeX = 0.0; |
| 178 |
|
| 179 |
for (double x = 0.0; x <= branchLength; x += step, ++i) { |
| 180 |
|
| 181 |
double density = 0; |
| 182 |
density = densityFunction.value(x); |
| 183 |
|
| 184 |
if (x == 0.0) { |
| 185 |
modeY = density; |
| 186 |
} else { |
| 187 |
if (density > modeY) { |
| 188 |
modeY = density; |
| 189 |
modeX = x; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
if (x == 0.0 || x == branchLength) { |
| 194 |
sum += density; |
| 195 |
} else { |
| 196 |
sum += 2.0 * density; |
| 197 |
} |
| 198 |
|
| 199 |
if (print) { |
| 200 |
System.out.println(i + "\t" + String.format("%3.2f", x) + "\t" + String.format("%5.3e", density)); |
| 201 |
} |
| 202 |
} |
| 203 |
sum *= (branchLength / 2.0 / length); |
| 204 |
|
| 205 |
|
| 206 |
System.out.println("branchLength = " + branchLength); |
| 207 |
System.out.println("rate = " + rate); |
| 208 |
System.out.println("prop = " + prop); |
| 209 |
System.out.println("Integral = " + sum); |
| 210 |
System.out.println("Mode = " + String.format("%3.2e", modeY) + " at " + modeX); |
| 211 |
|
| 212 |
assertEquals(sum, 1.0, tolerance); |
| 213 |
|
| 214 |
TrapezoidIntegrator integrator = new TrapezoidIntegrator(); |
| 215 |
double integral = 0.0; |
| 216 |
try { |
| 217 |
integral = integrator.integrate(new UnitDensityFunction(markovReward, branchLength, rate, prop), 0.0, 1.0); |
| 218 |
} catch (MaxIterationsExceededException e) { |
| 219 |
e.printStackTrace(); |
| 220 |
} catch (FunctionEvaluationException e) { |
| 221 |
e.printStackTrace(); |
| 222 |
} |
| 223 |
System.out.println("unt int = " + integral); |
| 224 |
assertEquals(integral, 1.0, tolerance); |
| 225 |
|
| 226 |
System.out.println("\n"); |
| 227 |
} |
| 228 |
|
| 229 |
private class LatentStateDensityFunction implements UnivariateRealFunction { |
| 230 |
|
| 231 |
private final LatentStateBranchRateModel model; |
| 232 |
private final double branchLength; |
| 233 |
|
| 234 |
LatentStateDensityFunction(LatentStateBranchRateModel model, double branchLength) { |
| 235 |
this.model = model; |
| 236 |
this.branchLength = branchLength; |
| 237 |
} |
| 238 |
|
| 239 |
public double value(double prop) { |
| 240 |
return model.getBranchRewardDensity(prop, branchLength); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
private class DensityFunction implements UnivariateRealFunction { |
| 245 |
|
| 246 |
private final MarkovReward markovReward; |
| 247 |
private final double branchLength; |
| 248 |
private final double rate; |
| 249 |
private final double prop; |
| 250 |
|
| 251 |
DensityFunction(MarkovReward markovReward, double branchLength, double rate, double prop) { |
| 252 |
this.markovReward = markovReward; |
| 253 |
this.branchLength = branchLength; |
| 254 |
this.rate = rate; |
| 255 |
this.prop = prop; |
| 256 |
} |
| 257 |
|
| 258 |
@Override |
| 259 |
public double value(double v) { |
| 260 |
return markovReward.computePdf(v, branchLength, 0, 0) / |
| 261 |
(markovReward.computeConditionalProbability(branchLength, 0, 0) |
| 262 |
- Math.exp(-rate * prop * branchLength)); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
private class UnitDensityFunction implements UnivariateRealFunction { |
| 267 |
|
| 268 |
private final MarkovReward markovReward; |
| 269 |
private final double branchLength; |
| 270 |
private final double rate; |
| 271 |
private final double prop; |
| 272 |
|
| 273 |
UnitDensityFunction(MarkovReward markovReward, double branchLength, double rate, double prop) { |
| 274 |
this.markovReward = markovReward; |
| 275 |
this.branchLength = branchLength; |
| 276 |
this.rate = rate; |
| 277 |
this.prop = prop; |
| 278 |
} |
| 279 |
|
| 280 |
@Override |
| 281 |
public double value(double v) { |
| 282 |
double density = markovReward.computePdf(v * branchLength, branchLength, 0, 0) / |
| 283 |
(markovReward.computeConditionalProbability(branchLength, 0, 0) |
| 284 |
- Math.exp(-rate * prop * branchLength)); |
| 285 |
return density * branchLength; |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
private double[] createLatentInfinitesimalMatrix(final double rate, final double prop) { |
| 290 |
double[] mat = new double[]{ |
| 291 |
-rate * prop, rate * prop, |
| 292 |
rate * (1.0 - prop), -rate * (1.0 - prop) |
| 293 |
}; |
| 294 |
return mat; |
| 295 |
} |
| 296 |
|
| 297 |
private SericolaSeriesMarkovReward createSericolaMarkovReward(final double rate, final double prop) { |
| 298 |
double[] r = new double[]{0.0, 1.0}; |
| 299 |
return new SericolaSeriesMarkovReward(createLatentInfinitesimalMatrix(rate, prop), r, 2); |
| 300 |
} |
| 301 |
|
| 302 |
private TwoStateOccupancyMarkovReward createMarkovReward(final double rate, final double prop) { |
| 303 |
TwoStateOccupancyMarkovReward markovReward = new |
| 304 |
TwoStateOccupancyMarkovReward( |
| 305 |
createLatentInfinitesimalMatrix(rate, prop) |
| 306 |
); |
| 307 |
return markovReward; |
| 308 |
} |
| 309 |
} |