MultivariateNormalDistribution Class 
Namespace: Accord.Statistics.Distributions.Multivariate
[SerializableAttribute] public class MultivariateNormalDistribution : MultivariateContinuousDistribution, IFittableDistribution<double[], NormalOptions>, IFittable<double[], NormalOptions>, IFittable<double[]>, IFittableDistribution<double[]>, IDistribution<double[]>, IDistribution, ICloneable, ISampleableDistribution<double[]>, IRandomNumberGenerator<double[]>
The MultivariateNormalDistribution type exposes the following members.
Name  Description  

MultivariateNormalDistribution(Int32) 
Constructs a multivariate Gaussian distribution
with zero mean vector and identity covariance matrix.
 
MultivariateNormalDistribution(Double, Double) 
Constructs a multivariate Gaussian distribution
with given mean vector and covariance matrix.
 
MultivariateNormalDistribution(Double, Double) 
Constructs a multivariate Gaussian distribution
with given mean vector and covariance matrix.

Name  Description  

Covariance 
Gets the variancecovariance matrix
Σ (sigma) for the Gaussian distribution.
(Overrides MultivariateContinuousDistributionCovariance.)  
Dimension 
Gets the number of variables for this distribution.
(Inherited from MultivariateContinuousDistribution.)  
Mean 
Gets the Mean vector μ (mu) for
the Gaussian distribution.
(Overrides MultivariateContinuousDistributionMean.)  
Median 
Gets the median for this distribution.
(Inherited from MultivariateContinuousDistribution.)  
Mode 
Gets the mode for this distribution.
(Inherited from MultivariateContinuousDistribution.)  
Variance 
Gets the Variance vector diag(Σ), the diagonal of
the sigma matrix, for the Gaussian distribution.
(Overrides MultivariateContinuousDistributionVariance.) 
Name  Description  

Bivariate 
Creates a new bivariate Normal distribution.
 
Clone 
Creates a new object that is a copy of the current instance.
(Overrides DistributionBaseClone.)  
ComplementaryDistributionFunction 
Gets the complementary cumulative distribution function
(ccdf) for this distribution evaluated at point x.
This function is also known as the Survival function.
(Overrides MultivariateContinuousDistributionComplementaryDistributionFunction(Double).)  
DistributionFunction 
Computes the cumulative distribution function for distributions
up to two dimensions. For more than two dimensions, this method
is not supported.
(Overrides MultivariateContinuousDistributionDistributionFunction(Double).)  
Equals  Determines whether the specified object is equal to the current object. (Inherited from Object.)  
Estimate(Double) 
Estimates a new Normal distribution from a given set of observations.
 
Estimate(Double, NormalOptions) 
Estimates a new Normal distribution from a given set of observations.
 
Estimate(Double, Double) 
Estimates a new Normal distribution from a given set of observations.
 
Estimate(Double, Double, NormalOptions) 
Estimates a new Normal distribution from a given set of observations.
 
Finalize  Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)  
Fit(Double) 
Fits the underlying distribution to a given set of observations.
(Inherited from MultivariateContinuousDistribution.)  
Fit(Double, IFittingOptions) 
Fits the underlying distribution to a given set of observations.
(Inherited from MultivariateContinuousDistribution.)  
Fit(Double, Double) 
Fits the underlying distribution to a given set of observations.
(Inherited from MultivariateContinuousDistribution.)  
Fit(Double, Int32) 
Fits the underlying distribution to a given set of observations.
(Inherited from MultivariateContinuousDistribution.)  
Fit(Double, Int32, IFittingOptions) 
Fits the underlying distribution to a given set of observations.
(Inherited from MultivariateContinuousDistribution.)  
Fit(Double, Double, IFittingOptions) 
Fits the underlying distribution to a given set of observations.
(Overrides MultivariateContinuousDistributionFit(Double, Double, IFittingOptions).)  
Fit(Double, Double, NormalOptions) 
Fits the underlying distribution to a given set of observations.
 
Generate 
Generates a random observation from the current distribution.
(Inherited from MultivariateContinuousDistribution.)  
Generate(Double) 
Generates a random observation from the current distribution.
(Inherited from MultivariateContinuousDistribution.)  
Generate(Int32) 
Generates a random vector of observations from the current distribution.
(Inherited from MultivariateContinuousDistribution.)  
Generate(Int32) 
Generates a random observation from the current distribution.
(Inherited from MultivariateContinuousDistribution.)  
Generate(Int32, Int32) 
Generates a random vector of observations from the current distribution.
(Inherited from MultivariateContinuousDistribution.)  
Generate(Int32, Double) 
Generates a random vector of observations from the current distribution.
(Overrides MultivariateContinuousDistributionGenerate(Int32, Double).)  
Generate(Int32, Double, Double) 
Generates a random vector of observations from a distribution with the given parameters.
 
