Class SumOfSquaredDeviations
- java.lang.Object
-
- org.apache.commons.statistics.descriptive.FirstMoment
-
- org.apache.commons.statistics.descriptive.SumOfSquaredDeviations
-
- All Implemented Interfaces:
java.util.function.DoubleConsumer
- Direct Known Subclasses:
SumOfCubedDeviations
class SumOfSquaredDeviations extends FirstMoment
Computes the sum of squared deviations from the sample mean. This statistic is related to the second moment.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
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:
- 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
Fields Modifier and Type Field Description protected doublesumSquaredDevSum 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
Constructors Modifier Constructor Description (package private)SumOfSquaredDeviations()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)SumOfSquaredDeviations(SumOfSquaredDeviations source)Copy constructor.
-
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) SumOfSquaredDeviationscombine(SumOfSquaredDeviations other)Combines the state of anotherSumOfSquaredDeviationsinto this one.private static SumOfSquaredDeviationscreate(FirstMoment m1, double[] values, int from, int to)Creates the sum of squared deviations.(package private) static SumOfSquaredDeviationscreateFromRange(org.apache.commons.numbers.core.Sum sum, double[] values, int from, int to)Creates the sum of squared deviations.(package private) doublegetSumOfSquaredDeviations()Gets the sum of squared deviations of all input values.(package private) static SumOfSquaredDeviationsof(double... values)Returns an instance populated using the inputvalues.(package private) static SumOfSquaredDeviationsofRange(double[] values, int from, int to)Returns an instance populated using the specified range ofvalues.-
Methods inherited from class org.apache.commons.statistics.descriptive.FirstMoment
combine, getFirstMoment, getFirstMomentDifference, getFirstMomentHalfDifference
-
-
-
-
Constructor Detail
-
SumOfSquaredDeviations
SumOfSquaredDeviations()
Create an instance.
-
SumOfSquaredDeviations
SumOfSquaredDeviations(SumOfSquaredDeviations source)
Copy constructor.- Parameters:
source- Source to copy.
-
SumOfSquaredDeviations
private SumOfSquaredDeviations(double sumSquaredDev, FirstMoment m1)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 Detail
-
of
static SumOfSquaredDeviations of(double... values)
Returns an instance populated using the inputvalues.Note:
SumOfSquaredDeviationscomputed usingacceptmay be different from this instance.- Parameters:
values- Values.- Returns:
SumOfSquaredDeviationsinstance.
-
ofRange
static SumOfSquaredDeviations ofRange(double[] values, int from, int to)
Returns an instance populated using the specified range ofvalues.Note:
SumOfSquaredDeviationscomputed usingacceptmay be different from this instance.Warning: No range checks are performed.
- Parameters:
values- Values.from- Inclusive start of the range.to- Exclusive end of the range.- Returns:
SumOfSquaredDeviationsinstance.
-
createFromRange
static SumOfSquaredDeviations createFromRange(org.apache.commons.numbers.core.Sum sum, double[] values, int from, int to)
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.Warning: No range checks are performed.
- Parameters:
sum- Sum of the values.values- Values.from- Inclusive start of the range.to- Exclusive end of the range.- Returns:
SumOfSquaredDeviationsinstance.
-
create
private static SumOfSquaredDeviations create(FirstMoment m1, double[] values, int from, int to)
Creates the sum of squared deviations.- Parameters:
m1- First moment.values- Values.from- Inclusive start of the range.to- Exclusive end of the range.- Returns:
SumOfSquaredDeviationsinstance.
-
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 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
SumOfSquaredDeviations combine(SumOfSquaredDeviations other)
Combines the state of anotherSumOfSquaredDeviationsinto this one.- Parameters:
other- AnotherSumOfSquaredDeviationsto be combined.- Returns:
thisinstance after combiningother.
-
-