Debian Codegraph @

src/test/dr/app/beagle/HiddenMarkovRatesTest.java

raw symbols SCIP
1 /*
2 * HiddenMarkovRatesTest.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.app.beagle;
27
28 import dr.evomodel.substmodel.nucleotide.HKY;
29 import dr.evolution.datatype.Nucleotides;
30 import dr.evolution.datatype.TwoStates;
31 import dr.evomodel.substmodel.FrequencyModel;
32 import dr.evomodel.substmodel.GeneralSubstitutionModel;
33 import dr.evomodel.substmodel.SubstitutionModel;
34 import dr.evomodel.substmodel.UniformizedSubstitutionModel;
35 import dr.inference.markovjumps.MarkovJumpsType;
36 import dr.inference.model.Parameter;
37 import dr.math.matrixAlgebra.Vector;
38 import test.dr.inference.trace.TraceCorrelationAssert;
39
40 import java.util.List;
41
42 /**
43 * @author Marc Suchard
44 */
45
46 public class HiddenMarkovRatesTest extends TraceCorrelationAssert {
47
48 public HiddenMarkovRatesTest(String name) {
49 super(name);
50 }
51
52 private double[] getBinaryFreqs(int index) {
53 return new double[]{0.7, 0.3};
54 }
55
56 private double[] getHKYFreqs(int index) {
57 return new double[]{0.25, 0.25, 0.25, 0.25};
58 }
59
60 private double getKappa(int index) {
61 return 10.0;
62 }
63
64 private int getNumberReplicates(int index) {
65 return 1000000;
66 }
67
68 private double getLength(int index) {
69 return 1.5;
70 }
71
72 public void testHiddenRates() {
73
74 final int index = 0;
75
76 double[] freqs = getBinaryFreqs(index);
77 FrequencyModel binaryFreqModel = new FrequencyModel(TwoStates.INSTANCE, freqs);
78
79 int relativeTo = 0;
80
81 Parameter ratesParameter = new Parameter.Default(0);
82
83 GeneralSubstitutionModel binaryModel = new GeneralSubstitutionModel("binary", TwoStates.INSTANCE, binaryFreqModel, ratesParameter, relativeTo);
84 UniformizedSubstitutionModel uSM = new UniformizedSubstitutionModel(binaryModel, MarkovJumpsType.REWARDS);
85 uSM.setSaveCompleteHistory(true);
86 double[] rewardRegister = new double[]{0.0, 1.0};
87 uSM.setRegistration(rewardRegister);
88
89 final double[] hkyFreqs = getHKYFreqs(index);
90 FrequencyModel hkyFreqModel = new FrequencyModel(Nucleotides.INSTANCE, hkyFreqs);
91 final double kappa = getKappa(index);
92
93 final HKY hky = new HKY(kappa, hkyFreqModel);
94
95 final double length = getLength(index);
96
97 double[] resultCompleteHistory = new double[16];
98
99 final int replicates = getNumberReplicates(index);
100
101 double result = 0.0;
102 for (int r = 0; r < replicates; ++r) {
103 result += oneCompleteHistoryReplicate(resultCompleteHistory, hky, uSM, length);
104 }
105 result /= replicates;
106 normalize(resultCompleteHistory, replicates);
107
108 System.out.println("Averaged probabilities");
109 System.out.println(result);
110 System.out.println(new Vector(resultCompleteHistory));
111 System.out.println();
112
113 double[] intermediate = new double[16];
114 hky.getTransitionProbabilities(result, intermediate);
115
116 System.out.println("Intermediate using above average reward");
117 System.out.println(result);
118 System.out.println(new Vector(intermediate));
119 System.out.println();
120
121 double[] resultExpected = new double[16];
122 UniformizedSubstitutionModel expectedUSM = new UniformizedSubstitutionModel(binaryModel, MarkovJumpsType.REWARDS, replicates);
123 expectedUSM.setRegistration(rewardRegister);
124 result = oneCompleteHistoryReplicate(resultExpected, hky, expectedUSM, length);
125
126 System.out.println("Averaged reward");
127 System.out.println(result);
128 System.out.println(new Vector(resultExpected));
129 System.out.println();
130
131 double[] originalProbs = new double[16];
132 hky.getTransitionProbabilities(length, originalProbs);
133 System.out.println("Original probabilities");
134 System.out.println(new Vector(originalProbs));
135 System.out.println();
136 }
137
138 double oneCompleteHistoryReplicate(double[] result, final SubstitutionModel hky,
139 final UniformizedSubstitutionModel uSM, final double length) {
140 double reward = uSM.computeCondStatMarkovJumps(1, 1, length);
141 if (DEBUG) reward = DEBUG_REWARD;
142
143 double[] tmp = new double[hky.getDataType().getStateCount() * hky.getDataType().getStateCount()];
144 hky.getTransitionProbabilities(reward, tmp);
145 increment(result, tmp);
146 return reward;
147 }
148
149 private static final boolean DEBUG = false;
150 private static final double DEBUG_REWARD = 2.0;
151
152 private void normalize(double[] a, final int count) {
153 for (int i = 0; i < a.length; ++i) {
154 a[i] /= count;
155 }
156 }
157
158 private void increment(double[] out, final double[] in) {
159 for (int i = 0; i < out.length; ++i) {
160 out[i] += in[i];
161 }
162 }
163
164 private double[] getTransitionProbabilities(SubstitutionModel substModel, List<Double> times) {
165 final int stateCount = substModel.getDataType().getStateCount();
166 double[] result = getIdentityMatrix(stateCount);
167 double[] tmp = new double[stateCount * stateCount];
168 for (Double time : times) {
169 substModel.getTransitionProbabilities(time, tmp);
170 result = matrixMultiplication(result, tmp, stateCount);
171 }
172 return result;
173 }
174
175 private double[] matrixMultiplication(double[] a, double[] b, int dim) {
176 double[] result = new double[dim * dim];
177 for (int i = 0; i < dim; ++i) {
178 for (int j = 0; j < dim; ++j) {
179 for (int k = 0; k < dim; ++k) {
180 result[i * dim + j] += a[i * dim + k] * b[k * dim + j];
181 }
182 }
183 }
184 return result;
185 }
186
187 private double[] getIdentityMatrix(int stateCount) {
188 double[] result = new double[stateCount * stateCount];
189 for (int i = 0; i < stateCount; ++i) {
190 result[i * stateCount + i] = 1.0;
191 }
192 return result;
193 }
194 }