Class Averager

java.lang.Object
org.dellroad.stuff.util.Averager
All Implemented Interfaces:
Cloneable

public class Averager extends Object implements Cloneable
Calculates running averages and variance.

Instances are not thread safe.

See Also:
  • Constructor Details

    • Averager

      public Averager()
      Default constructor.

      Creates an empty (and therefore invalid) instance.

    • Averager

      public Averager(double firstValue)
      Constructor taking first datapoint.
      Parameters:
      firstValue - first data point
  • Method Details

    • addValue

      public void addValue(double value)
      Add a value.
      Parameters:
      value - value to add to the running average
      Throws:
      IllegalArgumentException - if value is NaN or infinity
    • add

      public Averager add(Averager that)
      Add this instance to the given instance and return a new combined instance.
      Parameters:
      that - other instance
      Returns:
      combined average
    • subtract

      public Averager subtract(Averager that)
      Subtract the given instance from this instance.
      Returns:
      average with that data points removed
    • reset

      public void reset()
      Reset this instance.
    • size

      public int size()
      Get the number of data points added so far.
      Returns:
      the number of data points added to this instance
    • isEmpty

      public boolean isEmpty()
      Determine whether any data points have been added to this instance yet.
      Returns:
      true if no data points have been added yet
    • getAverage

      public OptionalDouble getAverage()
      Get the average of all the data points added to this instance so far.
      Returns:
      average value, or empty if no data points have been added yet
    • getVariance

      public OptionalDouble getVariance()
      Get the population variance.
      Returns:
      population variance, or empty if no data points have been added yet
    • getStandardDeviation

      public OptionalDouble getStandardDeviation()
      Get the population standard deviation.
      Returns:
      population standard deviation, or empty if no data points have been added yet
    • getSampleVariance

      public OptionalDouble getSampleVariance()
      Get the sample (unbiased estimated) variance.
      Returns:
      sample variance, or empty if less than two data points have been added yet
    • getSampleStandardDeviation

      public OptionalDouble getSampleStandardDeviation()
      Get the sample (unbiased estimated) standard deviation.
      Returns:
      population standard deviation, or empty if less than two data points have been added yet
    • getCoefficientOfVariation

      public OptionalDouble getCoefficientOfVariation()
      Get the coefficient of variation (the standard deviation divided by the average).

      Note: if the average is zero, then Double.NaN is returned.

      Returns:
      coefficient of variation, or empty if no data points have been added yet
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • clone

      public Averager clone()
      Overrides:
      clone in class Object