使用 Apache Commons Math DescriptiveStatistics 跟踪多个移动平均线 [英] Track multiple moving averages with Apache Commons Math DescriptiveStatistics

查看:39
本文介绍了使用 Apache Commons Math DescriptiveStatistics 跟踪多个移动平均线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 DescriptiveStatistics 跟踪某些指标的移动平均值.我有一个线程每分钟提交一次指标值,我使用 DescriptiveStatistics 上的 setWindowSize(10) 方法跟踪指标的 10 分钟移动平均值.

I am using DescriptiveStatistics to track the moving average of some metrics. I have a thread that submits the metric value every minute, and I track the 10 minute moving average of the metric by using the setWindowSize(10) method on DescriptiveStatistics.

这适用于跟踪单个移动平均线,但我实际上需要跟踪多个移动平均线,即 1 分钟平均值、5 分钟平均值和 10 分钟平均值.

This works fine for tracking a single moving average but I actually need to track multiple moving averages, i.e. the 1 minute average, the 5 minute average, and the 10 minute average.

目前我有以下选择:

  1. 有 3 个不同的 DescriptiveStatistics 实例和 3 个不同的窗口.但是,这意味着我们多次存储原始指标,这并不理想.

  1. Have 3 different DescriptiveStatistics instances with 3 different windows. However, this means we store the raw metrics multiple times which is not ideal.

有 1 个 DescriptiveStatistics 实例,并在查询移动平均线时执行如下操作:

Have 1 instance of DescriptiveStatistics and do something like the following when querying for a moving average:

int minutes = <set from parameter>;
DescriptiveStatistics stats = <class variable>;

if (minutes == stats.getN()) return stats.getMean();
SummaryStatistics subsetStats = new SummaryStatistics();
for (int i = 0; i < minutes; i++) {
    subsetStats.addValue(stats.getElement((int)stats.getN() - i - 1));
}
return subsetStats.getMean();

但是,选项 2 意味着每次查询窗口小于 DescriptiveStats 窗口大小的移动平均线时,我都必须重新计算一堆平均值.

However, option 2 means that I have to re-compute a bunch of averages every time I query for a moving average whose window is smaller than the DescriptiveStats window size.

有没有更好的方法?我想存储指标数据的 1 个副本,并以不同的间隔不断计算它的 N 个移动平均值.这可能会进入 Codahale Metrics 或 Netflix Servo 领域,但我不想为此使用重量级库.

Is there a way to do this better? I want to store 1 copy of the metrics data and continually calculate N moving averages of it with different intervals. This might be getting into the land of Codahale Metrics or Netflix Servo, but I don't want to have to use a heavyweight library just for this.

推荐答案

您可以使用 StatUtils 实用程序类并在添加新值时管理数组.一种替代方法是使用 Apache Commons 大小为 10 和 Apache Utils 以简化到原始值数组的转换.

You could use StatUtils utility class and manage the array when adding new values. One alternative is to use CircularFifoQueue of Apache Commons with a size of 10 and Apache Utils to simplify the conversion to array of primitive values.

您可以在 用户指南,以下内容与您的用例类似.

You can find an example of StatUtils in the User Guide, the following would be something similar to your use case.

CircularFifoQueue<Double> queue = new CircularFifoQueue<>(10);

// Add your values

double[] values = ArrayUtils.toPrimitive(queue.toArray(new Double[0]))
mean1 = StatUtils.mean(values, 0, 1);
mean5 = StatUtils.mean(values, 0, 5);
mean10 = StatUtils.mean(values, 0, 10);

这篇关于使用 Apache Commons Math DescriptiveStatistics 跟踪多个移动平均线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