de.jstacs.utils
Class ToolBox

java.lang.Object
  extended by de.jstacs.utils.ToolBox

public class ToolBox
extends Object

This class is a collection of methods which might be useful for the programmer.

Author:
Jens Keilwagen, Jan Grau

Nested Class Summary
static class ToolBox.TiedRanks
          Handling of tied ranks in rank(double[], TiedRanks).
 
Constructor Summary
ToolBox()
           
 
Method Summary
static int getMaxIndex(double[] w)
          Returns the index with maximal value in a double array.
static int getMaxIndex(int start, int end, double[] w)
          Returns the index with maximal value in a double array.
static double[] getUniqueHueValues(int number)
          Creates an array of hue values that can be used for the representation of probabilities.
static double max(double... array)
          This method returns the maximum of the elements of an array.
static double max(int start, int end, double[] array)
          This method returns the maximum of the elements of an array between start and end.
static double mean(int start, int end, double[] array)
          This method returns the mean of the elements of an array between start and end.
static double median(double... array)
          This method returns the median of the elements of array.
static double median(int start, int end, double[] array)
          This method returns the median of the elements of an array between start and end.
static double min(double... array)
          This method returns the minimum of the elements of an array.
static double min(int start, int end, double[] array)
          This method returns the minimum of the elements of an array between start and end.
static int[] order(double[] values, boolean ascending)
          This method computes the order of the elements to obtain a sorted array.
static
<K> Hashtable<K,Integer>
parseHashSet2IndexHashtable(HashSet<K> set)
          This method converts a HashSet in a Hashtable with unique indices starting at 0.
static double pearsonCorrelation(double[] v1, double[] v2)
          The method computes the Pearson correlation of two vectors.
static double pearsonCorrelation(double[] v1, double[] v2, int off1, int off2)
          The method computes the Pearson correlation of two vectors beginning at specific offsets.
static double pearsonCorrelation(double[] v1, double[] v2, int off1, int off2, int length)
          The method computes the Pearson correlation of two vectors beginning at specific offsets and using a given number of entries.
static double percentile(double[] array, double percent)
          Returns the percent percentile of the array, i.e., returns the element at percent*(array.length) of the sorted array.
static double percentile(int start, int end, double[] array, double percent)
          Returns the percent percentile of the array between start and end, i.e., returns the element at percent*(end-start) of the sorted sub-array.
static int[] rank(double[] values, boolean sameRank)
          Ranks the values in values, where the greatest value obtains the lowest rank.
static int[] rank(double[] values, ToolBox.TiedRanks ties)
          Ranks the values in values, where the greatest value obtains the lowest rank.
static double sd(int start, int end, double[] array)
          This method returns the standard deviation of the elements of an array between start and end.
static void sortAlongWith(double[] arrayToBeSorted, double[]... alongWith)
          This method implements a sort algorithm on the array arrayToBeSorted.
static double spearmanCorrelation(double[] v1, double[] v2)
          The method computes the Spearman correlation of two vectors.
static double sum(double... array)
          Computes the sum of the values in array
static double sum(int start, int end, double[] array)
          Computes the sum of the values in array starting at start until end.
static String toString(double[] array, NumberFormat nf)
          This methods returns a String representation of a double array using the specified NumberFormat.
static double[][] transpose(double[][] sp)
          Transpose a double matrix.
static double weightPercentile(double[] array, double percent)
          Returns the element in the sorted variant of array (considered in descending order) that yields percent of the cumulative value of array elements.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ToolBox

public ToolBox()
Method Detail

parseHashSet2IndexHashtable

public static <K> Hashtable<K,Integer> parseHashSet2IndexHashtable(HashSet<K> set)
This method converts a HashSet in a Hashtable with unique indices starting at 0. The indices are derived from the order of the Iterator of the HashSet.

Type Parameters:
K - the type of the keys
Parameters:
set - the set of keys
Returns:
a Hashtable with keys and unique indices starting at 0

max

public static double max(double... array)
This method returns the maximum of the elements of an array.

Parameters:
array - the array of values
Returns:
the maximum

min

public static double min(double... array)
This method returns the minimum of the elements of an array.

Parameters:
array - the array of values
Returns:
the minimum

max

public static double max(int start,
                         int end,
                         double[] array)
This method returns the maximum of the elements of an array between start and end.

Parameters:
start - start position (inclusive)
end - end position (exclusive)
array - the array of values
Returns:
the maximum

min

public static double min(int start,
                         int end,
                         double[] array)
This method returns the minimum of the elements of an array between start and end.

Parameters:
start - start position (inclusive)
end - end position (exclusive)
array - the array of values
Returns:
the minimum

getMaxIndex

public static final int getMaxIndex(double[] w)
Returns the index with maximal value in a double array.

Parameters:
w - the given double array
Returns:
the index

getMaxIndex

public static final int getMaxIndex(int start,
                                    int end,
                                    double[] w)
Returns the index with maximal value in a double array.

Parameters:
w - the given double array
start - start position (inclusive)
end - end position (exclusive)
Returns:
the index

median

public static double median(double... array)
This method returns the median of the elements of array.

Parameters:
array - the array of values
Returns:
the median

median

public static double median(int start,
                            int end,
                            double[] array)
This method returns the median of the elements of an array between start and end.

Parameters:
start - start position (inclusive)
end - end position (exclusive)
array - the array of values
Returns:
the median

mean

