Class RunningSum
Object
RunningSum
- All Implemented Interfaces:
Serializable
Mutable class that allows for incrementing jointly sum and count variables, so as to eventually
calculate the mean.
It can also be reset to zero, as needed.
- Author:
- Owen Feehan
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(RunningSum toAdd) Adds the values of anotherRunningSum
to the current values.Deep-copy.long
getCount()
The running count.double
getSum()
The running sum.void
increment
(double sumIncrement) Adds a new item to the sum, and increments the count by 1.void
increment
(double sumIncrement, long countIncrement) Increments both the sum and count by particular values.void
increment
(RunningSum runningSum) Increments (adds) an existingRunningSum
to the current.double
mean()
Calculates the mean.double
mean
(double valueIfCountZero) Calculates the mean.double
Calculate the mean and then reset to zero.void
reset()
Reset the sum and count to zero.
-
Constructor Details
-
RunningSum
public RunningSum() -
RunningSum
public RunningSum(double sum, long count) Creates a newRunningSum
instance.- Parameters:
sum
- The running sum.count
- The running count.
-
-
Method Details
-
mean
public double mean()Calculates the mean.- Returns:
- the mean or
NaN
if the count is zero.
-
mean
public double mean(double valueIfCountZero) Calculates the mean.- Parameters:
valueIfCountZero
- value to use if the count is zero.- Returns:
- the mean or
valueIfCountZero
if the count is zero.
-
meanAndReset
public double meanAndReset()Calculate the mean and then reset to zero.- Returns:
- the mean, before being reset to zero.
-
reset
public void reset()Reset the sum and count to zero. -
increment
public void increment(double sumIncrement) Adds a new item to the sum, and increments the count by 1.- Parameters:
sumIncrement
- the value to add to sum.
-
increment
public void increment(double sumIncrement, long countIncrement) Increments both the sum and count by particular values.- Parameters:
sumIncrement
- increment-value for sum.countIncrement
- increment-value for count.
-
increment
Increments (adds) an existingRunningSum
to the current.- Parameters:
runningSum
- the existing sum, which is unmodified.
-
duplicate
Deep-copy.- Returns:
- a new object with new state.
-
add
Adds the values of anotherRunningSum
to the current values.- Parameters:
toAdd
- the other sum that will be added.
-
getSum
public double getSum()The running sum. -
getCount
public long getCount()The running count.
-