来源:
《算法·第四版》1.2 Data Abstraction
Creative Problems · 1.2.18
Source Code:
/** * Adds the specified data value to the accumulator. * @param x the data value */public void addDataValue(double x) { n++; double delta = x - mean; mean += delta / n; var += (double) (n - 1) / n * delta * delta;}
当向累加器中新加入一个data时,不需要和原来的data一起重新算一遍均值和方差,而是可以根据之前已经算出来的均值和方差,利用递推公式直接得到新的结果,这里就关注这个递推公式