Class MVNormal


  • public class MVNormal
    extends java.lang.Object
    Tools for working with multivariate normal distributions
    • Constructor Summary

      Constructors 
      Constructor Description
      MVNormal()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static double[] bandCholesky​(double[] input, int numRows)  
      static double[] bandMatrixRoot​(int dim, int bandwidth)
      For testing band cholesky factorization
      static double[] cholesky​(double[] input, int numRows)
      Simple Cholesky decomposition, with no checks on squareness, symmetricality, or positive definiteness.
      static java.lang.String diagonalToString​(double[] matrix, int dimension)  
      static java.lang.String doubleArrayToString​(double[] matrix, int dimension)
      Create a string representation of a square matrix in one-dimensional array format
      static double[] getScatterMatrix​(double[][] observationMatrix)  
      static double[] invertLowerTriangular​(double[] inputMatrix, int dimension)
      This method returns the (lower-triangular) inverse of a lower triangular matrix.
      static double[] invertSPD​(double[] inputMatrix, int dimension)  
      static double[] lowerTriangularCrossproduct​(double[] inputMatrix, int dimension)
      Returns L'L for lower triangular matrix L.
      static double[] lowerTriangularProduct​(double[] leftMatrix, double[] rightMatrix, int dimension)
      Returns (lower-triangular) X = AB for square lower-triangular matrices A and B
      static void main​(java.lang.String[] args)  
      static FeatureVector nextFeatureVector​(Alphabet alphabet, double[] mean, double[] precision, Randoms random)  
      static double[] nextMVNormal​(double[] mean, double[] precision, Randoms random)
      Sample a multivariate normal from a precision matrix (ie inverse covariance matrix)
      static double[][] nextMVNormal​(int n, double[] mean, double[] precision, Randoms random)  
      static double[] nextMVNormalPosterior​(double[] priorMean, double[] priorPrecisionDiagonal, double[] precision, double[] observedMean, int observations, Randoms random)  
      static double[] nextMVNormalWithCholesky​(double[] mean, double[] precisionLowerTriangular, Randoms random)  
      static double[] nextWishart​(double[] sqrtScaleMatrix, int dimension, int degreesOfFreedom, Randoms random)
      A Wishart random variate, based on R code by Bill Venables.
      static double[] nextWishartPosterior​(double[] scatterMatrix, int observations, double[] priorPrecisionDiagonal, int priorDF, int dimension, Randoms random)  
      static double[] nextZeroSumMVNormalWithCholesky​(double[] mean, double[] precisionLowerTriangular, Randoms random)
      Sample a vector x from N(m, (LL')-1, such that sum_i x_i = 0.
      static double[] solveWithBackSubstitution​(double[] b, double[] lowerTriangular)
      This method returns x such that L'x = b.
      static double[] solveWithForwardSubstitution​(double[] b, double[] lowerTriangular)
      This method returns x such that Lx = b where L is lower triangular
      static void testCholesky()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • MVNormal

        public MVNormal()
    • Method Detail

      • cholesky

        public static double[] cholesky​(double[] input,
                                        int numRows)
        Simple Cholesky decomposition, with no checks on squareness, symmetricality, or positive definiteness. This follows the implementation from JAMA fairly closely.

        Returns L such that LL' = A and L is lower-triangular.

      • bandCholesky

        public static double[] bandCholesky​(double[] input,
                                            int numRows)
      • bandMatrixRoot

        public static double[] bandMatrixRoot​(int dim,
                                              int bandwidth)
        For testing band cholesky factorization
      • nextMVNormal

        public static double[] nextMVNormal​(double[] mean,
                                            double[] precision,
                                            Randoms random)
        Sample a multivariate normal from a precision matrix (ie inverse covariance matrix)
      • nextMVNormalWithCholesky

        public static double[] nextMVNormalWithCholesky​(double[] mean,
                                                        double[] precisionLowerTriangular,
                                                        Randoms random)
      • nextZeroSumMVNormalWithCholesky

        public static double[] nextZeroSumMVNormalWithCholesky​(double[] mean,
                                                               double[] precisionLowerTriangular,
                                                               Randoms random)
        Sample a vector x from N(m, (LL')-1, such that sum_i x_i = 0.
      • nextMVNormal

        public static double[][] nextMVNormal​(int n,
                                              double[] mean,
                                              double[] precision,
                                              Randoms random)
      • nextFeatureVector

        public static FeatureVector nextFeatureVector​(Alphabet alphabet,
                                                      double[] mean,
                                                      double[] precision,
                                                      Randoms random)
      • nextMVNormalPosterior

        public static double[] nextMVNormalPosterior​(double[] priorMean,
                                                     double[] priorPrecisionDiagonal,
                                                     double[] precision,
                                                     double[] observedMean,
                                                     int observations,
                                                     Randoms random)
        Parameters:
        priorMean - A vector of mean values
        priorPrecisionDiagonal - A vector representing a diagonal prior precision matrix
        precision - A precision matrix
      • solveWithBackSubstitution

        public static double[] solveWithBackSubstitution​(double[] b,
                                                         double[] lowerTriangular)
        This method returns x such that L'x = b. Note the transpose: this method assumes that the input matrix is LOWER triangular, even though back substitution operates on UPPER triangular matrices.
      • solveWithForwardSubstitution

        public static double[] solveWithForwardSubstitution​(double[] b,
                                                            double[] lowerTriangular)
        This method returns x such that Lx = b where L is lower triangular
      • invertLowerTriangular

        public static double[] invertLowerTriangular​(double[] inputMatrix,
                                                     int dimension)
        This method returns the (lower-triangular) inverse of a lower triangular matrix.
      • lowerTriangularCrossproduct

        public static double[] lowerTriangularCrossproduct​(double[] inputMatrix,
                                                           int dimension)
        Returns L'L for lower triangular matrix L.
      • lowerTriangularProduct

        public static double[] lowerTriangularProduct​(double[] leftMatrix,
                                                      double[] rightMatrix,
                                                      int dimension)
        Returns (lower-triangular) X = AB for square lower-triangular matrices A and B
      • invertSPD

        public static double[] invertSPD​(double[] inputMatrix,
                                         int dimension)
      • nextWishart

        public static double[] nextWishart​(double[] sqrtScaleMatrix,
                                           int dimension,
                                           int degreesOfFreedom,
                                           Randoms random)
        A Wishart random variate, based on R code by Bill Venables.
        Parameters:
        sqrtScaleMatrix - The lower-triangular matrix square root of the scale matrix. To draw from the posterior of a precision (ie inverse covariance) matrix, this should be chol( S^{-1} ), where S is the scatter matrix X'X of columns of MV normal observations X.
        dimension - The size of the matrix
        degreesOfFreedom - The degree of freedom for the Wishart. Should be greater than dimension. For a posterior distribution, this is the number of observations + the df of the prior.
      • nextWishartPosterior

        public static double[] nextWishartPosterior​(double[] scatterMatrix,
                                                    int observations,
                                                    double[] priorPrecisionDiagonal,
                                                    int priorDF,
                                                    int dimension,
                                                    Randoms random)
      • doubleArrayToString

        public static java.lang.String doubleArrayToString​(double[] matrix,
                                                           int dimension)
        Create a string representation of a square matrix in one-dimensional array format
      • diagonalToString

        public static java.lang.String diagonalToString​(double[] matrix,
                                                        int dimension)
      • getScatterMatrix

        public static double[] getScatterMatrix​(double[][] observationMatrix)
      • testCholesky

        public static void testCholesky()
      • main

        public static void main​(java.lang.String[] args)