# Covariance calculation is wrong?

**URL:** <https://community.deeplearning.ai/t/covariance-calculation-is-wrong/505368>\
**Category:** Unsupervised Learning, Recommenders, Reinforcement\
**Tags:** week-module-1\
**Created:** [November 27, 2023, 11:48pm UTC](https://community.deeplearning.ai/t/covariance-calculation-is-wrong/505368 "2023-11-27T23:48:16Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Captain\_Riggs](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@Captain\_Riggs](https://community.deeplearning.ai/u/Captain_Riggs)\
**Post date:** [November 27, 2023, 11:48pm UTC](https://community.deeplearning.ai/t/covariance-calculation-is-wrong/505368/1 "2023-11-27T23:48:16Z")

</div>

Hi all.

In week one the ‘def multivariate\_gaussian(X, mu, var)’ is used to calculate the probability values. One of the arguments is ‘var’ which in our case is in a 1-d vector format. So this code converts it to a shape of (n,n) so we can use it in the determinant calculation.

# Changing to a (n,n) matrix

if var.ndim == 1:  
var = np.diag(var)

# Determination code which is taken from the longer line of code

‘p.linalg.det(var)\*\*(-0.5)’

However ‘var’ in this (n,n) matrix is missing the cov(x,y) values which is the top right and bottom left of the var matrix.

So they have the default value of 0.

So the code ‘p.linalg.det(var)\*\*(-0.5)’ will not calculate the correct determinant value and hence all the probability values will be wrong?

Should it not be convariance=np.cov(var, rowvar=False) then p.linalg.det(convariance)\*\*(-0.5)’ ?

---

<div class="post-metadata">

**Author:** ![TMosh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/tmosh/32/9321_2.png) [@TMosh](https://community.deeplearning.ai/u/TMosh)\
**Post date:** [November 28, 2023, 1:55am UTC](https://community.deeplearning.ai/t/covariance-calculation-is-wrong/505368/2 "2023-11-28T01:55:24Z")

</div>

Are you referring to the Course 3 Week 1 “Anomaly Detection” notebook, in the multivariate\_gaussian() function in the utils.py file?

---

<div class="post-metadata">

**Author:** ![Captain\_Riggs](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@Captain\_Riggs](https://community.deeplearning.ai/u/Captain_Riggs)\
**Post date:** [November 28, 2023, 2:41am UTC](https://community.deeplearning.ai/t/covariance-calculation-is-wrong/505368/3 "2023-11-28T02:41:22Z")

</div>

Yep. That is the one.

Sorry should have been more clear about where that piece was code was from.

---

<div class="post-metadata">

**Author:** ![TMosh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/tmosh/32/9321_2.png) [@TMosh](https://community.deeplearning.ai/u/TMosh)\
**Post date:** [November 28, 2023, 5:37am UTC](https://community.deeplearning.ai/t/covariance-calculation-is-wrong/505368/4 "2023-11-28T05:37:53Z")

</div>

I’ll pass it along to the course staff. Statistics is not my strong suit.

---

<div class="post-metadata">

**Author:** ![Captain\_Riggs](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@Captain\_Riggs](https://community.deeplearning.ai/u/Captain_Riggs)\
**Post date:** [November 28, 2023, 5:51pm UTC](https://community.deeplearning.ai/t/covariance-calculation-is-wrong/505368/6 "2023-11-28T17:51:49Z")

</div>

Thanks.

Do you know how long it takes for them to reply?

---

<div class="post-metadata">

**Author:** ![TMosh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/tmosh/32/9321_2.png) [@TMosh](https://community.deeplearning.ai/u/TMosh)\
**Post date:** [November 28, 2023, 6:19pm UTC](https://community.deeplearning.ai/t/covariance-calculation-is-wrong/505368/7 "2023-11-28T18:19:59Z")

</div>

Sorry, I do not know.

---

<div class="post-metadata">

**Author:** ![Captain\_Riggs](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@Captain\_Riggs](https://community.deeplearning.ai/u/Captain_Riggs)\
**Post date:** [December 7, 2023, 12:23am UTC](https://community.deeplearning.ai/t/covariance-calculation-is-wrong/505368/8 "2023-12-07T00:23:12Z")

</div>

Any word back?

---

<div class="post-metadata">

**Author:** ![TMosh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/tmosh/32/9321_2.png) [@TMosh](https://community.deeplearning.ai/u/TMosh)\
**Post date:** [December 7, 2023, 12:25am UTC](https://community.deeplearning.ai/t/covariance-calculation-is-wrong/505368/9 "2023-12-07T00:25:35Z")

</div>

Not yet.

---

<div class="post-metadata">

**Author:** ![Captain\_Riggs](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@Captain\_Riggs](https://community.deeplearning.ai/u/Captain_Riggs)\
**Post date:** [December 7, 2023, 12:28am UTC](https://community.deeplearning.ai/t/covariance-calculation-is-wrong/505368/10 "2023-12-07T00:28:04Z")

</div>

Hmm. Thanks for the reply. How does one find out how long this process takes?

---

<div class="post-metadata">

**Author:** ![TMosh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/tmosh/32/9321_2.png) [@TMosh](https://community.deeplearning.ai/u/TMosh)\
**Post date:** [December 7, 2023, 12:30am UTC](https://community.deeplearning.ai/t/covariance-calculation-is-wrong/505368/11 "2023-12-07T00:30:08Z")

</div>

One cannot find that out. It is unpredictable.

I don’t work for DLAI (mentors are community volunteers), but I would guess that how quickly issues are addressed depends on their severity and the workload of the DLAI staff.

As your report is the only one in several years of this course being active, I’d guess it is not a high priority.

---

<div class="post-metadata">

**Author:** ![Captain\_Riggs](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@Captain\_Riggs](https://community.deeplearning.ai/u/Captain_Riggs)\
**Post date:** [December 7, 2023, 12:36am UTC](https://community.deeplearning.ai/t/covariance-calculation-is-wrong/505368/12 "2023-12-07T00:36:24Z")

</div>

Ok. Appreciate your side.

I want to speak to whoever governs you please.

Thanks

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [December 7, 2023, 12:39am UTC](https://community.deeplearning.ai/t/covariance-calculation-is-wrong/505368/13 "2023-12-07T00:39:39Z")

</div>

Not sure what you mean by that. As Tom says, the mentors are just fellow students. We are volunteers, we do not work for DLAI and we don’t get paid to do this. We did not create any of the course materials and we cannot change them.

Do you mean that you want to talk to someone on the Course Staff or who actually works for DeepLearning.AI?

---

<div class="post-metadata">

**Author:** ![Captain\_Riggs](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@Captain\_Riggs](https://community.deeplearning.ai/u/Captain_Riggs)\
**Post date:** [December 7, 2023, 1:14am UTC](https://community.deeplearning.ai/t/covariance-calculation-is-wrong/505368/14 "2023-12-07T01:14:34Z")

</div>

Hmm. Confused myself. Let me work through the process as maybe I am barking up the wrong tree.

I am doing the Coursea Machine Learning Specialization course.

I have run into a problem with the course notes and I want to resolve that issue.

Part of the pre-course material pointed me here for any issues with the course material. Is this were I go for help with relation to the ‘Coursea Machine Learning Specialization course.’?

Thanks

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [December 7, 2023, 1:15am UTC](https://community.deeplearning.ai/t/covariance-calculation-is-wrong/505368/15 "2023-12-07T01:15:45Z")

</div>

I am not a mentor for that course but Tom sent me the source code for that function. It’s a given function in the utility file for that assignment.

They explain the meaning of the arguments in the “docstring” of the function. The third argument `var` gives the covariance matrix, but it can be given in two different forms:

If it is given in vector form, then they create the covariance matrix by using `var` as the variance in each dimension of the distribution and creating a diagonal matrix with those values as the diagonal.

But the function will also accept `var` as an n x n matrix, in which case it simply uses it “as is”.

The determinant of the covariance matrix is used in the calculation which is explained [in the course materials for Prof Ng’s CS 229 course at Stanford.](https://cs229.stanford.edu/section/gaussians.pdf)

Notice that the covariance matrix also appears elsewhere in the computation, when they compute the [Moore-Penrose pseudoinverse](https://en.wikipedia.org/wiki/Moore%E2%80%93Penrose_inverse) of it.

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [December 7, 2023, 1:19am UTC](https://community.deeplearning.ai/t/covariance-calculation-is-wrong/505368/16 "2023-12-07T01:19:39Z")

</div>

> [@Captain\_Riggs](#):
>
> Part of the pre-course material pointed me here for any issues with the course material. Is this were I go for help with relation to the ‘Coursea Machine Learning Specialization course.’?

Yes, it is. But we are only the first line of defense. There is no guarantee that we can answer every possible question that can be formulated. We will try to call for backups in cases in which we can’t answer, but there is no guarantee that will happen in as timely a fashion as you might wish.

---

<div class="post-metadata">

**Author:** ![Captain\_Riggs](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@Captain\_Riggs](https://community.deeplearning.ai/u/Captain_Riggs)\
**Post date:** [December 7, 2023, 1:41am UTC](https://community.deeplearning.ai/t/covariance-calculation-is-wrong/505368/17 "2023-12-07T01:41:05Z")

</div>

Thanks for the reply. I think communication is important in any course.

The people who run this course expect me to pay my money for the right to do this course. Note that this is a two way process as I then expect to be taught appropriately.

Any issues that arise I would expect, **as I am paying** , that such issues get resolved in a timely manner.

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [December 7, 2023, 1:44am UTC](https://community.deeplearning.ai/t/covariance-calculation-is-wrong/505368/18 "2023-12-07T01:44:35Z")

</div>

Well as Tom and I have said, that is above our pay grade, which as we mentioned is zero.

But I think I answered your question above if you have had time to read it.

---

<div class="post-metadata">

**Author:** ![Captain\_Riggs](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@Captain\_Riggs](https://community.deeplearning.ai/u/Captain_Riggs)\
**Post date:** [December 7, 2023, 2:27am UTC](https://community.deeplearning.ai/t/covariance-calculation-is-wrong/505368/19 "2023-12-07T02:27:49Z")

</div>

Thanks. That was a big help in a weird way.

Those notes are way, way over my head BUT I managed to deduce that there are two covariance arguments. Diagonal and non-diagonal covariance. What you use depends on your data spread. The course notes used diagonal covariance whereas I used non-diagonal covariance. Something about whether is are relationship between the features or not. No relationships then you use diagonal covariance otherwise non-diagonal.

I will read more on this later tonight.

Thanks

---

<div class="post-metadata">

**Author:** ![Captain\_Riggs](https://avatars.discourse-cdn.com/v4/letter/c/e495f1/32.png) [@Captain\_Riggs](https://community.deeplearning.ai/u/Captain_Riggs)\
**Post date:** [December 7, 2023, 2:30am UTC](https://community.deeplearning.ai/t/covariance-calculation-is-wrong/505368/20 "2023-12-07T02:30:07Z")

</div>

I understand.

Funny really, but to me it is you people, who are unpaid, that keep this course going.

So thanks for the hard work.

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [December 7, 2023, 2:49am UTC](https://community.deeplearning.ai/t/covariance-calculation-is-wrong/505368/21 "2023-12-07T02:49:55Z")

</div>

You can see in the notebook that they use the `estimate_gaussian` function to compute the arguments that they later pass to `multivariate_gaussian`. And what they do is compute the “elementwise” variance for each feature across all the samples in the input dataset. So apparently that “diagonal” version of covariance, which doesn’t account for interactions between features, is good enough for the purposes here. So the whole distribution is really being computed independently “per feature”. At least that’s my interpretation of how they are doing things.

The simplistic point just looking at the implementation of that specific `multivariate_gaussian` function is that they are not _computing_ the covariance but just taking it as an input.

[Next page](https://community.deeplearning.ai/t/covariance-calculation-is-wrong/505368.md?page=2)
