Debian Codegraph @

src/dr/app/beauti/options/Predictor.java

raw symbols SCIP
1 /*
2 * TraitData.java
3 *
4 * Copyright (c) 2002-2015 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 dr.app.beauti.options;
27
28 import dr.stats.DiscreteStatistics;
29
30 import java.io.Serializable;
31 import java.util.*;
32
33 /**
34 * @author Andrew Rambaut
35 */
36 public class Predictor implements Serializable {
37 private static final long serialVersionUID = -9152518508699327745L;
38
39 public enum Type {
40 MATRIX,
41 ORIGIN_VECTOR,
42 DESTINATION_VECTOR,
43 BOTH_VECTOR;
44
45 public String toString() {
46 return name().toLowerCase();
47 }
48 }
49
50 private final Type predictorType;
51 private final TraitData trait;
52
53 private String name;
54 private boolean isIncluded;
55 private boolean isLogged;
56 private boolean isStandardized;
57 private boolean isOrigin;
58 private boolean isDestination;
59
60 private final double[][] data;
61 private final String[] rowLabels;
62
63 protected final BeautiOptions options;
64
65 public Predictor(BeautiOptions options, String name, TraitData trait, String[] rowLabels, double[][] data, Type predictorType) {
66 this.options = options;
67 this.name = name;
68 this.trait = trait;
69 this.rowLabels = rowLabels;
70 this.data = data;
71 this.predictorType = predictorType;
72 this.isIncluded = true;
73 this.isLogged = true;
74 this.isStandardized = true;
75 this.isOrigin = predictorType == Type.ORIGIN_VECTOR || predictorType == Type.BOTH_VECTOR;
76 this.isDestination = predictorType == Type.DESTINATION_VECTOR || predictorType == Type.BOTH_VECTOR;
77 }
78
79 /////////////////////////////////////////////////////////////////////////
80
81 public Type getType() {
82 return predictorType;
83 }
84
85 public String getName() {
86 return name;
87 }
88
89 public void setName(final String name) {
90 this.name = name;
91 }
92
93 public boolean isIncluded() {
94 return isIncluded;
95 }
96
97 public void setIncluded(boolean included) {
98 isIncluded = included;
99 }
100
101 public boolean isLogged() {
102 return isLogged;
103 }
104
105 public void setLogged(boolean logged) {
106 isLogged = logged;
107 }
108
109 public boolean isStandardized() {
110 return isStandardized;
111 }
112
113 public boolean isOrigin() {
114 return isOrigin;
115 }
116
117 public void setOrigin(boolean origin) {
118 isOrigin = origin;
119 }
120
121 public boolean isDestination() {
122 return isDestination;
123 }
124
125 public void setDestination(boolean destination) {
126 isDestination = destination;
127 }
128
129 public void setStandardized(boolean standardized) {
130 isStandardized = standardized;
131 }
132
133 public boolean hasZeroValues(boolean ignoreDiagonals) {
134 //matrices are neither origin nor destination
135 if (isOrigin() || isDestination()) {
136 for (int i = 0; i < data.length; i++) {
137 if (data[i][0] == 0.0) {
138 return true;
139 }
140 }
141 } else {
142 for (int i = 0; i < data.length; i++) {
143 for (int j = 0; j < data.length; j++) {
144 if (data[i][j] == 0) {
145 if (i != j) {
146 return true;
147 } else if (!ignoreDiagonals) {
148 return true;
149 }
150 }
151 }
152 }
153 }
154
155 return false;
156 }
157
158 public boolean isBinary() {
159 for (int i = 0; i < data.length; i++) {
160 for (int j = 0; j < data.length; j++) {
161 if (data[i][j] != 0 &&
162 data[i][j] != 1) {
163 return false;
164 }
165 }
166 }
167
168 return true;
169 }
170
171
172 public double[][] getMatrixValues(Type predictorType) {
173 double[][] matrixValues = new double[data.length][];
174
175 // create a mapping of the states in order the trait has them to that
176 // of the imported data.
177 List<String> states = new ArrayList<String>(trait.getStatesOfTrait());
178 int[] stateIndices = new int[data.length];
179 for (int i = 0; i < data.length; i++) {
180 stateIndices[states.indexOf(rowLabels[i])] = i;
181 }
182
183 if (getType() == Type.MATRIX) {
184 if (predictorType != Type.MATRIX) {
185 throw new IllegalArgumentException("Predictor is a matrix");
186 }
187 for (int i = 0; i < data.length; i++) {
188 matrixValues[i] = new double[data.length];
189 for (int j = 0; j < data.length; j++) {
190 matrixValues[i][j] = data[stateIndices[i]][stateIndices[j]];
191 }
192 }
193 } else {
194
195 if (predictorType != Type.ORIGIN_VECTOR && predictorType != Type.DESTINATION_VECTOR) {
196 throw new IllegalArgumentException("Predictor is a vector");
197 }
198
199 for (int i = 0; i < data.length; i++) {
200 matrixValues[i] = new double[data.length];
201 }
202 for (int i = 0; i < data.length; i++) {
203 for (int j = 0; j < data.length; j++) {
204 if (predictorType == Type.ORIGIN_VECTOR) {
205 matrixValues[i][j] = data[stateIndices[i]][0];
206 } else {
207 matrixValues[j][i] = data[stateIndices[i]][0];
208 }
209 }
210 }
211 }
212
213 // set the diagonal to NaN - this doesn't enter the final vector but helps debugging
214 for (int i = 0; i < data.length; i++) {
215 matrixValues[i][i] = Double.NaN;
216 }
217
218 if (isLogged()) {
219 for (int i = 0; i < matrixValues.length; i++) {
220 for (int j = 0; j < matrixValues[i].length; j++) {
221 if (i != j) {
222 matrixValues[i][j] = Math.log(matrixValues[i][j]);
223 }
224 }
225 }
226 }
227
228 if (isStandardized()) {
229 double[] values = new double[matrixValues.length * (matrixValues.length - 1)];
230
231 int k = 0;
232 for (int i = 0; i < matrixValues.length; i++) {
233 for (int j = 0; j < matrixValues.length; j++) {
234
235 if (i != j) {
236 values[k] = matrixValues[i][j];
237 k++;
238 }
239 }
240 }
241 double mean = DiscreteStatistics.mean(values);
242 double stdev = DiscreteStatistics.stdev(values);
243
244 for (int i = 0; i < matrixValues.length; i++) {
245 for (int j = 0; j < matrixValues.length; j++) {
246 if (i != j) {
247 matrixValues[i][j] = ((matrixValues[i][j] - mean) / stdev);
248 }
249
250 }
251 }
252 }
253 return matrixValues;
254 }
255
256 /**
257 * Return string with the values in the linear top triangle, bottom triangle format
258 * @return
259 */
260 public String getValueString(Type predictorType) {
261 StringBuilder valueString = new StringBuilder();
262 double[][] matrix = getMatrixValues(predictorType);
263
264 boolean first = true;
265
266 int n = matrix.length;
267
268 // upper triangle
269 for (int i = 0; i < n; i++) {
270 for (int j = i + 1; j < n; j++) {
271 if (!first) {
272 valueString.append(" ");
273 } else {
274 first = false;
275 }
276 valueString.append(matrix[i][j]);
277 }
278 }
279 // lower triangle
280 for (int i = 0; i < n; i++) {
281 for (int j = i + 1; j < n; j++) {
282 valueString.append(" ");
283 valueString.append(matrix[j][i]);
284 }
285 }
286 return valueString.toString();
287 }
288
289 public String toString() {
290 return name;
291 }
292 }