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