/* * MultivariateNormalDistribution.java * * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard * * This file is part of BEAST. * See the NOTICE file distributed with this work for additional * information regarding copyright ownership and licensing. * * BEAST is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * BEAST is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with BEAST; if not, write to the * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, * Boston, MA 02110-1301 USA */ package dr.math.distributions; import dr.inference.model.GradientProvider; import dr.inference.model.Likelihood; import dr.math.MathUtils; import dr.math.matrixAlgebra.*; /** * @author Marc Suchard */ public class MultivariateNormalDistribution implements MultivariateDistribution, GaussianProcessRandomGenerator, GradientProvider { public static final String TYPE = "MultivariateNormal"; private final double[] mean; private final double[][] precision; private double[][] variance = null; private double[][] cholesky = null; private Double logDet = null; private final boolean hasSinglePrecision; private final double singlePrecision; public MultivariateNormalDistribution(double[] mean, double[][] precision) { this.mean = mean; this.precision = precision; this.hasSinglePrecision = false; this.singlePrecision = 1.0; } public MultivariateNormalDistribution(double[] mean, double singlePrecision) { this.mean = mean; this.hasSinglePrecision = true; this.singlePrecision = singlePrecision; final int dim = mean.length; this.precision = new double[dim][dim]; for (int i = 0; i < dim; ++i) { this.precision[i][i] = singlePrecision; } } public String getType() { return TYPE; } public double[][] getVariance() { if (variance == null) { variance = new SymmetricMatrix(precision).inverse().toComponents(); } return variance; } public double[][] getCholeskyDecomposition() { if (cholesky == null) { cholesky = getCholeskyDecomposition(getVariance()); } return cholesky; } public double getLogDet() { if (logDet == null) { logDet = Math.log(calculatePrecisionMatrixDeterminate(precision)); } if (Double.isInfinite(logDet)) { if (isDiagonal(precision)) { logDet = logDetForDiagonal(precision); } } return logDet; } private boolean isDiagonal(double x[][]) { for (int i = 0; i < x.length; ++i) { for (int j = i + 1; j < x.length; ++j) { if (x[i][j] != 0.0) { return false; } } } return true; } private double logDetForDiagonal(double x[][]) { double logDet = 0; for (int i = 0; i < x.length; ++i) { logDet += Math.log(x[i][i]); } return logDet; } public double[][] getScaleMatrix() { return precision; } public double[] getMean() { return mean; } public double[] nextMultivariateNormal() { return nextMultivariateNormalCholesky(mean, getCholeskyDecomposition(), 1.0); } public double[] nextMultivariateNormal(double[] x) { return nextMultivariateNormalCholesky(x, getCholeskyDecomposition(), 1.0); } // Scale lives in variance-space public double[] nextScaledMultivariateNormal(double[] mean, double scale) { return nextMultivariateNormalCholesky(mean, getCholeskyDecomposition(), Math.sqrt(scale)); } // Scale lives in variance-space public void nextScaledMultivariateNormal(double[] mean, double scale, double[] result) { nextMultivariateNormalCholesky(mean, getCholeskyDecomposition(), Math.sqrt(scale), result); } public static double calculatePrecisionMatrixDeterminate(double[][] precision) { try { return new Matrix(precision).determinant(); } catch (IllegalDimension e) { throw new RuntimeException(e.getMessage()); } } public double logPdf(double[] x) { if (hasSinglePrecision) { return logPdf(x, mean, singlePrecision, 1.0); } else { return logPdf(x, mean, precision, getLogDet(), 1.0); } } public double[] gradLogPdf(double[] x) { if (hasSinglePrecision) { return gradLogPdf(x, mean, singlePrecision); } else { return gradLogPdf(x, mean, precision); } } public static double[] gradLogPdf(double[] x, double[] mean, double singlePrecision) { final int dim = x .length; final double[] gradient = new double[dim]; for (int i = 0; i < dim; ++i) { gradient[i] = singlePrecision * (mean[i] - x[i]); } return gradient; } public static double[] gradLogPdf(double[] x, double[] mean, double[][] precision) { final int dim = x.length; final double[] gradient = new double[dim]; final double[] delta = new double[dim]; for (int i = 0; i < dim; ++i) { delta[i] = mean[i] - x[i]; } for (int i = 0; i < dim; ++i) { double sum = 0; for (int j = 0; j