Debian Codegraph @

src/dr/evomodel/continuous/StandardizeTraits.java

raw symbols SCIP
1 /*
2 * StandarizeTraits.java
3 *
4 * Copyright (c) 2002-2017 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.evomodel.continuous;
27
28 import dr.evolution.util.Taxa;
29 import dr.evolution.util.Taxon;
30 import dr.inference.model.MatrixParameterInterface;
31 import dr.math.Polynomial;
32 import dr.xml.*;
33
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.StringTokenizer;
37
38 /**
39 * Created by msuchard on 2/3/17.
40 */
41 public class StandardizeTraits {
42
43 public static final String STANDARDIZE_TRAITS = "standardizeTraits";
44 public static final String TRAIT_NAME = dr.evomodelxml.treelikelihood.TreeTraitParserUtilities.TRAIT_NAME;
45
46 private final MatrixParameterInterface matrix;
47
48 public StandardizeTraits(MatrixParameterInterface matrix) {
49 this.matrix = matrix;
50 // System.err.println("rows: " + matrix.getRowDimension());
51 // System.err.println("cols: " + matrix.getColumnDimension());
52 }
53
54 // private final Taxa taxa;
55 // private final String traitName;
56 //
57 // public StandardizeTraits(Taxa taxa, String traitName) {
58 // this.taxa = taxa;
59 // this.traitName = traitName;
60 // }
61 //
62
63 public String doStandardization(boolean byColumn) {
64
65 StringBuilder sb = new StringBuilder();
66 sb.append("Trait standardization report:\n");
67
68 final int dim = (byColumn ? matrix.getColumnDimension() : matrix.getRowDimension());
69
70 for (int index = 0; index < dim; ++index) {
71 MeanVariance mv = getStatistics(matrix, index, byColumn);
72
73 sb.append("\tBEFORE\n");
74 sb.append("\tindex: " + (index + 1) + "\n");
75 sb.append("\tmean : " + mv.mean + "\n");
76 sb.append("\tvar : " + mv.variance + "\n");
77 sb.append("\tcnt : " + mv.count + "\n");
78
79 updateValues(matrix, mv, index, byColumn);
80
81 mv = getStatistics(matrix, index, byColumn);
82 sb.append("\tAFTER\n");
83 sb.append("\tindex: " + (index + 1) + "\n");
84 sb.append("\tmean : " + mv.mean + "\n");
85 sb.append("\tvar : " + mv.variance + "\n");
86 sb.append("\tcnt : " + mv.count + "\n\n");
87
88 }
89
90 return sb.toString();
91 }
92
93 // public void doStandardization() throws Exception {
94 // List<Taxon> taxonList = taxa.asList();
95 //
96 //
97 // List<double[]> values = new ArrayList<double[]>();
98 //
99 // for (Taxon taxon : taxonList) {
100 // String attribute = (String) taxon.getAttribute(traitName);
101 // if (attribute == null) {
102 // throw new Exception("Taxon " + taxon.getId() + " does not contain trait " + traitName);
103 // }
104 // values.add(convert(attribute));
105 // }
106 //
107 // final int dim = values.get(0).length;
108 //
109 // for (int col = 0; col < dim; ++col) {
110 // standardize(values, col);
111 // }
112 //
113 // int index = 0;
114 // for (Taxon taxon : taxonList) {
115 // taxon.setAttribute(traitName, convert(values.get(index)));
116 // ++index;
117 // }
118 //// System.exit(-1);
119 // }
120 //
121 // private double[] convert(String string) {
122 // StringTokenizer st = new StringTokenizer(string);
123 // double[] values = new double[st.countTokens()];
124 //
125 // for (int i = 0; i < st.countTokens(); ++i) {
126 // String str = st.nextToken();
127 //
128 // double value = Double.NaN;
129 // if (str.equals("NA") || str.equals("?")) {
130 // // Do nothing
131 // } else {
132 // value = Double.valueOf(str);
133 // }
134 // values[i] = value;
135 // }
136 //
137 // return values;
138 // }
139 //
140 // private String convert(double[] values) {
141 // StringBuilder sb = new StringBuilder();
142 // for (double x : values) {
143 // if (!Double.isNaN(x)) {
144 // sb.append(x);
145 // } else {
146 // sb.append("NA");
147 // }
148 // sb.append(" ");
149 // }
150 // return sb.toString().trim();
151 // }
152 //
153 // private void standardize(List<double[]> values, int column) {
154 //
155 // MeanVariance mv = getStatistics(values, column);
156 // final double sd = Math.sqrt(mv.variance);
157 //
158 //
159 // System.err.println("Col: " + column);
160 // System.err.println("Mean: " + mv.mean);
161 // System.err.println("Var: " + mv.variance);
162 // System.err.println("Count:" + mv.count);
163 // System.err.println("");
164 //
165 // for (double[] row : values) {
166 // double x = row[column];
167 // if (!Double.isNaN(x)) {
168 // row[column] = (x - mv.mean) / sd;
169 // }
170 // }
171 // }
172
173 private void updateValues(MatrixParameterInterface matrix, final MeanVariance mv, int major, boolean byColumn) {
174
175 final int dim = (byColumn ? matrix.getRowDimension() : matrix.getColumnDimension());
176 final double sd = Math.sqrt(mv.variance);
177
178 for (int index = 0; index < dim; ++index) {
179
180 final int row = byColumn ? index : major;
181 final int col = byColumn ? major : index;
182
183 double x = matrix.getParameterValue(row, col);
184 if (!Double.isNaN(x)) {
185 x = (x - mv.mean) / sd;
186 matrix.setParameterValueQuietly(row, col, x);
187 }
188 }
189
190 matrix.fireParameterChangedEvent();
191 }
192
193
194 // private MeanVariance getStatistics(List<double[]> values, int column) {
195 //
196 // double s = 0.0;
197 // double ss = 0.0;
198 // int c = 0;
199 // for (double[] row : values) {
200 // double x = row[column];
201 // if (!Double.isNaN(x)) {
202 // s += x;
203 // ss += x * x;
204 // ++c;
205 // }
206 // }
207 //
208 // MeanVariance mv = new MeanVariance();
209 // mv.mean = s / c;
210 // mv.variance = ss / c - mv.mean * mv.mean;
211 // mv.count = c;
212 //
213 // return mv;
214 // }
215
216 private MeanVariance getStatistics(MatrixParameterInterface matrix, int major, boolean byColumn) {
217
218 double s = 0.0;
219 double ss = 0.0;
220 int c = 0;
221
222 final int dim = (byColumn ? matrix.getRowDimension() : matrix.getColumnDimension());
223 for (int index = 0; index < dim; ++index) {
224 double x = byColumn ? matrix.getParameterValue(index, major) : matrix.getParameterValue(major, index);
225 if (!Double.isNaN(x)) {
226 s += x;
227 ss += x * x;
228 ++c;
229 }
230 }
231
232 MeanVariance mv = new MeanVariance();
233 mv.mean = s / c;
234 mv.variance = ss / c - mv.mean * mv.mean;
235 mv.count = c;
236
237 return mv;
238 }
239
240 private class MeanVariance {
241 double mean;
242 double variance;
243 int count;
244 }
245
246 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
247
248 @Override
249 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
250 // Taxa taxa = (Taxa) xo.getChild(Taxa.class);
251 // String traitName = (String) xo.getAttribute(TRAIT_NAME);
252
253 MatrixParameterInterface matrix = (MatrixParameterInterface) xo.getChild(MatrixParameterInterface.class);
254
255 StandardizeTraits st = new StandardizeTraits(matrix);
256
257 st.doStandardization(false);
258
259 // try {
260 // st.doStandardization();
261 // } catch (Exception e) {
262 // throw new XMLParseException(e.getMessage());
263 // }
264
265 // System.exit(-1);
266
267 return st;
268 }
269
270 /**
271 * @return an array of syntax rules required by this element.
272 * Order is not important.
273 */
274 @Override
275 public XMLSyntaxRule[] getSyntaxRules() {
276 return rules;
277 }
278
279 @Override
280 public String getParserDescription() {
281 return null;
282 }
283
284 @Override
285 public Class getReturnType() {
286 return StandardizeTraits.class;
287 }
288
289 /**
290 * @return Parser name, which is identical to name of xml element parsed by it.
291 */
292 @Override
293 public String getParserName() {
294 return STANDARDIZE_TRAITS;
295 }
296
297 private final XMLSyntaxRule[] rules = new XMLSyntaxRule[] {
298 // AttributeRule.newStringRule(TRAIT_NAME),
299 // new ElementRule(Taxa.class),
300 new ElementRule(MatrixParameterInterface.class),
301 };
302 };
303 }