Class Kurtosis

  • All Implemented Interfaces:
    java.util.function.DoubleConsumer, java.util.function.DoubleSupplier, java.util.function.IntSupplier, java.util.function.LongSupplier, DoubleStatistic, StatisticAccumulator<Kurtosis>, StatisticResult

    public final class Kurtosis
    extends java.lang.Object
    implements DoubleStatistic, StatisticAccumulator<Kurtosis>
    Computes the kurtosis of the available values. The kurtosis is defined as:

    \[ \operatorname{Kurt} = \operatorname{E}\left[ \left(\frac{X-\mu}{\sigma}\right)^4 \right] = \frac{\mu_4}{\sigma^4} \]

    where \( \mu \) is the mean of \( X \), \( \sigma \) is the standard deviation of \( X \), \( \operatorname{E} \) represents the expectation operator, and \( \mu_4 \) is the fourth central moment.

    The default implementation uses the following definition of the sample kurtosis:

    \[ G_2 = \frac{k_4}{k_2^2} = \; \frac{n-1}{(n-2)\,(n-3)} \left[(n+1)\,\frac{m_4}{m_{2}^2} - 3\,(n-1) \right] \]

    where \( k_4 \) is the unique symmetric unbiased estimator of the fourth cumulant, \( k_2 \) is the symmetric unbiased estimator of the second cumulant (i.e. the sample variance), \( m_4 \) is the fourth sample moment about the mean, \( m_2 \) is the second sample moment about the mean, \( \overline{x} \) is the sample mean, and \( n \) is the number of samples.

    • The result is NaN if less than 4 values are added.
    • The result is NaN if any of the values is NaN or infinite.
    • The result is NaN if the sum of the fourth deviations from the mean is infinite.

    The default computation is for the adjusted Fisher–Pearson standardized moment coefficient \( G_2 \). If the biased option is enabled the following equation applies:

    \[ g_2 = \frac{m_4}{m_2^2} - 3 = \frac{\tfrac{1}{n} \sum_{i=1}^n (x_i-\overline{x})^4} {\left[\tfrac{1}{n} \sum_{i=1}^n (x_i-\overline{x})^2 \right]^2} - 3 \]

    In this case the computation only requires 2 values are added (i.e. the result is NaN if less than 2 values are added).

    Note that the computation requires division by the second central moment \( m_2 \). If this is effectively zero then the result is NaN. This occurs when the value \( m_2 \) approaches the machine precision of the mean: \( m_2 \le (m_1 \times 10^{-15})^2 \).

    The accept(double) method uses a recursive updating algorithm.

    The of(double...) method uses a two-pass algorithm, starting with computation of the mean, and then computing the sum of deviations in a second pass.

    Note that adding values using accept and then executing getAsDouble will sometimes give a different result than executing of with the full array of values. The former approach should only be used when the full array of values is not available.

    Supports up to 263 (exclusive) observations. This implementation does not check for overflow of the count.

    This class is designed to work with (though does not require) streams.

    Note that this instance is not synchronized. If multiple threads access an instance of this class concurrently, and at least one of the threads invokes the accept or combine method, it must be synchronized externally.

    However, it is safe to use accept and combine as accumulator and combiner functions of Collector on a parallel stream, because the parallel instance of Stream.collect() provides the necessary partitioning, isolation, and merging of results for safe and efficient parallel execution.

    Since:
    1.1
    See Also:
    Kurtosis (Wikipedia)
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private boolean biased
      Flag to control if the statistic is biased, or should use a bias correction.
      private static int LENGTH_FOUR
      4, the length limit where the kurtosis is undefined.
      private static int LENGTH_TWO
      2, the length limit where the biased skewness is undefined.
      private SumOfFourthDeviations sq
      An instance of SumOfFourthDeviations, which is used to compute the kurtosis.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Kurtosis()
      Create an instance.
      (package private) Kurtosis​(SumOfFourthDeviations sq)
      Creates an instance with the sum of fourth deviations from the mean.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void accept​(double value)
      Updates the state of the statistic to reflect the addition of value.
      Kurtosis combine​(Kurtosis other)
      Combines the state of the other statistic into this one.
      static Kurtosis create()
      Creates an instance.
      double getAsDouble()
      Gets the kurtosis of all input values.
      static Kurtosis of​(double... values)
      Returns an instance populated using the input values.
      static Kurtosis of​(int... values)
      Returns an instance populated using the input values.
      static Kurtosis of​(long... values)
      Returns an instance populated using the input values.
      Kurtosis setBiased​(boolean v)
      Sets the value of the biased flag.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.function.DoubleConsumer

        andThen
    • Field Detail

      • LENGTH_TWO

        private static final int LENGTH_TWO
        2, the length limit where the biased skewness is undefined. This limit effectively imposes the result m4 / m2^2 = 0 / 0 = NaN when 1 value has been added. Note that when more samples are added and the variance approaches zero the result is also returned as NaN.
        See Also:
        Constant Field Values
      • LENGTH_FOUR

        private static final int LENGTH_FOUR
        4, the length limit where the kurtosis is undefined.
        See Also:
        Constant Field Values
      • biased

        private boolean biased
        Flag to control if the statistic is biased, or should use a bias correction.
    • Constructor Detail

      • Kurtosis

        private Kurtosis()
        Create an instance.
      • Kurtosis

        Kurtosis​(SumOfFourthDeviations sq)
        Creates an instance with the sum of fourth deviations from the mean.
        Parameters:
        sq - Sum of fourth deviations.
    • Method Detail

      • create

        public static Kurtosis create()
        Creates an instance.

        The initial result is NaN.

        Returns:
        Kurtosis instance.
      • of

        public static Kurtosis of​(double... values)
        Returns an instance populated using the input values.

        Note: Kurtosis computed using accept may be different from this instance.

        Parameters:
        values - Values.
        Returns:
        Kurtosis instance.
      • of

        public static Kurtosis of​(int... values)
        Returns an instance populated using the input values.

        Note: Kurtosis computed using accept may be different from this instance.

        Parameters:
        values - Values.
        Returns:
        Kurtosis instance.
      • of

        public static Kurtosis of​(long... values)
        Returns an instance populated using the input values.

        Note: Kurtosis computed using accept may be different from this instance.

        Parameters:
        values - Values.
        Returns:
        Kurtosis instance.
      • accept

        public void accept​(double value)
        Updates the state of the statistic to reflect the addition of value.
        Specified by:
        accept in interface java.util.function.DoubleConsumer
        Parameters:
        value - Value.
      • getAsDouble

        public double getAsDouble()
        Gets the kurtosis of all input values.

        When fewer than 4 values have been added, the result is NaN.

        Specified by:
        getAsDouble in interface java.util.function.DoubleSupplier
        Returns:
        kurtosis of all values.
      • setBiased

        public Kurtosis setBiased​(boolean v)
        Sets the value of the biased flag. The default value is false. See Kurtosis for details on the computing algorithm.

        This flag only controls the final computation of the statistic. The value of this flag will not affect compatibility between instances during a combine operation.

        Parameters:
        v - Value.
        Returns:
        this instance