Skip to content
Search

Average

lastmod 2023-10-26created 2017-04-0512 edits

In statistics, the average (mean) has two related meanings.

  • What is called the average in everyday life, also known as the arithmetic mean. (It is distinct from the geometric mean or harmonic mean.) This average is also called the sample mean.
  • The expected value of a random variable. Also called the population mean.

Averages are used not only in statistics but also in geometry and analysis. For these purposes, various means that are not used much in statistics have been devised. For these means, see the pages on other means.

The sample mean is often used as an estimator of central tendency, such as the population mean. However, other estimators are also used.

Calculating the average value of a function

The average value of a function is calculated using a definite integral.

Arithmetic mean

In mathematics and statistics, the arithmetic mean is the sum of the given numbers divided by the count of numbers.

The basic form of the arithmetic mean is as follows.

\(\begin{align} c_N &= \frac{1}{N}(x_1 + x_2 + x_3 + ... + x_N) \\ &= \frac{1}{N} \sum_{i=1}^{N}x_i \end{align}\)

Computing the average of continuous input values

This method can be used when the average has already been computed (\(c_{N-1}\)) and you want to obtain the final average (\(c_{N}\)) when the next value arrives:

\(\begin{align} c_N &= \frac{1}{N}(x_1 + x_2 + x_3 + ... + x_N) \\ &= \frac{1}{N} \sum_{i=1}^{N}x_i \\ &= \frac{1}{N} \left( \sum_{i=1}^{N-1}x_i + x_N \right) \\ &= \frac{N-1}{N} \frac{1}{N-1} \sum_{i=1}^{N-1}x_i + \frac{1}{N}x_N \end{align}\)

Also, \(c_{N-1}\) can be defined as follows.

\(\begin{align} c_{N-1} = \frac{1}{N-1} \sum_{i=1}^{N-1}x_i \end{align}\)

Combining the previous average (\(c_{N-1}\)) and the final average (\(c_{N}\)),

\(\begin{align} c_N &= \frac{N-1}{N} \frac{1}{N-1} \sum_{i=1}^{N-1}x_i + \frac{1}{N}x_N \\ &= \frac{N-1}{N} c_{N-1} + \frac{1}{N}x_N \\ \end{align}\)

is obtained.

To simplify the expression, if \(\alpha\) is defined as follows,

\(\begin{align} \alpha &= \frac{N-1}{N} \end{align}\)

then \(1 - \alpha\) is:

\(\begin{align} \alpha &= \frac{N-1}{N} \\ \alpha N &= N - 1 \\ \alpha N - N &= - 1 \\ N - \alpha N &= 1 \\ \frac{N - \alpha N}{N} &= \frac{1}{N} \\ 1 - \alpha &= \frac{1}{N} \end{align}\)

Finally,

\(\begin{align} c_N = \alpha c_{N-1} + (1 - \alpha)x_N \end{align}\)

is the result.

C++ Code

template <typename Base, typename Result = Base, typename Integer = int>
Result averageStep(Base prev_average, Base current, Integer n)
{
    Result const ALPHA = (static_cast<Result>(n) - static_cast<Result>(1)) / static_cast<Result>(n);
    return (ALPHA * static_cast<Result>(prev_average)) +
            ((static_cast<Result>(1) - ALPHA) * static_cast<Result>(current));
}

Python Code

def average_step(prev_average: float, x: float, n: int) -> float:
    alpha = (n - 1) / n
    return (alpha * prev_average) + ((1 - alpha) * x)

See also

Favorite site

author Zer0 sitemap