{'fraction_non_alphanumeric': 0.047919957872564506, 'fraction_numerical': 0.011321748288572932, 'mean_word_length': 4.121348314606742, 'pattern_counts': {'":': 0, '<': 2, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Metric distances derived from cosine similarity and Pearson and Spearman correlations

We investigate two classes of transformations of cosine similarity and Pearson and Spearman correlations into metric distances, utilising the simple tool of metric-preserving functions. The first class puts anti-correlated objects maximally far apart. Previously known transforms fall within this class. The second class collates correlated and anti-correlated objects. An example of such a transformation that yields a metric distance is the sine function when applied to centered data.


Results
We derive metric distances from the sample Pearson coefficient, sample Spearman coefficient, and cosine similarity. Using A to denote any of these, it is already known that θ = arccos(A(x, y)) yields a metric distance, known as the angular distance. We further obtain the correlation distance sin( 1 2 θ), or equivalently 1 2 (1 − A(x, y)). Both distances place anti-correlated objects maximally far apart. A second class of metric distances is obtained that collate correlated and anticorrelated objects. Examples are the acute angular distance 1 2 π − | 1 2 π − θ| and the absolute correlation distance sin(θ), or equivalently 1 − A(x, y) 2 .

Background
The Pearson correlation coefficient, Spearman correlation coefficient and the cosine similarity are staples of data analysis. The Pearson and Spearman coefficients measure strength of association between two variables X and Y . The Pearson coefficient, commonly denoted by ρ, is defined as the covariance of the two variables divided by the product of their respective standard deviations.
The Spearman coefficient is obtained by applying the Pearson coefficient to ranktransformed data. Both are unaffected by linear transformations of the data. Given vectors x and y, respectively sampling X and Y and each of length n, the sample Pearson coefficient r x,y is obtained by estimating the population covariance and standard deviations from the samples, as defined in Equation (2). Here x and y denote the sample means. (2) The cosine similarity is a standard measure used in information retrieval. It is the cosine of the angle between two Euclidean vectors, and thus unaffected by scalar 1 arXiv:1208.3145v1 [stat.ME] 14 Aug 2012 transformations in the data. It is defined below in Equation (3) for vectors x and y. (3) These measures are related; Pearson is identical to the cosine applied to centered data (centered cosine), as evident from equations (2) and (3). For the purpose of this paper the terminology of vectors and samples is used interchangeably. We are not concerned with statistical properties of the Pearson coefficient under certain models, but solely interested in its properties as a function mapping Euclidean spaces to the interval [−1, 1]. We will henceforth refer to Pearson, Spearman, and cosine similarity as P , S, and C, and use A to indicate all of them are applicable.
Where dissimilarities are used, it is desirable that they satisfy the triangular inequality and are thus a metric distance. Informally, this means that detours take longer: the distance from a to c should always be shorter than the distance from a to b plus the distance from b to c. Metric distances abound in data analysis, formalizing a property that is intuitively expected and that allows stringent reasoning about data points. Several methods require this, such as for building M -trees [1] and accelerated algorithms that use the triangle inequality to skip computations by tracking bounds [2,3].

Metric distances
A metric distance takes as input two objects and outputs a real number. It requires four properties. These are i) all distances are nonnegative, ii) the distance of an object to itself is zero and distinct objects are never at distance zero, iii) the distance between two objects is the same in both directions, and iv) the distance satisfies the property that detours are longer, more commonly stated as the triangle inequality. More formally, given a distance d, it states that d(x, y) ≤ d(x, z)+d(z, y) for all objects x, y, and z. In this formulation, the distance between x and y is compared to the distance when using z as a detour.
In the analysis of distances derived from correlations and cosine similarity we will use a class of functions called metric preserving. A function f is metric preserving if the distance d f (x, y) = f (d(x, y)) is again metric for any metric d. More specifically, we shall make use of an important subclass of metric-preserving functions, namely those that are concave and increasing. A function f is called concave on an interval I if for all x and y in I and for t in [0, 1] the inequality holds. We refer to this as the chord condition. It is the formal way of stating that It essentially means that f is curving inward on I, as shown in the figure below.
The following lemma, relating concave functions to metric preserving functions is well-known (see e.g. [4]). We include a short detailed proof as it is an important prerequisite to this paper, consisting of several steps gathered here for ease of reference. It shows subadditivity to be the key property making certain concave functions also metric-preserving.
Lemma 1 For f to be metric preserving it is sufficient if f (0) = 0, and f (x) is both increasing and concave for x > 0.
Proof We first prove that functions that are concave for x > 0 and satisfy This follows by setting y = 0 in the chord condition (4) and using the postulate , noting that a a+tb and b a+tb both lie in [0, 1]. Using the scalar inequality just derived we bound the rewritten expression from above by f ( a a+b (a + b)) + f ( b a+b (a + b)), equaling f (a) + f (b). The proof of the lemma can now be concluded. We need to prove that d f is Finally, given that f is concave and f (0) = 0 we know that f is also subadditive and thus The following lemma yields a quick way to determine whether a function is concave.
Lemma 2 A function f that is twice differentiable on an interval I is concave The lemma can heuristically be understood as f (x) ≤ 0 implies that the rate of acceleration of f is slowing. Hence f curves inward, implying it is concave. The lemma is part of standard calculus, and for a formal proof we refer to [5]. If f is twice-differentiable, increasing, and satisfies f (x) ≤ 0 for x > 0 with f (0) = 0 it is thus metric-preserving, and we will use this later.

