聊聊Elasticsearch的ExponentiallyWeightedMovingAverage

本文主要研究一下Elasticsearch的ExponentiallyWeightedMovingAveragejava

ExponentiallyWeightedMovingAverage

elasticsearch-7.0.1/server/src/main/java/org/elasticsearch/common/ExponentiallyWeightedMovingAverage.javagit

public class ExponentiallyWeightedMovingAverage {

    private final double alpha;
    private final AtomicLong averageBits;

    /**
     * Create a new EWMA with a given {@code alpha} and {@code initialAvg}. A smaller alpha means
     * that new data points will have less weight, where a high alpha means older data points will
     * have a lower influence.
     */
    public ExponentiallyWeightedMovingAverage(double alpha, double initialAvg) {
        if (alpha < 0 || alpha > 1) {
            throw new IllegalArgumentException("alpha must be greater or equal to 0 and less than or equal to 1");
        }
        this.alpha = alpha;
        this.averageBits = new AtomicLong(Double.doubleToLongBits(initialAvg));
    }

    public double getAverage() {
        return Double.longBitsToDouble(this.averageBits.get());
    }

    public void addValue(double newValue) {
        boolean successful = false;
        do {
            final long currentBits = this.averageBits.get();
            final double currentAvg = getAverage();
            final double newAvg = (alpha * newValue) + ((1 - alpha) * currentAvg);
            final long newBits = Double.doubleToLongBits(newAvg);
            successful = averageBits.compareAndSet(currentBits, newBits);
        } while (successful == false);
    }
}
複製代碼
  • ExponentiallyWeightedMovingAverage實現了EWMA,它是線程安全的;其構造器要求輸入alpha及initialAvg;alpha越大表示新數據權重越大舊數據權重越小
  • getAverage返回的是averageBits的值,不過它存儲的是double的bit形式,返回的時候使用Double.longBitsToDouble轉換會double
  • addValue方法使用(alpha * newValue) + ((1 - alpha) * currentAvg)計算新值,而後使用averageBits.compareAndSet方法來實現原子更新

實例

elasticsearch-7.0.1/server/src/test/java/org/elasticsearch/common/ExponentiallyWeightedMovingAverageTests.javagithub

public class ExponentiallyWeightedMovingAverageTests extends ESTestCase {

    public void testEWMA() {
        final ExponentiallyWeightedMovingAverage ewma = new ExponentiallyWeightedMovingAverage(0.5, 10);
        ewma.addValue(12);
        assertThat(ewma.getAverage(), equalTo(11.0));
        ewma.addValue(10);
        ewma.addValue(15);
        ewma.addValue(13);
        assertThat(ewma.getAverage(), equalTo(12.875));
    }

    public void testInvalidAlpha() {
        IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> new ExponentiallyWeightedMovingAverage(-0.5, 10));
        assertThat(ex.getMessage(), equalTo("alpha must be greater or equal to 0 and less than or equal to 1"));

        ex = expectThrows(IllegalArgumentException.class, () -> new ExponentiallyWeightedMovingAverage(1.5, 10));
        assertThat(ex.getMessage(), equalTo("alpha must be greater or equal to 0 and less than or equal to 1"));
    }

    public void testConvergingToValue() {
        final ExponentiallyWeightedMovingAverage ewma = new ExponentiallyWeightedMovingAverage(0.5, 10000);
        for (int i = 0; i < 100000; i++) {
            ewma.addValue(1);
        }
        assertThat(ewma.getAverage(), lessThan(2.0));
    }
}
複製代碼
  • testEWMA方法測試算法的計算邏輯;testInvalidAlpha測試alpha參數的校驗;testConvergingToValue則測試ewma值的收斂

小結

  • ExponentiallyWeightedMovingAverage實現了EWMA,它是線程安全的;其構造器要求輸入alpha及initialAvg;alpha越大表示新數據權重越大舊數據權重越小
  • getAverage返回的是averageBits的值,不過它存儲的是double的bit形式,返回的時候使用Double.longBitsToDouble轉換會double
  • addValue方法使用(alpha * newValue) + ((1 - alpha) * currentAvg)計算新值,而後使用averageBits.compareAndSet方法來實現原子更新

doc

相關文章
相關標籤/搜索