skip to content
← Scientific Spaces English

Similarity Metrics Based on the Rearrangement Inequality

Yesterday, I saw that @Ji_Ha_Kim shared an interesting paper, 《Beyond Cosine Similarity》, which proposes constructing new, looser similarity metrics based on the rearrangement inequality. Having read it, I found it quite interesting and decided to write a brief note about it.

Cosine Similarity #

For two vectors , a commonly used normalized similarity metric is cosine similarity:

Setting aside geometric meaning, from a purely algebraic perspective, cosine similarity is based on the Cauchy-Schwarz inequality

to construct a normalized inner product. When , it means that there exists such that , which indicates that cosine similarity is insensitive to norm length; it measures the directional similarity of the two vectors, or in other words, their degree of linear correlation. In particular, by the AM-GM inequality, we have , and substituting this into the Cauchy-Schwarz inequality to further relax it yields

The condition for the first equality to hold is , and the condition for the second equality to hold is ; from this, we can define a new normalized inner product:

In this case, we have , meaning that is a metric measuring the degree of complete consistency between two vectors.

Rearrangement Inequality #

For the inner product , the rearrangement inequality also holds:

Here, denote the new vectors obtained by sorting the components of in descending/ascending order (and similarly for ); simply put, "reverse-order inner product ≤ unordered inner product ≤ same-order inner product". The condition for the first equality to hold is that are completely oppositely ordered, that is, for any it always holds that ; the condition for the second equality to hold is that are completely ordered alike, that is, .

Clearly, reordering does not change the norm, that is, , so combining this further with the Cauchy-Schwarz inequality yields

This shows that for , the rearrangement inequality is tighter than the Cauchy-Schwarz inequality. The standard proof method for the rearrangement inequality is the local adjustment method; starting from the identity

, the first term on the right-hand side is the inner product of and , and the second term is the inner product after swapping or . If and (in the same order), then the left-hand side is strictly greater than 0, which means that when swapping changes them from the same order to opposite order, the inner product decreases. Thus, the maximum is achieved in the case of complete same order, and similarly the minimum is achieved under complete reverse order.

Rearrangement Similarity #

If it is known that , and both equality signs are achievable, then we can generally construct a similarity metric

Substituting into the rearrangement inequality, we obtain

Then means that are completely identically ordered, while means that are completely oppositely ordered. If cosine similarity describes the degree of linear correlation, then to some extent describes some non-linear correlation. remains invariant under translation and positive scaling, that is,

However, this is not true for general component-wise strictly monotonic transformations (such as component-wise cubing), which means preserves and utilizes numerical magnitudes rather than merely relying on order.

Connections and Differences #

It should be pointed out that although this post was inspired by 《Beyond Cosine Similarity》, the rearrangement similarity constructed here is slightly different from the original paper, where the construction is ( is also the notation from the original paper)

That is, selecting the bound for identical order or reversed order based on the sign of the inner product. Although such a definition can also restrict the result to within , the sign of the inner product can no longer be used to distinguish between identical and reversed order; I personally consider it less reasonable than the definition in this post.

For example, for , these two vectors are completely oppositely ordered, and we would expect a rearrangement similarity metric to at least give a negative result; but evidently gives a positive result, whereas can yield a negative result. The definition in this post directly normalizes using both bounds of the rearrangement inequality, allowing to anchor completely identical order and completely reversed order, respectively, which I believe is semantically more reasonable.

Of course, in practical applications, whether or is more reasonable depends on the specific scenario; the claim of being more reasonable here is strictly in terms of the meaning of the term "rearrangement similarity".

Rank Correlation Coefficient #

If we treat as individual two-dimensional data points , then cosine similarity is related to their linear correlation coefficient (Pearson correlation coefficient):

where are 's respective component means. Building on the linear correlation coefficient, we arrive at the rank correlation coefficient (Spearman's rank correlation coefficient):

are 's respective rank vectors; that is, if is 's -th smallest component, then . From the definition, the rank correlation coefficient completely ignores the magnitude of the data itself and cares only about the order of the data. Previously, when we evaluated semantic similarity, we made use of this metric. Interestingly, the rank correlation coefficient can also be viewed as a special case of :

Readers are invited to verify this identity on their own, so I will not elaborate further. As a result, can be regarded as a generalized rearrangement similarity that preserves the original numerical information.

In a Nutshell #

This article has introduced the idea of constructing similarity metrics based on two-sided inner product inequalities from the perspective of "similarity = normalized inner product", with a focus on the rearrangement similarity metric constructed using the rearrangement inequality.