/// <summary> /// Construct a new Gaussian object given its parameters. /// </summary> /// <param name="mean">Gaussian mean value.</param> /// <param name="covariance">Gaussian covariance matrix.</param> /// <param name="weight">Gaussian coefficient in the mixture.</param> public Gaussian(double[] mean, double[][] covariance, double weight) { Mean = mean; Covariance = covariance; CovarianceDeterminant = covariance.PseudoDeterminant(); CovarianceInverse = covariance.PseudoInverse(); Weight = (!double.IsNaN(weight)) ? weight : 0; Multiplier = Math.Pow(2 * Math.PI, -mean.Length / 2) / Math.Sqrt(CovarianceDeterminant); CanonicalVector = CovarianceInverse.Multiply(Mean); }
/// <summary> /// Obtain the squared Mahalanobis distance between the gaussian distribution and a point. /// </summary> /// <param name="point">Point in R^N space.</param> /// <returns>Squared distance between distribution and point.</returns> public double SquareMahalanobis(double[] point) { double[] diff = Mean.Subtract(point); return(diff.InnerProduct(CovarianceInverse.Multiply(diff))); }
/// <summary> /// Evaluate the logarithm of the gaussian component, although without the weight multiplication. /// </summary> /// <param name="x">Evaluation point.</param> /// <returns>Distribution density.</returns> public double LogEvaluate(double[] x) { double[] diff = x.Subtract(Mean); return(Math.Log(Multiplier) - 0.5 * diff.InnerProduct(CovarianceInverse.Multiply(diff))); }
/// <summary> /// Evaluate the gaussian component, although without the weight multiplication. /// </summary> /// <param name="x">Evaluation point.</param> /// <returns>Distribution density.</returns> public double Evaluate(double[] x) { double[] diff = x.Subtract(Mean); //return Multiplier * ApproximateNegExp(0.5 * diff.InnerProduct(CovarianceInverse.Multiply(diff))); return(Multiplier * Math.Exp(-0.5 * diff.InnerProduct(CovarianceInverse.Multiply(diff)))); }