简体   繁体   English

如何将多个commons.math SummaryStatistics对象一起添加?

[英]How to add multiple commons.math SummaryStatistics objects together?

We have profiling code that collections durations of methods along with a bunch of other data points, and we store those numbers inside a SummaryStatistics object from commons math to provide the min, max, mean, count etc. However we need to flush this object to disk every hour or so and start collecting again for the next one. 我们已经分析了集合持续时间的方法以及一堆其他数据点的代码,我们将这些数字存储在一个SummaryStatistics对象中,从commons math中提供min,max,mean,count等。但是我们需要将这个对象刷新到磁盘每小时左右开始收集下一个磁盘。

My question is how can we reliably add these values together, so if we have 24 summary statistics objects we can display the summary for the entire day without skewing the data? 我的问题是我们如何可靠地将这些值一起添加,所以如果我们有24个汇总统计对象,我们可以显示整天的汇总而不会扭曲数据? The objects themselves have the running averages as well as how many items were counted, so is there a utility class that will allow two weighted averages to be combined? 对象本身具有运行平均值以及计算的项目数,因此是否有一个实用程序类可以合并两个加权平均值?

You can also do this directly, using AggregateSummaryStatistics . 您也可以使用AggregateSummaryStatistics直接执行此操作。 See the section titled "Compute statistics for multiple samples and overall statistics concurrently" In the statistics section of the Commons Math User Guide. 请参阅“Commons Math用户指南”的统计信息部分中标题为“同时计算多个样本和总体统计信息的统计信息”一节。

Since you say you have both the mean and the count, the general formula you want to use is to sum the product of the means by their count and then divide that by the sum of their counts. 既然你说你同时拥有均值和计数,那么你想要使用的通用公式是将均值的乘积与其计数相加,然后除以它们的计数之和。

Eg , for two SummaryStatistics objects A and B , you would use: 例如 ,对于两个SummaryStatistics对象AB ,您将使用:

double weightedMean = (A.getMean() * A.getN() + B.getMean() * B.getN()) /
                      (A.getN() + B.getN());

For many of them ( eg , a List of them called `manyStats') you might do something like: 对于他们中的许多人( 例如 ,他们称为“manyStats”的List ),您可能会执行以下操作:

double accum = 0.0;
long n = 0;
for (SummaryStatisics s: manyStats) {
  accum += s.getMean() * s.getN();
  n += s.getN();
}
double weightedMean = accum / n;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM