Class StatisticsUtils


  • public class StatisticsUtils
    extends Object
    Class with some statistics methods for calculating values such as the average, median, sum, max and min of a list of numbers.
    Author:
    Eduardo Ramos
    • Constructor Detail

      • StatisticsUtils

        public StatisticsUtils()
    • Method Detail

      • average

        public static BigDecimal average​(Number[] numbers)
        Get average calculation of various numbers as a BigDecimal.

        Null values will not be counted.

        Parameters:
        numbers - Numbers to calculate average
        Returns:
        Average as a BigDecimal
      • average

        public static BigDecimal average​(Collection<Number> numbers)
        Get average calculation of various numbers as a BigDecimal.

        Null values will not be counted.

        Parameters:
        numbers - Numbers to calculate average
        Returns:
        Average as a BigDecimal
      • median

        public static BigDecimal median​(Number[] numbers)
        Calculate median of various numbers as a BigDecimal.

        The elements can't be null.

        The elements don't need to be sorted.

        Parameters:
        numbers - Not null numbers to calculate median
        Returns:
        Median as a BigDecimal
      • median

        public static BigDecimal median​(Collection<Number> numbers)
        Calculate median of various numbers as a BigDecimal.

        The elements can't be null.

        The elements don't need to be sorted.

        Parameters:
        numbers - Not null numbers to calculate median
        Returns:
        Median as a BigDecimal
      • quartile1

        public static BigDecimal quartile1​(Number[] numbers)
        Calculate first quartile (Q1) of various numbers as a BigDecimal.

        The elements can't be null.

        The elements don't need to be sorted.

        Parameters:
        numbers - Not null numbers to calculate Q1
        Returns:
        Q1 as a BigDecimal
      • quartile1

        public static BigDecimal quartile1​(Collection<Number> numbers)
        Calculate first quartile (Q1) of various numbers as a BigDecimal.

        The elements can't be null.

        The elements don't need to be sorted.

        Parameters:
        numbers - Not null numbers to calculate Q1
        Returns:
        Q1 as a BigDecimal
      • quartile3

        public static BigDecimal quartile3​(Number[] numbers)
        Calculate third quartile (Q3) of various numbers as a BigDecimal.

        The elements can't be null.

        The elements don't need to be sorted.

        Parameters:
        numbers - Not null numbers to calculate Q3
        Returns:
        Q3 as a BigDecimal
      • quartile3

        public static BigDecimal quartile3​(Collection<Number> numbers)
        Calculate third quartile (Q3) of various numbers as a BigDecimal.

        The elements can't be null.

        The elements don't need to be sorted.

        Parameters:
        numbers - Not null numbers to calculate Q3
        Returns:
        Q3 as a BigDecimal
      • sum

        public static BigDecimal sum​(Number[] numbers)
        Get sum of various numbers as a BigDecimal.

        Null values will not be counted.

        Parameters:
        numbers - Numbers to calculate sum
        Returns:
        Sum as a BigDecimal
      • sum

        public static BigDecimal sum​(Collection<Number> numbers)
        Get sum of various numbers as a BigDecimal.

        Null values will not be counted.

        Parameters:
        numbers - Numbers to calculate sum
        Returns:
        Sum as a BigDecimal
      • minValue

        public static BigDecimal minValue​(Number[] numbers)
        Get the minimum value of an array of Number elements as a BigDecimal.

        The elements can't be null.

        The elements don't need to be sorted.

        Parameters:
        numbers - Numbers to get min
        Returns:
        Minimum value as a BigDecimal
      • minValue

        public static BigDecimal minValue​(Collection<Number> numbers)
        Get the minimum value of a collection of Number elements as a BigDecimal.

        The elements can't be null.

        The elements don't need to be sorted.

        Parameters:
        numbers - Numbers to get min
        Returns:
        Minimum value as a BigDecimal
      • maxValue

        public static BigDecimal maxValue​(Number[] numbers)
        Get the maximum value of an array of Number elements as a BigDecimal.

        The elements can't be null.

        The elements don't need to be sorted.

        Parameters:
        numbers - Numbers to get max
        Returns:
        Maximum value as a BigDecimal
      • maxValue

        public static BigDecimal maxValue​(Collection<Number> numbers)
        Get the maximum value of a collection of Number elements as a BigDecimal.

        The elements can't be null.

        The elements don't need to be sorted.

        Parameters:
        numbers - Numbers to get max
        Returns:
        Maximum value as a BigDecimal
      • getAllStatistics

        public static BigDecimal[] getAllStatistics​(Number[] numbers)
        Calculates all statistics and returns them in a BigDecimal numbers array.

        Using this will be faster than calling all statistics separately.

        Returns an array of length=8 of BigDecimal numbers with the results in the following order:

        1. average
        2. first quartile (Q1)
        3. median
        4. third quartile (Q3)
        5. interquartile range (IQR)
        6. sum
        7. minimumValue
        8. maximumValue

        The elements can't be null.

        The elements don't need to be sorted.

        Parameters:
        numbers - Numbers to get all statistics
        Returns:
        Array with all statistics
      • getAllStatistics

        public static BigDecimal[] getAllStatistics​(Collection<Number> numbers)
        Calculates all statistics and returns them in a BigDecimal numbers array.

        Using this will be faster than calling all statistics separately.

        Returns an array of length=8 of BigDecimal numbers with the results in the following order:

        1. average
        2. first quartile (Q1)
        3. median
        4. third quartile (Q3)
        5. interquartile range (IQR)
        6. sum
        7. minimumValue
        8. maximumValue

        The elements can't be null.

        The elements don't need to be sorted.

        Parameters:
        numbers - Numbers to get all statistics
        Returns:
        Array with all statistics
      • numbersArrayToSortedBigDecimalArray

        public static BigDecimal[] numbersArrayToSortedBigDecimalArray​(Number[] numbers)
        Takes an array of numbers of any type combination and returns an array with their BigDecimal equivalent numbers.
        Parameters:
        numbers - input
        Returns:
        BigDecimal array
      • median

        private static BigDecimal median​(BigDecimal[] bigDecimalNumbers,
                                         int start,
                                         int end)