# User:Timothee Flutre/Notebook/Postdoc/2012/01/02

Project name Main project page
Previous entry      Next entry

## Learn about the multivariate Normal and matrix calculus

(Caution, this is my own quick-and-dirty tutorial, see the references at the end for presentations by professional statisticians.)

• Motivation: when we measure items, we often have to measure several properties for each item. For instance, we extract cells from each individual in our study, and we measure the expression level of all genes. We hence have, for each individual, a vector of measurements (one per gene), which leads us to the world of multivariate statistics.
• Data: we have N observations, noted X1,X2,...,XN, each being of dimension P. This means that each Xn is a vector belonging to $\mathbb{R}^P$. Traditionally, we record all the data into an $N \times P$ matrix named X, with samples (observations) in rows and variables (dimensions) in columns. Also, it is usual to assume vectors are in column by default. Therefore, we can write $X = (X_1^T, ..., X_N^T)$.
• Descriptive statistics: the sample mean is the P-dimensional vector $\bar{X} = (\bar{X}_1, ..., \bar{X}_P)$ with $\bar{X}_p = X_{\bullet p} = \frac{1}{N} \sum_{n=1}^N X_{np}$. The sample covariance is the $P \times P$ matrix noted S2 with $S^2_{pq} = \frac{1}{N} \sum_{n=1}^N (X_{np} - \bar{X}_p)(X_{nq} - \bar{X}_q)$.
• Model: we suppose that the Xn are independent and identically distributed according to a multivariate Normal distribution NP(μ,Σ). μ is the P-dimensional mean vector, and Σ the $P \times P$ covariance matrix. If Σ is positive definite (which we will assume), the density function for a given observation is: $f(X_n|\mu,\Sigma) = (2 \pi)^{-P/2} |\Sigma|^{-1/2} exp(-\frac{1}{2} (X_n-\mu)^T \Sigma^{-1} (X_n-\mu))$, with | M | denoting the determinant of a matrix M and MT its transpose.
• Likelihood: as usual, we will start by writing down the likelihood of the data, the parameters being θ = (μ,Σ):

L(θ) = f(X | θ)

As the observations are independent:

$L(\theta) = \prod_{i=1}^N f(x_i | \theta)$

It is easier to work with the log-likelihood:

$l(\theta) = ln(L(\theta)) = \sum_{i=1}^N ln( f(x_i | \theta) )$

$l(\theta) = -\frac{NP}{2} ln(2\pi) - \frac{N}{2}ln(|\Sigma|) - \frac{1}{2} \sum_{i=1}^N (x_i-\mu)^T \Sigma^{-1} (x_i-\mu)$

• ML estimation: as usual, to find the maximum-likelihood estimates of the parameters, we need to derive the log-likelihood with respect to each parameter, and then take the values of the parameters at which the log-likelihood is zero. However, in the case of multivariate distributions, this requires knowing a bit of matrix calculus, which is not always straightforward...
• Matrix calculus: some useful formulas

d(f(u)) = f'(u)du, eg. useful here: d(ln( | Σ | )) = | Σ | − 1d( | Σ | )

d( | U | ) = | U | tr(U − 1dU)

d(U − 1) = − U − 1(dU)U − 1

• Technical details: from Magnus and Neudecker (third edition, Part Six, Chapter 15, Section 3, p.353). First, they re-write the log-likelihood, noting that (xi − μ)TΣ − 1(xi − μ) is a scalar, ie. a 1x1 matrix, and is therefore equal to its trace:

$\sum_{i=1}^N (x_i-\mu)^T \Sigma^{-1} (x_i-\mu) = \sum_{i=1}^N tr( (x_i-\mu)^T \Sigma^{-1} (x_i-\mu) )$

As the trace is invariant under cyclic permutations (tr(ABC) = tr(BCA) = tr(CAB)):

$\sum_{i=1}^N (x_i-\mu)^T \Sigma^{-1} (x_i-\mu) = \sum_{i=1}^N tr( \Sigma^{-1} (x_i-\mu) (x_i-\mu)^T )$

The trace is also a linear map (tr(A + B) = tr(A) + tr(B)):

$\sum_{i=1}^N (x_i-\mu)^T \Sigma^{-1} (x_i-\mu) = tr( \sum_{i=1}^N \Sigma^{-1} (x_i-\mu) (x_i-\mu)^T )$

And finally:

$\sum_{i=1}^N (x_i-\mu)^T \Sigma^{-1} (x_i-\mu) = tr( \Sigma^{-1} \sum_{i=1}^N (x_i-\mu) (x_i-\mu)^T )$

As a result:

$l(\theta) = -\frac{NP}{2} ln(2\pi) - \frac{N}{2}ln(|\Sigma|) - \frac{1}{2} tr(\Sigma^{-1} Z)$ with $Z=\sum_{i=1}^N(x_i-\mu)(x_i-\mu)^T$

We can now write the first differential of the log-likelihood:

