Overview

Purpose

Compute the arithmetic mean of x or, if w is provided, the weighted mean.

Parameters

  • x: numeric data.

  • w: optional weights. They must have the same shape as x and be strictly positive.

Returns

A float with the simple or weighted mean.

Errors and exceptions

  • ValueError if x is empty or not finite.

  • ValueError if w has an incompatible shape.

  • ValueError if w contains non-positive values or has a non-positive sum.

Example

from mespy import weighted_mean

x = [1.0, 2.0, 3.0]
w = [3.0, 1.0, 1.0]

print(weighted_mean(x))     # 2.0
print(weighted_mean(x, w))  # 1.6

Notes

  • If w is None, the function uses numpy.mean.

  • Weight validation goes through _validate_weights.