std_weighted

mdtools.statistics.std_weighted(a, weights=None, axis=None, return_mean=False)[source]

Compute the weighted standard deviation along a given axis.

Parameters:
  • a (array_like) – Input array.

  • weights (array_like or None, optional) – An array of weights associated with the values in a. Each value in a contributes to the standard deviation according to its associated weight. The weights array can either be 1-dimensional (in which case its length must be the size of a along the given axis) or of the same shape as a. If weights is None, then all data in a are assumed to have a weight equal to one. The only constraint on weights is that np.sum(weights) must not be 0 along the given axis.

  • axis (None or int or tuple of ints, optional) – Axis or axes along which the standard deviation is computed. The default is to compute the standard deviation of the flattened array.

  • return_mean (bool, optional) – If True, also return the weighted sample mean \(\bar{X}\) used to calculate the weighted standard deviation.

Returns:

  • std (scalar) – Weighted standard deviation.

  • mean (scalar) – Weighted sample mean. Only returned if return_mean is True.

See also

numpy.average()

Compute the weighted average along the specified axis

numpy.std()

Compute the standard deviation along the specified axis

mdtools.statistics.var_weighted()

Compute the weighted variance along a given axis

Notes

The weighted standard deviation is given by

\[\sigma = \sqrt{\sum_{i=1}^N (X_i - \bar{X})^2 \cdot p(X_i)}\]

with the weighted sample mean

\[\bar{X} = \sum_{i=1}^N X_i \cdot p(X_i)\]

the probability of element \(X_i\)

\[p(X_i) = \frac{\sum_{i=1}^N X_i w_i}{\sum_{j=1}^N w_j}\]

and the weight \(w_i\) of the \(i\)-th element.