Facemorph
Class Gaussian

java.lang.Object
  extended by Facemorph.BigMat
      extended by Facemorph.Gaussian
All Implemented Interfaces:
java.io.Serializable

public class Gaussian
extends BigMat

A Gaussian distribution class

See Also:
Serialized Form

Constructor Summary
Gaussian()
          Constructor for an empty Gaussian
Gaussian(double[] mean, BigMat covar, boolean invert)
          Construct a Gaussian using the data given
Gaussian(int dim)
          Constructs a Gaussian for the dimensions specified
 
Method Summary
 boolean build(java.util.Vector samples)
          Builds a Gaussian from a set of arrays
 boolean build(java.util.Vector samples, BigMat dist)
          Build a Gaussian from the supplied samples
 double build(java.util.Vector samples, java.util.Vector sampleWeights, int index)
          Builds the mean and covariance matrix from the samples given
 void display(java.lang.String label)
          DIsplay the eigenvector matrix, the eigenvalue array and the mean
 double[] eigenAnalysis(double[] sample)
          Analyse the sample into its principal components
 double[] eigenSynthesis(double[] sample)
          Synthesise a sample with the given principal component weights
 BigMat getCovar(boolean invert)
          Uses the eigenvalues and eigenvectors to reconstruct the covariance matrix or it inverse
 double[] getDiag()
          gets the variances of this Gaussian
 double[] getMean()
          Get the mean of this Gaussian
 double[] getRandomSample(java.util.Random rand)
          Gets a random vector sampled from this Gaussian distribution
static void main(java.lang.String[] args)
          Used for testing
 double probability(double[] sample)
          Calculates the probability of a sample vector
 boolean read(java.io.InputStream in)
          Read this Gaussian from the given InputStream
 boolean read(java.io.StreamTokenizer st)
          Read from the given StreamTokenizer
 void setSize(int dim)
          Sets the size of this Gaussian
 Gaussian slice(double[] sample, double[] weight)
          Takes a slice through the Gaussian, to produce a new Gaussian first "fixed" elements are fixed remaining elements of the Gaussian are used in the new Gaussian
 double[] slice1d(double[] start, double[] direction)
          Takes a 1D slice through the Gaussian along the line through start pointing in direction the output Gaussian is centred on start and the units are 1 equals the length of direction
 double squaredMeanDistance(double[] sample)
          Get the squared distance of the mean from the sample
 double T_squared(Gaussian g, double weight1, double weight2)
          Hotelling's T^2 statistic (the N-D t-test equivalent)
 boolean write(java.io.PrintStream out)
          Writes this to the given PrintStream
 
Methods inherited from class Facemorph.BigMat
add, add, copy, copy, dotProduct, equals, equals, get, getColumn, getColumnCopy, getHeight, getRowCopy, getWidth, invertSVD, jacobi, magnitude, multiply, multiply, multiply, multiplyOtherTranspose, multiplySelfTranspose, multiplyTranspose, multiplyTranspose, normaliseColumns, normaliseRows, put, qrdcmp, qrdcmp, qrsolv, read, read, readBinary, readBinary, reconstructQR, removeNaNRecords, rsolv, setColumn, setRow, setSize, SIGN, sort, subMat, subMat, subMat, subMatCols, subMatRows, subtract, sumSquares, svbksb, svbksb, svbksbt, svbksbt, svdcmp, svdcmpt, transposeCopy, upperTriangularBacksub, upperTriangularInverseMultiply, vectorise, write, writeBinary, writeBinary
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Gaussian

public Gaussian()
Constructor for an empty Gaussian


Gaussian

public Gaussian(int dim)
Constructs a Gaussian for the dimensions specified

Parameters:
dim - the required dimensionality of this Gaussian

Gaussian

public Gaussian(double[] mean,
                BigMat covar,
                boolean invert)
Construct a Gaussian using the data given

Parameters:
mean - The mean vector
covar - The covariance matrix
invert - specifies if the covariane matrix should be inverted
Method Detail

setSize

public void setSize(int dim)
             throws BigMatException
Sets the size of this Gaussian

Parameters:
dim - The number of dimensions
Throws:
BigMatException - thrown if problems setting the matrix size

build

public boolean build(java.util.Vector samples)
Builds a Gaussian from a set of arrays