public static double mean(int start,
                          int end,
                          double[] array)
This method returns the mean of the elements of an array between start and end.

Parameters:
start - start position (inclusive)
end - end position (exclusive)
array - the array of values
Returns:
the mean

sd

public static double sd(int start,
                        int end,
                        double[] array)
This method returns the standard deviation of the elements of an array between start and end.

Parameters:
start - start position (inclusive)
end - end position (exclusive)
array - the array of values
Returns:
the standard deviation

percentile

public static double percentile(double[] array,
                                double percent)
Returns the percent percentile of the array, i.e., returns the element at percent*(array.length) of the sorted array.

Parameters:
array - the array
percent - the percentage
Returns:
the percentile value

percentile

public static double percentile(int start,
                                int end,
                                double[] array,
                                double percent)
Returns the percent percentile of the array between start and end, i.e., returns the element at percent*(end-start) of the sorted sub-array.

Parameters:
start - the first index to consider
end - the end index (exclusive)
array - the array
percent - the percentage
Returns:
the percentile value

weightPercentile

public static double weightPercentile(double[] array,
                                      double percent)
Returns the element in the sorted variant of array (considered in descending order) that yields percent of the cumulative value of array elements.

Parameters:
array - the array
percent - the percentage
Returns:
the element

rank

public static final int[] rank(double[] values,
                               boolean sameRank)
Ranks the values in values, where the greatest value obtains the lowest rank. The boolean sameRank allows to decide whether tied values should obtain the same rank.

Parameters:
values - the values
sameRank - a switch whether tied values obtain the same rank.
Returns:
the ranks

order

public static final int[] order(double[] values,
                                boolean ascending)
This method computes the order of the elements to obtain a sorted array.

Parameters:
values - the array
ascending - a switch whether the array should be sorted ascending or descending
Returns:
the array containing the indices of the elements to obtain a sorted array

rank

public static final int[] rank(double[] values,
                               ToolBox.TiedRanks ties)
Ranks the values in values, where the greatest value obtains the lowest rank. The enum ties allows to choose how tied ranks are handled.

Parameters:
values - the values
ties - a switch how to handle tied ranks
Returns:
the ranks
See Also:
ToolBox.TiedRanks

spearmanCorrelation

public static double spearmanCorrelation(double[] v1,
                                         double[] v2)
                                  throws Exception
The method computes the Spearman correlation of two vectors.

Parameters:
v1 - the first vector
v2 - the second vector
Returns:
the Spearman correlation of the two vectors
Throws:
Exception - if the vectors have different length

pearsonCorrelation

public static double pearsonCorrelation(double[] v1,
                                        double[] v2)
                                 throws Exception
The method computes the Pearson correlation of two vectors.

Parameters:
v1 - the first vector
v2 - the second vector
Returns:
the Pearson correlation of the two vectors
Throws:
Exception - if the vectors have different length

pearsonCorrelation

public static double pearsonCorrelation(double[] v1,
                                        double[] v2,
                                        int off1,
                                        int off2)
                                 throws Exception
The method computes the Pearson correlation of two vectors beginning at specific offsets.

Parameters:
v1 - the first vector
v2 - the second vector
off1 - the offset for the first vector
off2 - the offset for the second vector
Returns:
the Pearson correlation of the two vectors
Throws:
Exception - if the vectors have different length

pearsonCorrelation

public static double pearsonCorrelation(double[] v1,
                                        double[] v2,
                                        int off1,
                                        int off2,
                                        int length)
                                 throws Exception
The method computes the Pearson correlation of two vectors beginning at specific offsets and using a given number of entries.

Parameters:
v1 - the first vector
v2 - the second vector
off1 - the offset for the first vector
off2 - the offset for the second vector
length - the number of elements to be used for computing the correlation
Returns:
the Pearson correlation of the two vectors
Throws:
Exception - if the vectors have different length

sum

public static double sum(double... array)
Computes the sum of the values in array

Parameters:
array - the array
Returns:
the sum of the elements in array

sum

public static double sum(int start,
                         int end,
                         double[] array)
Computes the sum of the values in array starting at start until end.

Parameters:
start - the index of the first element in the sum (inclusive)
end - the end index (exclusive)
array - the array
Returns:
the sum of the elements in array from start to end

transpose

public static double[][] transpose(double[][] sp)
                            throws Exception
Transpose a double matrix.

Parameters:
sp - the array
Returns:
the transposed array
Throws:
Exception - if sp is no matrix

getUniqueHueValues

public static double[] getUniqueHueValues(int number)
Creates an array of hue values that can be used for the representation of probabilities. The first four hues are always equal to those of the sequence-logo colors.

Parameters:
number - the number of colors
Returns:
the hue values

sortAlongWith

public static void sortAlongWith(double[] arrayToBeSorted,
                                 double[]... alongWith)
This method implements a sort algorithm on the array arrayToBeSorted. However, the entries of the array alongWith are reordered in the same way asarrayToBeSorted.

Parameters:
arrayToBeSorted - the array to be sorted
alongWith - a series of arrays that is in the same way reordered as arrayToBeSorted.
See Also:
Arrays.sort(Object[], Comparator), DoubleArrayComparator

toString

public static String toString(double[] array,
                              NumberFormat nf)
This methods returns a String representation of a double array using the specified NumberFormat.

Parameters:
array - the array to be represented
nf - the NumberFormat to be used
Returns:
a String representation