gr.demokritos.iit.conceptualIndex.structs
Class Distribution<TKeyType>

java.lang.Object
  extended by gr.demokritos.iit.conceptualIndex.structs.Distribution<TKeyType>
All Implemented Interfaces:
IDistributionComparisonListener, java.io.Serializable

public class Distribution<TKeyType>
extends java.lang.Object
implements java.io.Serializable, IDistributionComparisonListener

Represents a ditribution of values, either in terms of a simple histogram, or in terms of a probability distribution. Also provides methods for distribution normalization , comparison and other useful functions.

See Also:
Serialized Form

Constructor Summary
Distribution()
          Creates a new instance of Distribution
Distribution(java.util.TreeMap<TKeyType,java.lang.Double> tm)
          Creates a new instance of Distribution, given a corresponding TreeMap.
 
Method Summary
 Distribution addTo(Distribution dOther)
          Creates a new distribution containing the sum of values of the given distribution and a second distribution.
 java.util.TreeMap<TKeyType,java.lang.Double> asTreeMap()
          Returns the underlying TreeMap structure.
 double average(boolean bOnlyValueAverage)
          Calculates the average value of the distribution.
 double calcSumSquaredErrorFrom(Distribution dOther)
           
 double calcTotalValues()
          Calculates the sum of all the values in the distribution.
 double compareDistributions(Distribution d1, Distribution d2)
          Use default comparison between d1 and d2.
 TKeyType getKeyOfMaxValue()
          Looks up the key corresponding to the maximum value of the distribution.
 java.lang.Object getNextResult()
          Returns a random key using this probability distribution.
 Distribution getProbabilityDistribution()
          Creates a new probability distribution, corresponding to the occurences of the features appearing in this distribution.
 double getValue(TKeyType oKey)
          Looks up the value of a selected key, or zero if the key has no value.
 void increaseValue(TKeyType oXValue, double dYValue)
          Increases the value of a selected key by a quantity.
 void invertProbability()
          Inverts the probabilities of this distribution.
static void main(java.lang.String[] args)
          Testing function.
 double maxValue()
          Looks up the maximum value appearing in the distribution.
 double minValue()
          Looks up the minimum value appearing in the distribution.
 void normalize()
          Normalizes the values of the distribution to the range [0,1]
 void normalizeTo(double dNewMax)
          Normalizes the values of the distribution to a range of numbers between zero (0) and a selected value.
 void normalizeToSum()
          Normalizes the values of the distribution to the sum of values, resolving to a probability distribution, as the sum of the new distribution will amount to 1.
 void setValue(TKeyType oXValue, double dYValue)
          Sets the value of a selected key.
 double similarityTo(Distribution dOther)
          Calculates the degree of similarity between two distributions.
 double standardDeviation(boolean bOnlyValue)
          Calculates the standard deviation (square root of the variance) of the distribution, either for the values only, or taking into account the keys.
 double sumOfValues()
          Calculates the sum of values over all distribution points.
 java.lang.String toString()
          Composes a string representation of this distribution, referring only to non-zero elements.
 double variance(boolean bOnlyValue)
          Calculates the variance of the distribution, either for the values only, or taking into account the keys.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Distribution

public Distribution()
Creates a new instance of Distribution


Distribution

public Distribution(java.util.TreeMap<TKeyType,java.lang.Double> tm)
Creates a new instance of Distribution, given a corresponding TreeMap. The TreeMap should contain entries.

Parameters:
tm - The TreeMap.
Method Detail

getValue

public double getValue(TKeyType oKey)
Looks up the value of a selected key, or zero if the key has no value.

Parameters:
oKey - The key to look up.
Returns:
The value mapped to the selected key.

setValue

public void setValue(TKeyType oXValue,
                     double dYValue)
Sets the value of a selected key.

Parameters:
oXValue - The key to use. The key can either be a double number or an object.
dYValue - The value to apply to the selected key.

increaseValue

public void increaseValue(TKeyType oXValue,
                          double dYValue)
Increases the value of a selected key by a quantity.

Parameters:
oXValue - The key to use. The key can either be a double number or an object.
dYValue - The value by which to increase the selected key value.

toString

public java.lang.String toString()
Composes a string representation of this distribution, referring only to non-zero elements.

