Debian Codegraph @

src/dr/inference/operators/ModeIndependenceOperator.java

raw symbols SCIP
1 /*
2 * MultivariateNormalOperator.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.inference.operators;
27
28 import cern.colt.matrix.impl.DenseDoubleMatrix2D;
29 import cern.colt.matrix.linalg.SingularValueDecomposition;
30 import dr.inference.model.MatrixParameter;
31 import dr.inference.model.Parameter;
32 import dr.math.MathUtils;
33 import dr.math.matrixAlgebra.CholeskyDecomposition;
34 import dr.math.matrixAlgebra.IllegalDimension;
35 import dr.math.matrixAlgebra.SymmetricMatrix;
36 import dr.xml.*;
37
38
39 /**
40 * @author Marc Suchard
41 */
42 public class ModeIndependenceOperator extends AbstractAdaptableOperator {
43
44 public static final String MVN_OPERATOR = "modeIndependenceOperator";
45 public static final String SCALE_FACTOR = "scaleFactor";
46 public static final String VARIANCE_MATRIX = "varMatrix";
47 public static final String FORM_XTX = "formXtXInverse";
48
49 private double scaleFactor;
50 private final Parameter parameter;
51 private final int dim;
52
53 private double[][] cholesky;
54
55 public ModeIndependenceOperator(Parameter parameter, double scaleFactor, double[][] inMatrix, double weight,
56 AdaptationMode mode, boolean isVarianceMatrix) {
57
58 super(mode);
59 this.scaleFactor = scaleFactor;
60 this.parameter = parameter;
61 setWeight(weight);
62 dim = parameter.getDimension();
63
64 SingularValueDecomposition svd = new SingularValueDecomposition(new DenseDoubleMatrix2D(inMatrix));
65 if (inMatrix[0].length != svd.rank()) {
66 throw new RuntimeException("Variance matrix in mvnOperator is not of full rank");
67 }
68
69 final double[][] matrix;
70 if (isVarianceMatrix) {
71 matrix = inMatrix;
72 } else {
73 matrix = formXtXInverse(inMatrix);
74 }
75
76 // System.err.println("Matrix:");
77 // System.err.println(new Matrix(matrix));
78
79 try {
80 cholesky = (new CholeskyDecomposition(matrix)).getL();
81 } catch (IllegalDimension illegalDimension) {
82 throw new RuntimeException("Unable to decompose matrix in mvnOperator");
83 }
84
85 // System.err.println("Cholesky:");
86 // System.err.println(new Matrix(cholesky));
87 // System.exit(-1);
88 }
89
90 public ModeIndependenceOperator(Parameter parameter, double scaleFactor,
91 MatrixParameter varMatrix, double weight, AdaptationMode mode, boolean isVariance) {
92 this(parameter, scaleFactor, varMatrix.getParameterAsMatrix(), weight, mode, isVariance);
93 }
94
95 private double[][] formXtXInverse(double[][] X) {
96 int N = X.length;
97 int P = X[0].length;
98
99 double[][] matrix = new double[P][P];
100 for (int i = 0; i < P; i++) {
101 for (int j = 0; j < P; j++) {
102 double total = 0.0;
103 for (int k = 0; k < N; k++) {
104 total += X[k][i] * X[k][j];
105 }
106 matrix[i][j] = total;
107 }
108 }
109
110 // System.err.println("XtX:");
111 // System.err.println(new Matrix(matrix));
112
113 // Take inverse
114 matrix = new SymmetricMatrix(matrix).inverse().toComponents();
115 return matrix;
116 }
117
118 public double doOperation() {
119
120 double[] x = parameter.getParameterValues();
121 double[] epsilon = new double[dim];
122 //double[] y = new double[dim];
123 for (int i = 0; i < dim; i++)
124 epsilon[i] = scaleFactor * MathUtils.nextGaussian();
125
126 for (int i = 0; i < dim; i++) {
127 for (int j = i; j < dim; j++) {
128 x[i] += cholesky[j][i] * epsilon[j];
129 // caution: decomposition returns lower triangular
130 }
131 parameter.setParameterValueQuietly(i, x[i]);
132 // System.out.println(i+" : "+x[i]);
133 }
134 parameter.fireParameterChangedEvent();
135 // System.exit(-1);
136 return 0;
137 }
138
139 //MCMCOperator INTERFACE
140 public final String getOperatorName() {
141 return parameter.getParameterName();
142 }
143
144 public double getAdaptableParameter() {
145 return Math.log(scaleFactor);
146 }
147
148 public void setAdaptableParameter(double value) {
149 scaleFactor = Math.exp(value);
150 }
151
152 public double getRawParameter() {
153 return scaleFactor;
154 }
155
156 public double getScaleFactor() {
157 return scaleFactor;
158 }
159
160 public String getAdaptableParameterName() {
161 return "scaleFactor";
162 }
163
164 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
165
166 public String getParserName() {
167 return MVN_OPERATOR;
168 }
169
170 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
171
172 AdaptationMode mode = AdaptationMode.parseMode(xo);
173
174 double weight = xo.getDoubleAttribute(WEIGHT);
175 double scaleFactor = xo.getDoubleAttribute(SCALE_FACTOR);
176
177 if (scaleFactor <= 0.0) {
178 throw new XMLParseException("scaleFactor must be greater than 0.0");
179 }
180
181 Parameter parameter = (Parameter) xo.getChild(Parameter.class);
182
183 boolean formXtXInverse = xo.getAttribute(FORM_XTX, false);
184
185 XMLObject cxo = xo.getChild(VARIANCE_MATRIX);
186 MatrixParameter varMatrix = (MatrixParameter) cxo.getChild(MatrixParameter.class);
187
188 // Make sure varMatrix is square and dim(varMatrix) = dim(parameter)
189
190 if (!formXtXInverse) {
191 if (varMatrix.getColumnDimension() != varMatrix.getRowDimension())
192 throw new XMLParseException("The variance matrix is not square");
193 }
194
195 if (varMatrix.getColumnDimension() != parameter.getDimension())
196 throw new XMLParseException("The parameter and variance matrix have differing dimensions");
197
198 return new ModeIndependenceOperator(parameter, scaleFactor, varMatrix, weight, mode, !formXtXInverse);
199 }
200
201 //************************************************************************
202 // AbstractXMLObjectParser implementation
203 //************************************************************************
204
205 public String getParserDescription() {
206 return "This element returns a multivariate normal random walk operator on a given parameter.";
207 }
208
209 public Class getReturnType() {
210 return MCMCOperator.class;
211 }
212
213 public XMLSyntaxRule[] getSyntaxRules() {
214 return rules;
215 }
216
217 private final XMLSyntaxRule[] rules = {
218 AttributeRule.newDoubleRule(SCALE_FACTOR),
219 AttributeRule.newDoubleRule(WEIGHT),
220 AttributeRule.newBooleanRule(AUTO_OPTIMIZE, true),
221 AttributeRule.newBooleanRule(FORM_XTX, true),
222 new ElementRule(Parameter.class),
223 new ElementRule(VARIANCE_MATRIX,
224 new XMLSyntaxRule[]{new ElementRule(MatrixParameter.class)})
225
226 };
227
228 };
229 }