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 TypeMethodDescriptionvoidadd(RunningSum toAdd) Adds the values of anotherRunningSumto the current values.Deep-copy.longgetCount()The running count.doublegetSum()The running sum.voidincrement(double sumIncrement) Adds a new item to the sum, and increments the count by 1.voidincrement(double sumIncrement, long countIncrement) Increments both the sum and count by particular values.voidincrement(RunningSum runningSum) Increments (adds) an existingRunningSumto the current.doublemean()Calculates the mean.doublemean(double valueIfCountZero) Calculates the mean.doubleCalculate the mean and then reset to zero.voidreset()Reset the sum and count to zero.
-
Constructor Details
-
RunningSum
public RunningSum() -
RunningSum
public RunningSum(double sum, long count) Creates a newRunningSuminstance.- Parameters:
sum- The running sum.count- The running count.
-
-
Method Details
-
mean
public double mean()Calculates the mean.- Returns:
- the mean or
NaNif 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
valueIfCountZeroif 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 existingRunningSumto 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 anotherRunningSumto 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.
-