On this page Execution sequence

API Reference / mespy.weighted_mean

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:

  1. Converts x into a one-dimensional, finite float64 vector.

  2. If w is provided, it validates it as a vector with the same shape as x, strictly positive, and with a finite positive sum.

  3. If w is None, it computes the simple arithmetic mean with np.mean(values).

  4. If w is valid, it computes the weighted mean as the ratio between weighted sum and sum of weights.

  5. 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(...) returns None and weighted_mean explicitly falls back to the unweighted branch.

  • If w is given, _validate_weights(...) enforces shape compatibility with x, strict positivity of every weight, and a finite positive total sum.

  • In other words, weighted_mean never 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

\[\bar{x} = \frac{1}{n}\sum_i x_i,\]

through np.mean(values).

In the weighted case the code implements instead

\[\bar{x}_w = \frac{\sum_i w_i x_i}{\sum_i w_i}.\]

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_i contributions multiplied by their respective weight.

  • The denominator normalizes the result using the sum of the same weights.

  • The function does not normalize w in 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_i by 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.