var_weighted
- mdtools.statistics.var_weighted(a, weights=None, axis=None, return_mean=False)[source]
Compute the weighted variance along a given axis.
- Parameters:
a (
array_like
) – Input array.weights (
array_like
orNone
, optional) – An array of weights associated with the values in a. Each value in a contributes to the variance 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 isNone
, then all data in a are assumed to have a weight equal to one. The only constraint on weights is thatnp.sum(weights)
must not be 0 along the given axis.axis (
None
orint
ortuple
ofints
, optional) – Axis or axes along which the variance is computed. The default is to compute the variance of the flattened array.return_mean (
bool
, optional) – IfTrue
, also return the weighted sample mean \(\bar{X}\) used to calculate the weighted variance.
- Returns:
var (
scalar
) – Weighted variance.mean (
scalar
) – Weighted sample mean. Only returned if return_mean isTrue
.
See also
numpy.average()
Compute the weighted average along the specified axis
numpy.var()
Compute the variance along the specified axis
mdtools.statistics.std_weighted()
Compute the weighted standard deviation along a given axis
Notes
The weighted variance is given by
\[\sigma^2 = \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.