Class SumOfFourthDeviations
- java.lang.Object
-
- org.apache.commons.statistics.descriptive.FirstMoment
-
- org.apache.commons.statistics.descriptive.SumOfSquaredDeviations
-
- org.apache.commons.statistics.descriptive.SumOfCubedDeviations
-
- org.apache.commons.statistics.descriptive.SumOfFourthDeviations
-
- All Implemented Interfaces:
java.util.function.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
acceptorcombinemethod, it must be synchronized externally.However, it is safe to use
acceptandcombineasaccumulatorandcombinerfunctions ofCollectoron a parallel stream, because the parallel implementation ofStream.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 Summary
Fields Modifier and Type Field Description private doublesumFourthDevSum of forth deviations of the values that have been added.-
Fields inherited from class org.apache.commons.statistics.descriptive.SumOfCubedDeviations
LENGTH_TWO, sumCubedDev
-
Fields inherited from class org.apache.commons.statistics.descriptive.SumOfSquaredDeviations
sumSquaredDev
-
Fields inherited from class org.apache.commons.statistics.descriptive.FirstMoment
dev, n, nDev
-
-
Constructor Summary
Constructors Modifier Constructor Description (package private)SumOfFourthDeviations()Create an instance.privateSumOfFourthDeviations(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.privateSumOfFourthDeviations(double sq, SumOfCubedDeviations sc)Create an instance with the given sum of fourth and squared deviations.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaccept(double value)Updates the state of the statistic to reflect the addition ofvalue.(package private) SumOfFourthDeviationscombine(SumOfFourthDeviations other)Combines the state of anotherSumOfFourthDeviationsinto this one.(package private) static SumOfFourthDeviationscreate(org.apache.commons.numbers.core.Sum sum, double[] values)Creates the sum of fourth deviations.private static SumOfFourthDeviationscreate(SumOfCubedDeviations sc, double[] values)Creates the sum of fourth deviations.(package private) doublegetSumOfFourthDeviations()Gets the sum of fourth deviations of all input values.(package private) static SumOfFourthDeviationsof(double... values)Returns an instance populated using the inputvalues.(package private) static SumOfFourthDeviationsof(int... values)Returns an instance populated using the inputvalues.(package private) static SumOfFourthDeviationsof(long... values)Returns an instance populated using the inputvalues.private static doublepow4(double x)Computex^4.-
Methods inherited from class org.apache.commons.statistics.descriptive.SumOfCubedDeviations
combine, getSumOfCubedDeviations
-
Methods inherited from class org.apache.commons.statistics.descriptive.SumOfSquaredDeviations
combine, getSumOfSquaredDeviations
-
Methods inherited from class org.apache.commons.statistics.descriptive.FirstMoment
combine, getFirstMoment, getFirstMomentDifference, getFirstMomentHalfDifference
-
-
-
-
Constructor Detail
-
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 Detail
-
of
static SumOfFourthDeviations of(double... values)
Returns an instance populated using the inputvalues.Note:
SumOfFourthDeviationscomputed usingacceptmay be different from this instance.- Parameters:
values- Values.- Returns:
SumOfFourthDeviationsinstance.
-
create
static SumOfFourthDeviations create(org.apache.commons.numbers.core.Sum sum, double[] values)
Creates the sum of fourth deviations.Uses the provided
sumto create the first moment. This method is used byDoubleStatisticsusing a sum that can be reused for theSumstatistic.- Parameters:
sum- Sum of the values.values- Values.- Returns:
SumOfFourthDeviationsinstance.
-
create
private static SumOfFourthDeviations create(SumOfCubedDeviations sc, double[] values)
Creates the sum of fourth deviations.- Parameters:
sc- Sum of cubed deviations.values- Values.- Returns:
SumOfFourthDeviationsinstance.
-
pow4
private static double pow4(double x)
Computex^4. Uses compound multiplication.- Parameters:
x- Value.- Returns:
- x^4
-
of
static SumOfFourthDeviations of(int... values)
Returns an instance populated using the inputvalues.Note:
SumOfCubedDeviationscomputed usingacceptmay be different from this instance.- Parameters:
values- Values.- Returns:
SumOfCubedDeviationsinstance.
-
of
static SumOfFourthDeviations of(long... values)
Returns an instance populated using the inputvalues.Note:
SumOfCubedDeviationscomputed usingacceptmay be different from this instance.- Parameters:
values- Values.- Returns:
SumOfCubedDeviationsinstance.
-
accept
public void accept(double value)
Updates the state of the statistic to reflect the addition ofvalue.- Specified by:
acceptin interfacejava.util.function.DoubleConsumer- Overrides:
acceptin classSumOfCubedDeviations- 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
NaNresult 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
SumOfFourthDeviations combine(SumOfFourthDeviations other)
Combines the state of anotherSumOfFourthDeviationsinto this one.- Parameters:
other- AnotherSumOfFourthDeviationsto be combined.- Returns:
thisinstance after combiningother.
-
-