前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊Elasticsearch的ExponentiallyWeightedMovingAverage

聊聊Elasticsearch的ExponentiallyWeightedMovingAverage

作者头像
code4it
发布2019-06-17 15:07:35
3840
发布2019-06-17 15:07:35
举报
文章被收录于专栏:码匠的流水账

本文主要研究一下Elasticsearch的ExponentiallyWeightedMovingAverage

ExponentiallyWeightedMovingAverage

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

代码语言:javascript
复制
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.java

代码语言:javascript
复制
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

  • 聊聊rsocket load balancer的Ewma
  • ExponentiallyWeightedMovingAverage
  • ExponentiallyWeightedMovingAverageTests
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-06-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 码匠的流水账 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ExponentiallyWeightedMovingAverage
  • 实例
  • 小结
  • doc
相关产品与服务
Elasticsearch Service
腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档