Given a list of numbers, there are multiple ways of computing the average and variance (including the standard deviation). A blog entry by Dr. Cook outlines various ways computing them, as well as their characteristics.

One particular method that I would like to focus on, is the iterative method detailed here. It’s a wonderful algorithm that requires only space for computing the average and variance even if entries come in one at a time. I first encountered this method when studying under Dr. Kardi Teknomo and was enamored by its utility and simplicity.

In implementing systems using those methods, we came upon the requirement that the average and variance should be updated in response to entries being revised (a new value replaces the old) or deleted. Moreover, it would be preferred if it could be done incrementally using only space like the original method.

Below are the formulae for doing such. A reasonable requirement for the formulae is that the value to be deleted or revised should be known, as well as the current count, average, and “denormalized” variance (sum of squared difference from the mean, same as the term in Dr. Cook’s article). These formulas are really handy when using SQL triggers to update averages and variances.

### Deleting Values

Where:

- is the value to be deleted,
- is the current number of elements,
- is the current average,
- is the current “denormalized” variance,
- is the new average with deleted, and
- is the new “denormalized” variance with deleted.

The formula for computing and is:

### Updating Values

Where:

- is the old value
- is the new value that replaces
- is the current number of elements,
- is the current average,
- is the current “denormalized” variance,
- is the new average with replaced by , and
- is the new “denormalized” variance with with replaced by .

The formula for computing the and is:

### Derivation

The above formulae are derived by simple algebra. Let be a sequence of length . Without loss of generality, the value to be deleted is the last value, , the average with removed can be computed from the formula for adding :

is easily derived from the addition formula, given that we already know :

The update formulas are derived by applying the addition formula again: