de.unibi.techfak.jpredictor.clustering
Class MinkowskiDistance

java.lang.Object
  extended by de.unibi.techfak.jpredictor.clustering.RelationMeasure
      extended by de.unibi.techfak.jpredictor.clustering.VectorRelationMeasure
          extended by de.unibi.techfak.jpredictor.clustering.MinkowskiDistance
All Implemented Interfaces:
Computable, java.lang.Comparable
Direct Known Subclasses:
EuclideanDistance, ManhattanDistance

public class MinkowskiDistance
extends VectorRelationMeasure

Calculates the distance between two vectors. The formula behind is: (sum_{i=1}^{n}{|x_i-y_i|^p})^(1/p). Special distances are defined for p=1 (Manhatten Distance Measure) and p=2 (Euclidian Distance Measure).

This class computes the distance for only two vectors. If more than two arrays were added the latest added are used for computation.


Field Summary
(package private)  double p
          Defines the exponent for the minkowski distance.
 
Fields inherited from class de.unibi.techfak.jpredictor.clustering.VectorRelationMeasure
vectorList
 
Fields inherited from class de.unibi.techfak.jpredictor.clustering.RelationMeasure
computationResult
 
Fields inherited from interface de.unibi.techfak.jpredictor.clustering.Computable
COMPUTATION_FAILED, COMPUTATION_PENDING, COMPUTATION_SUCCESS, COMPUTATION_WAITING
 
Constructor Summary
MinkowskiDistance(double p)
          Inits the Minkowski distance measure.
 
Method Summary
 double atomicCombine(double rel0, double rel1)
           Two relations measures are combined to one result, which is not stored but returned.
 double atomicNormalize(double rel, double norm)
           A relations measures is normalized by a certain amount.
static double calculate(double[] x, double[] y, double p)
          Calculates the Minkowski distance between two given arrays.
 int compute()
          Uses all internal informations available and starts and controlls the computational process.
 double getP()
          Returns the exponents value of the Minkowski Distance.
 RelationMeasure newInstance()
          Creates a copy of this relation measure as if it was newly instantiated through a call to the constructor.
 int optimum(double rel0, double rel1)
           Compares two relation measure results.
(package private)  void setP(double p)
          Sets the new exponent for the Minkowski Distance.
 java.lang.String toString()
          Creates a short string of this relation measure and maybe some results if available.
 
Methods inherited from class de.unibi.techfak.jpredictor.clustering.VectorRelationMeasure
add, add, add, clear, length
 
Methods inherited from class de.unibi.techfak.jpredictor.clustering.RelationMeasure
atomicCombine, compareTo, optimum, result
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

p

double p
Defines the exponent for the minkowski distance. Special values are p=1 (Manhatten) and p=2 (Euclid). May not be zero, nor negative.

Constructor Detail

MinkowskiDistance

public MinkowskiDistance(double p)
                  throws java.lang.IllegalArgumentException
Inits the Minkowski distance measure.

Parameters:
p - The exponent of the Minkowski distance measure.
Throws:
java.lang.IllegalArgumentException - If p is zero or less than zero.
Method Detail

getP

public double getP()
Returns the exponents value of the Minkowski Distance.

Returns:
The exponent of the Minkowski Distance.

setP

void setP(double p)
    throws java.lang.IllegalArgumentException
Sets the new exponent for the Minkowski Distance.

Parameters:
p - The new exponent.
Throws:
java.lang.IllegalArgumentException - If p is zero or less than zero.

compute

public int compute()
Description copied from interface: Computable
Uses all internal informations available and starts and controlls the computational process. In its simplest case it is a method to calculate a formula, a more complicate way would be to run e.g. a whole clustering through this method.

Returns:
A status code about the process, e.g. COMPUTATION_SUCCESS.
See Also:
Computable.COMPUTATION_SUCCESS, Computable.COMPUTATION_FAILED

optimum

public int optimum(double rel0,
                   double rel1)
Description copied from class: RelationMeasure

Compares two relation measure results. Returns -1, zero or 1, if the first (rel0) measure is worse, equal, or better than the second one (rel1). The terms worse and better depend on the implementation of the RelationMeasure inheriting this method. Usually, when dealing with distances, the lower the better. On the other side, when dealing with likelihoods the higher the better.

Note, that it is possible to mix up optimization of different relation measures, e.g. distances and likelihoods, since this method works on any numbers.

Specified by:
optimum in class RelationMeasure
Parameters:
rel0 - The first relation measure to be compared to the second.
rel1 - The second relation measure to be compared to the first.
Returns:
-1, zero or 1 in case the first result is worse, equal or better than the second one.

calculate

public static double calculate(double[] x,
                               double[] y,
                               double p)
Calculates the Minkowski distance between two given arrays. Returns Double.NaN in case of any error, e.g. x or y are null or of inequal length, or p is less or equal zero.

Parameters:
x - The first array.
y - The second array
p - The exponent of calculation.
Returns:
The Minkowski distance between two arrays.

atomicCombine

public double atomicCombine(double rel0,
                            double rel1)
Description copied from class: RelationMeasure

Two relations measures are combined to one result, which is not stored but returned. This method provides a way to calculate the combined result, when the way how to do this is not known. For instance, distance relation measures normally sum up over single results while likelihood/probabilities are multiplied.

Note, that it is possible to mix up optimization of different relation measures, e.g. distances and likelihoods, since this method works on any numbers.

Specified by:
atomicCombine in class RelationMeasure
Parameters:
rel0 - The first relation measure to be compared to the second.
rel1 - The second relation measure to be compared to the first.
Returns:
The result of combining two relation measures, e.g. through adding them up, or multiplying them or such.

atomicNormalize

public double atomicNormalize(double rel,
                              double norm)
Description copied from class: RelationMeasure

A relations measures is normalized by a certain amount. This method provides a way to normalize a relation measure after numbers of them were combined. For instance, distance relation measures normally are normalized by dividing through the count, while likelihoods are rooted.

Note, that it is possible to mix up optimization of different relation measures, e.g. distances and likelihoods, since this method works on any numbers.

Specified by:
atomicNormalize in class RelationMeasure
Parameters:
rel - The relation measure to be normalized.
norm - The normalization value.
Returns:
The result of normalizing a relation measures. Or Double.NaN in case of error.

newInstance

public RelationMeasure newInstance()
Description copied from class: RelationMeasure
Creates a copy of this relation measure as if it was newly instantiated through a call to the constructor. Note: if a class relies on really unique relation measures that are never shared with other instances it should call this method in the constructor. This might be the case, if a class uses a relation measures to store results of partially results.

Specified by:
newInstance in class RelationMeasure
Returns:
A copy of the relation measure as if it was newly instantiated.

toString

public java.lang.String toString()
Description copied from class: RelationMeasure
Creates a short string of this relation measure and maybe some results if available.

Specified by:
toString in class RelationMeasure
Returns:
A string with a short description of the class.