PMEAN
Overview
The PMEAN
function calculates the power mean (also known as the generalized mean) of the input data for a given power p
. The power mean is a generalization of the arithmetic, geometric, and harmonic means, and is defined as:
where is the power parameter, and are the data values. For , the power mean is the arithmetic mean; for , it is the geometric mean; and for , it is the harmonic mean. This function flattens the input, ignores non-numeric values, and does not support weights or axis arguments, unlike scipy.stats.pmean
. Excel does not have a direct equivalent; this function is a custom implementation for 2D lists.
This example function is provided as-is without any representation of accuracy.
Usage
To use the function in Excel:
=PMEAN(data, p)
data
(2D list, required): The input data as a 2D list (column or matrix). Non-numeric values are ignored.p
(float, required): The power parameter for the mean. Common values: 1 (arithmetic), 0 (geometric), -1 (harmonic).
The function returns a single value (float): the power mean of the input data, or an error message (string) if the input is invalid.
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:
Result |
---|
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:
Result |
---|
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:
Result |
---|
1.9200 |
Example 4: Power Mean (p=2)
Inputs:
data | p | |
---|---|---|
1 | 2 | 2 |
3 | 4 |
Excel formula:
=PMEAN({1,2;3,4}, 2)
Expected output:
Result |
---|
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.
Flattens the input, ignores non-numeric values, and does not support weights or axis arguments.
This example function is provided as-is without any representation of accuracy.
"""
# Flatten and filter numeric values
flat = []
if isinstance(data, (int, float)):
return "Two or more data elements are needed"
for row in data:
for val in row:
try:
v = float(val)
flat.append(v)
except (ValueError, TypeError):
continue
if len(flat) < 2:
return "Two or more data elements are needed"
n = len(flat)
if p == 0:
# Geometric mean
prod = 1.0
for v in flat:
if v <= 0:
return "All values must be positive for geometric mean"
prod *= v
return round(prod ** (1.0 / n), 4)
else:
try:
mean = sum(v ** p for v in flat) / n
return round(mean ** (1.0 / p), 4)
except Exception as e:
return str(e)
Live Notebook
Edit this function in a live notebook .