Class Statistics


  • final class Statistics
    extends java.lang.Object
    Utility methods for statistics.
    Since:
    1.1
    • Field Summary

      Fields 
      Modifier and Type Field Description
      (package private) static java.util.function.DoubleConsumer DOUBLE_NOOP
      A no-operation double consumer.
      private static java.lang.String INCOMPATIBLE_STATISTICS
      Error message for an incompatible statistics.
      (package private) static java.util.function.IntConsumer INT_NOOP
      A no-operation int consumer.
      (package private) static java.util.function.LongConsumer LONG_NOOP
      A no-operation long consumer.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Statistics()
      No instances.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) static <T extends java.util.function.DoubleConsumer>
      T
      add​(T statistic, double[] values)
      Add all the values to the statistic.
      (package private) static <T extends java.util.function.DoubleConsumer>
      T
      add​(T statistic, double[] values, int from, int to)
      Add the specified range of values to the statistic.
      (package private) static <T extends java.util.function.DoubleConsumer>
      T
      add​(T statistic, int[] values)
      Add all the values to the statistic.
      (package private) static <T extends java.util.function.DoubleConsumer>
      T
      add​(T statistic, int[] values, int from, int to)
      Add the specified range of values to the statistic.
      (package private) static <T extends java.util.function.DoubleConsumer>
      T
      add​(T statistic, long[] values)
      Add all the values to the statistic.
      (package private) static <T extends java.util.function.DoubleConsumer>
      T
      add​(T statistic, long[] values, int from, int to)
      Add the specified range of values to the statistic.
      (package private) static void checkCombineAssignable​(FirstMoment a, FirstMoment b)
      Check left-hand side argument a is null or else the right-hand side argument b must be run-time assignable to the same class as a so the statistics can be combined.
      (package private) static <T extends StatisticResult & StatisticAccumulator<T>>
      void
      checkCombineCompatible​(T a, T b)
      Check left-hand side argument a is null or else the right-hand side argument b must also be non-null so the statistics can be combined.
      (package private) static void checkFromToIndex​(int fromIndex, int toIndex, int length)
      Checks if the sub-range from fromIndex (inclusive) to toIndex (exclusive) is within the bounds of range from 0 (inclusive) to length (exclusive).
      (package private) static <T extends StatisticResult & StatisticAccumulator<T>>
      void
      combine​(T a, T b)
      If the left-hand side argument a is non-null, combine it with the right-hand side argument b.
      (package private) static void combineMoment​(FirstMoment a, FirstMoment b)
      If the left-hand side argument a is non-null, combine it with the right-hand side argument b.
      (package private) static java.util.function.DoubleConsumer composeDoubleConsumers​(java.util.function.DoubleConsumer... consumers)
      Chain the consumers into a single composite consumer.
      (package private) static java.util.function.IntConsumer composeIntConsumers​(java.util.function.IntConsumer... consumers)
      Chain the consumers into a single composite consumer.
      (package private) static java.util.function.LongConsumer composeLongConsumers​(java.util.function.LongConsumer... consumers)
      Chain the consumers into a single composite consumer.
      (package private) static int[] copy​(int[] data, int from, int to)
      Copy the specified range of data.
      (package private) static StatisticResult getResultAsBigIntegerOrNull​(StatisticResult s)
      Gets the statistic result using the BigInteger value.
      (package private) static StatisticResult getResultAsDoubleOrNull​(StatisticResult s)
      Gets the statistic result using the double value.
      (package private) static StatisticResult getResultAsIntOrNull​(StatisticResult s)
      Gets the statistic result using the int value.
      (package private) static StatisticResult getResultAsLongOrNull​(StatisticResult s)
      Gets the statistic result using the long value.
      private static java.lang.String msgRangeOutOfBounds​(int fromIndex, int toIndex, int length)
      Format a message when range [from, to) is not entirely within the length.
      (package private) static org.apache.commons.numbers.core.Sum sum​(double[] values, int from, int to)
      Sum the specified range of values.
      (package private) static boolean zeroVariance​(double m1, double m2)
      Returns true if the second central moment m2 is effectively zero given the magnitude of the first raw moment m1.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DOUBLE_NOOP

        static final java.util.function.DoubleConsumer DOUBLE_NOOP
        A no-operation double consumer. This is exposed for testing.
      • INT_NOOP

        static final java.util.function.IntConsumer INT_NOOP
        A no-operation int consumer. This is exposed for testing.
      • LONG_NOOP

        static final java.util.function.LongConsumer LONG_NOOP
        A no-operation long consumer. This is exposed for testing.
      • INCOMPATIBLE_STATISTICS

        private static final java.lang.String INCOMPATIBLE_STATISTICS
        Error message for an incompatible statistics.
        See Also:
        Constant Field Values
    • Constructor Detail

      • Statistics

        private Statistics()
        No instances.
    • Method Detail

      • add

        static <T extends java.util.function.DoubleConsumer> T add​(T statistic,
                                                                   double[] values)
        Add all the values to the statistic.
        Type Parameters:
        T - Type of the statistic
        Parameters:
        statistic - Statistic.
        values - Values.
        Returns:
        the statistic
      • add

        static <T extends java.util.function.DoubleConsumer> T add​(T statistic,
                                                                   double[] values,
                                                                   int from,
                                                                   int to)
        Add the specified range of values to the statistic.

        Warning: No range checks are performed.

        Type Parameters:
        T - Type of the statistic
        Parameters:
        statistic - Statistic.
        values - Values.
        from - Inclusive start of the range.
        to - Exclusive end of the range.
        Returns:
        the statistic
      • add

        static <T extends java.util.function.DoubleConsumer> T add​(T statistic,
                                                                   int[] values)
        Add all the values to the statistic.
        Type Parameters:
        T - Type of the statistic
        Parameters:
        statistic - Statistic.
        values - Values.
        Returns:
        the statistic
      • add

        static <T extends java.util.function.DoubleConsumer> T add​(T statistic,
                                                                   int[] values,
                                                                   int from,
                                                                   int to)
        Add the specified range of values to the statistic.

        Warning: No range checks are performed.

        Type Parameters:
        T - Type of the statistic
        Parameters:
        statistic - Statistic.
        values - Values.
        from - Inclusive start of the range.
        to - Exclusive end of the range.
        Returns:
        the statistic
      • add

        static <T extends java.util.function.DoubleConsumer> T add​(T statistic,
                                                                   long[] values)
        Add all the values to the statistic.
        Type Parameters:
        T - Type of the statistic
        Parameters:
        statistic - Statistic.
        values - Values.
        Returns:
        the statistic
      • add

        static <T extends java.util.function.DoubleConsumer> T add​(T statistic,
                                                                   long[] values,
                                                                   int from,
                                                                   int to)
        Add the specified range of values to the statistic.

        Warning: No range checks are performed.

        Type Parameters:
        T - Type of the statistic
        Parameters:
        statistic - Statistic.
        values - Values.
        from - Inclusive start of the range.
        to - Exclusive end of the range.
        Returns:
        the statistic
      • add

        static <T extends java.util.function.IntConsumer> T add​(T statistic,
                                                                int[] values)
        Add all the values to the statistic.
        Type Parameters:
        T - Type of the statistic
        Parameters:
        statistic - Statistic.
        values - Values.
        Returns:
        the statistic
      • add

        static <T extends java.util.function.IntConsumer> T add​(T statistic,
                                                                int[] values,
                                                                int from,
                                                                int to)
        Add the specified range of values to the statistic.

        Warning: No range checks are performed.

        Type Parameters:
        T - Type of the statistic
        Parameters:
        statistic - Statistic.
        values - Values.
        from - Inclusive start of the range.
        to - Exclusive end of the range.
        Returns:
        the statistic
      • add

        static <T extends java.util.function.LongConsumer> T add​(T statistic,
                                                                 long[] values)
        Add all the values to the statistic.
        Type Parameters:
        T - Type of the statistic
        Parameters:
        statistic - Statistic.
        values - Values.
        Returns:
        the statistic
      • add

        static <T extends java.util.function.LongConsumer> T add​(T statistic,
                                                                 long[] values,
                                                                 int from,
                                                                 int to)
        Add the specified range of values to the statistic.

        Warning: No range checks are performed.

        Type Parameters:
        T - Type of the statistic
        Parameters:
        statistic - Statistic.
        values - Values.
        from - Inclusive start of the range.
        to - Exclusive end of the range.
        Returns:
        the statistic
      • zeroVariance

        static boolean zeroVariance​(double m1,
                                    double m2)
        Returns true if the second central moment m2 is effectively zero given the magnitude of the first raw moment m1.

        This method shares the logic for detecting a zero variance among implementations that divide by the variance (e.g. skewness, kurtosis).

        Parameters:
        m1 - First raw moment (mean).
        m2 - Second central moment (biased variance).
        Returns:
        true if the variance is zero
      • composeDoubleConsumers

        static java.util.function.DoubleConsumer composeDoubleConsumers​(java.util.function.DoubleConsumer... consumers)
        Chain the consumers into a single composite consumer. Ignore any null consumer. Returns null if all arguments are null.
        Parameters:
        consumers - Consumers.
        Returns:
        a composed consumer (or null)
      • composeIntConsumers

        static java.util.function.IntConsumer composeIntConsumers​(java.util.function.IntConsumer... consumers)
        Chain the consumers into a single composite consumer. Ignore any null consumer. Returns null if all arguments are null.
        Parameters:
        consumers - Consumers.
        Returns:
        a composed consumer (or null)
      • composeLongConsumers

        static java.util.function.LongConsumer composeLongConsumers​(java.util.function.LongConsumer... consumers)
        Chain the consumers into a single composite consumer. Ignore any null consumer. Returns null if all arguments are null.
        Parameters:
        consumers - Consumers.
        Returns:
        a composed consumer (or null)
      • getResultAsIntOrNull

        static StatisticResult getResultAsIntOrNull​(StatisticResult s)
        Gets the statistic result using the int value. Return null is the statistic is null.
        Parameters:
        s - Statistic.
        Returns:
        the result or null
      • getResultAsLongOrNull

        static StatisticResult getResultAsLongOrNull​(StatisticResult s)
        Gets the statistic result using the long value. Return null is the statistic is null.
        Parameters:
        s - Statistic.
        Returns:
        the result or null
      • getResultAsDoubleOrNull

        static StatisticResult getResultAsDoubleOrNull​(StatisticResult s)
        Gets the statistic result using the double value. Return null is the statistic is null.
        Parameters:
        s - Statistic.
        Returns:
        the result or null
      • getResultAsBigIntegerOrNull

        static StatisticResult getResultAsBigIntegerOrNull​(StatisticResult s)
        Gets the statistic result using the BigInteger value. Return null is the statistic is null.
        Parameters:
        s - Statistic.
        Returns:
        the result or null
      • checkCombineCompatible

        static <T extends StatisticResult & StatisticAccumulator<T>> void checkCombineCompatible​(T a,
                                                                                                 T b)
        Check left-hand side argument a is null or else the right-hand side argument b must also be non-null so the statistics can be combined.
        Type Parameters:
        T - StatisticResult being accumulated.
        Parameters:
        a - LHS.
        b - RHS.
        Throws:
        java.lang.IllegalArgumentException - if the objects cannot be combined
      • checkCombineAssignable

        static void checkCombineAssignable​(FirstMoment a,
                                           FirstMoment b)
        Check left-hand side argument a is null or else the right-hand side argument b must be run-time assignable to the same class as a so the statistics can be combined.
        Parameters:
        a - LHS.
        b - RHS.
        Throws:
        java.lang.IllegalArgumentException - if the objects cannot be combined
      • combine

        static <T extends StatisticResult & StatisticAccumulator<T>> void combine​(T a,
                                                                                  T b)
        If the left-hand side argument a is non-null, combine it with the right-hand side argument b.
        Type Parameters:
        T - StatisticResult being accumulated.
        Parameters:
        a - LHS.
        b - RHS.
      • checkFromToIndex

        static void checkFromToIndex​(int fromIndex,
                                     int toIndex,
                                     int length)
        Checks if the sub-range from fromIndex (inclusive) to toIndex (exclusive) is within the bounds of range from 0 (inclusive) to length (exclusive).

        This function provides the functionality of java.utils.Objects.checkFromToIndex introduced in JDK 9. The Objects javadoc has been reproduced for reference. The return value has been changed to void.

        The sub-range is defined to be out of bounds if any of the following inequalities is true:

        • fromIndex < 0
        • fromIndex > toIndex
        • toIndex > length
        • length < 0, which is implied from the former inequalities
        Parameters:
        fromIndex - Lower-bound (inclusive) of the sub-range.
        toIndex - Upper-bound (exclusive) of the sub-range.
        length - Upper-bound (exclusive) of the range.
        Throws:
        java.lang.IndexOutOfBoundsException - if the sub-range is out of bounds
      • msgRangeOutOfBounds

        private static java.lang.String msgRangeOutOfBounds​(int fromIndex,
                                                            int toIndex,
                                                            int length)
        Format a message when range [from, to) is not entirely within the length.
        Parameters:
        fromIndex - Lower-bound (inclusive) of the sub-range.
        toIndex - Upper-bound (exclusive) of the sub-range.
        length - Upper-bound (exclusive) of the range.
        Returns:
        the message
      • sum

        static org.apache.commons.numbers.core.Sum sum​(double[] values,
                                                       int from,
                                                       int to)
        Sum the specified range of values.

        Warning: No range checks are performed.

        Parameters:
        values - Values.
        from - Inclusive start of the range.
        to - Exclusive end of the range.
        Returns:
        the sum
      • copy

        static int[] copy​(int[] data,
                          int from,
                          int to)
        Copy the specified range of data.

        This is a simplification of Arrays#copyOfRange(double[], int, int) and does not support range checks or padding of the original input to a longer output.

        Parameters:
        data - Values.
        from - Inclusive start of the range.
        to - Exclusive end of the range.
        Returns:
        the copy