Accord.NET (logo) MultivariateNormalDistribution Class Accord.NET Framework
Online Show table of contents (goes to the online documentation index).
Multivariate Normal (Gaussian) distribution.
Inheritance Hierarchy

Online System Object
  Accord.Statistics.Distributions DistributionBase
    Accord.Statistics.Distributions.Multivariate MultivariateContinuousDistribution
      Accord.Statistics.Distributions.Multivariate MultivariateNormalDistribution

Namespace: Accord.Statistics.Distributions.Multivariate
Assembly: Accord.Statistics (in Accord.Statistics.dll) Version: 2.10.0.0 (2.10.0.4632)
Syntax

[SerializableAttribute]
public class MultivariateNormalDistribution : MultivariateContinuousDistribution, 
	IFittableDistribution<double[], NormalOptions>, ISampleableDistribution<double[]>, 
	IDistribution, ICloneable
Remarks

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 variance-covariance 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 r-dimensional subspace. Applying a rotation to an orthogonal basis to recover a non-degenerate r-dimensional distribution may help in this case.

References:

Examples

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 

// 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.9E-324, 4.9E-324 } (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 = 1e-10 // this value will be added to the diagonal until it becomes positive-definite
});
See Also