Top Description Inners Fields Constructors Methods
org.apache.commons.math3.distribution.fitting

public Class MultivariateNormalMixtureExpectationMaximization

extends Object
Class Inheritance
Imports
java.util.ArrayList, .Arrays, .List, org.apache.commons.math3.distribution.MultivariateNormalDistribution, .MixtureMultivariateNormalDistribution, org.apache.commons.math3.exception.ConvergenceException, .DimensionMismatchException, .NotStrictlyPositiveException, .NumberIsTooSmallException, .NumberIsTooLargeException, org.apache.commons.math3.exception.util.LocalizedFormats, org.apache.commons.math3.linear.Array2DRowRealMatrix, .RealMatrix, .SingularMatrixException, org.apache.commons.math3.stat.correlation.Covariance, org.apache.commons.math3.util.FastMath, .MathArrays, .Pair

Expectation-Maximization algorithm for fitting the parameters of multivariate normal mixture model distributions. This implementation is pure original code based on EM Demystified: An Expectation-Maximization Tutorial by Yihua Chen and Maya R. Gupta, Department of Electrical Engineering, University of Washington, Seattle, WA 98195. It was verified using external tools like CRAN Mixtools (see the JUnit test cases) but it is not based on Mixtools code at all. The discussion of the origin of this class can be seen in the comments of the MATH-817 JIRA issue.
Since
3.2

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
MultivariateNormalMixtureExpectationMaximization.DataRow

Class used for sorting user-supplied data.

Field Summary

Modifier and TypeField and Description
private final double[][]
data

The data to fit.

private static final int
DEFAULT_MAX_ITERATIONS

Default maximum number of iterations allowed per fitting process.

private static final double
DEFAULT_THRESHOLD

Default convergence threshold for fitting.

private MixtureMultivariateNormalDistribution
fittedModel

The model fit against the data.

private double
logLikelihood

The log likelihood of the data given the fitted model.

Constructor Summary

AccessConstructor and Description
public
MultivariateNormalMixtureExpectationMaximization(double[][]
Data to use in fitting procedure
data
)

Creates an object to fit a multivariate normal mixture model to data.

Method Summary

Modifier and TypeMethod and Description
public static MixtureMultivariateNormalDistribution

Returns:

Multivariate normal mixture model estimated from the data
estimate
(final double[][]
Data to estimate distribution
data
,
final int
Number of components for estimated mixture
numComponents
)

Helper method to create a multivariate normal mixture model which can be used to initialize fit(MixtureMultivariateNormalDistribution).

public void
fit(final MixtureMultivariateNormalDistribution
Model containing initial values of weights and multivariate normals
initialMixture
,
final int
Maximum iterations allowed for fit
maxIterations
,
final double
Convergence threshold computed as difference in logLikelihoods between successive iterations
threshold
)

Fit a mixture model to the data supplied to the constructor.

public void
fit(MixtureMultivariateNormalDistribution
Model containing initial values of weights and multivariate normals
initialMixture
)

Fit a mixture model to the data supplied to the constructor.

public MixtureMultivariateNormalDistribution

Returns:

fitted model or null if no fit has been performed yet.
getFittedModel
()

Gets the fitted model.

public double

Returns:

Log likelihood of data or zero of no data has been fit
getLogLikelihood
()

Gets the log likelihood of the data under the fitted model.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

databack to summary
private final double[][] data

The data to fit.

DEFAULT_MAX_ITERATIONSback to summary
private static final int DEFAULT_MAX_ITERATIONS

Default maximum number of iterations allowed per fitting process.

DEFAULT_THRESHOLDback to summary
private static final double DEFAULT_THRESHOLD

Default convergence threshold for fitting.

fittedModelback to summary
private MixtureMultivariateNormalDistribution fittedModel

The model fit against the data.

logLikelihoodback to summary
private double logLikelihood

The log likelihood of the data given the fitted model.

Constructor Detail

MultivariateNormalMixtureExpectationMaximizationback to summary
public MultivariateNormalMixtureExpectationMaximization(double[][] data) throws NotStrictlyPositiveException, DimensionMismatchException, NumberIsTooSmallException

Creates an object to fit a multivariate normal mixture model to data.

Parameters
data:double[][]

Data to use in fitting procedure

Exceptions
NotStrictlyPositiveException:
if data has no rows
DimensionMismatchException:
if rows of data have different numbers of columns
NumberIsTooSmallException:
if the number of columns in the data is less than 2

Method Detail

estimateback to summary
public static MixtureMultivariateNormalDistribution estimate(final double[][] data, final int numComponents) throws NotStrictlyPositiveException, DimensionMismatchException

