Class SumOfFourthDeviations

All Implemented Interfaces:
DoubleConsumer

class SumOfFourthDeviations extends SumOfCubedDeviations
Computes the sum of fourth deviations from the sample mean. This statistic is related to the fourth moment.

Uses a recursive updating formula as defined in Manca and Marin (2010), equation 16. Note that third term in that equation has been corrected by expansion of the same term from equation 15. Two sum of fourth (quad) deviations (Sq) can be combined using:

\[ Sq(X) = {Sq}_1 + {Sq}_2 + \frac{4(m_1 - m_2)(g_1 - g_2) N_1 N_2}{N_1 + N_2} + \frac{6(m_1 - m_2)^2(N_2^2 ss_1 + N_1^2 ss_2)}{(N_1 + N_2)^2} + \frac{(m_1 - m_2)^4((N_1^2 - N_1 N_2 + N_2^2) N_1 N_2}{(N_1 + N_2)^3} \]

where \( N \) is the group size, \( m \) is the mean, \( ss \) is the sum of squared deviations from the mean, and \( g \) is the asymmetrical index where \( g * N \) is the sum of fourth deviations from the mean. Note the term \( ({g_1} - {g_2}) N_1 N_2 == (sc_1 * N_2 - sc_2 * N_1 \) where \( sc \) is the sum of fourth deviations.

If \( N_1 \) is size 1 this reduces to:

\[ SC_{N+1} = {SC}_N + \frac{4(x - m) -sc}{N + 1} + \frac{6(x - m)^2 ss}{(N + 1)^2} + \frac{(x - m)^4((1 - N + N^2) N}{(N + 1)^3} \]

where \( ss \) is the sum of squared deviations, and \( sc \) is the sum of fourth deviations. This updating formula is identical to that used in org.apache.commons.math3.stat.descriptive.moment.FourthMoment. The final term uses a rearrangement \( (1 - N + N^2) = (N+1)^2 - 3N \).

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

Note that this implementation 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 implementation of Stream.collect() provides the necessary partitioning, isolation, and merging of results for safe and efficient parallel execution.

References:

  • Manca and Marin (2020) Decomposition of the Sum of Cubes, the Sum Raised to the Power of Four and Codeviance. Applied Mathematics, 11, 1013-1020. doi: 10.4236/am.2020.1110067
Since:
1.1
  • Field Details

    • sumFourthDev

      private double sumFourthDev
      Sum of forth deviations of the values that have been added.
  • Constructor Details

    • SumOfFourthDeviations

      SumOfFourthDeviations()
      Create an instance.
    • SumOfFourthDeviations

      private SumOfFourthDeviations(double sq, SumOfCubedDeviations sc)
      Create an instance with the given sum of fourth and squared deviations.
      Parameters:
      sq - Sum of fourth (quad) deviations.
      sc - Sum of fourth deviations.
    • SumOfFourthDeviations

      private SumOfFourthDeviations(double sq, double sc, double ss, double m1, long n)
      Create an instance with the given sum of cubed and squared deviations, and first moment.
      Parameters:
      sq - Sum of fouth deviations.
      sc - Sum of cubed deviations.
      ss - Sum of squared deviations.
      m1 - First moment.
      n - Count of values.
  • Method Details

    • of

      static SumOfFourthDeviations of(double... values)
      Returns an instance populated using the input values.

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

      Parameters:
      values - Values.
      Returns:
      SumOfFourthDeviations instance.
    • create

      static SumOfFourthDeviations create(org.apache.commons.numbers.core.Sum sum, double[] values)
      Creates the sum of fourth deviations.

      Uses the provided sum to create the first moment. This method is used by DoubleStatistics using a sum that can be reused for the Sum statistic.

      Parameters:
      sum - Sum of the values.
      values - Values.
      Returns:
      SumOfFourthDeviations instance.
    • create

      private static SumOfFourthDeviations create(SumOfCubedDeviations sc, double[] values)
      Creates the sum of fourth deviations.
      Parameters:
      sc - Sum of cubed deviations.
      values - Values.
      Returns:
      SumOfFourthDeviations instance.
    • pow4

      private static double pow4(double x)
      Compute x^4. Uses compound multiplication.
      Parameters:
      x - Value.
      Returns:
      x^4
    • of

      static SumOfFourthDeviations of(int... values)
      Returns an instance populated using the input values.

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

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

      static SumOfFourthDeviations of(long... values)
      Returns an instance populated using the input values.

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

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

      public void accept(double value)
      Updates the state of the statistic to reflect the addition of value.
      Specified by:
      accept in interface DoubleConsumer
      Overrides:
      accept in class SumOfCubedDeviations
      Parameters:
      value - Value.
    • getSumOfFourthDeviations

      double getSumOfFourthDeviations()
      Gets the sum of fourth deviations of all input values.

      Note that the result should be positive. However the updating sum is subject to cancellation of potentially large positive and negative terms. Overflow of these terms can result in a sum of opposite signed infinities and a NaN result for finite input values where the correct result is positive infinity.

      Note: Any non-finite result should be considered a failed computation. The result is returned as computed and not consolidated to a single NaN. This is done for testing purposes to allow the result to be reported. It is possible to track input values to finite/non-finite (e.g. using bit mask manipulation of the exponent field). However this statistic in currently used in the kurtosis and in the case of failed computation distinguishing a non-finite result is not useful.

      Returns:
      sum of fourth deviations of all values.
    • combine

      Combines the state of another SumOfFourthDeviations into this one.
      Parameters:
      other - Another SumOfFourthDeviations to be combined.
      Returns:
      this instance after combining other.