# How to calculate variance - covariance matrix of a matrix?

For example, we have an NxP matrix with N rows and P variables. Then we need to calculate a PxP sample covariance matrix. How do I do that?

## 2 Answers

Seems you're looking for this Wikipedia page. Here's the relevant excerpt:

The sample covariance of N observations of K variables is the K-by-K matrix $\bar{\bar q}=[[q_{jk}]]$ with the entries
$q_{jk}=\frac{1}{N-1}\sum_{i=1}^{N}\left( x_{ij}-\bar{x}_j \right) \left( x_{ik}-\bar{x}_k \right)$,
which is an estimate of the covariance between variable $j$ and variable $k$.

All I can think to add is that the diagonal entries $q_{jj}$ are equivalent to the variables' variances $(s^2)$.

In addition to @Nick Stauner, I could say that if you use R you can do

cov(yourMatrix)