32
$\begingroup$

When computing the covariance matrix of a sample, is one then guaranteed to get a symmetric and positive-definite matrix?

Currently my problem has a sample of 4600 observation vectors and 24 dimensions.

$\endgroup$
  • $\begingroup$ For sampling the covariance matrix I use the formula: $Q_n = \frac{1}{n} \sum\limits_{i=1}^n (x_i-\bar{x})(x_i-\bar{x})^\top $ where $n$ is the number of samples and $\bar{x}$ is the sample mean. $\endgroup$ – Morten Mar 23 '13 at 10:00
  • 4
    $\begingroup$ That would normally be called 'calculating the sample covariance matrix', or 'estimating the covariance matrix' rather than 'sampling the covariance matrix'. $\endgroup$ – Glen_b Mar 23 '13 at 10:13
  • 1
    $\begingroup$ A common situation in which the covariance matrix is not definite is when the 24 "dimensions" record the composition of a mixture that sums to 100%. $\endgroup$ – whuber Jul 11 '17 at 22:35
39
$\begingroup$

For a sample of vectors $x_i=(x_{i1},\dots,x_{ik})^\top$, with $i=1,\dots,n$, the sample mean vector is $$ \bar{x}=\frac{1}{n} \sum_{i=1}^n x_i \, , $$ and the sample covariance matrix is $$ Q = \frac{1}{n} \sum_{i=1}^n (x_i-\bar{x})(x_i-\bar{x})^\top \, . $$ For a nonzero vector $y\in\mathbb{R}^k$, we have $$ y^\top Qy = y^\top\left(\frac{1}{n} \sum_{i=1}^n (x_i-\bar{x})(x_i-\bar{x})^\top\right) y $$ $$ = \frac{1}{n} \sum_{i=1}^n y^\top (x_i-\bar{x})(x_i-\bar{x})^\top y $$ $$ = \frac{1}{n} \sum_{i=1}^n \left( (x_i-\bar{x})^\top y \right)^2 \geq 0 \, . \quad (*) $$ Therefore, $Q$ is always positive semi-definite.

The additional condition for $Q$ to be positive definite was given in whuber's comment bellow. It goes as follows.

Define $z_i=(x_i-\bar{x})$, for $i=1,\dots,n$. For any nonzero $y\in\mathbb{R}^k$, $(*)$ is zero if and only if $z_i^\top y=0$, for each $i=1,\dots,n$. Suppose the set $\{z_1,\dots,z_n\}$ spans $\mathbb{R}^k$. Then, there are real numbers $\alpha_1,\dots,\alpha_n$ such that $y=\alpha_1 z_1 +\dots+\alpha_n z_n$. But then we have $y^\top y=\alpha_1 z_1^\top y + \dots +\alpha_n z_n^\top y=0$, yielding that $y=0$, a contradiction. Hence, if the $z_i$'s span $\mathbb{R}^k$, then $Q$ is positive definite. This condition is equivalent to $\mathrm{rank} [z_1 \dots z_n] = k$.

$\endgroup$
  • 2
    $\begingroup$ I like this approach, but would advise some care: $Q$ is not necessarily positive definite. The (necessary and sufficient) conditions for it to be so are described in my comment to Konstantin's answer. $\endgroup$ – whuber Mar 24 '13 at 3:26
  • 1
    $\begingroup$ Since the rank of $[z_1, z_2, \cdots, z_n]$ is less or equal to $k$, the condition can be simplified to the rank is equal to k. $\endgroup$ – 喵喵是我的猫猫 Nov 6 '18 at 15:43
13
$\begingroup$

A correct covariance matrix is always symmetric and positive *semi*definite.

The covariance between two variables is defied as $\sigma(x,y) = E [(x-E(x))(y-E(y))]$.

This equation doesn't change if you switch the positions of $x$ and $y$. Hence the matrix has to be symmetric.

It also has to be positive *semi-*definite because:

You can always find a transformation of your variables in a way that the covariance-matrix becomes diagonal. On the diagonal, you find the variances of your transformed variables which are either zero or positive, it is easy to see that this makes the transformed matrix positive semidefinite. However, since the definition of definity is transformation-invariant, it follows that the covariance-matrix is positive semidefinite in any chosen coordinate system.

When you estimate your covariance matrix (that is, when you calculate your sample covariance) with the formula you stated above, it will obv. still be symmetric. It also has to be positive semidefinite (I think), because for each sample, the pdf that gives each sample point equal probability has the sample covariance as its covariance (somebody please verify this), so everything stated above still applies.

