@CodeReview(reviewer="Jonathan McClain", date="2006-05-15", changesNeeded=true, comments={"A few minor changes needed.","Comments indicated with a / / / comment in the first column."}, response=@CodeReviewResponse(respondent="Kevin R. Dixon",date="2006-05-15",moreChangesNeeded=false,comments="Fixed.")) @PublicationReference(author="Wikipedia", title="Multivariate normal distribution", type=WebPage, year=2009, url="http://en.wikipedia.org/wiki/Multivariate_normal_distribution") public class MultivariateGaussian extends AbstractDistribution<Vector> implements ClosedFormComputableDistribution<Vector>, EstimableDistribution<Vector,MultivariateGaussian>
Modifier and Type | Class and Description |
---|---|
static class |
MultivariateGaussian.IncrementalEstimator
The estimator that creates a MultivariateGaussian from a stream of
values.
|
static class |
MultivariateGaussian.IncrementalEstimatorCovarianceInverse
The estimator that creates a MultivariateGaussian from a stream of values
by estimating the mean and covariance inverse (as opposed to the
covariance directly), without ever performing a matrix inversion.
|
static class |
MultivariateGaussian.MaximumLikelihoodEstimator
Computes the Maximum Likelihood Estimate of the MultivariateGaussian
given a set of Vectors
|
static class |
MultivariateGaussian.PDF
PDF of a multivariate Gaussian
|
static class |
MultivariateGaussian.SufficientStatistic
Implements the sufficient statistics of the MultivariateGaussian.
|
static class |
MultivariateGaussian.SufficientStatisticCovarianceInverse
Implements the sufficient statistics of the MultivariateGaussian while
estimating the inverse of the covariance matrix.
|
static class |
MultivariateGaussian.WeightedMaximumLikelihoodEstimator
Computes the Weighted Maximum Likelihood Estimate of the
MultivariateGaussian given a weighted set of Vectors
|
Modifier and Type | Field and Description |
---|---|
static double |
DEFAULT_COVARIANCE_SYMMETRY_TOLERANCE
Tolerance check for symmetry covariance tolerance, 1.0E-5.
|
static int |
DEFAULT_DIMENSIONALITY
Default dimensionality of the Gaussian, 2
|
static double |
LOG_TWO_PI
Natural logarithm of 2pi.
|
Constructor and Description |
---|
MultivariateGaussian()
Default constructor.
|
MultivariateGaussian(int dimensionality)
Creates a new instance of MultivariateGaussian.
|
MultivariateGaussian(MultivariateGaussian other)
Creates a new instance of MultivariateGaussian.
|
MultivariateGaussian(Vector mean,
Matrix covariance)
Creates a new instance of MultivariateGaussian.
|
Modifier and Type | Method and Description |
---|---|
MultivariateGaussian |
clone()
This makes public the clone method on the
Object class and
removes the exception that it throws. |
double |
computeZSquared(Vector input)
Computes the z value squared, such that p(x) = coefficient *
exp{-0.5*z^2}
|
void |
convertFromVector(Vector parameters)
Converts the object from a Vector of parameters.
|
Vector |
convertToVector()
Converts the object to a vector.
|
MultivariateGaussian |
convolve(MultivariateGaussian other)
Convolves this Gaussian with the other Gaussian.
|
boolean |
equals(java.lang.Object randomVariable) |
Matrix |
getCovariance()
Gets the covariance matrix of the Gaussian.
|
Matrix |
getCovarianceInverse()
Gets the inverse of the covariance matrix.
|
MultivariateGaussian.MaximumLikelihoodEstimator |
getEstimator()
Gets an estimator associated with this distribution.
|
int |
getInputDimensionality()
Gets the dimensionality of the Gaussian.
|
double |
getLogCovarianceDeterminant()
Getter for logCovarianceDeterminant
|
double |
getLogLeadingCoefficient()
Getter for logLeadingCoefficient
|
Vector |
getMean()
Gets the arithmetic mean, or "first central moment" or "expectation",
of the distribution.
|
MultivariateGaussian.PDF |
getProbabilityFunction()
Gets the distribution function associated with this Distribution,
either the PDF or PMF.
|
int |
hashCode() |
MultivariateGaussian |
plus(MultivariateGaussian other)
Adds two MultivariateGaussian random variables together and returns the
resulting MultivariateGaussian
|
static Vector |
sample(Vector mean,
Matrix covarianceSquareRoot,
java.util.Random random)
Returns a single draw from the Gaussian with the given mean and
covariance.
|
static java.util.ArrayList<Vector> |
sample(Vector mean,
Matrix covarianceSquareRoot,
java.util.Random random,
int sampleCount)
Returns a collection of draws this Gaussian with the given mean and
covariance.
|
void |
sampleInto(java.util.Random random,
int sampleCount,
java.util.Collection<? super Vector> output)
Draws multiple random samples from the distribution and puts the result
into the given collection.
|
static void |
sampleInto(Vector mean,
Matrix covarianceSquareRoot,
java.util.Random random,
int sampleCount,
java.util.Collection<? super Vector> output)
Performs a collection of draws this Gaussian with the given mean and
covariance.
|
MultivariateGaussian |
scale(Matrix premultiplyMatrix)
Scales the MultivariateGaussian by premultiplying by the given Matrix
|
void |
setCovariance(Matrix covariance)
Sets the covariance matrix.
|
void |
setCovariance(Matrix covariance,
double symmetryTolerance)
Sets the covariance matrix.
|
void |
setCovarianceInverse(Matrix covarianceInverse)
Sets the covariance inverse
|
void |
setCovarianceInverse(Matrix covarianceInverse,
double symmetryTolerance)
Sets the covariance inverse
|
void |
setMean(Vector mean)
Sets the mean vector.
|
MultivariateGaussian |
times(MultivariateGaussian other)
Multiplies this Gaussian with the other Gaussian.
|
java.lang.String |
toString() |
sample, sample
finalize, getClass, notify, notifyAll, wait, wait, wait
sample, sample
public static final double DEFAULT_COVARIANCE_SYMMETRY_TOLERANCE
public static final int DEFAULT_DIMENSIONALITY
public static final double LOG_TWO_PI
public MultivariateGaussian()
public MultivariateGaussian(int dimensionality)
dimensionality
- Dimensionality of the Gaussian to create.public MultivariateGaussian(Vector mean, Matrix covariance)
mean
- The mean of the Gaussian distribution.covariance
- The covariance matrix, which should be a symmetric
matrix.public MultivariateGaussian(MultivariateGaussian other)
other
- The other MultivariateGaussian to copy.public MultivariateGaussian clone()
AbstractCloneableSerializable
Object
class and
removes the exception that it throws. Its default behavior is to
automatically create a clone of the exact type of object that the
clone is called on and to copy all primitives but to keep all references,
which means it is a shallow copy.
Extensions of this class may want to override this method (but call
super.clone()
to implement a "smart copy". That is, to target
the most common use case for creating a copy of the object. Because of
the default behavior being a shallow copy, extending classes only need
to handle fields that need to have a deeper copy (or those that need to
be reset). Some of the methods in ObjectUtil
may be helpful in
implementing a custom clone method.
Note: The contract of this method is that you must use
super.clone()
as the basis for your implementation.clone
in interface Vectorizable
clone
in interface CloneableSerializable
clone
in class AbstractCloneableSerializable
public MultivariateGaussian.PDF getProbabilityFunction()
ComputableDistribution
getProbabilityFunction
in interface ComputableDistribution<Vector>
public double computeZSquared(Vector input)
input
- input about which to compute the z-value squaredpublic MultivariateGaussian times(MultivariateGaussian other)
other
- Other Gaussian to multiply with this.public MultivariateGaussian convolve(MultivariateGaussian other)
other
- Other Gaussian to convolve with this.public int getInputDimensionality()
public Vector getMean()
DistributionWithMean
getMean
in interface DistributionWithMean<Vector>
public void setMean(Vector mean)
mean
- The new mean, throws NullPointerException if nullpublic Matrix getCovariance()
public void setCovariance(Matrix covariance)
covariance
- The new covariance matrix, the method forces the matrix
to be symmetric by averaging the off-diagonal components into a clone of
this parameter.public void setCovariance(Matrix covariance, double symmetryTolerance)
covariance
- The new covariance matrix, the method forces the matrix
to be symmetric by averaging the off-diagonal components into a clone of
this parameter.symmetryTolerance
- Tolerance for symmetry checkpublic Matrix getCovarianceInverse()
public void setCovarianceInverse(Matrix covarianceInverse)
covarianceInverse
- Inverse of the covariance matrixpublic void setCovarianceInverse(Matrix covarianceInverse, double symmetryTolerance)
covarianceInverse
- Inverse of the covariance matrixsymmetryTolerance
- Tolerance to enforce symmetrypublic double getLogCovarianceDeterminant()
public double getLogLeadingCoefficient()
public boolean equals(java.lang.Object randomVariable)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public void sampleInto(java.util.Random random, int sampleCount, java.util.Collection<? super Vector> output)
Distribution
sampleInto
in interface Distribution<Vector>
random
- Random number generator to use.sampleCount
- The number of samples to draw. Cannot be negative.output
- The collection to add the samples into.public static java.util.ArrayList<Vector> sample(Vector mean, Matrix covarianceSquareRoot, java.util.Random random, int sampleCount)
mean
- mean of the GaussiancovarianceSquareRoot
- square-root decomposition of the covariance
of the GaussiansampleCount
- number of times to draw from this random variablerandom
- Random-number generatorpublic static void sampleInto(Vector mean, Matrix covarianceSquareRoot, java.util.Random random, int sampleCount, java.util.Collection<? super Vector> output)
mean
- mean of the GaussiancovarianceSquareRoot
- square-root decomposition of the covariance
of the GaussiansampleCount
- number of times to draw from this random variablerandom
- Random-number generatoroutput
- The collection to put the samples into.public static Vector sample(Vector mean, Matrix covarianceSquareRoot, java.util.Random random)
mean
- mean of the GaussiancovarianceSquareRoot
- square-root decomposition of the covariance
of the Gaussianrandom
- Random-number generatorpublic MultivariateGaussian scale(Matrix premultiplyMatrix)
premultiplyMatrix
- Matrix against which to premultiply thispublic MultivariateGaussian plus(MultivariateGaussian other)
other
- MultivariateGaussian to add to this MultivariateGaussianpublic java.lang.String toString()
toString
in class java.lang.Object
public Vector convertToVector()
Vectorizable
convertToVector
in interface Vectorizable
public void convertFromVector(Vector parameters)
Vectorizable
convertFromVector
in interface Vectorizable
parameters
- The parameters to incorporate.public MultivariateGaussian.MaximumLikelihoodEstimator getEstimator()
EstimableDistribution
getEstimator
in interface EstimableDistribution<Vector,MultivariateGaussian>