Class BasicMathFunctions

java.lang.Object
com.compomics.util.math.BasicMathFunctions

public class BasicMathFunctions extends Object
Class used to perform basic mathematical functions.
Author:
Marc Vaudel, Harald Barsnes
  • Constructor Summary

    Constructors
    Constructor
    Description
    Empty default constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    Checks that a probability is between 0 and 1 and throws an IllegalArgumentException otherwise.
    static void
    Checks that a probability is between 0 % and 100 % and throws an IllegalArgumentException otherwise.
    static long
    factorial(int n)
    Returns n! as a long.
    static long
    factorial(int n, int k)
    Returns n!/k!, an error is thrown if it cannot fit in a long.
    static double
    Returns n! as a double.
    static double
    factorialDouble(int n, int k)
    Returns n!/k! as double.
    static long
    getCombination(int k, int n)
    Returns the number of k-combinations in a set of n elements.
    static double
    getCombinationDouble(int k, int n)
    Returns the number of k-combinations in a set of n elements.
    static double
    Returns the population Pearson correlation r between series1 and series2.
    static long[]
    Returns factorial 0 to 20 in an array.
    static double
    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 int
    getRandomInteger(int min, int max)
    Returns an integer randomly chosen between min and max included.
    static double
    Returns the population Pearson correlation r between series1 and series2.
    static double
    log(double input, double base)
    Returns the log of the input in the desired base.
    static double
    mad(double[] ratios)
    Method estimating the median absolute deviation.
    static double
    mad(ArrayList<Double> ratios)
    Method estimating the median absolute deviation.
    static double
    Convenience method returning the mean of a list of doubles.
    static double
    median(double[] input)
    Method to estimate the median for an unsorted list.
    static double
    Method to estimate the median of an unsorted list.
    static double
    medianSorted(double[] input)
    Method to estimate the median for a sorted list.
    static double
    Method to estimate the median of a sorted list.
    static double
    percentile(double[] input, double percentile)
    Returns the desired percentile in a given array of unsorted double values.
    static double
    percentile(ArrayList<Double> input, double percentile)
    Returns the desired percentile in a list of unsorted double values.
    static double
    percentileSorted(double[] input, double percentile)
    Returns the desired percentile in an array of sorted double values.
    static double
    percentileSorted(ArrayList<Double> input, double percentile)
    Returns the desired percentile in a list of sorted double values.
    static double
    Convenience method returning the standard deviation of a list of doubles.
    static double
    Convenience method returning the sum of a list of doubles.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 - n
      k - 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 - n
      k - 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-combinations
      n - 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-combinations
      n - 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-combinations
      n - 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

      public static double median(ArrayList<Double> input)
      Method to estimate the median of an unsorted list.
      Parameters:
      input - ArrayList of double
      Returns:
      median of the input
    • medianSorted

      public static double medianSorted(ArrayList<Double> input)
      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 array
      percentile - 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 array
      percentile - the desired percentile. 0.01 returns the first percentile. 0.5 returns the median.
      Returns:
      the desired percentile
    • percentile

      public static double percentile(ArrayList<Double> input, double 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 list
      percentile - the desired percentile. 0.01 returns the first percentile. 0.5 returns the median.
      Returns:
      the desired percentile
    • percentileSorted

      public static double percentileSorted(ArrayList<Double> input, double percentile)
      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 list
      percentile - 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

      public static double mad(ArrayList<Double> ratios)
      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 input
      base - the log base
      Returns:
      the log value of the input in the desired base.
    • std

      public static double std(ArrayList<Double> input)
      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

      public static double mean(ArrayList<Double> input)
      Convenience method returning the mean of a list of doubles.
      Parameters:
      input - input list
      Returns:
      the corresponding mean
    • sum

      public static double sum(ArrayList<Double> input)
      Convenience method returning the sum of a list of doubles.
      Parameters:
      input - input list
      Returns:
      the corresponding mean
    • getCorrelation

      public static double getCorrelation(ArrayList<Double> series1, ArrayList<Double> series2)
      Returns the population Pearson correlation r between series1 and series2.
      Parameters:
      series1 - first series to compare
      series2 - second series to compare
      Returns:
      the Pearson correlation factor
    • getRobustCorrelation

      public static double getRobustCorrelation(ArrayList<Double> series1, ArrayList<Double> series2)
      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 inspect
      series2 - 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 limit
      max - the higher limit
      Returns:
      a random integer
    • getRandomIndexes

      public static ArrayList<Integer> getRandomIndexes(int n, int min, int max)
      Returns a list of n random indexes between min and max included. The list is not sorted.
      Parameters:
      n - the number of indexes to return
      min - the lower limit
      max - the higher limit
      Returns:
      a list of n random indexes between min and max included