Class SumOfSquaredDeviations
- All Implemented Interfaces:
DoubleConsumer
- Direct Known Subclasses:
SumOfCubedDeviations
The following recursive updating formula is used:
Let
- dev = (current obs - previous mean)
- n = number of observations (including current obs)
Then
new value = old value + dev^2 * (n - 1) / n
returns the sum of squared deviations of all values seen so far.
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:
- Chan, Golub and Levesque (1983) Algorithms for Computing the Sample Variance: Analysis and Recommendations. American Statistician, 37, 242-247. doi: 10.2307/2683386
- Since:
- 1.1
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected double
Sum of squared deviations of the values that have been added.Fields inherited from class org.apache.commons.statistics.descriptive.FirstMoment
dev, n, nDev
-
Constructor Summary
ConstructorsModifierConstructorDescription(package private)
Create an instance.(package private)
SumOfSquaredDeviations
(double sumSquaredDev, double m1, long n) Create an instance with the given sum of squared deviations and first moment.private
SumOfSquaredDeviations
(double sumSquaredDev, FirstMoment m1) Create an instance with the given sum of squared deviations and first moment.(package private)
Copy constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
accept
(double value) Updates the state of the statistic to reflect the addition ofvalue
.(package private) SumOfSquaredDeviations
combine
(SumOfSquaredDeviations other) Combines the state of anotherSumOfSquaredDeviations
into this one.(package private) static SumOfSquaredDeviations
create
(org.apache.commons.numbers.core.Sum sum, double[] values) Creates the sum of squared deviations.private static SumOfSquaredDeviations
create
(FirstMoment m1, double[] values) Creates the sum of squared deviations.(package private) double
Gets the sum of squared deviations of all input values.(package private) static SumOfSquaredDeviations
of
(double... values) Returns an instance populated using the inputvalues
.Methods inherited from class org.apache.commons.statistics.descriptive.FirstMoment
combine, getFirstMoment, getFirstMomentDifference, getFirstMomentHalfDifference
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 Details
-
sumSquaredDev
protected double sumSquaredDevSum of squared deviations of the values that have been added.
-
-
Constructor Details
-
SumOfSquaredDeviations
SumOfSquaredDeviations()Create an instance. -
SumOfSquaredDeviations
SumOfSquaredDeviations(SumOfSquaredDeviations source) Copy constructor.- Parameters:
source
- Source to copy.
-
SumOfSquaredDeviations
Create an instance with the given sum of squared deviations and first moment.- Parameters:
sumSquaredDev
- Sum of squared deviations.m1
- First moment.
-
SumOfSquaredDeviations
SumOfSquaredDeviations(double sumSquaredDev, double m1, long n) Create an instance with the given sum of squared deviations and first moment.This constructor is used when creating the moment from integer values.
- Parameters:
sumSquaredDev
- Sum of squared deviations.m1
- First moment.n
- Count of values.
-
-
Method Details
-
of
Returns an instance populated using the inputvalues
.Note:
SumOfSquaredDeviations
computed usingaccept
may be different from this instance.- Parameters:
values
- Values.- Returns:
SumOfSquaredDeviations
instance.
-
create
Creates the sum of squared deviations.Uses the provided
sum
to create the first moment. This method is used byDoubleStatistics
using a sum that can be reused for theSum
statistic.- Parameters:
sum
- Sum of the values.values
- Values.- Returns:
SumOfSquaredDeviations
instance.
-
create
Creates the sum of squared deviations.- Parameters:
m1
- First moment.values
- Values.- Returns:
SumOfSquaredDeviations
instance.
-
accept
public void accept(double value) Updates the state of the statistic to reflect the addition ofvalue
.- Specified by:
accept
in interfaceDoubleConsumer
- Overrides:
accept
in classFirstMoment
- Parameters:
value
- Value.
-
getSumOfSquaredDeviations
double getSumOfSquaredDeviations()Gets the sum of squared deviations of all input values.- Returns:
- sum of squared deviations of all values.
-
combine
Combines the state of anotherSumOfSquaredDeviations
into this one.- Parameters:
other
- AnotherSumOfSquaredDeviations
to be combined.- Returns:
this
instance after combiningother
.
-