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 int
s.
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 newVarianceCalculatorDouble
instance. -
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(double value) Adds a value to the running sum.long
getCount()
The running count of values.double
mean()
Calculate the mean, based on the current state of running-sums.subtract
(VarianceCalculatorDouble toSubtract) Subtracts the running-sums and count from anotherVarianceCalculatorDouble
from the current object's state.double
variance()
Calculate the variance, based on the current state of running-sums.
-
Constructor Details
-
VarianceCalculatorDouble
public VarianceCalculatorDouble(double sum, double sumSquares, long count) Creates a newVarianceCalculatorDouble
instance.- 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 anotherVarianceCalculatorDouble
from the current object's state.This occurs immutably, and the currently object's state is unaffected.
- Parameters:
toSubtract
- the otherVarianceCalculatorDouble
whose state is subtracted.- Returns:
- a newly created
VarianceCalculatorDouble
whose 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.
-