Class MovingAverage

java.lang.Object
org.rapidcontext.core.data.stat.MovingAverage

public class MovingAverage extends Object
A set of approximated moving averages. The updated averages are calculated using only the previous average (for each window) and the new value. Three constant window sizes are used to smooth the output average to a different degree. Calculations use double internally, but serialize to long values multiplied by the corresponding window size (to preserve limited precision).
Version:
1.0
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    The predefined window sizes.
  • Constructor Summary

    Constructors
    Constructor
    Description
    MovingAverage(double initial)
    Initializes a new set of moving averages with an initial value.
    Initializes a new set of moving averages from a serialized array.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(double value)
    Adds a value to the moving average (for all windows).
    Returns an array with the averages for each of the window sizes (small to large).
    double
    Returns the approximate average for the specified window.
    Returns an array with the averages for each of the window sizes (small to large).

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • MovingAverage

      public MovingAverage(double initial)
      Initializes a new set of moving averages with an initial value.
      Parameters:
      initial - the initial value to use
    • MovingAverage

      public MovingAverage(Array arr)
      Initializes a new set of moving averages from a serialized array.
      Parameters:
      arr - the serialized array of long values
  • Method Details

    • serialize

      public Array serialize()
      Returns an array with the averages for each of the window sizes (small to large). The returned averages are multiplied by each window size (to preserve some precision) and rounded to long values.
      Returns:
      the serialized array of long values
    • values

      public Array values()
      Returns an array with the averages for each of the window sizes (small to large). The returned averages will be a rounded to long values.
      Returns:
      an array of long values
    • valueFor

      public double valueFor(MovingAverage.Window window)
      Returns the approximate average for the specified window.
      Parameters:
      window - the window to fetch
      Returns:
      the approximate average for the specified window
    • add

      public void add(double value)
      Adds a value to the moving average (for all windows).
      Parameters:
      value - the value to add