| 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.CholeskyDecomposition; |
| 34 |
import dr.math.matrixAlgebra.IllegalDimension; |
| 35 |
import dr.math.matrixAlgebra.SymmetricMatrix; |
| 36 |
import dr.xml.*; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 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 |
|
| 77 |
|
| 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 |
|
| 86 |
|
| 87 |
|
| 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 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 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 |
|
| 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 |
|
| 130 |
} |
| 131 |
parameter.setParameterValueQuietly(i, x[i]); |
| 132 |
|
| 133 |
} |
| 134 |
parameter.fireParameterChangedEvent(); |
| 135 |
|
| 136 |
return 0; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 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 |
|
| 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 |
|
| 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 |
} |