Class Skewness
- All Implemented Interfaces:
DoubleConsumer, DoubleSupplier, IntSupplier, LongSupplier, DoubleStatistic, StatisticAccumulator<Skewness>, StatisticResult
\[ \gamma_1 = \operatorname{E}\left[ \left(\frac{X-\mu}{\sigma}\right)^3 \right] = \frac{\mu_3}{\sigma^3} \]
where \( \mu \) is the mean of \( X \), \( \sigma \) is the standard deviation of \( X \), \( \operatorname{E} \) represents the expectation operator, and \( \mu_3 \) is the third central moment.
The default implementation uses the following definition of the sample skewness:
\[ G_1 = \frac{k_3}{k_2^{3/2}} = \frac{\sqrt{n(n-1)}}{n-2}\; g_1 = \frac{n^2}{(n-1)(n-2)}\; \frac{\tfrac{1}{n} \sum_{i=1}^n (x_i-\overline{x})^3} {\left[\tfrac{1}{n-1} \sum_{i=1}^n (x_i-\overline{x})^2 \right]^{3/2}} \]
where \( k_3 \) is the unique symmetric unbiased estimator of the third cumulant, \( k_2 \) is the symmetric unbiased estimator of the second cumulant (i.e. the sample variance), \( g_1 \) is a method of moments estimator (see below), \( \overline{x} \) is the sample mean, and \( n \) is the number of samples.
- The result is
NaNif less than 3 values are added. - The result is
NaNif any of the values isNaNor infinite. - The result is
NaNif the sum of the cubed deviations from the mean is infinite.
The default computation is for the adjusted Fisher–Pearson standardized moment coefficient
\( G_1 \). If the biased option is enabled the following equation
applies:
\[ g_1 = \frac{m_3}{m_2^{3/2}} = \frac{\tfrac{1}{n} \sum_{i=1}^n (x_i-\overline{x})^3} {\left[\tfrac{1}{n} \sum_{i=1}^n (x_i-\overline{x})^2 \right]^{3/2}} \]
where \( g_2 \) is a method of moments estimator, \( m_3 \) is the (biased) sample third central moment and \( m_2^{3/2} \) is the (biased) sample second central moment.
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:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanFlag to control if the statistic is biased, or should use a bias correction.private static final int3, the length limit where the unbiased skewness is undefined.private static final int2, the length limit where the biased skewness is undefined.private final SumOfCubedDeviationsAn instance ofSumOfCubedDeviations, which is used to compute the skewness. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateSkewness()Create an instance.(package private)Creates an instance with the sum of cubed deviations from the mean. -
Method Summary
Modifier and TypeMethodDescriptionvoidaccept(double value) Updates the state of the statistic to reflect the addition ofvalue.Combines the state of theotherstatistic into this one.static Skewnesscreate()Creates an instance.doubleGets the skewness of all input values.static Skewnessof(double... values) Returns an instance populated using the inputvalues.static Skewnessof(int... values) Returns an instance populated using the inputvalues.static Skewnessof(long... values) Returns an instance populated using the inputvalues.static SkewnessofRange(double[] values, int from, int to) Returns an instance populated using the specified range ofvalues.static SkewnessofRange(int[] values, int from, int to) Returns an instance populated using the specified range ofvalues.static SkewnessofRange(long[] values, int from, int to) Returns an instance populated using the specified range ofvalues.setBiased(boolean v) Sets the value of the biased flag.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface DoubleConsumer
andThenMethods inherited from interface StatisticResult
getAsBigInteger, getAsInt, getAsLong
-
Field Details
-
LENGTH_TWO
private static final int LENGTH_TWO2, the length limit where the biased skewness is undefined. This limit effectively imposes the result m3 / m2^1.5 = 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:
-
LENGTH_THREE
private static final int LENGTH_THREE3, the length limit where the unbiased skewness is undefined.- See Also:
-
sc
An instance ofSumOfCubedDeviations, which is used to compute the skewness. -
biased
private boolean biasedFlag to control if the statistic is biased, or should use a bias correction.
-
-
Constructor Details
-
Skewness
private Skewness()Create an instance. -
Skewness
Skewness(SumOfCubedDeviations sc) Creates an instance with the sum of cubed deviations from the mean.- Parameters:
sc- Sum of cubed deviations.
-
-
Method Details
-
create
Creates an instance.The initial result is
NaN.- Returns:
Skewnessinstance.
-
of
-
ofRange
Returns an instance populated using the specified range ofvalues.Note:
Skewnesscomputed usingacceptmay be different from this instance.- Parameters:
values- Values.from- Inclusive start of the range.to- Exclusive end of the range.- Returns:
Skewnessinstance.- Throws:
IndexOutOfBoundsException- if the sub-range is out of bounds- Since:
- 1.2
-
of
-
ofRange
Returns an instance populated using the specified range ofvalues.Note:
Skewnesscomputed usingacceptmay be different from this instance.- Parameters:
values- Values.from- Inclusive start of the range.to- Exclusive end of the range.- Returns:
Skewnessinstance.- Throws:
IndexOutOfBoundsException- if the sub-range is out of bounds- Since:
- 1.2
-
of
-
ofRange
Returns an instance populated using the specified range ofvalues.Note:
Skewnesscomputed usingacceptmay be different from this instance.- Parameters:
values- Values.from- Inclusive start of the range.to- Exclusive end of the range.- Returns:
Skewnessinstance.- Throws:
IndexOutOfBoundsException- if the sub-range is out of bounds- Since:
- 1.2
-
accept
public void accept(double value) Updates the state of the statistic to reflect the addition ofvalue.- Specified by:
acceptin interfaceDoubleConsumer- Parameters:
value- Value.
-
getAsDouble
public double getAsDouble()Gets the skewness of all input values.When fewer than 3 values have been added, the result is
NaN.- Specified by:
getAsDoublein interfaceDoubleSupplier- Returns:
- skewness of all values.
-
combine
Description copied from interface:StatisticAccumulatorCombines the state of theotherstatistic into this one.- Specified by:
combinein interfaceStatisticAccumulator<Skewness>- Parameters:
other- Another statistic to be combined.- Returns:
thisinstance after combiningother.
-
setBiased
Sets the value of the biased flag. The default value isfalse. SeeSkewnessfor 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
combineoperation.- Parameters:
v- Value.- Returns:
thisinstance
-