net.sourceforge.cilib.util
Class CosineDistanceMeasure

java.lang.Object
  extended by net.sourceforge.cilib.util.CosineDistanceMeasure
All Implemented Interfaces:
DistanceMeasure

public class CosineDistanceMeasure
extends Object
implements DistanceMeasure

The Cosine Distance Measure (or vector dot product) is not a distance measure, but rather a similarity metric. It is defined in:
Learning structure and concepts in data through data clustering by Gregory James Hamerly, 2003

More positive values indicate similarity. The vector dot product is the sum of the product of each attribute from two vectors being compared. Here we use a normalized version of the metric so that the dot product is always a value between -1 and 1. The vector dot product does not measure the difference in magnitude between two vectors; only the difference in the angle between the vectors. If the (normalized) result = 1, the two vectors are collinear and pointed in the same direction, if the (normalized) result = -1, the two vectors are collinear and pointed in opposite directions, and if the (normalized) result = 0, then the two vectors are orthogonal. We can convert this similarity measure to a "distance" by subtracting it from one, which will always give a value between 0 and 2. This is the approach we follow.


Constructor Summary
CosineDistanceMeasure()
           
 
Method Summary
<T extends Collection<? extends Number>>
double
distance(T x, T y)
          Calculate the "distance" (dot product) between (of) two vectors represented by Java Collection objects.
<T extends Type,U extends StructuredType<T>>
double
distance(U x, U y)
          Calculate the "distance" (dot product) between (of) two vectors.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CosineDistanceMeasure

public CosineDistanceMeasure()
Method Detail

distance

public <T extends Type,U extends StructuredType<T>> double distance(U x,
                                                                    U y)
Calculate the "distance" (dot product) between (of) two vectors.

Specified by:
distance in interface DistanceMeasure
Type Parameters:
T - The Vector type.
Parameters:
x - the one vector.
y - the other vector.
Returns:
the "distance" (or angle or dot product) (as a double) between the two vectors.
Throws:
IllegalArgumentException - when the two vectors' dimension differ.

distance

public <T extends Collection<? extends Number>> double distance(T x,
                                                                T y)
Calculate the "distance" (dot product) between (of) two vectors represented by Java Collection objects.

Specified by:
distance in interface DistanceMeasure
Type Parameters:
T - The Vector type.
Parameters:
x - the one Java Collection object.
y - the other Java Collection object.
Returns:
the distance (or angle or dot product) (as a double) between the two vectors.
Throws:
IllegalArgumentException - when the two vectors' dimension differ.


Copyright © 2009 CIRG. All Rights Reserved.