PMEAN

Overview

The PMEAN function computes the power mean (also known as the generalized mean or Hölder mean) of a dataset for a given exponent p. The power mean is a family of functions that generalizes many common averages, including the arithmetic mean, geometric mean, and harmonic mean, by varying the exponent parameter.

For a set of positive values x_1, x_2, \ldots, x_n and exponent p, the power mean is defined as:

M_p(x_1, \ldots, x_n) = \left( \frac{1}{n} \sum_{i=1}^{n} x_i^p \right)^{1/p}

Special cases of the power mean include:

Exponent Mean Type
p = -1 Harmonic mean
p = 0 Geometric mean (limit case)
p = 1 Arithmetic mean
p = 2 Quadratic mean (root mean square)

When p = 0, the power mean is defined as the limit, which equals the geometric mean:

M_0(x_1, \ldots, x_n) = \left( \prod_{i=1}^{n} x_i \right)^{1/n}

A fundamental property is the generalized mean inequality: for p < q, we have M_p \leq M_q, with equality only when all values are identical. This means the harmonic mean is always less than or equal to the geometric mean, which is less than or equal to the arithmetic mean.

Power means are used in signal processing, economics, and statistics where different types of averaging are appropriate depending on the nature of the data. For example, the harmonic mean is suitable for averaging rates, while the quadratic mean is used for measuring signal magnitudes.

This implementation follows the mathematical definition described in the SciPy pmean documentation and the Wikipedia article on generalized means. For additional mathematical background, see Bullen’s Handbook of Means and Their Inequalities (2003).

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

Excel Usage

=PMEAN(data, p)
  • data (list[list], required): 2D array of numeric values. Non-numeric values are ignored.
  • p (int, required): The power exponent for the generalized mean (e.g., 1 for arithmetic, 0 for geometric, -1 for harmonic).

Returns (float): Power mean (float), or error message string.

Examples

Example 1: Arithmetic mean (p=1)

Inputs:

data p
1 2 1
3 4

Excel formula:

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

Expected output:

2.5

Example 2: Geometric mean (p=0)

Inputs:

data p
1 2 0
3 4

Excel formula:

=PMEAN({1,2;3,4}, 0)

Expected output:

2.2134

Example 3: Harmonic mean (p=-1)

Inputs:

data p
1 2 -1
3 4

Excel formula:

=PMEAN({1,2;3,4}, -1)

Expected output:

1.92

Example 4: Quadratic mean (p=2)

Inputs:

data p
1 2 2
3 4

Excel formula:

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

Expected output:

2.7386

Python Code

import math

def pmean(data, p):
    """
    Computes the power mean (generalized mean) of the input data for a given power p.

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

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

    Args:
        data (list[list]): 2D array of numeric values. Non-numeric values are ignored.
        p (int): The power exponent for the generalized mean (e.g., 1 for arithmetic, 0 for geometric, -1 for harmonic).

    Returns:
        float: Power mean (float), or error message string.
    """
    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 "Invalid input: data must be a 2D list."

    flat = []
    for row in data:
        for val in row:
            try:
                v = float(val)
                if math.isfinite(v):
                    flat.append(v)
            except (ValueError, TypeError):
                continue

    if len(flat) < 2:
        return "Invalid input: data must contain at least two numeric values."

    if not isinstance(p, (int, float)) or int(p) != p:
        return "Invalid input: p must be an integer."

    p = int(p)
    n = len(flat)

    if p == 0:
        prod = 1.0
        for v in flat:
            if v <= 0:
                return "Invalid input: all values must be positive for p=0 (geometric mean)."
            prod *= v
        return float(prod ** (1.0 / n))

    if p > 0:
        for v in flat:
            if v <= 0:
                return "Invalid input: all values must be positive for p > 0."
    elif p < 0:
        for v in flat:
            if v <= 0:
                return "Invalid input: all values must be positive for p < 0."

    try:
        mean = sum(v ** p for v in flat) / n
        return float(mean ** (1.0 / p))
    except Exception as e:
        return f"Computation error: {e}"

Online Calculator