Class VarianceCalculatorLong
Object
VarianceCalculatorLong
Calculates variance efficiently, as successive values are added, using
long to store sums
internally.
This is similar to VarianceCalculatorDouble but is designed for int values
rather than doubles.
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
ConstructorsConstructorDescriptionVarianceCalculatorLong(long sum, long sumSquares, long count) Creates a newVarianceCalculatorLonginstance. -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(int value) Adds a value to the running sum.voidadd(int value, int instances) Adds a multiple instances of 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(VarianceCalculatorLong toSubtract) Subtracts the running-sums and count from anotherVarianceCalculatorLongfrom the current object's state.doublevariance()Calculate the variance, based on the current state of running-sums.
-
Constructor Details
-
VarianceCalculatorLong
public VarianceCalculatorLong(long sum, long sumSquares, long count) Creates a newVarianceCalculatorLonginstance.- Parameters:
sum- The running sum of values.sumSquares- The running sum of squares of values.count- The running count of values.
-
VarianceCalculatorLong
public VarianceCalculatorLong()
-
-
Method Details
-
add
public void add(int value) Adds a value to the running sum.- Parameters:
value- the value to add.
-
add
public void add(int value, int instances) Adds a multiple instances of a value to the running sum.- Parameters:
value- the value to add.instances- how many instances ofvalueto add.
-
subtract
Subtracts the running-sums and count from anotherVarianceCalculatorLongfrom the current object's state.This occurs immutably, and the currently object's state is unaffected.
- Parameters:
toSubtract- the otherVarianceCalculatorLongwhose state is subtracted.- Returns:
- a newly created
VarianceCalculatorLongwhose 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.
-