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

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

问题描述

我正在使用

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实用工具类并管理数组.一种替代方法是使用

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.

您可以在用户指南中找到StatUtils的示例,以下内容将与您的用例类似.

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天全站免登陆