# Multivariate gaussian formula

**URL:** <https://community.deeplearning.ai/t/multivariate-gaussian-formula/206781>\
**Category:** Unsupervised Learning, Recommenders, Reinforcement\
**Tags:** week-module-1\
**Created:** [September 19, 2022, 1:14pm UTC](https://community.deeplearning.ai/t/multivariate-gaussian-formula/206781 "2022-09-19T13:14:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![FrederikH](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/frederikh/32/20718_2.png) [@FrederikH](https://community.deeplearning.ai/u/FrederikH)\
**Post date:** [September 19, 2022, 1:14pm UTC](https://community.deeplearning.ai/t/multivariate-gaussian-formula/206781/1 "2022-09-19T13:14:19Z")

</div>

(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/sigma_1/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.

 ![Screenshot_20220919-083206__01](https://global.discourse-cdn.com/dlai/original/3X/9/f/9f0c815daab99d2ff16f57df50935f6accd2f052.jpeg)

---

<div class="post-metadata">

**Author:** ![rmwkwok](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/rmwkwok/32/126977_2.png) [@rmwkwok](https://community.deeplearning.ai/u/rmwkwok)\
**Post date:** [September 19, 2022, 3:53pm UTC](https://community.deeplearning.ai/t/multivariate-gaussian-formula/206781/2 "2022-09-19T15:53:11Z")

</div>

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.

Raymond

---

<div class="post-metadata">

**Author:** ![FrederikH](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/frederikh/32/20718_2.png) [@FrederikH](https://community.deeplearning.ai/u/FrederikH)\
**Post date:** [September 19, 2022, 4:03pm UTC](https://community.deeplearning.ai/t/multivariate-gaussian-formula/206781/3 "2022-09-19T16:03:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rmwkwok](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/rmwkwok/32/126977_2.png) [@rmwkwok](https://community.deeplearning.ai/u/rmwkwok)\
**Post date:** [September 19, 2022, 4:21pm UTC](https://community.deeplearning.ai/t/multivariate-gaussian-formula/206781/4 "2022-09-19T16:21:32Z")

</div>

Good luck @FrederikH 🙂
