PMEAN
This function computes the generalized (power) mean of positive numeric data for exponent p.
For values x_1, x_2, \ldots, x_n, the power mean is:
M_p = \left(\frac{1}{n}\sum_{i=1}^{n} x_i^p\right)^{1/p}
Special cases include arithmetic mean (p=1), geometric mean as the limit at p \to 0, and harmonic mean (p=-1).
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
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).