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.

  • $\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, 2013 at 10:00
  • 6
    $\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, 2013 at 10:13
  • 3
    $\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, 2017 at 22:35

4 Answers 4


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$.

  • 5
    $\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, 2013 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, 2018 at 15:43
  • $\begingroup$ y does not have the be non-zero. It can be any y $\endgroup$
    – user3180
    Jan 19 at 4:56

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.

  • 2
    $\begingroup$ PS: I am starting to think that this wasn't your question... $\endgroup$ Mar 22, 2013 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$ Mar 22, 2013 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, 2013 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$ Mar 23, 2013 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$ Mar 23, 2013 at 22:17

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.


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.

  • 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$ Jul 5, 2016 at 14:38
  • 4
    $\begingroup$ This answer could be improved by addressing the issue of positive definiteness $\endgroup$
    – Silverfish
    Jul 5, 2016 at 14:45
  • 1
    $\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, 2016 at 15:01

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.