| 1 |
package test.dr.evomodel.substmodel; |
| 2 |
|
| 3 |
import test.dr.math.MathTestCase; |
| 4 |
import dr.evomodel.substmodel.nucleotide.HKY; |
| 5 |
import dr.evomodel.substmodel.FrequencyModel; |
| 6 |
import dr.evomodel.substmodel.MarkovJumpsSubstitutionModel; |
| 7 |
import dr.evolution.datatype.Nucleotides; |
| 8 |
import dr.inference.markovjumps.MarkovJumpsType; |
| 9 |
import dr.inference.markovjumps.MarkovJumpsCore; |
| 10 |
import dr.math.matrixAlgebra.Vector; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
public class MarkovJumpsSubstitutionModelTest extends MathTestCase { |
| 17 |
|
| 18 |
public void testMarkovJumpsCounts() { |
| 19 |
HKY substModel = new HKY(2.0, |
| 20 |
new FrequencyModel(Nucleotides.INSTANCE, |
| 21 |
new double[]{0.3, 0.2, 0.25, 0.25})); |
| 22 |
|
| 23 |
int states = substModel.getDataType().getStateCount(); |
| 24 |
|
| 25 |
MarkovJumpsSubstitutionModel markovjumps = new MarkovJumpsSubstitutionModel(substModel, |
| 26 |
MarkovJumpsType.COUNTS); |
| 27 |
double[] r = new double[states * states]; |
| 28 |
double[] q = new double[states * states]; |
| 29 |
double[] j = new double[states * states]; |
| 30 |
double[] c = new double[states * states]; |
| 31 |
double[] p = new double[states * states]; |
| 32 |
|
| 33 |
double time = 1.0; |
| 34 |
int from = 0; |
| 35 |
int to = 1; |
| 36 |
MarkovJumpsCore.fillRegistrationMatrix(r, from, to, states, 1.0); |
| 37 |
markovjumps.setRegistration(r); |
| 38 |
|
| 39 |
substModel.getInfinitesimalMatrix(q); |
| 40 |
|
| 41 |
substModel.getTransitionProbabilities(time, p); |
| 42 |
|
| 43 |
markovjumps.computeJointStatMarkovJumps(time, j); |
| 44 |
|
| 45 |
markovjumps.computeCondStatMarkovJumps(time, c); |
| 46 |
|
| 47 |
MarkovJumpsCore.makeComparableToRPackage(q); |
| 48 |
System.out.println("Q = " + new Vector(q)); |
| 49 |
|
| 50 |
MarkovJumpsCore.makeComparableToRPackage(p); |
| 51 |
System.out.println("P = " + new Vector(p)); |
| 52 |
|
| 53 |
System.out.println("Counts:"); |
| 54 |
MarkovJumpsCore.makeComparableToRPackage(r); |
| 55 |
System.out.println("R = " + new Vector(r)); |
| 56 |
|
| 57 |
MarkovJumpsCore.makeComparableToRPackage(j); |
| 58 |
System.out.println("J = " + new Vector(j)); |
| 59 |
|
| 60 |
assertEquals(rMarkovJumpsJ, j, tolerance); |
| 61 |
|
| 62 |
MarkovJumpsCore.makeComparableToRPackage(c); |
| 63 |
System.err.println("C = " + new Vector(c)); |
| 64 |
|
| 65 |
assertEquals(rMarkovJumpsC, c, tolerance); |
| 66 |
} |
| 67 |
|
| 68 |
public void testMarkovJumpsReward() { |
| 69 |
HKY substModel = new HKY(2.0, |
| 70 |
new FrequencyModel(Nucleotides.INSTANCE, |
| 71 |
new double[]{0.3, 0.2, 0.25, 0.25})); |
| 72 |
|
| 73 |
int states = substModel.getDataType().getStateCount(); |
| 74 |
|
| 75 |
double[] j = new double[states * states]; |
| 76 |
double[] c = new double[states * states]; |
| 77 |
|
| 78 |
double time = 1.0; |
| 79 |
|
| 80 |
MarkovJumpsSubstitutionModel markovjumps = new MarkovJumpsSubstitutionModel(substModel, MarkovJumpsType.REWARDS); |
| 81 |
double[] rewards = {1.0, 1.0, 1.0, 1.0}; |
| 82 |
markovjumps.setRegistration(rewards); |
| 83 |
|
| 84 |
markovjumps.computeJointStatMarkovJumps(time, j); |
| 85 |
|
| 86 |
markovjumps.computeCondStatMarkovJumps(time, c); |
| 87 |
|
| 88 |
System.out.println("Rewards:"); |
| 89 |
MarkovJumpsCore.makeComparableToRPackage(rewards); |
| 90 |
System.out.println("R = " + new Vector(rewards)); |
| 91 |
|
| 92 |
MarkovJumpsCore.makeComparableToRPackage(j); |
| 93 |
System.out.println("J = " + new Vector(j)); |
| 94 |
|
| 95 |
assertEquals(rMarkovRewardsJ, j, tolerance); |
| 96 |
|
| 97 |
MarkovJumpsCore.makeComparableToRPackage(c); |
| 98 |
System.out.println("C = " + new Vector(c)); |
| 99 |
|
| 100 |
assertEquals(rMarkovRewardsC, c, tolerance); |
| 101 |
} |
| 102 |
|
| 103 |
public void testMarginalRates() { |
| 104 |
HKY substModel = new HKY(2.0, |
| 105 |
new FrequencyModel(Nucleotides.INSTANCE, |
| 106 |
new double[]{0.3, 0.2, 0.25, 0.25})); |
| 107 |
|
| 108 |
int states = substModel.getDataType().getStateCount(); |
| 109 |
|
| 110 |
MarkovJumpsSubstitutionModel markovjumps = new MarkovJumpsSubstitutionModel(substModel, |
| 111 |
MarkovJumpsType.COUNTS); |
| 112 |
double[] r = new double[states * states]; |
| 113 |
|
| 114 |
int from = 0; |
| 115 |
int to = 1; |
| 116 |
MarkovJumpsCore.fillRegistrationMatrix(r, from, to, states, 1.0); |
| 117 |
markovjumps.setRegistration(r); |
| 118 |
|
| 119 |
double marginalRate = markovjumps.getMarginalRate(); |
| 120 |
System.out.println("Marginal rate = " + marginalRate); |
| 121 |
|
| 122 |
assertEquals(rMarkovMarginalRate, marginalRate, tolerance); |
| 123 |
|
| 124 |
MarkovJumpsCore.fillRegistrationMatrix(r, states); |
| 125 |
markovjumps.setRegistration(r); |
| 126 |
|
| 127 |
marginalRate = markovjumps.getMarginalRate(); |
| 128 |
System.out.println("Marginal rate = " + marginalRate); |
| 129 |
assertEquals(1.0, marginalRate, tolerance); |
| 130 |
} |
| 131 |
|
| 132 |
private static double tolerance = 1E-6; |
| 133 |
|
| 134 |
private static double[] rMarkovJumpsJ = { |
| 135 |
0.016780099, 0.013983416, 0.08448885, 0.022470093, |
| 136 |
0.003179188, 0.002649323, 0.02556076, 0.004475270, |
| 137 |
0.001889475, 0.001574562, 0.01612631, 0.002673292, |
| 138 |
0.001889475, 0.001574562, 0.01612631, 0.002673292 |
| 139 |
}; |
| 140 |
|
| 141 |
private static double[] rMarkovJumpsC = { |
| 142 |
0.034557323, 0.061024826, 0.66635307, 0.141775072, |
| 143 |
0.011561873, 0.006024690, 0.20159458, 0.028236719, |
| 144 |
0.009934702, 0.009934702, 0.03850175, 0.011499342, |
| 145 |
0.009934702, 0.009934702, 0.08671048, 0.005744804 |
| 146 |
}; |
| 147 |
|
| 148 |
private static double[] rMarkovRewardsJ = { |
| 149 |
0.4855729, 0.2291431, 0.1267929, 0.1584911, |
| 150 |
0.2749717, 0.4397443, 0.1267929, 0.1584911, |
| 151 |
0.1901894, 0.1584911, 0.4188461, 0.2324734, |
| 152 |
0.1901894, 0.1584911, 0.1859787, 0.4653407 |
| 153 |
}; |
| 154 |
|
| 155 |
private static double[] rMarkovRewardsC = { |
| 156 |
1, 1, 1, 1, |
| 157 |
1, 1, 1, 1, |
| 158 |
1, 1, 1, 1, |
| 159 |
1, 1, 1, 1 |
| 160 |
}; |
| 161 |
|
| 162 |
private static double rMarkovMarginalRate = 0.2010050 * 0.3; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|