Package elki.distance

## Class SqrtCosineUnitlengthDistance

• All Implemented Interfaces:
Distance<NumberVector>, NumberVectorDistance<NumberVector>, PrimitiveDistance<NumberVector>, SpatialPrimitiveDistance<NumberVector>

public class SqrtCosineUnitlengthDistance
extends CosineUnitlengthDistance
Cosine distance function for unit length feature vectors using the square root.

The cosine distance is computed from the cosine similarity by sqrt(1-cosine similarity).

Cosine similarity is defined as $\tfrac{\vec{x}\cdot\vec{y}}{||a||\cdot||b||} =_{||a||=||b||=1} \vec{x}\cdot\vec{y}$ Cosine distance then is defined as $\sqrt{2 - 2 \tfrac{\vec{x}\cdot\vec{y}}{||a||\cdot||b||}} =_{||a||=||b||=1} \sqrt{2 - 2\vec{x}\cdot\vec{y}} \in [0;2]$

This implementation assumes that $$||a||=||b||=1$$. If this does not hold for your data, use SqrtCosineDistance instead!

Because of the square root, this is more expensive than regular cosine, but because this corresponds to Euclidean distance on normalized vectors, it is a metric on normalized vectors.

Since:
0.8.0
Author:
Erich Schubert
• ### Nested Class Summary

Nested Classes
Modifier and Type Class Description
static class  SqrtCosineUnitlengthDistance.Par
Parameterization class.
• ### Field Summary

Fields
Modifier and Type Field Description
static SqrtCosineUnitlengthDistance STATIC
Static instance
• ### Constructor Summary

Constructors
Constructor Description
SqrtCosineUnitlengthDistance()
Deprecated.
Use static instance
• ### Method Summary

All Methods
Modifier and Type Method Description
double distance​(NumberVector v1, NumberVector v2)
Computes the distance between two given DatabaseObjects according to this distance function.
boolean isMetric()
Is this distance function metric (satisfy the triangle inequality)
boolean isSquared()
Squared distances, that would become metric after square root.
double minDist​(SpatialComparable mbr1, SpatialComparable mbr2)
Computes the distance between the two given MBRs according to this distance function.
java.lang.String toString()
• ### Methods inherited from class elki.distance.CosineUnitlengthDistance

equals, getInputTypeRestriction, hashCode
• ### Methods inherited from class java.lang.Object

clone, finalize, getClass, notify, notifyAll, wait, wait, wait
• ### Methods inherited from interface elki.distance.Distance

isSymmetric
• ### Methods inherited from interface elki.distance.SpatialPrimitiveDistance

instantiate
• ### Field Detail

• #### STATIC

public static final SqrtCosineUnitlengthDistance STATIC
Static instance
• ### Constructor Detail

• #### SqrtCosineUnitlengthDistance

@Deprecated
public SqrtCosineUnitlengthDistance()
Deprecated.
Use static instance
Constructor - use STATIC instead.
• ### Method Detail

• #### distance

public double distance​(NumberVector v1,
NumberVector v2)
Description copied from interface: PrimitiveDistance
Computes the distance between two given DatabaseObjects according to this distance function.
Specified by:
distance in interface NumberVectorDistance<NumberVector>
Specified by:
distance in interface PrimitiveDistance<NumberVector>
Overrides:
distance in class CosineUnitlengthDistance
Parameters:
v1 - first DatabaseObject
v2 - second DatabaseObject
Returns:
the distance between two given DatabaseObjects according to this distance function
• #### minDist

public double minDist​(SpatialComparable mbr1,
SpatialComparable mbr2)
Description copied from interface: SpatialPrimitiveDistance
Computes the distance between the two given MBRs according to this distance function.
Specified by:
minDist in interface SpatialPrimitiveDistance<NumberVector>
Overrides:
minDist in class CosineUnitlengthDistance
Parameters:
mbr1 - the first MBR object
mbr2 - the second MBR object
Returns:
the distance between the two given MBRs according to this distance function
• #### toString

public java.lang.String toString()
Overrides:
toString in class CosineUnitlengthDistance
• #### isSquared

public boolean isSquared()
Description copied from interface: Distance
Squared distances, that would become metric after square root.

E.g. squared Euclidean.

Specified by:
isSquared in interface Distance<NumberVector>
Overrides:
isSquared in class CosineUnitlengthDistance
Returns:
true when squared.
• #### isMetric

public boolean isMetric()
Description copied from interface: Distance
Is this distance function metric (satisfy the triangle inequality)
Returns:
true when metric.