GetHashCode  Serves as the default hash function. (Inherited from Object.)  
GetType  Gets the Type of the current instance. (Inherited from Object.)  
LogProbabilityDensityFunction 
Gets the logprobability density function (pdf)
for this distribution evaluated at point x.
(Overrides MultivariateContinuousDistributionLogProbabilityDensityFunction(Double).)  
Mahalanobis 
Gets the Mahalanobis distance between a sample and this distribution.
 
MemberwiseClone  Creates a shallow copy of the current Object. (Inherited from Object.)  
ProbabilityDensityFunction 
Gets the probability density function (pdf) for
this distribution evaluated at point x.
(Overrides MultivariateContinuousDistributionProbabilityDensityFunction(Double).)  
ToIndependentNormalDistribution 
Converts this multivariate
normal distribution into a joint distribution
of independentnormal distributions.
 
ToString 
Returns a String that represents this instance.
(Inherited from DistributionBase.)  
ToString(IFormatProvider) 
Returns a String that represents this instance.
(Inherited from DistributionBase.)  
ToString(String) 
Returns a String that represents this instance.
(Inherited from DistributionBase.)  
ToString(String, IFormatProvider) 
Returns a String that represents this instance.
(Overrides DistributionBaseToString(String, IFormatProvider).)  
Univariate 
Creates a new univariate Normal distribution.

Name  Description  

HasMethod 
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.)  
IsEqual  Compares two objects for equality, performing an elementwise comparison if the elements are vectors or matrices. (Defined by Matrix.)  
ToT  Overloaded.
Converts an object into another type, irrespective of whether
the conversion can be done at compile time or not. This can be
used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)  
ToT  Overloaded.
Converts an object into another type, irrespective of whether
the conversion can be done at compile time or not. This can be
used to convert generic types to numeric types during runtime.
(Defined by Matrix.) 
The Gaussian is the most widely used distribution for continuous variables. In the case of many variables, it is governed by two parameters, the mean vector and the variancecovariance matrix.
When a covariance matrix given to the class constructor is not positive definite, the distribution is degenerate and this may be an indication indication that it may be entirely contained in a rdimensional subspace. Applying a rotation to an orthogonal basis to recover a nondegenerate rdimensional distribution may help in this case.
References:
The following example shows how to create a Multivariate Gaussian distribution with known parameters mean vector and covariance matrix
// Create a multivariate Gaussian distribution var dist = new MultivariateNormalDistribution( // mean vector mu mean: new double[] { 4, 2 }, // covariance matrix sigma covariance: new double[,] { { 0.3, 0.1 }, { 0.1, 0.7 } } ); // Common measures double[] mean = dist.Mean; // { 4, 2 } double[] median = dist.Median; // { 4, 2 } double[] var = dist.Variance; // { 0.3, 0.7 } (diagonal from cov) double[,] cov = dist.Covariance; // { { 0.3, 0.1 }, { 0.1, 0.7 } } // Probability mass functions double pdf1 = dist.ProbabilityDensityFunction(new double[] { 2, 5 }); // 0.000000018917884164743237 double pdf2 = dist.ProbabilityDensityFunction(new double[] { 4, 2 }); // 0.35588127170858852 double pdf3 = dist.ProbabilityDensityFunction(new double[] { 3, 7 }); // 0.000000000036520107734505265 double lpdf = dist.LogProbabilityDensityFunction(new double[] { 3, 7 }); // 24.033158110192296 // Cumulative distribution function (for up to two dimensions) double cdf = dist.DistributionFunction(new double[] { 3, 5 }); // 0.033944035782101548 // Generate samples from this distribution: double[][] sample = dist.Generate(1000000);
The following example demonstrates how to fit a multivariate Gaussian to a set of observations. Since those observations would lead to numerical difficulties, the example also demonstrates how to specify a regularization constant to avoid getting a NonPositiveDefiniteMatrixException.
double[][] observations = { new double[] { 1, 2 }, new double[] { 1, 2 }, new double[] { 1, 2 }, new double[] { 1, 2 } }; // Create a multivariate Gaussian for 2 dimensions var normal = new MultivariateNormalDistribution(2); // Specify a regularization constant in the fitting options NormalOptions options = new NormalOptions() { Regularization = double.Epsilon }; // Fit the distribution to the data normal.Fit(observations, options); // Check distribution parameters double[] mean = normal.Mean; // { 1, 2 } double[] var = normal.Variance; // { 4.9E324, 4.9E324 } (almost machine zero)
The next example shows how to estimate a Gaussian distribution from data available inside a Microsoft Excel spreadsheet using the ExcelReader class.
// Create a new Excel reader to read data from a spreadsheet ExcelReader reader = new ExcelReader(@"test.xls", hasHeaders: false); // Extract the "Data" worksheet from the xls DataTable table = reader.GetWorksheet("Data"); // Convert the data table to a jagged matrix double[][] observations = table.ToArray(); // Estimate a new Multivariate Normal Distribution from the observations var dist = MultivariateNormalDistribution.Estimate(observations, new NormalOptions() { Regularization = 1e10 // this value will be added to the diagonal until it becomes positivedefinite });