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:
Receives
x,w, andddofwithout performing local validation.Delegates the variance computation to
variance.Applies
np.sqrt(...)to the returned value.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
xdirectly: validation happens insidevariance(...).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
or, in the weighted case,
For this reason, everything that affects the variance automatically affects the standard deviation as well.
ddofis passed unchanged tovariance(...).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_deviationitself.
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 withddof=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 asksvariance(...)to use a corrected denominator.Consequently, to really understand errors, edge cases, and the role of
ddof, the reference page remainsvariance.