Parameters:
samples - The vector of sample arrays
Returns:
true if built OK, false otherwise

build

public boolean build(java.util.Vector samples,
                     BigMat dist)
Build a Gaussian from the supplied samples

Parameters:
samples - the data samples
dist - The a priri distribution
Returns:
returns true if built OK, false otherwise

build

public double build(java.util.Vector samples,
                    java.util.Vector sampleWeights,
                    int index)
Builds the mean and covariance matrix from the samples given

Parameters:
samples - the Vector of double[] samples
sampleWeights - the Vector of double[] sample weights
index - the index in each sampleWeight array of this Gaussian
Returns:
returns the sum of weights

getCovar

public BigMat getCovar(boolean invert)
                throws BigMatException
Uses the eigenvalues and eigenvectors to reconstruct the covariance matrix or it inverse

Parameters:
invert - flag indicating if the inverse covariance matrix is required
Returns:
returns the covariance matrix or its inverse
Throws:
BigMatException

getMean

public double[] getMean()
Get the mean of this Gaussian

Returns:
the mean vector

getDiag

public double[] getDiag()
gets the variances of this Gaussian

Returns:
the variances

eigenAnalysis

public double[] eigenAnalysis(double[] sample)
Analyse the sample into its principal components

Parameters:
sample - the sample to analyse
Returns:
the sample weights

eigenSynthesis

public double[] eigenSynthesis(double[] sample)
Synthesise a sample with the given principal component weights

Parameters:
sample - the weights
Returns:
the reconstructed sample

squaredMeanDistance

public double squaredMeanDistance(double[] sample)
Get the squared distance of the mean from the sample

Parameters:
sample - the sample to test
Returns:
the squared distance from sample to the mean

probability

public double probability(double[] sample)
                   throws BigMatException
Calculates the probability of a sample vector

Parameters:
sample - The sample to test
Returns:
the probability of the sample in this Gaussian
Throws:
BigMatException

T_squared

public double T_squared(Gaussian g,
                        double weight1,
                        double weight2)
                 throws BigMatException
Hotelling's T^2 statistic (the N-D t-test equivalent)

Parameters:
g - The Gaussian to compare with
weight1 - the weight of this Gaussian
weight2 - the weight of g
Returns:
the T^2 statistic
Throws:
BigMatException

getRandomSample

public double[] getRandomSample(java.util.Random rand)
Gets a random vector sampled from this Gaussian distribution

Parameters:
rand - A random number generating object
Returns:
Returns a random sample from this distribution

slice

public Gaussian slice(double[] sample,
                      double[] weight)
               throws BigMatException
Takes a slice through the Gaussian, to produce a new Gaussian first "fixed" elements are fixed remaining elements of the Gaussian are used in the new Gaussian

Parameters:
sample - the fixed values of the first n dimensions
weight - an optional array of size 1 used to store the slice probability, can be null if not needed
Returns:
The Gaussian slice through this Gaussian
Throws:
BigMatException

slice1d

public double[] slice1d(double[] start,
                        double[] direction)
Takes a 1D slice through the Gaussian along the line through start pointing in direction the output Gaussian is centred on start and the units are 1 equals the length of direction

Parameters:
start - the starting point
direction - the direction the line is going in
Returns:
returns a the mean and standard deviation centred on start in the direction given

display

public void display(java.lang.String label)
DIsplay the eigenvector matrix, the eigenvalue array and the mean

Overrides:
display in class BigMat
Parameters:
label - The label to display with this Gaussian

write

public boolean write(java.io.PrintStream out)
Writes this to the given PrintStream

Overrides:
write in class BigMat
Parameters:
out - the PrintStream to write to
Returns:
return's true if written OK

read

public boolean read(java.io.InputStream in)
Read this Gaussian from the given InputStream

Parameters:
in - the InputStream to read from
Returns:
return true if read OK

read

public boolean read(java.io.StreamTokenizer st)
Read from the given StreamTokenizer

Overrides:
read in class BigMat
Parameters:
st - the StreamTokenizer to read from
Returns:
return true if all OK

main

public static void main(java.lang.String[] args)
                 throws BigMatException
Used for testing

Parameters:
args - the command line arguments
Throws:
BigMatException