Multivariate gaussian formula

(Note: this is not for the course it itself since i have already completed it, i just though this wpuld be the most fitting category)

Hello, so i am trying to implement the multivariate gaussian in java. But it seems like i got something wrong in the formula and i am not sure.

My implemention:
double value = (1/2*PI) * Math.abs(sigma) - 1/2 * Math.exp(-1/2 (x - mu) * 1/sigma1/2 * (x- mu)

x = 2d array
mu & sigma = 1d array

I am probably thinking its likely an error in the the converstion i have written in java. But i am actually not really sure what part i have written wrong.

I have seen a thread that gives the python wih numpy implemention. But that really doesnt make me understand what i have done wrong in the implemention of the raw formula, since i would like to understand math aspect more.

The formula that i have used.

Hi @FrederikH,

I don’t use Java but we can talk about the meaning of the symbol so you can find the right Java library/function yourself.

|\Sigma| is the determinant of the covariance matrix \Sigma, instead of absolute value of something.

\Sigma^{-1} is the inverse of the covariance matrix \Sigma. This is a matrix inverse operation instead of taking reciprocal of something.

Lastly, the 3 terms inside the exponential are matrix-multiplicated instead of simple multiplication.

When you search for the right Java function yourself, please be reminded that you are working with matrices here, so the algebra is matrix algebra. I suggest you to verify you have the right function by initializing the variables into some small arrays such as a 2x2 array for x. With these sample variables you can cross check the result from the function you find with the result you calculated using pencil and paper.


1 Like

Thank you, this was absolutely what i was looking for, i was pretty sure i had misunerstood some of the symbols myself. The implementation itself should be easy now.

Good luck @FrederikH :slight_smile: