Re: PowerMean
Eric Sosman wrote:
....
Answer #2: Instead of total-and-divide-by-count-at-end,
you can maintain a "running average" of the terms seen so far,
and make an adjustment for each new one as you get it.
class RunningMean() {
private int count;
private double mean;
void update(double term) {
++count;
double delta = (term - mean) / count;
Can't this overflow if e.g. term is very positive and mean is very
negative? Consider the mean of -Double.MAX_VALUE and Double.MAX_VALUE.
At the start of the second iteration, mean is -Double.MAX_VALUE,
count is 1, and term is Double.MAX_VALUE:
double delta = (Double.MAX_VALUE - -Double.MAX_VALUE)/2;
I think Knuth may have been solving a rather different problem,
minimizing loss of precision in the mean and variance calculation if the
numbers have to be processed in a fixed order, in O(1) working storage.
He was immersed in a different world, in which just stacking up a large
data set in memory and working on it as a unit was not usually feasible.
There are ways of avoiding overflow, and reducing rounding error, by
reordering the calculations if you have all the numbers at once.
Usually, the real solution is to pick a data type that has enough head
room, compared to the numbers, for simple methods to work.
mean += delta;
}
double getMean() { return mean; }
}
....
Patricia