On this page Execution sequence

API Reference / mespy.standard_deviation

Implementation

This page describes the internal flow of standard_deviation and how the function delegates almost all of its work to variance. Unlike Overview, the goal here is not to repeat the parameters, but to show why the practical behavior of standard deviation almost entirely coincides with that of variance.

The following snippets are taken from the current src/mespy/stats_utils.py implementation. Even though _as_float_vector does not appear directly in the body of standard_deviation, it comes into play immediately through variance(...); the same is true for _validate_weights when w is provided.

Execution sequence

The implementation follows this sequence:

  1. Receives x, w, and ddof without performing local validation.

  2. Delegates the variance computation to variance.

  3. Applies np.sqrt(...) to the returned value.

  4. Converts the result to a Python float.

Minimal wrapper over variance

def standard_deviation(
    x: ArrayLike,
    w: ArrayLike | None = None,
    ddof: int | float = 0,
) -> float:
    return float(np.sqrt(variance(x, w=w, ddof=ddof)))

This snippet shows that standard_deviation does not reimplement any part of the statistical logic.

  • It does not validate x directly: validation happens inside variance(...).

  • It does not handle the weights directly: this check is delegated too.

  • It has no branches of its own for the weighted and unweighted cases: it inherits the two branches already present in the variance.

Implemented formula

The operational definition is simply

\[\sigma = \sqrt{\mathrm{Var}(x)}\]

or, in the weighted case,

\[\sigma_w = \sqrt{\mathrm{Var}_w(x)}.\]

For this reason, everything that affects the variance automatically affects the standard deviation as well.

  • ddof is passed unchanged to variance(...).

  • If the variance denominator becomes non-positive, the error is raised before the square root.

  • If the weights are invalid, the failure happens in the shared weight validator, not in standard_deviation itself.

Important interactions with variance

The practical behaviour of the function is easiest to understand by reading this explicit dependency.

  • standard_deviation(x) is equivalent to taking the square root of the unweighted variance with ddof=0.

  • standard_deviation(x, w=w) is equivalent to taking the square root of the weighted variance built with the same weights.

  • standard_deviation(x, ddof=1) does not introduce a new convention: it simply asks variance(...) to use a corrected denominator.

  • Consequently, to really understand errors, edge cases, and the role of ddof, the reference page remains variance.