Debian Codegraph @

src/test/dr/evomodel/substmodel/StateHistoryTest.java

raw symbols SCIP
1 /*
2 * StateHistoryTest.java
3 *
4 * Copyright (c) 2002-2014 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package test.dr.evomodel.substmodel;
27
28 import dr.evomodel.substmodel.nucleotide.HKY;
29 import dr.evolution.datatype.Nucleotides;
30 import dr.evomodel.substmodel.FrequencyModel;
31 import dr.evomodel.substmodel.MarkovJumpsSubstitutionModel;
32 import dr.evomodel.substmodel.SubstitutionModel;
33 import dr.evomodel.substmodel.UniformizedSubstitutionModel;
34 import dr.inference.markovjumps.MarkovJumpsCore;
35 import dr.inference.markovjumps.StateHistory;
36 import dr.math.LogTricks;
37 import dr.math.MathUtils;
38 import dr.math.matrixAlgebra.Vector;
39 import test.dr.math.MathTestCase;
40
41 /**
42 * @author Marc A. Suchard
43 */
44 public class StateHistoryTest extends MathTestCase {
45
46 public static final int N = 1000000;
47
48 public void setUp() {
49
50 MathUtils.setSeed(666);
51
52 freqModel = new FrequencyModel(Nucleotides.INSTANCE,
53 new double[]{0.45, 0.25, 0.05, 0.25});
54 baseModel = new HKY(2.0, freqModel);
55 stateCount = baseModel.getDataType().getStateCount();
56
57 lambda = new double[stateCount * stateCount];
58 baseModel.getInfinitesimalMatrix(lambda);
59 System.out.println("lambda = " + new Vector(lambda));
60
61 markovjumps = new MarkovJumpsSubstitutionModel(baseModel);
62 }
63
64
65 public void testGetLogLikelihood() {
66 System.out.println("Start of getLogLikelihood test");
67
68 int startingState = 1;
69 int endingState = 1;
70 double duration = 0.5;
71 int iterations = 5000000;
72 // int iterations = 1;
73
74 double[] probs = new double[16];
75 baseModel.getTransitionProbabilities(duration, probs);
76 double trueProb = probs[startingState * 4 + endingState];
77 System.out.println("Tru prob = " + trueProb);
78
79 UniformizedSubstitutionModel uSM = new UniformizedSubstitutionModel(baseModel);
80 uSM.setSaveCompleteHistory(true);
81
82 double logProb = Double.NEGATIVE_INFINITY;
83 double prob = 0.0;
84 double condProb = 0.0;
85 for (int i = 0; i < iterations; ++i) {
86 uSM.computeCondStatMarkovJumps(startingState, endingState, duration, trueProb);
87 StateHistory history = uSM.getStateHistory();
88 // System.out.println(history.getEndingTime() - history.getStartingTime());
89 assertEquals(history.getEndingTime() - history.getStartingTime(), duration, 10E-3);
90 assertEquals(history.getStartingState(), startingState);
91 assertEquals(history.getEndingState(), endingState);
92 double logLikelihood = history.getLogLikelihood(lambda, 4);
93 prob += Math.exp(logLikelihood);
94 logProb = LogTricks.logSum(logProb, logLikelihood);
95 condProb += Math.exp(-logLikelihood);
96 // System.err.println(logLikelihood);
97 }
98 logProb = Math.exp(logProb - Math.log(iterations));
99 prob /= iterations;
100 condProb /= iterations;
101 System.out.println("Sim prob = " + prob);
102 System.out.println("Inv prob = " + (1.0 / condProb));
103 // System.out.println("log prob = " + logProb);
104 // System.exit(-1);
105
106
107 System.out.println();
108 System.out.println();
109
110 // Try using unconditioned simulation
111 double marginalProb = 0.0;
112 double mcProb = 0.0;
113 double invMcProb = 0.0;
114 int totalTries = 0;
115 int i = 0;
116 while (i < iterations) {
117 startingState = MathUtils.randomChoicePDF(freqModel.getFrequencies());
118 StateHistory history = StateHistory.simulateUnconditionalOnEndingState(0, startingState, duration, lambda, 4);
119 if (//history.getEndingState() == endingState &&
120 // history.getNumberOfJumps() == randomChoice1
121 true
122 ) {
123 marginalProb += 1.0;
124 i++;
125 double logLike = history.getLogLikelihood(lambda, 4);
126 mcProb += Math.exp(logLike);
127 invMcProb += Math.exp(-logLike);
128
129 // if (i % 100000 == 0) System.out.println(i);
130 }
131 totalTries++;
132 }
133 marginalProb /= totalTries;
134 mcProb /= iterations;
135 invMcProb /= iterations;
136 System.out.println("Sim uncd = " + marginalProb);
137 System.out.println("mc prob = " + mcProb);
138 System.out.println("m2 prob = " + (1.0 / invMcProb));
139
140 assertEquals(prob, trueProb);
141 }
142
143 public void testFreqDistribution() {
144
145 System.out.println("Start of FreqDistribution test");
146 int startingState = 0;
147 double duration = 10; // 10 expected substitutions is close to \infty
148
149 double[] freq = new double[stateCount];
150
151 for (int i = 0; i < N; i++) {
152 StateHistory simultant = StateHistory.simulateUnconditionalOnEndingState(0.0, startingState, duration,
153 lambda, stateCount);
154 freq[simultant.getEndingState()]++;
155 }
156
157 for (int i = 0; i < stateCount; i++) {
158 freq[i] /= N;
159 }
160
161 System.out.println("freq = " + new Vector(freq));
162 assertEquals(freq, freqModel.getFrequencies(), 1E-3);
163 System.out.println("End of FreqDistribution test\n");
164 }
165
166 public void testCounts() {
167
168 System.out.println("State of Counts test");
169 int startingState = 2;
170 double duration = 0.5;
171
172 int[] counts = new int[stateCount * stateCount];
173 double[] expectedCounts = new double[stateCount * stateCount];
174
175 for (int i = 0; i < N; i++) {
176 StateHistory simultant = StateHistory.simulateUnconditionalOnEndingState(0.0, startingState, duration,
177 lambda, stateCount);
178 simultant.accumulateSufficientStatistics(counts, null);
179 }
180
181 for (int i = 0; i < stateCount * stateCount; i++) {
182 expectedCounts[i] = (double) counts[i] / (double) N;
183 }
184
185 double[] r = new double[stateCount * stateCount];
186 double[] joint = new double[stateCount * stateCount];
187 double[] analytic = new double[stateCount * stateCount];
188
189 for (int from = 0; from < stateCount; from++) {
190 for (int to = 0; to < stateCount; to++) {
191 double marginal = 0;
192 if (from != to) {
193 MarkovJumpsCore.fillRegistrationMatrix(r, from, to, stateCount);
194 markovjumps.setRegistration(r);
195 markovjumps.computeJointStatMarkovJumps(duration, joint);
196
197 for (int j = 0; j < stateCount; j++) {
198 marginal += joint[startingState * stateCount + j]; // Marginalize out ending state
199 }
200 }
201 analytic[from * stateCount + to] = marginal;
202 }
203 }
204
205 System.out.println("unconditional expected counts = " + new Vector(expectedCounts));
206 System.out.println("analytic counts = " + new Vector(analytic));
207
208 assertEquals(expectedCounts, analytic, 1E-3);
209 System.out.println("End of Counts test\n");
210 }
211
212 double[] lambda;
213 FrequencyModel freqModel;
214 SubstitutionModel baseModel;
215 MarkovJumpsSubstitutionModel markovjumps;
216 int stateCount;
217
218
219 }