Before I keep writing about the visualization of trajectory data, I want to show something cool about inner products that I came across quite by accident today.

Imagine you have two sets of points in , and , and you want to measure the distance between the two sets of points globally. This comes up all the time when doing clustering. One way of doing this is to consider the distance between the two point sets to be the minimum distance between any point in and any other point in : . This is fine, but it is not a metric: two point sets might be different and have distance zero. You can also look at the max-min distance: . This is not symmetric, but we can then use the symmetric version . This is fine, but it’s not what I want to write about today.

Yet another idea of computing the distance between clusters is to somehow take the average distance between points in and . We’ll look at the sum for now, but all of this applies to the average up to a constant factor, so . This seems like a reasonable way of computing the distance between clusters, but it takes quadratic time to compute. Can we fix this? Pretty much, and this is what we will do now.

Let’s start thinking about a slightly different distance function, namely, . It turns out that this sum can be evaluated exactly in linear time, and it’s very simple to show that.

First, recall that , and so . Then,

The first two terms of the right hand side can be evaluated in linear time, and what’s interesting about the last sum is that it turns out to be exactly zero. To see that, we first write and in terms of cluster mean and their displacement: . The trick we will use is that , and, in particular, . With this in mind everything else is trivial:

The last three terms are zero, and so

With a little bit of rearranging, you can also write this as

This is kind of neat: even though the original sum had a quadratic number of terms, most of them cancel out nicely. However, we can go further. This function we have defined is not really a metric, because . We can, in fact, fix this, and it turns out that the natural fix to make it a metric will end up looking very familiar. Looking at that function above, it seems like what we want to do is to somehow get rid of the first couple of terms: they do not actually measure any distance from to at all.

We were trying to define the distance between two point sets by summing over the distance of the points. We know, however, how to compute distances from inner products. So let’s instead define an inner product between two point sets, and see what kind of distance function falls out of it. In particular, let’s just use an inner product in perfect analogy with the way we were trying to define distances: . Now, we look at what happens with :

We do the same trick of writing the elements in terms of their means and displacements:

Now the biggest difference between this expression and the one before it is that all of the inner products have terms in them, which all cancel out! So the whole thing, in the end, is:

So this fancy-looking inner product between clusters we were trying to define turns out to define a distance that is exactly the distance between the two cluster centroids! This was a surprise to me. In a sense, this is a long-winded justification for using the distance between cluster centroids as a distance measure between the entire clusters.

Wait a minute, you say! This means that two clusters will have zero distance even though they are not the same! Yes, this is because that inner product defined about defines, in fact, a semi-Hilbert space, instead of a Hilbert space. This is an inner product space where some nonzero vectors are allowed to have length zero. This happens, essentially, because if you squint hard enough, you can think of the inner product that we wrote as , where is a matrix entirely composed of ones. This means we can also write that inner product as , where and are vectors entirely composed of ones. Notice that the whole inner product becomes an inner product of the sums of the points, which is exactly where the centroids come in. If this matrix were full rank, then our point-set inner product would, in fact, be a true Hilbert space. More about that in future posts.