Class VarianceCalculatorDouble
Object
VarianceCalculatorDouble
Calculates variance efficiently, as successive values are added, using
double to store
sums internally.
This is similar to VarianceCalculatorLong but is designed for double values
rather than ints.
Efficiency occurs by maintaining a running sum, sum-of-squares and count as values are added.
This is useful for calculating variance in sequential order in an online manner.
- Author:
- Owen Feehan
-
Constructor Summary
ConstructorsConstructorDescriptionVarianceCalculatorDouble(double sum, double sumSquares, long count) Creates a newVarianceCalculatorDoubleinstance. -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(double value) Adds a value to the running sum.longgetCount()The running count of values.doublemean()Calculate the mean, based on the current state of running-sums.subtract(VarianceCalculatorDouble toSubtract) Subtracts the running-sums and count from anotherVarianceCalculatorDoublefrom the current object's state.doublevariance()Calculate the variance, based on the current state of running-sums.
-
Constructor Details
-
VarianceCalculatorDouble
public VarianceCalculatorDouble(double sum, double sumSquares, long count) Creates a newVarianceCalculatorDoubleinstance.- Parameters:
sum- The running sum of values.sumSquares- The running sum of squares of values.count- The running count of values.
-
VarianceCalculatorDouble
public VarianceCalculatorDouble()
-
-
Method Details
-
add
public void add(double value) Adds a value to the running sum.- Parameters:
value- the value to add.
-
subtract
Subtracts the running-sums and count from anotherVarianceCalculatorDoublefrom the current object's state.This occurs immutably, and the currently object's state is unaffected.
- Parameters:
toSubtract- the otherVarianceCalculatorDoublewhose state is subtracted.- Returns:
- a newly created
VarianceCalculatorDoublewhose state is the current object minustoSubtract.
-
mean
public double mean()Calculate the mean, based on the current state of running-sums.- Returns:
- the mean.
-
variance
public double variance()Calculate the variance, based on the current state of running-sums.- Returns:
- the variance.
-
getCount
public long getCount()The running count of values.
-