GMEAN

Overview

The GMEAN function computes the geometric mean of a dataset, which is particularly useful for averaging rates, ratios, and values that span multiple orders of magnitude. Unlike the arithmetic mean, the geometric mean is less sensitive to extreme outliers and is the preferred measure of central tendency for data that grows multiplicatively, such as compound interest rates, population growth, or investment returns.

This implementation uses the SciPy library’s scipy.stats.gmean function. For detailed documentation, see the official SciPy gmean reference.

For an array of n positive values a_1, a_2, \ldots, a_n, the geometric mean is defined as the nth root of the product of all values:

\bar{x}_{\text{geo}} = \left( \prod_{i=1}^{n} a_i \right)^{1/n} = \sqrt[n]{a_1 \cdot a_2 \cdots a_n}

In practice, SciPy computes this as the exponential of the arithmetic mean of the natural logarithms, which is numerically more stable for large datasets:

\bar{x}_{\text{geo}} = \exp\left( \frac{1}{n} \sum_{i=1}^{n} \ln(a_i) \right)

The geometric mean requires all input values to be strictly positive. This function automatically flattens two-dimensional input and ignores non-numeric values. If no valid positive numbers are found, an error message is returned.

Common applications include analyzing financial returns (where multiplicative growth is compounded), normalizing benchmarks or index values, and computing average growth rates in scientific datasets. For more background, see the Wikipedia article on geometric mean.

This example function is provided as-is without any representation of accuracy.

Excel Usage

=GMEAN(data)
  • data (list[list], required): 2D array of positive numeric values. Non-numeric values are ignored.

Returns (float): Geometric mean of the input data, or error message (str) if input is invalid.

Examples

Example 1: Geometric mean of 2x2 matrix

Inputs:

data
1 2
3 4

Excel formula:

=GMEAN({1,2;3,4})

Expected output:

2.213

Example 2: Non-numeric values are ignored

Inputs:

data
2 a
8 4

Excel formula:

=GMEAN({2,"a";8,4})

Expected output:

4

Example 3: Single row of values

Inputs:

data
5 10

Excel formula:

=GMEAN({5,10})

Expected output:

7.071

Example 4: Single column with multiple rows

Inputs:

data
2
8
32

Excel formula:

=GMEAN({2;8;32})

Expected output:

8

Python Code

from scipy.stats import gmean as scipy_gmean

def gmean(data):
    """
    Compute the geometric mean of the input data, flattening the input and ignoring non-numeric values.

    See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.gmean.html

    This example function is provided as-is without any representation of accuracy.

    Args:
        data (list[list]): 2D array of positive numeric values. Non-numeric values are ignored.

    Returns:
        float: Geometric mean of the input data, or error message (str) if input is invalid.
    """
    def to2d(x):
        return [[x]] if not isinstance(x, list) else x

    data = to2d(data)

    if not isinstance(data, list) or not all(isinstance(row, list) for row in data):
        return "Error: Invalid input: data must be a 2D list."

    flat = []
    for row in data:
        for x in row:
            try:
                val = float(x)
                if val > 0:
                    flat.append(val)
            except (TypeError, ValueError):
                continue

    if not flat:
        return "Error: Input must contain at least one positive number."

    result = scipy_gmean(flat)
    return float(result)

Online Calculator