Class ExponentialAverageCalculationContext

java.lang.Object
org.elasticsearch.xpack.core.ml.utils.ExponentialAverageCalculationContext
All Implemented Interfaces:
Writeable, org.elasticsearch.xcontent.ToXContent, org.elasticsearch.xcontent.ToXContentObject

public class ExponentialAverageCalculationContext extends Object implements Writeable, org.elasticsearch.xcontent.ToXContentObject
Utility for calculating current value of exponentially-weighted moving average per fixed-sized time window. The formula for the current value of the exponentially-weighted moving average is: currentExponentialAverageMs = alpha * previousExponentialAverageMs + (1 - alpha) * incrementalMetricValueMs where alpha depends on what fraction of the current time window we've already seen: alpha = e^(-time_elapsed_since_window_start/window_size) time_elapsed_since_window_start = latestTimestamp - window_start The class holds 3 values based on which it performs the calculation: - incrementalMetricValueMs - accumulated value of the metric in the current time window - latestTimestamp - timestamp updated as the time passes through the current time window - previousExponentialAverageMs - exponential average for previous time windows incrementalMetricValueMs should be updated using increment(double). latestTimestamp should be updated using setLatestTimestamp(java.time.Instant). Because it can happen that the timestamp is not available while incrementing the metric value, it is the responsibility of the user of this class to always call setLatestTimestamp(java.time.Instant) *after* all the relevant (i.e. referring to the points in time before the latest timestamp mentioned) increment(double) calls are made.
  • Field Details

    • INCREMENTAL_METRIC_VALUE_MS

      public static final org.elasticsearch.xcontent.ParseField INCREMENTAL_METRIC_VALUE_MS
    • LATEST_TIMESTAMP

      public static final org.elasticsearch.xcontent.ParseField LATEST_TIMESTAMP
    • PREVIOUS_EXPONENTIAL_AVERAGE_MS

      public static final org.elasticsearch.xcontent.ParseField PREVIOUS_EXPONENTIAL_AVERAGE_MS
    • PARSER

      public static final org.elasticsearch.xcontent.ConstructingObjectParser<ExponentialAverageCalculationContext,Void> PARSER
  • Constructor Details

    • ExponentialAverageCalculationContext

      public ExponentialAverageCalculationContext()
    • ExponentialAverageCalculationContext

      public ExponentialAverageCalculationContext(double incrementalMetricValueMs, @Nullable Instant latestTimestamp, @Nullable Double previousExponentialAverageMs)
    • ExponentialAverageCalculationContext

      public ExponentialAverageCalculationContext(ExponentialAverageCalculationContext lhs)
    • ExponentialAverageCalculationContext

      public ExponentialAverageCalculationContext(StreamInput in) throws IOException
      Throws:
      IOException
  • Method Details

    • getIncrementalMetricValueMs

      public double getIncrementalMetricValueMs()
    • getLatestTimestamp

      public Instant getLatestTimestamp()
    • getPreviousExponentialAverageMs

      public Double getPreviousExponentialAverageMs()
    • getCurrentExponentialAverageMs

      public Double getCurrentExponentialAverageMs()
    • increment

      public void increment(double metricValueDeltaMs)
      Increments the current accumulated metric value by the given delta.
    • setLatestTimestamp

      public void setLatestTimestamp(Instant newLatestTimestamp)
      Sets the latest timestamp that serves as an indication of the current point in time. Before calling this method make sure all the associated calls to increment(double) were already made.
    • writeTo

      public void writeTo(StreamOutput out) throws IOException
      Specified by:
      writeTo in interface Writeable
      Throws:
      IOException
    • toXContent

      public org.elasticsearch.xcontent.XContentBuilder toXContent(org.elasticsearch.xcontent.XContentBuilder builder, org.elasticsearch.xcontent.ToXContent.Params params) throws IOException
      Specified by:
      toXContent in interface org.elasticsearch.xcontent.ToXContent
      Throws:
      IOException
    • equals

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

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

      public String toString()
      Overrides:
      toString in class Object