From correlations to distances
The first three properties of a metric distance are easily obtained when transforming one of the A measures to a dissimilarity by a natural transformation such as d(x, y) = 1 − A(x, y). However, the dissimilarity thus obtained does not guarantee the triangle inequality. We show below why this is the case using generic principles rather than explicit calculations, and why transformations such as d(x, y) : x, y → 1 − A(x, y) 2 and d(x, y) : x, y →  A(x, y)) do result in a metric distance.
Currently two metric distances are known to derive from the triple (P, S, C), namely the angle θ between vectors, and derived from it, 2 − 2 cos(θ), which may be obtained as 2 − 2 A(x, y). For the angle θ the triangle inequality derives from Proposition XI.20 of Euclid's The Elements and the fact that three vectors in a high-dimensional space can be embedded in three-dimensional space. It follows that arccos (A(x, y)) yields a metric distance, where A may be any of P , S, or C. It is known (e.g. [6]) that 2 − 2 cos(θ) is equal to the Euclidean distance between the two unit-scaled object vectors x and y. This follows from (using x = 1 and y = 1) It can additionally be observed using a trigonometric identity for sin( 1 2 θ) ( [7], page 72) that 2 − 2 cos(θ) is equal to √ 2 sin( 1 2 θ) in the interval [0, π] and is seen to be concave on that interval by considering its second derivative. Hence √ 2 sin( 1 2 θ) is a metric-preserving function for the angular distance (but not metric-preserving in general).
We formalise this finding and derive another class of metric distances derived from P , S, and C whose members collate correlated and anti-correlated objects. The canonical representative of this class is the sine function sin. In the lemma below we do not use generic metric-preserving functions, as stronger results can be obtained by utilising traits of the angular distance. However, the functions used share on certain intervals of interest the general traits of an important class of metric-preserving functions, namely being concave and increasing.
Lemma 3 i) A function f of the angular distance that satisfies f (0) = 0, is defined on [0, π], and is either a) increasing and concave on the interval [0, π], or b) increasing and concave on the interval [0, 1 2 π] and satisfies f (x) = f (π − x) (f is symmetric around 1 2 π), is a metric preserving distance for the angular distance. In case b) this requires disregarding the directionality of vectors and collating a vector and its sign-reversed counterpart into a single object.
ii) A function g of the angular distance that satisfies g(0) = 0 and is increasing and strictly convex on some interval [0, ], where is positive, yields a dissimilarity that violates the triangular inequality. An example of such a function is g : x → 1 − cos(x), or equivalently, 1 − A(x, y).
In scenario b) we may assume that α and β are both smaller than 1 2 π because of the following. By sign-reversing a we obtain vectors −a, b, c and angles α, π − β, π − γ. This transform leaves the values of f on the transformed angles invariant, and the triangular inequality can now be applied to α , β , γ = α, π − β, π − γ. Thus we may sign-reverse any of the three input vectors while preserving the inequality to be proven. By choosing which of a, b, or c to flip we can always make sure that both α and β are smaller than 1 2 π. The inequality f (γ) ≤ f (α) + f (β) is the same as f (γ ) ≤ f (α ) + f (β ), where α , β , γ are the angles corresponding with a triple of vectors (a , b , c ), allowing the use of the triangle inequality γ ≤ α + β . If one of γ or π − γ is smaller than either of α or β there is nothing to prove because f is increasing on [0, 1 2 π] and symmetric around 1 2 π. If γ is bigger than 1 2 π, we observe that α + β ≥ γ ≥ π − γ and we can choose to work with γ = π − γ rather than γ . If γ is smaller than 1 2 π, we simply set γ = γ . This leaves us to prove f (γ ) ≤ f (α ) + f (β ) where γ , α , and β are all smaller than 1 2 π, where γ is larger than both α and β , and where α + β ≥ γ . The same reasoning as under a) now applies, restricted to the interval [0, 1 2 π]. ii) Pick vectors a, b and c lying in the cartesian plane, such that the angles satisfy γ = α + β, γ < . Then g(γ) = g(α + β) > g(α) + g(β) (by super-additivity of strictly convex functions with f (0) ≤ 0).

Notes
For a distance d and a metric-preserving function f the distance d f is ordinally equivalent with d, that is, rankings of distances are preserved. The correlation distance d 2 is ordinally equivalent to the angular distance d 1 and the acute angular distance d 3 is equivalent to the absolute correlation distance d 4 .
Further distances can be obtained by composition of concave functions; for example f 5 : x → sin(x) p , where 0 < p ≤ 1, also yields a distance. Such distances are again ordinally equivalent to the absolute correlation distance and preserve rankings of distances.