Variance for accumulator. Validate that the following code, which adds the methods var() and stddev() to Accumulator, computes both the sample mean, sample variance, and sample standard deviation of the numbers presented as arguments to addDataValue():
public class Accumulator {private double m;private double s;private int N;public void addDataValue(double x){N++;s = s + 1.0 * (N-1) / N * (x - m) * (x - m);m = m + (x - m) / N;}public double mean(){ return m; }public double var(){ return s/(N - 1); }public double stddev(){ return Math.sqrt(this.var()); }}
This implementation is less susceptible to roundoff error than the straightforward implementation based on saving the sum of the squares of the numbers.
We need at least 10 more requests to produce the solution.
0 / 10 have requested this problem solution
The more requests, the faster the answer.