Is a sample covariance matrix always symmetric and positive definite?

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.

• 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. Commented Mar 23, 2013 at 10:00
• That would normally be called 'calculating the sample covariance matrix', or 'estimating the covariance matrix' rather than 'sampling the covariance matrix'. Commented Mar 23, 2013 at 10:13
• 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%.
– whuber
Commented Jul 11, 2017 at 22:35

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

• 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.
– whuber
Commented Mar 24, 2013 at 3:26
• 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. Commented Nov 6, 2018 at 15:43
• y does not have the be non-zero. It can be any y Commented Jan 19, 2022 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.

• PS: I am starting to think that this wasn't your question... Commented Mar 22, 2013 at 13:53
• But if you want to know whether your sampling algorithm guarantees it, you will have to state how you are sampling. Commented Mar 22, 2013 at 15:19
• 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.
– whuber
Commented Mar 23, 2013 at 16:21
• @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. Commented Mar 23, 2013 at 22:09
• @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. Commented Mar 23, 2013 at 22:17

@Zen's answer plus @whuber's comment to @Konstantin's answer provide a complete proof. Nevertheless, I'll rephrase the proof by trying to place more statistical emphasis.

Indeed, one can say that the sample covariance matrix $$S$$ is always positive and semi-definite because it can be seen as the variance of a suitable univariate variable, which is always non-negative.

In detail, let $$x_1,\ldots,x_n$$ be the observed sample, with $$x_i = (x_{i1},\ldots,x_{ik})^\top$$, $$i=1,\ldots,n$$. The sample covariance matrix is $$Q = n^{-1}\sum_{i=1}^n(x_i-\bar x)(x_i-\bar x)^\top,$$ where $$\bar x=n^{-1}\sum_{i}x_i$$ is the sample average.

Consider now any vector $$a = (a_1,\ldots,a_k)^\top$$ and take the $$y_i$$, linear combination of $$x_i$$ with coefficients $$a_i$$, i.e. $$y_i = a^\top x_i = a_1x_{11}+\cdots+a_{k}x_{ik},\quad\text{for all } i.$$

Let $$\bar y$$ be the sample average of $$y_i$$'s and note that $$\bar y = a^\top \bar x$$. The variance of $$y_i$$ is \begin{align*} 0\leq s_{y}^2 &= n^{-1}\sum_i(y_i-\bar y)^2 = n^{-1}\sum_{i}(y_i-\bar y)(y_i-\bar y)^\top\\ & = n^{-1}\sum_{i} (a^\top x_i - a^\top \bar x)(a^\top x_i - a^\top \bar x)\\ & = a^\top\left(n^{-1}\sum_{i} (x_i - a^\top \bar x)(x_i -\bar x)\right)a\\ & = a^\top S a. \end{align*}

Since $$a$$ was arbitrary, this completes the proof.

Let $$X= \begin{pmatrix} x_{11} & x_{12} & \cdots & x_{1k} \\ x_{21} & x_{22} & \cdots & x_{2k} \\ \vdots & \vdots & \ddots & \vdots\\ x_{n1} & x_{n2} & \cdots & x_{nk} \end{pmatrix}$$ denote the data matrix whose $$\left(i,j\right)$$-th entry is the $$i$$-th measurement of the $$j$$-th variable (with $$i \in \{1,\ldots, n\}, j \in \{1,\ldots,k \}$$).

The sample covariance matrix $$\mathcal S$$ can be written as $$\mathcal S=n^{-1}X^\top C_n X,$$ where $$C_n=I_n-n^{-1}\mathbb{1}_n\mathbb{1}_n^\top$$ is the centering matrix.
Since $$C_n$$ is symmetric and idempotent, we also have $$\mathcal S=n^{-1}X^\top C_n^\top C_n X$$.1 But with $$Y\mathrel{:=}C_n X$$ this becomes $$\mathcal S=n^{-1}Y^\top Y$$, which is generally positive semi-definite, and positive definite only if the columns of $$Y$$ are linearly independent.
This means that $$\mathcal S$$ is positive definite iff the centered measurement vectors of the $$k$$ variables, i.e. the vectors $$\left(x_{1j}-\bar{x}_{.j},\ldots,x_{nj}-\bar{x}_{.j}\right)^\top$$ indexed by $$j$$, are linearly independent.

1Another way to see that $$\mathcal S$$ can be written as $$n^{-1}X^\top C_n^\top C_n X$$ is to interpret $$X^\top C_n^\top C_n X = \left(C_n X\right)^\top \left(C_n X\right)$$ as sum of outer products of the rows of the column-wise centered $$X$$ with itself.

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.