问题
I admit I am being lazy here but is anyone aware of a free math .net library which gives me the output of a normal distribution given a mean vector and a covariance matrix? Thanks.
回答1:
Math.NET might work for you.
http://www.mathdotnet.com/
http://numerics.mathdotnet.com/probability-distributions/
Multivariate Distributions
Dirichlet Inverse Wishart Matrix Normal Multinomial NormalGamma Wishart
Function Reference:
http://api.mathdotnet.com/Numerics/MathNet.Numerics.Distributions/MatrixNormal.htm
EDIT : Note that you have to download from here
http://mathnetnumerics.codeplex.com/releases/view/56448
other links on mathdotnet.com are out of date.
回答2:
In the bivariate case, you can express the covariance matrix as a single parameter rho. Here is a method that implements this directly:
[Pure]
public static double GetBivariateGuassian(double muX, double sigmaX, double muY, double sigmaY, double x, double y, double rho = 0)
{
var sigmaXSquared = Math.Pow(sigmaX, 2);
var sigmaYSquared = Math.Pow(sigmaY, 2);
var dX = x - muX;
var dY = y - muY;
var exponent = -0.5;
var normaliser = 2 * Math.PI * sigmaX * sigmaY;
if (rho != 0)
{
normaliser *= Math.Sqrt(1 - Math.Pow(rho, 2));
exponent /= 1 - Math.Pow(rho, 2);
}
var sum = Math.Pow(dX, 2)/sigmaXSquared;
sum += Math.Pow(dY, 2)/sigmaYSquared;
sum -= 2*rho*dX*dY/(sigmaX*sigmaY);
exponent *= sum;
return Math.Exp(exponent) / normaliser;
}
See Bivariate Normal Distributions on Wikipedia for reference.
来源:https://stackoverflow.com/questions/9398331/multivariate-normal-distribution-given-mean-vector-and-covariance-matrix