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 (float, 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.

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.21336

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.73861

Python Code

Show Code
import math
from scipy.stats import pmean as scipy_pmean

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 (float): 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

    try:
      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 val in row:
          try:
            v = float(val)
            if math.isfinite(v):
              flat.append(v)
          except (ValueError, TypeError):
            continue

      if not flat:
        return "Error: Invalid input: data must contain at least one numeric value."

      if not isinstance(p, (int, float)):
        return "Error: Invalid input: p must be a number."

      p = float(p)
      if not math.isfinite(p):
        return "Error: Invalid input: p must be a finite number."

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

      result = scipy_pmean(flat, p, axis=None, nan_policy='omit', keepdims=False)
      return float(result)
    except Exception as e:
      return f"Error: {str(e)}"

Online Calculator

2D array of numeric values. Non-numeric values are ignored.
The power exponent for the generalized mean (e.g., 1 for arithmetic, 0 for geometric, -1 for harmonic).