Debian Codegraph @

src/test/dr/math/UniformizedStateHistoryTest.java

raw symbols SCIP
1 package test.dr.math;
2
3 import dr.evomodel.substmodel.FrequencyModel;
4 import dr.evomodel.substmodel.nucleotide.HKY;
5 import dr.evomodel.substmodel.MarkovJumpsSubstitutionModel;
6 import dr.evolution.datatype.Nucleotides;
7 import dr.inference.markovjumps.*;
8 import dr.inference.model.Parameter;
9 import dr.math.MathUtils;
10 import dr.math.matrixAlgebra.Vector;
11
12 import java.util.Arrays;
13
14 /**
15 * @author Marc A. Suchard
16 */
17 public class UniformizedStateHistoryTest extends MathTestCase {
18
19 public void setUp() {
20
21 MathUtils.setSeed(666);
22
23 Parameter kappa = new Parameter.Default(1, 2.0);
24 double[] pi = {0.45, 0.05, 0.30, 0.20};
25 Parameter freqs = new Parameter.Default(pi);
26 FrequencyModel f = new FrequencyModel(Nucleotides.INSTANCE, freqs);
27 hky = new HKY(kappa, f);
28 stateCount = hky.getDataType().getStateCount();
29
30 double[] lambda = new double[stateCount * stateCount];
31 hky.getInfinitesimalMatrix(lambda);
32
33 process = new SubordinatedProcess(lambda, stateCount);
34 }
35
36 HKY hky;
37 SubordinatedProcess process;
38 int stateCount;
39
40 public void testSubordinatedProcessGeneration() {
41
42 double[] oneStep = process.getDtmcProbabilities(1);
43 double[] threeStep = process.getDtmcProbabilities(3);
44
45 double[] rOneStep = {
46 0.2608696, 0.5217391, 4.347826e-02, 0.1739130,
47 0.7826087, 0.0000000, 4.347826e-02, 0.1739130,
48 0.3913043, 0.2608696, 2.220446e-16, 0.3478261,
49 0.3913043, 0.2608696, 8.695652e-02, 0.2608696
50 };
51 MarkovJumpsCore.makeComparableToRPackage(rOneStep);
52
53 double[] rThreeStep = {
54 0.3935235, 0.3570313, 0.04988904, 0.1995562,
55 0.5355470, 0.2150078, 0.04988904, 0.1995562,
56 0.4490014, 0.2993343, 0.04980685, 0.2018575,
57 0.4490014, 0.2993343, 0.05046437, 0.2012000
58 };
59 MarkovJumpsCore.makeComparableToRPackage(rThreeStep);
60
61 System.out.println("oneStep = " + new Vector(oneStep));
62 assertEquals(rOneStep, oneStep, 1E-6);
63
64 System.out.println("threeStep = " + new Vector(threeStep));
65 assertEquals(rThreeStep, threeStep, 1E-6);
66 }
67
68 public void testComputePdfForNextDraw() {
69
70 int startState = 1;
71 int endState = 0;
72
73 // int rStartState = 3; // 1 + 1 = 2 -> 3
74 // int rEndState = 1; // 0 + 1 = 1 -> 1
75
76 double[] pdf = new double[stateCount];
77
78 int n = 4;
79 int i = 1;
80
81 process.computePdfNextChainState(startState, endState, n, i, pdf);
82 pdf = MathUtils.getNormalized(pdf);
83
84 System.err.println("PDF = " + new Vector(pdf));
85
86 double[] rPDF = new double[]{3.422934e-01, 3.105519e-01, 2.216160e-16, 3.471547e-01};
87 MarkovJumpsCore.makeComparableToRPackage(rPDF);
88 assertEquals(rPDF, pdf, 1E-6);
89 }
90
91 public void testTotalChangesSamplingMethods() {
92
93 try {
94
95 int startState = 1;
96 int endState = 0;
97 double time = 0.5;
98 double[] ctmcProbabilities = new double[stateCount * stateCount];
99
100 hky.getTransitionProbabilities(time, ctmcProbabilities);
101 double ctmcProbability = ctmcProbabilities[startState * stateCount + endState];
102
103 double[] pdf = process.computePDFDirectly(startState, endState, time, ctmcProbability, 10);
104
105 System.out.println("PDF = " + new Vector(pdf));
106
107 double cutoff = pdf[0] + pdf[1] - 1E-6;
108 System.out.println("Test cutoff = " + cutoff);
109 int draw = process.drawNumberOfChanges(startState, endState, time, ctmcProbability, cutoff);
110 assertEquals(1, draw);
111
112 cutoff = pdf[0] + pdf[1] + 1E-6;
113 System.out.println("Test cutoff = " + cutoff);
114 draw = process.drawNumberOfChanges(startState, endState, time, ctmcProbability, cutoff);
115 assertEquals(2, draw);
116
117 cutoff = pdf[0] + pdf[1] + pdf[2] + 1E-6;
118 System.out.println("Test cutoff = " + cutoff);
119 draw = process.drawNumberOfChanges(startState, endState, time, ctmcProbability, cutoff);
120 assertEquals(3, draw);
121
122 System.out.println("");
123
124 startState = 1;
125 endState = 1;
126 time = 0.75;
127
128 hky.getTransitionProbabilities(time, ctmcProbabilities);
129 ctmcProbability = ctmcProbabilities[startState * stateCount + endState];
130 pdf = process.computePDFDirectly(startState, endState, time, ctmcProbability, 10);
131
132 System.out.println("PDF = " + new Vector(pdf));
133
134 cutoff = pdf[0] + pdf[1] + pdf[2] - 1E-6;
135 System.out.println("Test cutoff = " + cutoff);
136 draw = process.drawNumberOfChanges(startState, endState, time, ctmcProbability, cutoff);
137 assertEquals(2, draw);
138
139 cutoff = pdf[0] + pdf[1] + pdf[2] + 1E-6;
140 System.out.println("Test cutoff = " + cutoff);
141 draw = process.drawNumberOfChanges(startState, endState, time, ctmcProbability, cutoff);
142 assertEquals(3, draw);
143
144 } catch (SubordinatedProcess.Exception e) {
145 throw new RuntimeException("Subordinated process exception");
146 }
147 }
148
149 public void testStateHistorySimulationForJumps() {
150
151 try {
152
153 double startingTime = 1.0;
154 double endingTime = 3.0;
155 int startingState = 1;
156 int endingState = 3;
157
158 int N = 1000000;
159
160 double[] tmp = new double[stateCount * stateCount];
161 hky.getTransitionProbabilities(endingTime - startingTime, tmp);
162 double transitionProbability = tmp[startingState * stateCount + endingState];
163
164 double[][] registers = new double[2][stateCount * stateCount];
165 MarkovJumpsCore.fillRegistrationMatrix(registers[0], stateCount); // Count all jumps
166 registers[1][2 * stateCount + 1] = 1.0; // Mark just one state!
167
168 double[] expectations = new double[registers.length];
169
170 for (int i = 0; i < N; i++) {
171
172 StateHistory history = UniformizedStateHistory.simulateConditionalOnEndingState(
173 startingTime,
174 startingState,
175 endingTime,
176 endingState,
177 transitionProbability,
178 stateCount,
179 process);
180 for (int j = 0; j < registers.length; j++) {
181 expectations[j] += history.getTotalRegisteredCounts(registers[j]);
182 }
183 }
184
185 // Determine analytic solution
186 MarkovJumpsSubstitutionModel markovjumps = new MarkovJumpsSubstitutionModel(hky);
187 double[] mjExpectations = new double[stateCount * stateCount];
188
189 for (int j = 0; j < registers.length; j++) {
190 expectations[j] /= (double) N;
191 System.out.println("Expected number for register = " + expectations[j]);
192
193 markovjumps.setRegistration(registers[j]);
194 markovjumps.computeCondStatMarkovJumps(endingTime - startingTime, mjExpectations);
195
196 assertEquals(mjExpectations[startingState * stateCount + endingState], expectations[j], 1E-2);
197 }
198
199 } catch (SubordinatedProcess.Exception e) {
200 throw new RuntimeException("Subordinated process exception");
201 }
202 }
203 public void testStateHistorySimulationForRewards() {
204
205 try {
206
207 double startingTime = 1.0;
208 double endingTime = 3.0;
209 int startingState = 1;
210 int endingState = 3;
211
212 int N = 1000000;
213
214 double[] tmp = new double[stateCount * stateCount];
215 hky.getTransitionProbabilities(endingTime - startingTime, tmp);
216 double transitionProbability = tmp[startingState * stateCount + endingState];
217
218 double[][] registers = new double[3][stateCount];
219 Arrays.fill(registers[0], 1.0); // Reward all states
220 registers[1][0] = 1.0; // Reward just one state!
221 registers[2][3] = 1.0; // Reward just one state!
222
223 double[] expectations = new double[registers.length];
224
225 for (int i = 0; i < N; i++) {
226
227 StateHistory history = UniformizedStateHistory.simulateConditionalOnEndingState(
228 startingTime,
229 startingState,
230 endingTime,
231 endingState,
232 transitionProbability,
233 stateCount,
234 process);
235 for (int j = 0; j < registers.length; j++) {
236 expectations[j] += history.getTotalReward(registers[j]);
237 }
238 }
239
240 // Determine analytic solution
241 MarkovJumpsSubstitutionModel markovjumps = new MarkovJumpsSubstitutionModel(hky, MarkovJumpsType.REWARDS);
242 double[] mjExpectations = new double[stateCount * stateCount];
243
244 for (int j = 0; j < registers.length; j++) {
245 expectations[j] /= (double) N;
246 System.out.println("Expected reward for register[" + j +"] = " + expectations[j]);
247
248 markovjumps.setRegistration(registers[j]);
249 markovjumps.computeCondStatMarkovJumps(endingTime - startingTime, mjExpectations);
250
251 assertEquals(mjExpectations[startingState * stateCount + endingState], expectations[j], 1E-2);
252 }
253
254 } catch (SubordinatedProcess.Exception e) {
255 throw new RuntimeException("Subordinated process exception");
256 }
257 }
258 }
259
260 /*
261
262 # R markovjumps code
263
264 hky = as.eigen(hky.model(2, 1, c(0.45, 0.30, 0.05, 0.20), scale = T))
265 maxRate = - min(hky$rate.matrix)
266 R = diag(4) + hky$rate.matrix / maxRate
267 R3 = R %*% R %*% R
268 R4 = R %*% R3
269 (R[3,] * R3[,1]) / R4[3,1]
270 */