Implementation
This page describes the internal flow of weighted_mean and the order in which input validation, selection of the unweighted or weighted branch, and final mean calculation are applied. Unlike Overview, the goal here is not to repeat the parameters, but to show how the function actually builds the result.
The following snippets are taken from the current src/mespy/stats_utils.py implementation. Private helpers are mentioned only to clarify the flow; complete details are documented in _as_float_vector and _validate_weights.
Execution sequence
The implementation follows this sequence:
Converts
xinto a one-dimensional, finitefloat64vector.If
wis provided, it validates it as a vector with the same shape asx, strictly positive, and with a finite positive sum.If
w is None, it computes the simple arithmetic mean withnp.mean(values).If
wis valid, it computes the weighted mean as the ratio between weighted sum and sum of weights.Always converts the result to a Python
float.
Validation and selection of the numerical branch
def weighted_mean(x: ArrayLike, w: ArrayLike | None = None) -> float:
values = _as_float_vector("x", x)
weights = _validate_weights(values, w)
if weights is None:
return float(np.mean(values))
return float(np.sum(values * weights) / np.sum(weights))
The first step is shared across the whole module: values must be a one-dimensional, non-empty numeric vector without NaN values or infinities.
The second step decides whether the function stays in the base case or enters the weighted case.
If
w is None,_validate_weights(...)returnsNoneandweighted_meanexplicitly falls back to the unweighted branch.If
wis given,_validate_weights(...)enforces shape compatibility withx, strict positivity of every weight, and a finite positive total sum.In other words,
weighted_meannever tries to “repair” invalid weights: it requires the vector to already be consistent with the expected statistical meaning.
Implemented formula
In the unweighted case the function simply uses
through np.mean(values).
In the weighted case the code implements instead
This is reflected directly in the last line of the snippet:
return float(np.sum(values * weights) / np.sum(weights))
The numerator sums the
x_icontributions multiplied by their respective weight.The denominator normalizes the result using the sum of the same weights.
The function does not normalize
win advance: it uses the weights as they come, as long as they are valid.
Important interactions between inputs and the result
A few practical details define the actual behavior of weighted_mean.
Uniform weights produce the same result as the plain arithmetic mean, but the internal path stays the weighted one.
The function accepts any positive scale of the weights: multiplying all
w_iby the same constant does not change the final result.Zeros and negative values are not allowed, so there is no branch in which some points are “switched off” with zero weight.
The sum of the weights appears explicitly in the final formula, but the check on its positivity is done earlier in
_validate_weights, so the computation never reaches a zero or non-finite denominator.