cumav
- mdtools.statistics.cumav(a, axis=None, out=None)[source]
Calculate the cumulative average.
- Parameters:
a (
array_like
) – Array of values for which to calculate the cumulative average. The dtype is converted tonumpy.float64
.axis (
None
orint
, optional) – Axis along which the cumulative average is computed. The default is to compute the cumulative average of the flattened array.out (
array_like
) – Alternative output array in which to place the result. It must have the same shape and buffer length asa.flatten()
(if axis isNone
) or a (if axis is notNone
), but the type will be cast if necessary. Seenumpy.cumsum()
for more details. Note that the result will be cast to the dtype of out, even if this dtype cannot hold the resulting values (e.g. if the dtype of out isint
but the result contains floating point values, the fractional part of these floats is cut). So it’s safest, to parse an output array of dtypenumpy.float64
, also with respect to the accuracy of the average calculation (compare notes ofnumpy.mean()
).
- Returns:
rav (
numpy.ndarray
) – The cumulative average along the specified axis. The result has the same size as a, and also the same shape as a if axis is notNone
or if a is a 1-d array.
See also
numpy.cumsum()
Return the cumulative sum of the elements along a given axis
mdtools.statistics.movav()
Calculate a (weighted) moving average.
Notes
The cumulative average at the \(n\)-th position is given by
\[\mu_n = \frac{1}{n} \sum_{i=0}^{n} a_i\]Examples
>>> a = np.arange(6) >>> mdt.stats.cumav(a) array([0. , 0.5, 1. , 1.5, 2. , 2.5])
>>> a = np.arange(6).reshape(2,3) >>> mdt.stats.cumav(a) array([0. , 0.5, 1. , 1.5, 2. , 2.5]) >>> mdt.stats.cumav(a, axis=0) array([[0. , 1. , 2. ], [1.5, 2.5, 3.5]]) >>> mdt.stats.cumav(a, axis=1) array([[0. , 0.5, 1. ], [3. , 3.5, 4. ]])
>>> a = np.arange(12).reshape(2,2,3) >>> mdt.stats.cumav(a) array([0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. , 5.5]) >>> mdt.stats.cumav(a, axis=0) array([[[0., 1., 2.], [3., 4., 5.]], [[3., 4., 5.], [6., 7., 8.]]]) >>> mdt.stats.cumav(a, axis=1) array([[[0. , 1. , 2. ], [1.5, 2.5, 3.5]], [[6. , 7. , 8. ], [7.5, 8.5, 9.5]]]) >>> mdt.stats.cumav(a, axis=2) array([[[ 0. , 0.5, 1. ], [ 3. , 3.5, 4. ]], [[ 6. , 6.5, 7. ], [ 9. , 9.5, 10. ]]])