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 doubleSum 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.privateSumOfSquaredDeviations(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 TypeMethodDescriptionvoidaccept(double value) Updates the state of the statistic to reflect the addition ofvalue.(package private) SumOfSquaredDeviationscombine(SumOfSquaredDeviations other) Combines the state of anotherSumOfSquaredDeviationsinto this one.(package private) static SumOfSquaredDeviationscreate(org.apache.commons.numbers.core.Sum sum, double[] values) Creates the sum of squared deviations.private static SumOfSquaredDeviationscreate(FirstMoment m1, double[] values) Creates the sum of squared deviations.(package private) doubleGets the sum of squared deviations of all input values.(package private) static SumOfSquaredDeviationsof(double... values) Returns an instance populated using the inputvalues.Methods inherited from class org.apache.commons.statistics.descriptive.FirstMoment
combine, getFirstMoment, getFirstMomentDifference, getFirstMomentHalfDifferenceMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods 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:
SumOfSquaredDeviationscomputed usingacceptmay be different from this instance.- Parameters:
values- Values.- Returns:
SumOfSquaredDeviationsinstance.
-
create
Creates the sum of squared 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:
SumOfSquaredDeviationsinstance.
-
create
Creates the sum of squared deviations.- Parameters:
m1- First moment.values- Values.- Returns:
SumOfSquaredDeviationsinstance.
-
accept
public void accept(double value) Updates the state of the statistic to reflect the addition ofvalue.- Specified by:
acceptin interfaceDoubleConsumer- Overrides:
acceptin 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 anotherSumOfSquaredDeviationsinto this one.- Parameters:
other- AnotherSumOfSquaredDeviationsto be combined.- Returns:
thisinstance after combiningother.
-