Debian Codegraph @

src/dr/evomodel/continuous/ApproximateFactorAnalysisPrecisionMatrix.java

raw symbols SCIP
1 /*
2 * ApproximateFactorAnalysisPrecisionMatrix.java
3 *
4 * Copyright (c) 2002-2016 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 cern.colt.matrix.DoubleMatrix2D;
29 import cern.colt.matrix.impl.DenseDoubleMatrix2D;
30 import dr.inference.model.*;
31 import dr.math.matrixAlgebra.Matrix;
32 import dr.math.matrixAlgebra.RobustSingularValueDecomposition;
33 import dr.math.matrixAlgebra.Vector;
34 import dr.xml.*;
35
36 /**
37 * @author Marc A. Suchard
38 * @author Max R. Tolkoff
39 *
40 */
41
42 public class ApproximateFactorAnalysisPrecisionMatrix extends Parameter.Abstract implements
43 MatrixParameterInterface, VariableListener {
44
45 private double[] values;
46 private double[] storedValues;
47
48 private boolean valuesKnown;
49 private boolean storedValuesKnow;
50
51 private final MatrixParameterInterface L;
52 private final Parameter gamma;
53 private int dim;
54
55 private final static boolean DEBUG = false;
56
57 public ApproximateFactorAnalysisPrecisionMatrix(String name,
58 MatrixParameterInterface L,
59 Parameter gamma) {
60 super(name);
61
62 this.L = L;
63 this.gamma = gamma;
64
65 L.addVariableListener(this);
66 gamma.addVariableListener(this);
67
68 this.dim = L.getRowDimension();
69
70
71 values = new double[dim * dim];
72 }
73
74 private void computeValues() {
75 if (!valuesKnown) {
76 computeValuesImp();
77 valuesKnown = true;
78 }
79 }
80
81 private void computeValuesImp() {
82 dim = L.getRowDimension();
83 double[][] matrix = new double[dim][dim];
84
85 for (int row = 0; row < L.getRowDimension(); ++row) {
86 for (int col = 0; col < L.getRowDimension(); ++col) {
87 double sum = 0;
88 for (int k = 0; k < L.getColumnDimension(); ++k) {
89 sum += L.getParameterValue(row, k) * L.getParameterValue(col, k);
90 }
91 matrix[row][col] = sum;
92 }
93
94
95 }
96 // DoubleMatrix2D LL = new DenseDoubleMatrix2D(matrix);
97 // RobustSingularValueDecomposition SVD = new RobustSingularValueDecomposition(LL);
98 // double[][] U = SVD.getU().toArray();
99 // double[][] V = SVD.getV().toArray();
100 // DoubleMatrix2D EvalsTemp= SVD.getS();
101 //
102 // double[] EVals = new double[EvalsTemp.rows()];
103 // for (int i = 0; i < EVals.length ; i++) {
104 // EVals[i] = EvalsTemp.get(i, i);
105 // }
106 //
107 // for (int i = 0; i < EVals.length ; i++) {
108 // if(Math.abs(EVals[i]) >= Math.pow(10, -10)){
109 // EVals[i] = 1 / EVals[i];
110 // }
111 // else
112 // EVals[i] = 0;
113 // }
114 //
115 //
116 // for (int i = 0; i <U.length; i++) {
117 // for (int j = 0; j <V.length ; j++) {
118 // matrix[i][j] = 0;
119 // for (int k = 0; k < U.length; k++) {
120 // matrix[i][j] += V[i][k] * EVals[k] * U[j][k];
121 // }
122 //
123 // }
124 //
125 // }
126
127 for (int row = 0; row < dim; row++) {
128
129 matrix[row][row] += 1 / gamma.getParameterValue(row);
130 }
131
132 if (DEBUG) {
133 System.err.println("mult:");
134 System.err.println(new Matrix(L.getParameterAsMatrix()));
135 System.err.println(new Vector(gamma.getParameterValues()) + "\n");
136 System.err.println(new Matrix(matrix));
137 }
138
139 matrix = new Matrix(matrix).inverse().toComponents();
140
141
142 int index = 0;
143 for (int row = 0; row < dim; ++row) {
144 for (int col = 0; col < dim; ++col) {
145 values[index] = matrix[row][col];
146 ++index;
147 }
148 }
149 }
150
151 @Override
152 public String getDimensionName(int index) {
153 int row = index % dim + 1;
154 int col = index / dim + 1; // column-major
155 return getParameterName() + row + col;
156 }
157
158 @Override
159 public int getDimension() {
160 return dim * dim;
161 }
162
163 @Override
164 public double getParameterValue(int index) {
165 computeValues();
166 return values[index];
167 }
168
169 @Override
170 public double[][] getParameterAsMatrix() {
171 computeValues();
172 double[][] matrix = new double[dim][dim];
173 for (int i = 0; i < dim; ++i) {
174 System.arraycopy(values, i * dim, matrix[i], 0, dim);
175 }
176
177 if (DEBUG) {
178 System.err.println("vec:");
179 System.err.println(new Vector(values));
180 System.err.println(new Matrix(matrix));
181 System.err.println("");
182 }
183
184 return matrix;
185 }
186
187 @Override
188 public double getParameterValue(int row, int col) {
189 computeValues();
190 return values[col * dim + row]; // column-major
191 }
192
193 @Override
194 public Parameter getParameter(int column) {
195 return null;
196 }
197
198 @Override
199 public double[] getParameterValues() {
200 computeValues();
201 double[] matrix = new double[values.length];
202 System.arraycopy(values, 0, matrix, 0, values.length);
203 return matrix;
204 }
205
206 @Override
207 protected void storeValues() {
208 L.storeParameterValues();
209 gamma.storeParameterValues();
210
211 if (storedValues == null) {
212 storedValues = new double[dim * dim];
213 }
214
215 System.arraycopy(values, 0, storedValues, 0, values.length);
216 storedValuesKnow = valuesKnown;
217 }
218
219 @Override
220 protected void restoreValues() {
221 L.restoreParameterValues();
222 gamma.restoreParameterValues();
223
224 double[] tmp = values;
225 values = storedValues;
226 storedValues = tmp;
227 valuesKnown = storedValuesKnow;
228 }
229
230 @Override
231 protected void acceptValues() {
232 L.acceptParameterValues();
233 gamma.acceptParameterValues();
234 }
235
236 @Override
237 protected void adoptValues(Parameter source) {
238 throw new RuntimeException("Not implemented");
239 }
240
241 @Override
242 public void setParameterValue(int dim, double value) {
243 throw new RuntimeException("Not implemented");
244 }
245
246 @Override
247 public void setParameterValueQuietly(int dim, double value) {
248 throw new RuntimeException("Not implemented");
249 }
250
251 @Override
252 public void setParameterValueNotifyChangedAll(int dim, double value) {
253 throw new RuntimeException("Not implemented");
254 }
255
256 @Override
257 public String getParameterName() {
258 return getId();
259 }
260
261 @Override
262 public void addBounds(Bounds<Double> bounds) {
263
264 }
265
266 @Override
267 public Bounds<Double> getBounds() {
268 return null;
269 }
270
271 @Override
272 public void addDimension(int index, double value) {
273 throw new RuntimeException("Not implemented");
274 }
275
276 @Override
277 public double removeDimension(int index) {
278 throw new RuntimeException("Not implemented");
279 }
280
281 @Override
282 public void setParameterValue(int row, int col, double value) {
283 throw new RuntimeException("Not implemented");
284 }
285
286 @Override
287 public void setParameterValueQuietly(int row, int col, double value) {
288 throw new RuntimeException("Not implemented");
289 }
290
291 @Override
292 public void setParameterValueNotifyChangedAll(int row, int col, double value) {
293 throw new RuntimeException("Not implemented");
294 }
295
296 @Override
297 public double[] getColumnValues(int col) {
298 throw new RuntimeException("Not yet implemented");
299 }
300
301 @Override
302 public int getColumnDimension() {
303 return dim;
304 }
305
306 @Override
307 public int getRowDimension() {
308 return dim;
309 }
310
311 @Override
312 public int getUniqueParameterCount() {
313 return 2;
314 }
315
316 @Override
317 public Parameter getUniqueParameter(int index) {
318 return (index == 0) ? L : gamma;
319 }
320
321 @Override
322 public void copyParameterValues(double[] destination, int offset) {
323 throw new RuntimeException("Not yet implemented");
324 }
325
326 @Override
327 public void setAllParameterValuesQuietly(double[] values, int offset) {
328 throw new RuntimeException("Not implemented");
329 }
330
331 @Override
332 public String toSymmetricString() {
333 return MatrixParameter.toSymmetricString(this);
334 }
335
336 @Override
337 public void variableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
338 valuesKnown = false;
339 fireParameterChangedEvent();
340 }
341
342 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
343
344 public static final String APPROXIMATE_PARAMETER = "approximateFactorAnalysisPrecision";
345 public static final String L_LABEL = "L";
346 public static final String GAMMA = "gamma";
347
348 public String getParserName() {
349 return APPROXIMATE_PARAMETER;
350 }
351
352 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
353
354 MatrixParameterInterface L = (MatrixParameterInterface) xo.getElementFirstChild(L_LABEL);
355 Parameter gamma = (Parameter) xo.getElementFirstChild(GAMMA);
356
357 String name = xo.hasId() ? xo.getId() : APPROXIMATE_PARAMETER;
358
359 return new ApproximateFactorAnalysisPrecisionMatrix(name, L, gamma);
360 }
361
362 //************************************************************************
363 // AbstractXMLObjectParser implementation
364 //************************************************************************
365
366 public String getParserDescription() {
367 return "A diffusion approximation to a factor analysis";
368 }
369
370 public XMLSyntaxRule[] getSyntaxRules() {
371 return rules;
372 }
373
374 private final XMLSyntaxRule[] rules = {
375 new ElementRule(L_LABEL, new XMLSyntaxRule[] {
376 new ElementRule(MatrixParameterInterface.class),
377 }),
378 new ElementRule(GAMMA, new XMLSyntaxRule[] {
379 new ElementRule(Parameter.class),
380 }),
381 };
382
383 public Class getReturnType() {
384 return ApproximateFactorAnalysisPrecisionMatrix.class;
385 }
386 };
387 }
388 // }
389 // DoubleMatrix2D LL = new DenseDoubleMatrix2D(matrix);
390 // RobustSingularValueDecomposition SVD = new RobustSingularValueDecomposition(LL);
391 // double[][] U = SVD.getU().toArray();
392 // double[][] V = SVD.getV().toArray();
393 // double[] EVals = SVD.getSingularValues();
394 //
395 // for (int i = 0; i <EVals.length ; i++) {
396 // if(EVals[i] != 0){
397 // EVals[i] = 1 / EVals[i];
398 // }
399 // }
400 // for (int i = 0; i < EVals.length; i++) {
401 // for (int j = 0; j < EVals.length; j++) {
402 // U[i][j] = U[i][j] * EVals[i];
403 // }
404 // }
405 //
406 // for (int i = 0; i <U.length; i++) {
407 // for (int j = 0; j <V.length ; j++) {
408 // matrix[i][j] = U[j][i] * V[i][j];
409 // }
410 //
411 // }
412 //
413 // for (int row = 0; row < dim; row++) {