Helper method to create a multivariate normal mixture model which can be used to initialize fit(MixtureMultivariateNormalDistribution). This method uses the data supplied to the constructor to try to determine a good mixture model at which to start the fit, but it is not guaranteed to supply a model which will find the optimal solution or even converge.

Parameters
data:double[][]

Data to estimate distribution

numComponents:int

Number of components for estimated mixture

Returns:MixtureMultivariateNormalDistribution

Multivariate normal mixture model estimated from the data

Exceptions
NotStrictlyPositiveException:
if data has less than 2 rows
DimensionMismatchException:
if rows of data have different numbers of columns
NumberIsTooLargeException:
if numComponents is greater than the number of data rows.
NumberIsTooSmallException:
if numComponents < 2.
fitback to summary
public void fit(final MixtureMultivariateNormalDistribution initialMixture, final int maxIterations, final double threshold) throws SingularMatrixException, NotStrictlyPositiveException, DimensionMismatchException

Fit a mixture model to the data supplied to the constructor. The quality of the fit depends on the concavity of the data provided to the constructor and the initial mixture provided to this function. If the data has many local optima, multiple runs of the fitting function with different initial mixtures may be required to find the optimal solution. If a SingularMatrixException is encountered, it is possible that another initialization would work.

Parameters
initialMixture:MixtureMultivariateNormalDistribution

Model containing initial values of weights and multivariate normals

maxIterations:int

Maximum iterations allowed for fit

threshold:double

Convergence threshold computed as difference in logLikelihoods between successive iterations

Exceptions
SingularMatrixException:
if any component's covariance matrix is singular during fitting
NotStrictlyPositiveException:
if numComponents is less than one or threshold is less than Double.MIN_VALUE
DimensionMismatchException:
if initialMixture mean vector and data number of columns are not equal
fitback to summary
public void fit(MixtureMultivariateNormalDistribution initialMixture) throws SingularMatrixException, NotStrictlyPositiveException

Fit a mixture model to the data supplied to the constructor. The quality of the fit depends on the concavity of the data provided to the constructor and the initial mixture provided to this function. If the data has many local optima, multiple runs of the fitting function with different initial mixtures may be required to find the optimal solution. If a SingularMatrixException is encountered, it is possible that another initialization would work.

Parameters
initialMixture:MixtureMultivariateNormalDistribution

Model containing initial values of weights and multivariate normals

Exceptions
SingularMatrixException:
if any component's covariance matrix is singular during fitting
NotStrictlyPositiveException:
if numComponents is less than one or threshold is less than Double.MIN_VALUE
getFittedModelback to summary
public MixtureMultivariateNormalDistribution getFittedModel()

Gets the fitted model.

Returns:MixtureMultivariateNormalDistribution

fitted model or null if no fit has been performed yet.

getLogLikelihoodback to summary
public double getLogLikelihood()

Gets the log likelihood of the data under the fitted model.

Returns:double

Log likelihood of data or zero of no data has been fit

org.apache.commons.math3.distribution.fitting back to summary

private Class MultivariateNormalMixtureExpectationMaximization.DataRow

extends Object
implements Comparable<MultivariateNormalMixtureExpectationMaximization.DataRow>
Class Inheritance
  • java.lang.Object
  • org.apache.commons.math3.distribution.fitting.MultivariateNormalMixtureExpectationMaximization.DataRow
All Implemented Interfaces
java.lang.Comparable

Class used for sorting user-supplied data.

Field Summary

Modifier and TypeField and Description
private Double
mean

Mean of the data row.

private final double[]
row

One data row.

Constructor Summary

AccessConstructor and Description
pack-priv
DataRow(final double[]
Data to use for the row
data
)

Create a data row.

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

int for sorting
compareTo
(final MultivariateNormalMixtureExpectationMaximization.DataRow
The other row
other
)

Implements java.lang.Comparable.compareTo.

Compare two data rows.

public boolean
equals(Object
the reference object with which to compare.
other
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public double[]

Returns:

data row array
getRow
()

Get a data row.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait

Field Detail

meanback to summary
private Double mean

Mean of the data row.

rowback to summary
private final double[] row

One data row.

Constructor Detail

DataRowback to summary
pack-priv DataRow(final double[] data)

Create a data row.

Parameters
data:double[]

Data to use for the row

Method Detail

compareToback to summary
public int compareTo(final MultivariateNormalMixtureExpectationMaximization.DataRow other)

Implements java.lang.Comparable.compareTo.

Compare two data rows.

Parameters
other:MultivariateNormalMixtureExpectationMaximization.DataRow

The other row

Returns:int

int for sorting

equalsback to summary
public boolean equals(Object other)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
other:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
getRowback to summary
public double[] getRow()

Get a data row.

Returns:double[]

data row array

hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override