2017-07-29 80 views
0

我正在使用Apache DescriptiveStatistics来计算统计信息,但存在问题。我有一堆实体生成值,每次迭代时我都想更新与实体关联的值。键值描述性统计

比如我可以在整个城市的1000个不同的地点跟踪当前的温度,我想能够计算为城市的一些平均气温:

for (Location location: locations) { 
    double temperature = location.getCurrentTemperature(); 

    stats.update(location, temperature); 
} 

// Average: stats.getMean(); 

它有没有办法做到这一点?

回答

0

Apache Commons Math没有提供您请求的确切的,基于密钥的更新。

但是,如果对于每次迭代,您都使用来自所有位置的观测值,并且如果位置的数量和顺序从迭代维持到迭代,那么DescriptiveStatistics可能有效。

用窗口大小等于位置数初始化DescriptiveStatistics实例。然后,在每次迭代之后,来自DescriptiveStatistics的均值应该是所有位置的平均值。

// initialize stats with fixed window size 
DescriptiveStatistics stats = new DescriptiveStatistics(locations.size()); 

// add current value from each location 
for (Location location : locations) { 
    stats.addValue(location.getCurrentTemperature()); 
} 

// after each loop the stats will be computed across all the locations 
double average = stats.getMean();