\HeaderA{metrics}{RMSE, Expected Log Likelihood and KL Divergence Between
Two Multivariate Normal Distributions}{metrics}
\aliasA{Ellik.norm}{metrics}{Ellik.norm}
\aliasA{kl.norm}{metrics}{kl.norm}
\aliasA{rmse.muS}{metrics}{rmse.muS}
\keyword{multivariate}{metrics}
\begin{Description}\relax
These functions calculate the root-mean-squared-error,
the expected log likelihood, and Kullback-Leibler (KL) divergence
(a.k.a. distance), between two multivariate normal (MVN)
distributions described by their mean vector and covariance matrix
\end{Description}
\begin{Usage}
\begin{verbatim}
rmse.muS(mu1, S1, mu2, S2)
Ellik.norm(mu1, S1, mu2, S2, quiet=FALSE)
kl.norm(mu1, S1, mu2, S2, quiet=FALSE, symm=FALSE)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{mu1}] mean vector of first (estimated) MVN 
\item[\code{S1}] covariance matrix of first (estimated) MVN 
\item[\code{mu2}] mean vector of second (true, baseline, or comparator) MVN 
\item[\code{S2}] covariance matrix of second (true, baseline, or comparator) MVN 
\item[\code{quiet}] when \code{FALSE} (default), gives a warning if
the \pkg{accuracy} package cannot be loaded to deal with (possibly)
non-positive definite \code{S1} and \code{S2}
\item[\code{symm}] when \code{TRUE} a symmetrized version of the
KL divergence is used; see the note below 
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
The root-mean-squared-error is calculated between the entries of
the mean vectors, and the upper-triangular part of the covariance
matrices (including the diagonal).

The KL divergence is given by the formula:
\deqn{D_{\mbox{\tiny KL}}(N_1 \| N_2) = \frac{1}{2}
\left( \log \left( \frac{|\Sigma_1|}{|\Sigma_2|} \right)
+ \mbox{tr} \left( \Sigma_1^{-1} \Sigma_2 \right) +
\left( \mu_1 - \mu_2\right)^\top \Sigma_1^{-1}
( \mu_1 - \mu_2 ) - N \right)
}{}

where \eqn{N}{} is \code{length(mu1)}, and must agree with
the dimensions of the other parameters.

The expected log likelihood can be formulated in terms of the
KL divergence.  That is, the expected log likelihood of data
simulated from the normal distribution with parameters \code{mu2}
and \code{S2} under the estimated normal with parameters
\code{mu1} and \code{S1} is given by

\deqn{ -\frac{1}{2} \ln \{(2\pi e)^N |\Sigma_2|\} -
D_{\mbox{\tiny KL}}(N_1 \| N_2).
}{}

The \code{\LinkA{sechol}{sechol}} function from the \pkg{accuracy}
package is used to decompose
(possibly) non-positive definite \code{S1} and/or \code{S2}
to give more stable and robust calculations in the face of
numerical instabilities that might occur for larger problems
\end{Details}
\begin{Value}
In the case of the expected log likelihood the result is
a real number.  The RMSE is a positive real number.
The KL divergence method returns a positive
real number depicting the \emph{distance} between the
two normal distributions
\end{Value}
\begin{Note}\relax
The KL-divergence is not symmetric.  Therefore

\code{kl.norm(mu1,S1,mu2,S2) != kl.norm(mu2,S2,mu1,S1).}

But a symmetric metric can be constructed from

\code{0.5 * (kl.norm(mu1,S1,mu2,S2) + kl.norm(mu2,S2,mu1,S1))}

or by using \code{symm = TRUE}
\end{Note}
\begin{Author}\relax
Robert B. Gramacy \email{bobby@statslab.cam.ac.uk}
\end{Author}
\begin{References}\relax
\url{http://www.statslab.cam.ac.uk/~bobby/monomvn.html}
\end{References}
\begin{SeeAlso}\relax
\code{\LinkA{posdef.approx}{posdef.approx}}
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
mu1 <- rnorm(5)
s1 <- matrix(rnorm(100), ncol=5)
S1 <- t(s1) %*% s1

mu2 <- rnorm(5)
s2 <- matrix(rnorm(100), ncol=5)
S2 <- t(s2) %*% s2

## RMSE
rmse.muS(mu1, S1, mu2, S2)

## expected log likelihood
Ellik.norm(mu1, S1, mu2, S2)

## KL is not symmetric
kl.norm(mu1, S1, mu2, S2)
kl.norm(mu2, S2, mu1, S1)

## symmetric version
kl.norm(mu2, S2, mu1, S1, symm=TRUE)
\end{ExampleCode}
\end{Examples}