$\endgroup$
  • 1
    $\begingroup$ PS: I am starting to think that this wasn't your question... $\endgroup$ – Konstantin Schubert Mar 22 '13 at 13:53
  • $\begingroup$ But if you want to know whether your sampling algorithm guarantees it, you will have to state how you are sampling. $\endgroup$ – Konstantin Schubert Mar 22 '13 at 15:19
  • 1
    $\begingroup$ Morten, the symmetry is immediate from the formula. To show semi-definiteness, you need to establish that $uQ_nu'\ge 0$ for any vector $u$. But $Q_n$ is $1/n$ times a sum of $v_iv_i'$ (where $v_i=x_i-\bar{x})$, whence $n uQ_nu'$ is a sum of $u(v_iv_i')u'$ = $(uv_i)(uv_i)'$, which is the squared length of the vector $uv_i$. Because $n\gt 0$ and a sum of squares cannot ever be negative, $uQ_nu'\ge 0$, QED. This also shows that $uQ_nu'=0$ precisely for those vectors $u$ which are orthogonal to all the $v_i$ (i.e., $uv_i=0$ for all $i$). When the $v_i$ span, then $u=0$ and $Q_n$ is definite. $\endgroup$ – whuber Mar 23 '13 at 16:21
  • 1
    $\begingroup$ @Morten The transformation-invariance is pretty clear if you understand a matrix multiplication geometrically. Think of your vector as an arrow. The numbers that describe your vector change with the coordinate system, but the direction and length of your vector doesnt. Now, a multiplication with a matrix means that you change length and direction of that arrow, but again the effect is geometrically the same in each coordinate system. The same is with a scalar product: It is defined geometrically and Geometriy is transformation-invariant. So your equation has the same result in all systems. $\endgroup$ – Konstantin Schubert Mar 23 '13 at 22:09
  • 1
    $\begingroup$ @Morten When you think in coordinates, the argument goes like this: When $A$ is your transformation matrix then: $v'=Av$ with $v'$ as the transformed coordinate-vector, $M'=AMA^T$, so when you transform each element in the equation $v^T M v > 0$, you get $ v'^T M' v' = (Av)^T A M A^T A v > 0$, which equals $v^T A^T A M A^T A v > 0$, and, because A is orthogonal, $A^T A$ is the unit matrix and we again get $v^T M v > 0$, which means that the transformed and the untransformed equation have the same scalar as result, so their are either both or both not greater zero. $\endgroup$ – Konstantin Schubert Mar 23 '13 at 22:17
0
$\begingroup$

Variance-Covariance matrices are always symmetric, as it can be proven from the actual equation to calculate each term of said matrix.

Also, Variance-Covariance matrices are always square matrices of size n, where n is the number of variables in your experiment.

Eigenvectors of symmetric matrices are always orthogonal.

With PCA, you determine the eigenvalues of the matrix to see if you could reduce the number of variables used in your experiment.

$\endgroup$
  • 1
    $\begingroup$ Welcome Gen. Note that your username, identicon, & a link to your user page are automatically added to every post you make, so there is no need to sign your posts. $\endgroup$ – Antoine Vernet Jul 5 '16 at 14:38
  • 3
    $\begingroup$ This answer could be improved by addressing the issue of positive definiteness $\endgroup$ – Silverfish Jul 5 '16 at 14:45
  • $\begingroup$ This doesn't really answer the question: it's just a collection of unsupported assertions that might or might not be relevant. Could you reframe it in a way that shows how the question is answered and explains the reasoning? $\endgroup$ – whuber Jul 5 '16 at 15:01
0
$\begingroup$

I would add to the nice argument of Zen the following which explains why we often say that the covariance matrix is positive definite if $n-1\geq k$.

If $x_1,x_2,...,x_n$ are a random sample of a continuous probability distribution then $x_1,x_2,...,x_n$ are almost surely (in the probability theory sense) linearly independent. Now, $z_1,z_2,...,z_n$ are not linearly independent because $\sum_{i=1}^n z_i = 0$, but because of $x_1,x_2,...,x_n$ being a.s. linearly independent, $z_1,z_2,...,z_n$ a.s. span $\mathbb{R}^{n-1}$. If $n-1\geq k$, they also span $\mathbb{R}^k$.

To conclude, if $x_1,x_2,...,x_n$ are a random sample of a continuous probability distribution and $n-1\geq k$, the covariance matrix is positive definite.

$\endgroup$
0
$\begingroup$

For those with a non-mathematical background like me who don't quickly catch the abstract mathematical formulae, this is a worked out example excel for the most upvoted answer. The covariance matrix can be derived in other ways also.

enter image description here

enter image description here

$\endgroup$
  • $\begingroup$ Could you explain how this spreadsheet demonstrates positive-definiteness of the covariance matrix? $\endgroup$ – whuber Jun 26 at 15:12
  • $\begingroup$ It does not. I had a hard time visualizing the covariance matrix in its notational form itself. So i created this sheet for myself and thought it could help someone. $\endgroup$ – Parikshit Bhinde Jun 27 at 12:19
  • $\begingroup$ Please, then, edit it to include an answer to the question. $\endgroup$ – whuber Jun 27 at 12:38
  • $\begingroup$ Done :) Thanks for suggesting. $\endgroup$ – Parikshit Bhinde Jun 28 at 7:40
  • $\begingroup$ The question is "is one then guaranteed to get a symmetric and positive-definite matrix?" I am unable to perceive any element of your post that addresses this, because (1) it never identifies a covariance matrix; (2) it does not demonstrate positive-definiteness of anything. $\endgroup$ – whuber Jun 28 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.