| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 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 |
|
| 51 |
|
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 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 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 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 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 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 |
|
| 251 |
|
| 252 |
|
| 253 |
MatrixParameterInterface matrix = (MatrixParameterInterface) xo.getChild(MatrixParameterInterface.class); |
| 254 |
|
| 255 |
StandardizeTraits st = new StandardizeTraits(matrix); |
| 256 |
|
| 257 |
st.doStandardization(false); |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
return st; |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 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 |
|
| 291 |
|
| 292 |
@Override |
| 293 |
public String getParserName() { |
| 294 |
return STANDARDIZE_TRAITS; |
| 295 |
} |
| 296 |
|
| 297 |
private final XMLSyntaxRule[] rules = new XMLSyntaxRule[] { |
| 298 |
|
| 299 |
|
| 300 |
new ElementRule(MatrixParameterInterface.class), |
| 301 |
}; |
| 302 |
}; |
| 303 |
} |