$d l(\theta) = - \frac{N}{2} d(ln(|\Sigma|)) - \frac{1}{2} d(tr(\Sigma^{-1} Z))$

$d l(\theta) = - \frac{N}{2} |\Sigma|^{-1} d(|\Sigma|) - \frac{1}{2} tr(d(\Sigma^{-1}Z))$

$d l(\theta) = - \frac{N}{2} tr(\Sigma^{-1} d\Sigma) - \frac{1}{2} tr(d(\Sigma^{-1})Z) - \frac{1}{2} tr(\Sigma^{-1} dZ)$

$d l(\theta) = - \frac{N}{2} tr(\Sigma^{-1} d\Sigma) + \frac{1}{2} tr(\Sigma^{-1} (d\Sigma) \Sigma^{-1} Z) + \frac{1}{2} tr(\Sigma^{-1} (\sum_{i=1}^N (x_i - \mu) (d\mu)^T + \sum_{i=1}^N (d\mu) (x_i - \mu)^T))$

At this step in the book, I don't understand how we go from the line above to the line below:

$d l(\theta) = \frac{1}{2} tr(d\Sigma)\Sigma^{-1} (Z - N\Sigma) \Sigma^{-1} + (d\mu)^T \Sigma^{-1} \sum_{i=1}^N (x_i - \mu)$

$d l(\theta) = \frac{1}{2} tr(d\Sigma)\Sigma^{-1} (Z - N\Sigma) \Sigma^{-1} + N (d\mu)^T \Sigma^{-1} (\bar{x} - \mu)$

The first-order conditions (ie. when dl(θ) = 0) are:

$\hat{\Sigma}^{-1} (Z - N\hat{\Sigma}) \hat{\Sigma}^{-1} = 0$ and $\hat{\Sigma}^{-1} (\bar{x} - \hat{\mu}) = 0$

From which follow:

$\hat{\mu} = \bar{x} = \frac{1}{N} \sum_{i=1}^N x_i$

and:

$\hat{\Sigma} = \bar{S}_N = \frac{1}{N} \sum_{i=1}^N (x_i - \bar{x})(x_i - \bar{x})^T$

Note that $\bar{S}_N$ is a biased estimate of Σ. It is usually better to use the unbiased estimate $\bar{S}_{N-1}$.

• Sufficient statistics:

From Z defined above and $\bar{S}_{N-1}$ defined similarly as $\bar{S}_N$ but with N − 1 in the denominator, we can write the following:

$Z = \sum_{i=1}^N (x_i-\mu)(x_i-\mu)^T$

$Z = \sum_{i=1}^N (x_i - \bar{x} + \bar{x} - \mu)(x_i - \bar{x} + \bar{x} - \mu)^T$

$Z = \sum_{i=1}^N (x_i - \bar{x})(x_i - \bar{x})^T + \sum_{i=1}^N (\bar{x} - \mu)(\bar{x} - \mu)^T + \sum_{i=1}^N (x_i - \bar{x})(\bar{x} - \mu)^T + \sum_{i=1}^N (\bar{x} - \mu)(x_i - \bar{x})^T$

$Z = (N-1) \bar{S}_{N-1} + N (\bar{x} - \mu)(\bar{x} - \mu)^T$

Thus, by employing the same trick with the trace as above, we now have:

$L(\mu, \Sigma) = (2 \pi)^{-NP/2} |\Sigma|^{-N/2} exp \left( -\frac{N}{2}(\bar{x} - \mu)^T\Sigma^{-1}(\bar{x} - \mu) -\frac{N-1}{2}tr(\Sigma^{-1}\bar{S}_{N-1}) \right)$

The likelihood depends on the samples only through the pair $(\bar{x}, \bar{S}_{N-1})$. Thanks to the Factorization theorem, we can say that this pair of values is a sufficient statistic for (μ,Σ).

We can also transform a bit more the formula of the likelihood in order to find the distribution of this sufficient statistic:

$L(\mu, \Sigma) = (2 \pi)^{-(N-1)P/2} (2 \pi)^{-P/2} |\Sigma|^{-1/2} exp \left( -\frac{1}{2}(\bar{x} - \mu)^T(\frac{1}{N}\Sigma)^{-1}(\bar{x} - \mu) \right) \times |\Sigma|^{-(N-1)/2} exp \left(-\frac{N-1}{2}tr(\Sigma^{-1}\bar{S}_{N-1}) \right)$

$L(\mu, \Sigma) \propto N_P(\bar{x}; \mu, \Sigma/N) \times W_P(\bar{S}_{N-1}; \Sigma, N-1)$

The likelihood is only proportional because the first constant is not used in any of the two distributions and a few constants are missing (eg. the Gamma function appearing in the density of the Wishart distribution). This doesn't matter as we usually want to maximize the likelihood or compute a likelihood ratio.

• References:
• Magnus and Neudecker, Matrix differential calculus with applications in statistics and econometrics (2007)
• Wand, Vector differential calculus in statistics (The American Statistician, 2002)