Overrides:
toString in class java.lang.Object
Returns:
The string representation of this distribution.

asTreeMap

public java.util.TreeMap<TKeyType,java.lang.Double> asTreeMap()
Returns the underlying TreeMap structure.

Returns:
The underlying tree map.

similarityTo

public double similarityTo(Distribution dOther)
Calculates the degree of similarity between two distributions. The similarity is calculated in the following steps:
  1. The sum of the absolute difference of the values of each distribution over all points is calculated. Let this be SD.
  2. The values of both compared distributions are summed. Let this be SV.
  3. The returned value is 1.0 - (SD / SV).
Derived classes could override this function to use different similarity metrics.

Parameters:
dOther - The distribution to compare to.
Returns:
A value between 1.0 (identity) and 0.0 (no similarity) indicative of similarity between this distribution and the given one.

calcTotalValues

public double calcTotalValues()
Calculates the sum of all the values in the distribution.

Returns:
The sum of values over all points.

normalizeTo

public void normalizeTo(double dNewMax)
Normalizes the values of the distribution to a range of numbers between zero (0) and a selected value.

Parameters:
dNewMax - The new maximum value after the transformation.

normalize

public void normalize()
Normalizes the values of the distribution to the range [0,1]


normalizeToSum

public void normalizeToSum()
Normalizes the values of the distribution to the sum of values, resolving to a probability distribution, as the sum of the new distribution will amount to 1.


sumOfValues

public double sumOfValues()
Calculates the sum of values over all distribution points.

Returns:
The sum of values.

getProbabilityDistribution

public Distribution getProbabilityDistribution()
Creates a new probability distribution, corresponding to the occurences of the features appearing in this distribution. If all appearences are equal to zero, returns a uniform distribution.

Returns:
The derived probability distribution.

average

public double average(boolean bOnlyValueAverage)
Calculates the average value of the distribution. For a distribution where the keys are double values, this function can calculate the expected value (mean), taking into account the keys. In other cases the result is the average of values.

Parameters:
bOnlyValueAverage - If true, then only the value average is computed, otherwise the expected value is calculated, taking into account the keys (which are supposed to be Double objects)
Returns:
The expected (mean / average) value of the distribution.

maxValue

public double maxValue()
Looks up the maximum value appearing in the distribution.

Returns:
The maximum value of the distribution (considering non-zero elements).

minValue

public double minValue()
Looks up the minimum value appearing in the distribution.

Returns:
The minimum value of the distribution (considering non-zero elements).

variance

public double variance(boolean bOnlyValue)
Calculates the variance of the distribution, either for the values only, or taking into account the keys.

Parameters:
bOnlyValue - If true, then only the value variance is computed, otherwise the variance is calculated taking into account the keys (which are supposed to be Double objects).
Returns:
The variance of the distribution.

standardDeviation

public double standardDeviation(boolean bOnlyValue)
Calculates the standard deviation (square root of the variance) of the distribution, either for the values only, or taking into account the keys.

Parameters:
bOnlyValue - If true, then only the value standard deviation is computed, otherwise the standard deviation is calculated taking into account the keys (which are supposed to be Double objects).
Returns:
The standard deviation of the distribution.

getKeyOfMaxValue

public TKeyType getKeyOfMaxValue()
Looks up the key corresponding to the maximum value of the distribution.

Returns:
The key of the maximum value.

invertProbability

public void invertProbability()
Inverts the probabilities of this distribution.


getNextResult

public java.lang.Object getNextResult()
Returns a random key using this probability distribution. For example if the distribution represents the prbabilities of dice rolls in a six-sided dice, then this function will return the next roll result.

Returns:
The key of a random selection using this distribution.

main

public static void main(java.lang.String[] args)
Testing function.


calcSumSquaredErrorFrom

public double calcSumSquaredErrorFrom(Distribution dOther)

addTo

public Distribution addTo(Distribution dOther)
Creates a new distribution containing the sum of values of the given distribution and a second distribution.

Parameters:
dOther - The other distribution.
Returns:
A new Distribution which contains values corresponding to the sum of values of the original distributions.

compareDistributions

public double compareDistributions(Distribution d1,
                                   Distribution d2)
Use default comparison between d1 and d2.

Specified by:
compareDistributions in interface IDistributionComparisonListener