Distances, Divergences, Dirichlet distributions
by benmoran
Probability distributions and distances
There’s an interesting correspondence between unimodal p.d.f.’s on a metric space (like the reals ), and distance functions. I will dig through this for amusement below, with some R code to generate the pictures.
Squared Euclidean distance and Gaussians
For example, in one dimension the normal p.d.f. is
so the log likelihood for a single and fixed
is proportional to the squared distance from the mean:
. Here’s the log likelihood for unit variance with
:
mu <- 0.5; par(mfrow=c(2,1)); curve(dnorm(x, mean=mu, sd=1, log=T),-4,5,xlab="x",ylab=expression(log(phi*("x")))); abline(v=mu); curve(dnorm(x, mean=mu, sd=1, log=F),-4,5,xlab="x",ylab=expression(phi*("x"))); abline(v=mu); text(mu,0,expression(mu=="0.5"));
This is basic stuff at the core of classical statistical techniques: least squares models are computationally straightforward to fit, but they also happen to imply Gaussian errors, which is a reasonable first approximation for a lot of real life data.
Of course we could look at it the other way. If I want to measure the Euclidean distance from a point to a point
, I can back this out from the log likelihood of
under a unit variance Gaussian centred at
.
Putting :
a <- 2; b <- 4; sqrt(-2*dnorm(b, mean=a, sd=1, log=T) - log(2*pi));
This is rather a roundabout way of measuring distances. Would you ever want to do this? Well, in the multivariate case, if the covariance matrix isn’t spherical you get a distance measure that depends on direction; something a bit like the Mahalanobis distance.
L-1 distance and Laplace distribution
For our next trick, we can try measure distances with other norms than , the good old squared Euclidean distance. For instance if we minimize the absolute error instead of the squared error (e.g. for Quantile Regression) we’re still measuring distances but now they’re in the achingly fashionable
norm. We’ve implicitly assumed the errors follow the Laplace or “double exponential” distribution so the log likelihood looks like this:
mu <- 0.5; b <- 1; par(mfrow=c(2,1)); curve(log(exp(-abs(x-mu)/b)/2*b),-4,5,xlab="x",ylab=expression(log(f(x)))); abline(v=mu); curve(exp(-abs(x-mu)/b)/2*b,-4,5,xlab="x",ylab=expression(f(x))); abline(v=mu); text(mu,0,expression(mu==0.5));
KL divergence and Dirichlet distributions
What if we go a bit further off-piste: let’s consider the Kullback-Leibler divergence between two arbitrary N-dimensional probability distributions and
:
This is a bit different than the examples above, where the log-probability depends on metric distances between points in , Firstly: the KL divergence isn’t a proper metric (it’s not symmetric and doesn’t obey the triangle inequality). However, it is does act as a distance in the sense that
with equality only when
, which is good enough for our purposes (it’s a “premetric”).
Secondly, we’re now confining ourselves to points that live on probability simplex , instead of anywhere in
. That means our distributions
have to satisfy
, and
.
A widely used distribution on the simplex is the Dirichlet distribution:
which is also called the beta distributionin the case when . Then we have a single degree of freedom
:
Now; is it too much to hope for that the log-likelihood for the beta distribution follows the same form as the Gaussian and Laplace distributions above, but with the KL divergence replacing the metric distance? (SPOILERS: yes, but not by much, and let’s see where…)
Concentration and mean parameters
Firstly, the other distributions had two parameters: a mean or location () and a scale or dispersion parameter (
or
). Here we seem to have just an unnormalized
-dimensional parameter vector of pseudo-counts
. But as David Mackay will tell you in ITILA, you can factor this into a positive real concentration parameter and a normalized mean vector which lives on the simplex:
.
Let’s write out the log-likelihood in this form, lumping all the messy gamma functions at the front into a normalizing factor .
Recall that the KL divergence from the mean to
is
where is the entropy of the mean distribution
. Then our log likelihood is:
which is looking promising, especially because the doesn’t depend on
so will just become part of the normalizing constant
. But what about the other term?
That’s a more awkward customer, but we can still look at it in terms of KL divergence; not from our mean but from the uniform distribution
with
.
If we substitute in
and pull out the constants we end up with this:
Curiouser and curiouser!
How does it behave?
Our Dirichlet log likelihood doesn’t depend solely on the “distance” from the mean like other two examples, but:
- It does depend on the KL divergence from the mean, and asymptotically solely on that as we make
.
- We can also see that it does so exactly when
, with an effective concentration of
.
So the contours of the Dirichlet distribution with uniform mean are balls of constant , which is pleasing. (Someone has made nice 3d prints of these and other related “Bregman balls” here.)
dirichlet.logZ <- function(c, m) { sum(lgamma(c*m)) - lgamma(sum(c*m)); } logddirichlet <- function(p, c, m) { (c*m-1) %*% log(p) - dirichlet.logZ(c,m); } m <- c(1,1,1)/3.; c <- 25.; x <- 1:99/100.; y <- 1:99/100.; z <- matrix(nrow=99,ncol=99) for (xx in 1:99) { for (yy in 1:99) { p <- c(x[xx],y[yy],1-x[xx]-y[yy]); z[yy,xx] <- logddirichlet(p, c, m); } } filled.contour(x,y,exp(z));
There’s also a repulsive dependence on the divergence from the uniform p.d.f. , which is easiest to see in the univariate (beta distribution) case.
This accounts for the skewness of the distributions when , and it also explains why the distribution becomes multimodal with peaks at the boundaries when
is small: then the “centrifugal” term pushing away from
dominates over the “centripetal” term which pulls us toward
.
par(mfrow=c(2,1)); curve(dbeta(x, 25, 5)); curve(dbeta(x, 0.8, 0.8));
Connections
I’ve got some more questions about this interpretation of the beta/Dirichlet distributions as depending on divergences, please comment if you know the answers!
Why does this unexpected “centrifugal” term appear in the Dirichlet? It’s to do with the pesky ‘s in the exponents… why are they there?
Sometimes people want to approximate beta distributions by Gaussians. John D.Cook points out that this works reasonably well when the parameters are “large and approximately equal” (exactly the circumstances under which the distribution depends more on from the mean). This paper of David MacKay and Andrew Gelman’s blog both suggest that if you need to approximate Dirichlet distributions with Gaussians, this is best done with a change of coordinates to the softmax basis.
It feels to me like there is already a similarity between and squared Euclidean distance, and so between the Dirichlet and the Gaussian. Maybe it can be made more precise with a better understanding of information geometry; and maybe this might help to come up with or reinterpret a generalization of the Dirichlet with a more general precision/concentration structure for
.
[…] Distances, Divergences, Dirichlet distributions « Ben Moran […]
[…] Much of the material for this note is canonical, although I’m less familiar with the focus on relating metrics to distributions. One thing that got me interested in the topic long ago was this blog post by Ben Moran. […]