| 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.substmodel.ComplexSubstitutionModel; |
| 29 |
import dr.evomodel.substmodel.FrequencyModel; |
| 30 |
import dr.evomodel.substmodel.MarkovJumpsSubstitutionModel; |
| 31 |
import dr.evolution.datatype.DataType; |
| 32 |
import dr.evolution.datatype.TwoStates; |
| 33 |
import dr.inference.markovjumps.MarkovJumpsType; |
| 34 |
import dr.inference.model.Parameter; |
| 35 |
import dr.math.matrixAlgebra.Vector; |
| 36 |
import test.dr.math.MathTestCase; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class TwoStateMarkovRewardsTest extends MathTestCase { |
| 43 |
|
| 44 |
public void testTwoStateRewards() { |
| 45 |
DataType dataType = TwoStates.INSTANCE; |
| 46 |
FrequencyModel freqModel = new FrequencyModel(TwoStates.INSTANCE, new double[]{0.5, 0.5}); |
| 47 |
Parameter rates = new Parameter.Default(new double[]{4.0, 6.0}); |
| 48 |
ComplexSubstitutionModel twoStateModel = new ComplexSubstitutionModel("two", dataType, freqModel, rates) { |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
}; |
| 54 |
twoStateModel.setNormalization(false); |
| 55 |
|
| 56 |
MarkovJumpsSubstitutionModel markovRewards = new MarkovJumpsSubstitutionModel(twoStateModel, |
| 57 |
MarkovJumpsType.REWARDS); |
| 58 |
|
| 59 |
double[] r = new double[2]; |
| 60 |
double[] q = new double[4]; |
| 61 |
double[] c = new double[4]; |
| 62 |
|
| 63 |
int mark = 0; |
| 64 |
double weight = 1.0; |
| 65 |
|
| 66 |
r[mark] = weight; |
| 67 |
markovRewards.setRegistration(r); |
| 68 |
|
| 69 |
twoStateModel.getInfinitesimalMatrix(q); |
| 70 |
System.out.println("Q = " + new Vector(q)); |
| 71 |
|
| 72 |
System.out.println("Reward for state 0"); |
| 73 |
double time = 1.0; |
| 74 |
markovRewards.computeCondStatMarkovJumps(time, c); |
| 75 |
|
| 76 |
System.out.println("Reward conditional on X(0) = i, X(t) = j: " + new Vector(c)); |
| 77 |
|
| 78 |
double endTime = 10.0; |
| 79 |
int steps = 10; |
| 80 |
for (time = 0.0; time < endTime; time += (endTime / steps)) { |
| 81 |
markovRewards.computeCondStatMarkovJumps(time, c); |
| 82 |
System.out.println(time + "," + c[0]); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
private static double analyticProb(int from, int to, double alpha, double beta, double t) { |
| 100 |
|
| 101 |
double total = alpha + beta; |
| 102 |
int entry = from * 2 + to; |
| 103 |
|
| 104 |
switch (entry) { |
| 105 |
case 0: |
| 106 |
return (beta + alpha * Math.exp(-total * t)) / total; |
| 107 |
case 1: |
| 108 |
return (alpha - alpha * Math.exp(-total * t)) / total; |
| 109 |
case 2: |
| 110 |
return (beta - beta * Math.exp(-total * t)) / total; |
| 111 |
case 3: |
| 112 |
return (alpha + beta * Math.exp(-total * t)) / total; |
| 113 |
default: |
| 114 |
throw new RuntimeException(); |
| 115 |
} |
| 116 |
} |
| 117 |
} |