| 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.math.distributions; |
| 27 |
|
| 28 |
import dr.inference.model.GradientProvider; |
| 29 |
import dr.inference.model.Likelihood; |
| 30 |
import dr.math.MathUtils; |
| 31 |
import dr.math.matrixAlgebra.*; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public class MultivariateNormalDistribution implements MultivariateDistribution, GaussianProcessRandomGenerator, |
| 37 |
GradientProvider { |
| 38 |
|
| 39 |
public static final String TYPE = "MultivariateNormal"; |
| 40 |
|
| 41 |
private final double[] mean; |
| 42 |
private final double[][] precision; |
| 43 |
private double[][] variance = null; |
| 44 |
private double[][] cholesky = null; |
| 45 |
private Double logDet = null; |
| 46 |
|
| 47 |
private final boolean hasSinglePrecision; |
| 48 |
private final double singlePrecision; |
| 49 |
|
| 50 |
public MultivariateNormalDistribution(double[] mean, double[][] precision) { |
| 51 |
this.mean = mean; |
| 52 |
this.precision = precision; |
| 53 |
this.hasSinglePrecision = false; |
| 54 |
this.singlePrecision = 1.0; |
| 55 |
} |
| 56 |
|
| 57 |
public MultivariateNormalDistribution(double[] mean, double singlePrecision) { |
| 58 |
this.mean = mean; |
| 59 |
this.hasSinglePrecision = true; |
| 60 |
this.singlePrecision = singlePrecision; |
| 61 |
|
| 62 |
final int dim = mean.length; |
| 63 |
this.precision = new double[dim][dim]; |
| 64 |
for (int i = 0; i < dim; ++i) { |
| 65 |
this.precision[i][i] = singlePrecision; |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
public String getType() { |
| 70 |
return TYPE; |
| 71 |
} |
| 72 |
|
| 73 |
public double[][] getVariance() { |
| 74 |
if (variance == null) { |
| 75 |
variance = new SymmetricMatrix(precision).inverse().toComponents(); |
| 76 |
} |
| 77 |
return variance; |
| 78 |
} |
| 79 |
|
| 80 |
public double[][] getCholeskyDecomposition() { |
| 81 |
if (cholesky == null) { |
| 82 |
cholesky = getCholeskyDecomposition(getVariance()); |
| 83 |
} |
| 84 |
return cholesky; |
| 85 |
} |
| 86 |
|
| 87 |
public double getLogDet() { |
| 88 |
if (logDet == null) { |
| 89 |
logDet = Math.log(calculatePrecisionMatrixDeterminate(precision)); |
| 90 |
} |
| 91 |
if (Double.isInfinite(logDet)) { |
| 92 |
if (isDiagonal(precision)) { |
| 93 |
logDet = logDetForDiagonal(precision); |
| 94 |
} |
| 95 |
} |
| 96 |
return logDet; |
| 97 |
} |
| 98 |
|
| 99 |
private boolean isDiagonal(double x[][]) { |
| 100 |
for (int i = 0; i < x.length; ++i) { |
| 101 |
for (int j = i + 1; j < x.length; ++j) { |
| 102 |
if (x[i][j] != 0.0) { |
| 103 |
return false; |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
return true; |
| 108 |
} |
| 109 |
|
| 110 |
private double logDetForDiagonal(double x[][]) { |
| 111 |
double logDet = 0; |
| 112 |
for (int i = 0; i < x.length; ++i) { |
| 113 |
logDet += Math.log(x[i][i]); |
| 114 |
} |
| 115 |
return logDet; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
public double[][] getScaleMatrix() { |
| 120 |
return precision; |
| 121 |
} |
| 122 |
|
| 123 |
public double[] getMean() { |
| 124 |
return mean; |
| 125 |
} |
| 126 |
|
| 127 |
public double[] nextMultivariateNormal() { |
| 128 |
return nextMultivariateNormalCholesky(mean, getCholeskyDecomposition(), 1.0); |
| 129 |
} |
| 130 |
|
| 131 |
public double[] nextMultivariateNormal(double[] x) { |
| 132 |
return nextMultivariateNormalCholesky(x, getCholeskyDecomposition(), 1.0); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
public double[] nextScaledMultivariateNormal(double[] mean, double scale) { |
| 137 |
return nextMultivariateNormalCholesky(mean, getCholeskyDecomposition(), Math.sqrt(scale)); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
public void nextScaledMultivariateNormal(double[] mean, double scale, double[] result) { |
| 142 |
nextMultivariateNormalCholesky(mean, getCholeskyDecomposition(), Math.sqrt(scale), result); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
public static double calculatePrecisionMatrixDeterminate(double[][] precision) { |
| 147 |
try { |
| 148 |
return new Matrix(precision).determinant(); |
| 149 |
} catch (IllegalDimension e) { |
| 150 |
throw new RuntimeException(e.getMessage()); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
public double logPdf(double[] x) { |
| 155 |
if (hasSinglePrecision) { |
| 156 |
return logPdf(x, mean, singlePrecision, 1.0); |
| 157 |
} else { |
| 158 |
return logPdf(x, mean, precision, getLogDet(), 1.0); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
public double[] gradLogPdf(double[] x) { |
| 163 |
if (hasSinglePrecision) { |
| 164 |
return gradLogPdf(x, mean, singlePrecision); |
| 165 |
} else { |
| 166 |
return gradLogPdf(x, mean, precision); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
public static double[] gradLogPdf(double[] x, double[] mean, double singlePrecision) { |
| 171 |
|
| 172 |
final int dim = x .length; |
| 173 |
final double[] gradient = new double[dim]; |
| 174 |
|
| 175 |
for (int i = 0; i < dim; ++i) { |
| 176 |
gradient[i] = singlePrecision * (mean[i] - x[i]); |
| 177 |
} |
| 178 |
|
| 179 |
return gradient; |
| 180 |
} |
| 181 |
|
| 182 |
public static double[] gradLogPdf(double[] x, double[] mean, double[][] precision) { |
| 183 |
final int dim = x.length; |
| 184 |
final double[] gradient = new double[dim]; |
| 185 |
final double[] delta = new double[dim]; |
| 186 |
|
| 187 |
for (int i = 0; i < dim; ++i) { |
| 188 |
delta[i] = mean[i] - x[i]; |
| 189 |
} |
| 190 |
|
| 191 |
for (int i = 0; i < dim; ++i) { |
| 192 |
double sum = 0; |
| 193 |
for (int j = 0; j <dim; ++j) { |
| 194 |
sum += precision[i][j] * delta[j]; |
| 195 |
} |
| 196 |
gradient[i] = sum; |
| 197 |
} |
| 198 |
|
| 199 |
return gradient; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public static double logPdf(double[] x, double[] mean, double[][] precision, |
| 206 |
double logDet, double scale) { |
| 207 |
|
| 208 |
if (logDet == Double.NEGATIVE_INFINITY) |
| 209 |
return logDet; |
| 210 |
|
| 211 |
final int dim = x.length; |
| 212 |
final double[] delta = new double[dim]; |
| 213 |
final double[] tmp = new double[dim]; |
| 214 |
|
| 215 |
for (int i = 0; i < dim; i++) { |
| 216 |
delta[i] = x[i] - mean[i]; |
| 217 |
} |
| 218 |
|
| 219 |
for (int i = 0; i < dim; i++) { |
| 220 |
for (int j = 0; j < dim; j++) { |
| 221 |
tmp[i] += delta[j] * precision[j][i]; |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
double SSE = 0; |
| 226 |
|
| 227 |
for (int i = 0; i < dim; i++) |
| 228 |
SSE += tmp[i] * delta[i]; |
| 229 |
|
| 230 |
return dim * logNormalize + 0.5 * (logDet - dim * Math.log(scale) - SSE / scale); |
| 231 |
|
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
public static double logPdf(double[] x, double[] mean, double precision, double scale) { |
| 236 |
|
| 237 |
final int dim = x.length; |
| 238 |
|
| 239 |
double SSE = 0; |
| 240 |
for (int i = 0; i < dim; i++) { |
| 241 |
double delta = x[i] - mean[i]; |
| 242 |
SSE += delta * delta; |
| 243 |
} |
| 244 |
|
| 245 |
return dim * logNormalize + 0.5 * (dim * (Math.log(precision) - Math.log(scale)) - SSE * precision / scale); |
| 246 |
} |
| 247 |
|
| 248 |
private static double[][] getInverse(double[][] x) { |
| 249 |
return new SymmetricMatrix(x).inverse().toComponents(); |
| 250 |
} |
| 251 |
|
| 252 |
private static double[][] getCholeskyDecomposition(double[][] variance) { |
| 253 |
double[][] cholesky; |
| 254 |
try { |
| 255 |
cholesky = (new CholeskyDecomposition(variance)).getL(); |
| 256 |
} catch (IllegalDimension illegalDimension) { |
| 257 |
throw new RuntimeException("Attempted Cholesky decomposition on non-square matrix"); |
| 258 |
} |
| 259 |
return cholesky; |
| 260 |
} |
| 261 |
|
| 262 |
public static double[] nextMultivariateNormalPrecision(double[] mean, double[][] precision) { |
| 263 |
return nextMultivariateNormalVariance(mean, getInverse(precision)); |
| 264 |
} |
| 265 |
|
| 266 |
public static double[] nextMultivariateNormalVariance(double[] mean, double[][] variance) { |
| 267 |
return nextMultivariateNormalVariance(mean, variance, 1.0); |
| 268 |
} |
| 269 |
|
| 270 |
public static double[] nextMultivariateNormalVariance(double[] mean, double[][] variance, double scale) { |
| 271 |
return nextMultivariateNormalCholesky(mean, getCholeskyDecomposition(variance), Math.sqrt(scale)); |
| 272 |
} |
| 273 |
|
| 274 |
public static double[] nextMultivariateNormalCholesky(double[] mean, double[][] cholesky) { |
| 275 |
return nextMultivariateNormalCholesky(mean, cholesky, 1.0); |
| 276 |
} |
| 277 |
|
| 278 |
public static double[] nextMultivariateNormalCholesky(double[] mean, double[][] cholesky, double sqrtScale) { |
| 279 |
|
| 280 |
double[] result = new double[mean.length]; |
| 281 |
nextMultivariateNormalCholesky(mean, cholesky, sqrtScale, result); |
| 282 |
return result; |
| 283 |
} |
| 284 |
|
| 285 |
public static void nextMultivariateNormalCholesky(double[] mean, double[][] cholesky, double sqrtScale, double[] result) { |
| 286 |
|
| 287 |
final int dim = mean.length; |
| 288 |
|
| 289 |
System.arraycopy(mean, 0, result, 0, dim); |
| 290 |
|
| 291 |
double[] epsilon = new double[dim]; |
| 292 |
for (int i = 0; i < dim; i++) |
| 293 |
epsilon[i] = MathUtils.nextGaussian() * sqrtScale; |
| 294 |
|
| 295 |
for (int i = 0; i < dim; i++) { |
| 296 |
for (int j = 0; j <= i; j++) { |
| 297 |
result[i] += cholesky[i][j] * epsilon[j]; |
| 298 |
|
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
public static void nextMultivariateNormalCholesky(final WrappedVector mean, final double[][] cholesky, |
| 304 |
final double sqrtScale, final WrappedVector result, |
| 305 |
final double[] epsilon) { |
| 306 |
|
| 307 |
final int dim = mean.getDim(); |
| 308 |
for (int i = 0; i < dim; i++) { |
| 309 |
epsilon[i] = MathUtils.nextGaussian() * sqrtScale; |
| 310 |
} |
| 311 |
|
| 312 |
for (int i = 0; i < dim; i++) { |
| 313 |
double x = mean.get(i); |
| 314 |
for (int j = 0; j <= i; j++) { |
| 315 |
x += cholesky[i][j] * epsilon[j]; |
| 316 |
|
| 317 |
} |
| 318 |
result.set(i, x); |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
public static void nextMultivariateNormalCholesky(final double[] mean, final int meanOffset, final double[][] cholesky, |
| 323 |
final double sqrtScale, final double[] result, final int resultOffset, |
| 324 |
final double[] epsilon) { |
| 325 |
|
| 326 |
final int dim = epsilon.length; |
| 327 |
|
| 328 |
System.arraycopy(mean, meanOffset, result, resultOffset, dim); |
| 329 |
|
| 330 |
for (int i = 0; i < dim; i++) |
| 331 |
epsilon[i] = MathUtils.nextGaussian() * sqrtScale; |
| 332 |
|
| 333 |
for (int i = 0; i < dim; i++) { |
| 334 |
for (int j = 0; j <= i; j++) { |
| 335 |
result[resultOffset + i] += cholesky[i][j] * epsilon[j]; |
| 336 |
|
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
public static void main(String[] args) { |
| 343 |
testPdf(); |
| 344 |
testRandomDraws(); |
| 345 |
} |
| 346 |
|
| 347 |
public static void testPdf() { |
| 348 |
double[] start = {1, 2}; |
| 349 |
double[] stop = {0, 0}; |
| 350 |
double[][] precision = {{2, 0.5}, {0.5, 1}}; |
| 351 |
double scale = 0.2; |
| 352 |
System.err.println("logPDF = " + logPdf(start, stop, precision, Math.log(calculatePrecisionMatrixDeterminate(precision)), scale)); |
| 353 |
System.err.println("Should = -19.94863\n"); |
| 354 |
|
| 355 |
System.err.println("logPDF = " + logPdf(start, stop, 2, 0.2)); |
| 356 |
System.err.println("Should = -24.53529\n"); |
| 357 |
} |
| 358 |
|
| 359 |
public static void testRandomDraws() { |
| 360 |
|
| 361 |
double[] start = {1, 2}; |
| 362 |
double[][] precision = {{2, 0.5}, {0.5, 1}}; |
| 363 |
int length = 100000; |
| 364 |
|
| 365 |
|
| 366 |
System.err.println("Random draws (via precision) ..."); |
| 367 |
double[] mean = new double[2]; |
| 368 |
double[] SS = new double[2]; |
| 369 |
double[] var = new double[2]; |
| 370 |
double ZZ = 0; |
| 371 |
for (int i = 0; i < length; i++) { |
| 372 |
double[] draw = nextMultivariateNormalPrecision(start, precision); |
| 373 |
for (int j = 0; j < 2; j++) { |
| 374 |
mean[j] += draw[j]; |
| 375 |
SS[j] += draw[j] * draw[j]; |
| 376 |
} |
| 377 |
ZZ += draw[0] * draw[1]; |
| 378 |
} |
| 379 |
|
| 380 |
for (int j = 0; j < 2; j++) { |
| 381 |
mean[j] /= length; |
| 382 |
SS[j] /= length; |
| 383 |
var[j] = SS[j] - mean[j] * mean[j]; |
| 384 |
} |
| 385 |
ZZ /= length; |
| 386 |
ZZ -= mean[0] * mean[1]; |
| 387 |
|
| 388 |
System.err.println("Mean: " + new Vector(mean)); |
| 389 |
System.err.println("TRUE: [ 1 2 ]\n"); |
| 390 |
System.err.println("MVar: " + new Vector(var)); |
| 391 |
System.err.println("TRUE: [ 0.571 1.14 ]\n"); |
| 392 |
System.err.println("Covv: " + ZZ); |
| 393 |
System.err.println("TRUE: -0.286"); |
| 394 |
} |
| 395 |
|
| 396 |
public static final double logNormalize = -0.5 * Math.log(2.0 * Math.PI); |
| 397 |
|
| 398 |
|
| 399 |
public Object nextRandom() { |
| 400 |
return nextMultivariateNormal(); |
| 401 |
} |
| 402 |
|
| 403 |
public double logPdf(Object x) { |
| 404 |
double[] v = (double[]) x; |
| 405 |
return logPdf(v); |
| 406 |
} |
| 407 |
|
| 408 |
@Override |
| 409 |
public Likelihood getLikelihood() { |
| 410 |
return null; |
| 411 |
} |
| 412 |
|
| 413 |
@Override |
| 414 |
public int getDimension() { return mean.length; } |
| 415 |
|
| 416 |
@Override |
| 417 |
public double[] getGradientLogDensity(Object x) { |
| 418 |
return gradLogPdf((double[]) x); |
| 419 |
} |
| 420 |
|
| 421 |
@Override |
| 422 |
public double[][] getPrecisionMatrix() { |
| 423 |
return precision; |
| 424 |
} |
| 425 |
} |