Class MovingFunctions


  • public class MovingFunctions
    extends java.lang.Object
    Provides a collection of static utility methods that can be referenced from MovingFunction script contexts
    • Constructor Summary

      Constructors 
      Constructor Description
      MovingFunctions()  
    • Method Summary

      Modifier and Type Method Description
      static double ewma​(double[] values, double alpha)
      Calculate a exponentially weighted moving average.
      static double holt​(double[] values, double alpha, double beta)
      Calculate a doubly exponential weighted moving average Alpha controls the smoothing of the data.
      static double[] holtForecast​(double[] values, double alpha, double beta, int numForecasts)
      Version of holt that can "forecast", not exposed as a whitelisted function for moving_fn scripts, but here as compatibility/code sharing for existing moving_avg agg.
      static double holtWinters​(double[] values, double alpha, double beta, double gamma, int period, boolean multiplicative)
      Calculate a triple exponential weighted moving average Alpha controls the smoothing of the data.
      static double[] holtWintersForecast​(double[] values, double alpha, double beta, double gamma, int period, double padding, boolean multiplicative, int numForecasts)
      Version of holt-winters that can "forecast", not exposed as a whitelisted function for moving_fn scripts, but here as compatibility/code sharing for existing moving_avg agg.
      static double linearWeightedAvg​(double[] values)
      Calculate a linearly weighted moving average, such that older values are linearly less important.
      static double max​(double[] values)
      Find the maximum value in a window of values.
      static double min​(double[] values)
      Find the minimum value in a window of values If all values are missing/null/NaN, the return value will be NaN
      static double stdDev​(double[] values, double avg)
      Calculate a standard deviation over the values using the provided average.
      static double sum​(double[] values)
      Find the sum of a window of values If all values are missing/null/NaN, the return value will be 0.0
      static double unweightedAvg​(double[] values)
      Calculate a simple unweighted (arithmetic) moving average.
      • Methods inherited from class java.lang.Object

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

      • MovingFunctions

        public MovingFunctions()
    • Method Detail

      • max

        public static double max​(double[] values)
        Find the maximum value in a window of values. If all values are missing/null/NaN, the return value will be NaN
      • min

        public static double min​(double[] values)
        Find the minimum value in a window of values If all values are missing/null/NaN, the return value will be NaN
      • sum

        public static double sum​(double[] values)
        Find the sum of a window of values If all values are missing/null/NaN, the return value will be 0.0
      • unweightedAvg

        public static double unweightedAvg​(double[] values)
        Calculate a simple unweighted (arithmetic) moving average. Only finite values are averaged. NaN or null are ignored. If all values are missing/null/NaN, the return value will be NaN. The average is based on the count of non-null, non-NaN values.
      • stdDev

        public static double stdDev​(double[] values,
                                    double avg)
        Calculate a standard deviation over the values using the provided average. Only finite values are averaged. NaN or null are ignored. If all values are missing/null/NaN, the return value will be NaN. The average is based on the count of non-null, non-NaN values.
      • linearWeightedAvg

        public static double linearWeightedAvg​(double[] values)
        Calculate a linearly weighted moving average, such that older values are linearly less important. "Time" is determined by position in collection Only finite values are averaged. NaN or null are ignored. If all values are missing/null/NaN, the return value will be NaN The average is based on the count of non-null, non-NaN values.
      • ewma

        public static double ewma​(double[] values,
                                  double alpha)
        Calculate a exponentially weighted moving average. Alpha controls the smoothing of the data. Alpha = 1 retains no memory of past values (e.g. a random walk), while alpha = 0 retains infinite memory of past values (e.g. the series mean). Useful values are somewhere in between. Defaults to 0.5. Only finite values are averaged. NaN or null are ignored. If all values are missing/null/NaN, the return value will be NaN The average is based on the count of non-null, non-NaN values.
        Parameters:
        alpha - A double between 0-1 inclusive, controls data smoothing
      • holt

        public static double holt​(double[] values,
                                  double alpha,
                                  double beta)
        Calculate a doubly exponential weighted moving average Alpha controls the smoothing of the data. Alpha = 1 retains no memory of past values (e.g. a random walk), while alpha = 0 retains infinite memory of past values (e.g. the series mean). Useful values are somewhere in between. Defaults to 0.5. Beta is equivalent to alpha, but controls the smoothing of the trend instead of the data Only finite values are averaged. NaN or null are ignored. If all values are missing/null/NaN, the return value will be NaN The average is based on the count of non-null, non-NaN values.
        Parameters:
        alpha - A double between 0-1 inclusive, controls data smoothing
        beta - a double between 0-1 inclusive, controls trend smoothing
      • holtForecast

        public static double[] holtForecast​(double[] values,
                                            double alpha,
                                            double beta,
                                            int numForecasts)
        Version of holt that can "forecast", not exposed as a whitelisted function for moving_fn scripts, but here as compatibility/code sharing for existing moving_avg agg. Can be removed when moving_avg is gone.
      • holtWinters

        public static double holtWinters​(double[] values,
                                         double alpha,
                                         double beta,
                                         double gamma,
                                         int period,
                                         boolean multiplicative)
        Calculate a triple exponential weighted moving average Alpha controls the smoothing of the data. Alpha = 1 retains no memory of past values (e.g. a random walk), while alpha = 0 retains infinite memory of past values (e.g. the series mean). Useful values are somewhere in between. Defaults to 0.5. Beta is equivalent to alpha, but controls the smoothing of the trend instead of the data. Gamma is equivalent to alpha, but controls the smoothing of the seasonality instead of the data Only finite values are averaged. NaN or null are ignored. If all values are missing/null/NaN, the return value will be NaN The average is based on the count of non-null, non-NaN values.
        Parameters:
        alpha - A double between 0-1 inclusive, controls data smoothing
        beta - a double between 0-1 inclusive, controls trend smoothing
        gamma - a double between 0-1 inclusive, controls seasonality smoothing
        period - the expected periodicity of the data
        multiplicative - true if multiplicative HW should be used. False for additive
      • holtWintersForecast

        public static double[] holtWintersForecast​(double[] values,
                                                   double alpha,
                                                   double beta,
                                                   double gamma,
                                                   int period,
                                                   double padding,
                                                   boolean multiplicative,
                                                   int numForecasts)
        Version of holt-winters that can "forecast", not exposed as a whitelisted function for moving_fn scripts, but here as compatibility/code sharing for existing moving_avg agg. Can be removed when moving_avg is gone.