To calculate the average of some numbers, we can use the following simple formula:

Using the formula above can be problematic:
- When a new number is added to the data set, we have to sum all numbers to calculate the current average. As the data set grows large, it takes more and more time to evaluate the formula above.
- The algorithm based on this formula is numerically unstable: the sum of the numbers may overflow, even if the resulting average would not (see the unit test testNumericalStability).
Fortunately we can easily transform the formula to an "incremental" form:

This new form is almost as easy to implement as the original, but has two important advantages:
- It can be used to calculate the average incrementally (the current average is calculated from the current value and the previous average).
- It is numerically stable (see the unit test testNumericalStability).
Resources:


It's very useful. Thanks you!!
ReplyDeleteThanks!
ReplyDelete