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 double
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
ConstructorsConstructorDescriptionVarianceCalculatorLong
(long sum, long sumSquares, long count) Creates a newVarianceCalculatorLong
instance. -
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(int value) Adds a value to the running sum.void
add
(int value, int instances) Adds a multiple instances of 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
(VarianceCalculatorLong toSubtract) Subtracts the running-sums and count from anotherVarianceCalculatorLong
from the current object's state.double
variance()
Calculate the variance, based on the current state of running-sums.
-
Constructor Details
-
VarianceCalculatorLong
public VarianceCalculatorLong(long sum, long sumSquares, long count) Creates a newVarianceCalculatorLong
instance.- 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 ofvalue
to add.
-
subtract
Subtracts the running-sums and count from anotherVarianceCalculatorLong
from the current object's state.This occurs immutably, and the currently object's state is unaffected.
- Parameters:
toSubtract
- the otherVarianceCalculatorLong
whose state is subtracted.- Returns:
- a newly created
VarianceCalculatorLong
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.
-