skdh.utility.math.moving_skewness#

skdh.utility.math.moving_skewness(a, w_len, skip, trim=True, axis=-1, return_previous=True)#

Compute the moving sample skewness.

Parameters:
aarray-like

Signal to compute moving skewness for.

w_lenint

Window length in number of samples.

skipint

Window start location skip in number of samples.

trimbool, optional

Trim the ends of the result, where a value cannot be calculated. If False, these values will be set to NaN. Default is True.

axisint, optional

Axis to compute the moving mean along. Default is -1.

return_previousbool, optional

Return previous moments. These are computed either way, and are therefore optional returns. Default is True.

Returns:
mskewnumpy.ndarray

Moving skewness. Note that if the moving axis is not the last axis, then the result will not be c-contiguous.

msdnumpy.ndarray, optional

Moving sample standard deviation. Note that if the moving axis is not the last axis, then the result will not be c-contiguous. Only returned if return_previous=True.

mmeannumpy.ndarray, optional.

Moving mean. Note that if the moving axis is not the last axis, then the result will not be c-contiguous. Only returned if return_previous=True.

Warning

While this implementation is quite fast, it is also quite mememory inefficient. 3 arrays of equal length to the computation axis are created during computation, which can easily exceed system memory if already using a significant amount of memory.

Notes

On the moving axis, the output length can be computed as follows:

\[\frac{n - w_{len}}{skip} + 1\]

where n is the length of the moving axis. For cases where skip != 1 and trim=False, the length of the return on the moving axis can be calculated as:

\[\frac{n}{skip}\]

Examples

Compute the with non-overlapping windows:

>>> import numpy as np
>>> x = np.arange(10)**2
>>> moving_skewness(x, 3, 3, return_previous=True)
(array([0.52800497, 0.15164108, 0.08720961]),
 array([ 2.081666  ,  8.02080628, 14.0118997 ]),
 array([ 1.66666667, 16.66666667, 49.66666667]))

Compute with overlapping windows:

>>> moving_skewness(x, 3, 1, return_previous=False)
array([0.52800497, 0.29479961, 0.20070018, 0.15164108, 0.12172925,
       0.10163023, 0.08720961, 0.07636413])

Compute without trimming:

>>> moving_skewness(x, 3, 1, trim=False, return_previous=False)
array([0.52800497, 0.29479961, 0.20070018, 0.15164108, 0.12172925,
       0.10163023, 0.08720961, 0.07636413, nan, nan])

Compute on a nd-array to see output shape. On the moving axis, the output should be equal to \((n - w_{len}) / skip + 1\).

>>> n = 500
>>> window_length = 100
>>> window_skip = 50
>>> shape = (3, n, 5, 10)
>>> y = np.random.random(shape)
>>> res = moving_skewness(y, window_length, window_skip, axis=1, return_previous=False)
>>> print(res.shape)
(3, 9, 5, 10)

Check flags for different axis output

>>> z = np.random.random((10, 10, 10))
>>> moving_skewness(z, 3, 3, axis=0, return_previous=False).flags['C_CONTIGUOUS']
False
>>> moving_skewness(z, 3, 3, axis=1, return_previous=False).flags['C_CONTIGUOUS']
False
>>> moving_skewness(z, 3, 3, axis=2, return_previous=False).flags['C_CONTIGUOUS']
True