Class RunningSum

Object
RunningSum
All Implemented Interfaces:
Serializable

public class RunningSum extends Object implements 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 Details

    • RunningSum

      public RunningSum()
    • RunningSum

      public RunningSum(double sum, long count)
      Creates a new RunningSum 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

      public void increment(RunningSum runningSum)
      Increments (adds) an existing RunningSum to the current.
      Parameters:
      runningSum - the existing sum, which is unmodified.
    • duplicate

      public RunningSum duplicate()
      Deep-copy.
      Returns:
      a new object with new state.
    • add

      public void add(RunningSum toAdd)
      Adds the values of another RunningSum 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.