Package com.compomics.util.math
Class BasicMathFunctions
java.lang.Object
com.compomics.util.math.BasicMathFunctions
Class used to perform basic mathematical functions.
- Author:
- Marc Vaudel, Harald Barsnes
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcheckProbabilityRange(double p) Checks that a probability is between 0 and 1 and throws an IllegalArgumentException otherwise.static voidcheckProbabilityRangeInPercent(double p) Checks that a probability is between 0 % and 100 % and throws an IllegalArgumentException otherwise.static longfactorial(int n) Returns n! as a long.static longfactorial(int n, int k) Returns n!/k!, an error is thrown if it cannot fit in a long.static doublefactorialDouble(int n) Returns n! as a double.static doublefactorialDouble(int n, int k) Returns n!/k! as double.static longgetCombination(int k, int n) Returns the number of k-combinations in a set of n elements.static doublegetCombinationDouble(int k, int n) Returns the number of k-combinations in a set of n elements.static doublegetCorrelation(ArrayList<Double> series1, ArrayList<Double> series2) Returns the population Pearson correlation r between series1 and series2.static long[]Returns factorial 0 to 20 in an array.static doublegetOneOverCombinationDouble(int k, int n) Returns the invert of the number of k-combinations in a set of n elements.getRandomIndexes(int n, int min, int max) Returns a list of n random indexes between min and max included.static intgetRandomInteger(int min, int max) Returns an integer randomly chosen between min and max included.static doublegetRobustCorrelation(ArrayList<Double> series1, ArrayList<Double> series2) Returns the population Pearson correlation r between series1 and series2.static doublelog(double input, double base) Returns the log of the input in the desired base.static doublemad(double[] ratios) Method estimating the median absolute deviation.static doubleMethod estimating the median absolute deviation.static doubleConvenience method returning the mean of a list of doubles.static doublemedian(double[] input) Method to estimate the median for an unsorted list.static doubleMethod to estimate the median of an unsorted list.static doublemedianSorted(double[] input) Method to estimate the median for a sorted list.static doublemedianSorted(ArrayList<Double> input) Method to estimate the median of a sorted list.static doublepercentile(double[] input, double percentile) Returns the desired percentile in a given array of unsorted double values.static doublepercentile(ArrayList<Double> input, double percentile) Returns the desired percentile in a list of unsorted double values.static doublepercentileSorted(double[] input, double percentile) Returns the desired percentile in an array of sorted double values.static doublepercentileSorted(ArrayList<Double> input, double percentile) Returns the desired percentile in a list of sorted double values.static doubleConvenience method returning the standard deviation of a list of doubles.static doubleConvenience method returning the sum of a list of doubles.
-
Constructor Details
-
BasicMathFunctions
public BasicMathFunctions()Empty default constructor.
-
-
Method Details
-
getFactorialsCache
public static long[] getFactorialsCache()Returns factorial 0 to 20 in an array.- Returns:
- Factorial 0 to 20 in an array.
-
factorialDouble
public static double factorialDouble(int n) Returns n! as a double. Accuracy decreases if the capacity of a long is not sufficient (i.e. n higher than 20).- Parameters:
n- a given integer- Returns:
- the corresponding factorial
-
factorial
public static long factorial(int n) Returns n! as a long. Throws an error if the capacity of a long is not sufficient (i.e. n higher than 20).- Parameters:
n- a given integer- Returns:
- the corresponding factorial
-
factorialDouble
public static double factorialDouble(int n, int k) Returns n!/k! as double. Accuracy decreases if the capacity of a long is not sufficient- Parameters:
n- nk- k- Returns:
- n!/k!.
-
factorial
public static long factorial(int n, int k) Returns n!/k!, an error is thrown if it cannot fit in a long.- Parameters:
n- nk- k- Returns:
- n!/k!.
-
getCombinationDouble
public static double getCombinationDouble(int k, int n) Returns the number of k-combinations in a set of n elements. Accuracy decreases if the capacity of a long is not sufficient- Parameters:
k- the number of k-combinationsn- the number of elements- Returns:
- The number of k-combinations in a set of n elements.
-
getOneOverCombinationDouble
public static double getOneOverCombinationDouble(int k, int n) Returns the invert of the number of k-combinations in a set of n elements. Accuracy decreases if the capacity of a long is not sufficient- Parameters:
k- the number of k-combinationsn- the number of elements- Returns:
- The number of k-combinations in a set of n elements.
-
getCombination
public static long getCombination(int k, int n) Returns the number of k-combinations in a set of n elements. If n!/k! cannot fit in a long, an error is thrown.- Parameters:
k- the number of k-combinationsn- the number of elements- Returns:
- The number of k-combinations in a set of n elements.
-
median
public static double median(double[] input) Method to estimate the median for an unsorted list.- Parameters:
input- array of double- Returns:
- median of the input
-
medianSorted
public static double medianSorted(double[] input) Method to estimate the median for a sorted list.- Parameters:
input- array of double- Returns:
- median of the input
-
median
Method to estimate the median of an unsorted list.- Parameters:
input- ArrayList of double- Returns:
- median of the input
-
medianSorted
Method to estimate the median of a sorted list.- Parameters:
input- ArrayList of double- Returns:
- median of the input
-
percentile
public static double percentile(double[] input, double percentile) Returns the desired percentile in a given array of unsorted double values. If the percentile is between two values a linear interpolation is done. Note: When calculating multiple percentiles on the same list, it is advised to sort it and use percentileSorted.- Parameters:
input- the input arraypercentile- the desired percentile. 0.01 returns the first percentile. 0.5 returns the median.- Returns:
- the desired percentile
-
percentileSorted
public static double percentileSorted(double[] input, double percentile) Returns the desired percentile in an array of sorted double values. If the percentile is between two values a linear interpolation is done. The list must be sorted prior to submission.- Parameters:
input- the input arraypercentile- the desired percentile. 0.01 returns the first percentile. 0.5 returns the median.- Returns:
- the desired percentile
-
percentile
Returns the desired percentile in a list of unsorted double values. If the percentile is between two values a linear interpolation is done. Note: When calculating multiple percentiles on the same list, it is advised to sort it and use percentileSorted.- Parameters:
input- the input listpercentile- the desired percentile. 0.01 returns the first percentile. 0.5 returns the median.- Returns:
- the desired percentile
-
percentileSorted
Returns the desired percentile in a list of sorted double values. If the percentile is between two values a linear interpolation is done. The list must be sorted prior to submission.- Parameters:
input- the input listpercentile- the desired percentile. 0.01 returns the first percentile. 0.5 returns the median.- Returns:
- the desired percentile
-
mad
public static double mad(double[] ratios) Method estimating the median absolute deviation.- Parameters:
ratios- array of doubles- Returns:
- the mad of the input
-
mad
Method estimating the median absolute deviation.- Parameters:
ratios- array of doubles- Returns:
- the mad of the input
-
log
public static double log(double input, double base) Returns the log of the input in the desired base.- Parameters:
input- the inputbase- the log base- Returns:
- the log value of the input in the desired base.
-
std
Convenience method returning the standard deviation of a list of doubles. Returns 0 if the list is null or of size < 2.- Parameters:
input- input list- Returns:
- the corresponding standard deviation
-
mean
Convenience method returning the mean of a list of doubles.- Parameters:
input- input list- Returns:
- the corresponding mean
-
sum
Convenience method returning the sum of a list of doubles.- Parameters:
input- input list- Returns:
- the corresponding mean
-
getCorrelation
Returns the population Pearson correlation r between series1 and series2.- Parameters:
series1- first series to compareseries2- second series to compare- Returns:
- the Pearson correlation factor
-
getRobustCorrelation
Returns the population Pearson correlation r between series1 and series2. Here the correlation factor is estimated using median and percentile distance instead of mean and standard deviation.- Parameters:
series1- the first series to inspectseries2- the second series to inspect- Returns:
- a robust version of the Pearson correlation factor
-
checkProbabilityRange
public static void checkProbabilityRange(double p) Checks that a probability is between 0 and 1 and throws an IllegalArgumentException otherwise. 0.5 represents a probability of 50%.- Parameters:
p- the probability
-
checkProbabilityRangeInPercent
public static void checkProbabilityRangeInPercent(double p) Checks that a probability is between 0 % and 100 % and throws an IllegalArgumentException otherwise. 50 represents a probability of 50%.- Parameters:
p- the probability
-
getRandomInteger
public static int getRandomInteger(int min, int max) Returns an integer randomly chosen between min and max included.- Parameters:
min- the lower limitmax- the higher limit- Returns:
- a random integer
-
getRandomIndexes
Returns a list of n random indexes between min and max included. The list is not sorted.- Parameters:
n- the number of indexes to returnmin- the lower limitmax- the higher limit- Returns:
- a list of n random indexes between min and max included
-