Conversion routines (convert)#

Functions:

moments_type(values_in, *, mom_ndim[, to, ...])

Convert between central and raw moments type.

moments_to_comoments(values, *, mom[, ...])

Convert from moments to comoments data.

assign_weight(data, weight, *, mom_ndim[, copy])

Update weights of moments array.

concat(arrays, *[, axis, dim])

Concatenate moments objects.

cmomy.convert.moments_type(values_in, *, mom_ndim, to='central', out=None, dtype=None)[source]#

Convert between central and raw moments type.

The structure of arrays are as follow. Central moments:

  • values_in[..., 0] : weight

  • values_in[..., 1] : mean

  • values_in[..., k] : kth central moment

Central comoments of variables a and b:

  • values_in[..., 0, 0] : weight

  • values_in[..., 1, 0] : mean of a

  • values_in[....,0, 1] : mean of b

  • values_in[..., i, j] : \(\langle (\delta a)^i (\delta b)^j \rangle\),

where a and b are the variables being considered.

Raw moments array:

  • values_in[..., 0] : weight

  • values_in[..., k] : kth moment \(\langle a^k \rangle\)

Raw comoments array of variables a and b:

  • values_in[..., 0, 0] : weight

  • values_in[..., i, j] : \(\langle a^i b^j \rangle\),

Parameters:
  • values_in (ndarray) – The moments array to convert from.

  • mom_ndim ({1, 2}) – Value indicates if moments (mom_ndim = 1) or comoments (mom_ndim=2).

  • to ({"raw", "central"}) – The style of the values_in to convert to. If "raw", convert from central to raw. If "central" convert from raw to central moments.

  • out (ndarray) – Optional output array. If specified, output will be a reference to this array.

Returns:

ndarray – Moments array converted from input_style to opposite format.

cmomy.convert.moments_to_comoments(values, *, mom, dtype=None, mom_dims=None, keep_attrs=None)[source]#

Convert from moments to comoments data.

Parameters:
  • values (array-like or DataArray) – Moments array with mom_ndim==1. It is assumed that the last dimension is the moments dimension.

  • mom (tuple of int) – Moments for comoments array. Pass a negative value for one of the moments to fill all available moments for that dimensions. For example, if original array has moments m (i.e., values.shape=(..., m + 1)), and pass in mom = (2, -1), then this will be transformed to mom = (2, m - 2).

  • dtype (dtype) – Optional dtype for output data.

  • mom_dims (hashable or tuple of hashable) – Name of moment dimensions. Defaults to ("mom_0",) for mom_ndim==1 and (mom_0, mom_1) for mom_ndim==2

  • keep_attrs ({"drop", "identical", "no_conflicts", "drop_conflicts", "override"} or bool, optional) –

    • ‘drop’ or False: empty attrs on returned xarray object.

    • ’identical’: all attrs must be the same on every object.

    • ’no_conflicts’: attrs from all objects are combined, any that have the same name must also have the same value.

    • ’drop_conflicts’: attrs from all objects are combined, any that have the same name but different values are dropped.

    • ’override’ or True: skip comparing and copy attrs from the first object to the result.

Returns:

output (ndarray or DataArray) – Co-moments array. Same type as values.

Notes

mom_dims and keep_attrs are used only if values is a DataArray.

Examples

>>> import cmomy
>>> x = cmomy.random.default_rng(0).random(10)
>>> data1 = cmomy.reduce_vals(x, mom=4, axis=0)
>>> data1
array([10.    ,  0.5505,  0.1014, -0.0178,  0.02  ])
>>> moments_to_comoments(data1, mom=(2, -1))
array([[10.    ,  0.5505,  0.1014],
       [ 0.5505,  0.1014, -0.0178],
       [ 0.1014, -0.0178,  0.02  ]])

Which is identical to

>>> cmomy.reduction.reduce_vals(x, x, mom=(2, 2), axis=0)
array([[10.    ,  0.5505,  0.1014],
       [ 0.5505,  0.1014, -0.0178],
       [ 0.1014, -0.0178,  0.02  ]])

This also works for DataArray data

>>> xdata = xr.DataArray(data1, dims="mom")
>>> xdata
<xarray.DataArray (mom: 5)> Size: 40B
array([10.    ,  0.5505,  0.1014, -0.0178,  0.02  ])
Dimensions without coordinates: mom
>>> moments_to_comoments(xdata, mom=(2, -1))
<xarray.DataArray (mom_0: 3, mom_1: 3)> Size: 72B
array([[10.    ,  0.5505,  0.1014],
       [ 0.5505,  0.1014, -0.0178],
       [ 0.1014, -0.0178,  0.02  ]])
Dimensions without coordinates: mom_0, mom_1

Note that this also works for raw moments.

cmomy.convert.assign_weight(data, weight, *, mom_ndim, copy=True)[source]#

Update weights of moments array.

Parameters:
  • data (ndarray or DataArray) – Moments array.

  • weight (array-like, optional) – Optional weights. Can be scalar, 1d array of length args[0].shape[axis] or array of same form as args[0].

  • mom_ndim ({1, 2}) – Value indicates if moments (mom_ndim = 1) or comoments (mom_ndim=2).

  • copy (bool, default True) – If True (the default), return new array with updated weights. Otherwise, return the original array with weights updated inplace.

cmomy.convert.concat(arrays, *, axis=MISSING, dim=MISSING, **kwargs)[source]#

Concatenate moments objects.

Parameters:
Returns:

output (ndarray or DataArray or CentralMoments or xCentralMoments) – Concatenated object. Type is the same as the elements of arrays.

Examples

>>> import cmomy
>>> shape = (2, 1, 2)
>>> x = np.arange(np.prod(shape)).reshape(shape).astype(np.float64)
>>> y = -x
>>> out = concat((x, y), axis=1)
>>> out.shape
(2, 2, 2)
>>> out
array([[[ 0.,  1.],
        [-0., -1.]],

       [[ 2.,  3.],
        [-2., -3.]]])
>>> dx = xr.DataArray(x, dims=["a", "b", "mom"])
>>> dy = xr.DataArray(y, dims=["a", "b", "mom"])
>>> concat((dx, dy), dim="b")
<xarray.DataArray (a: 2, b: 2, mom: 2)> Size: 64B
array([[[ 0.,  1.],
        [-0., -1.]],

       [[ 2.,  3.],
        [-2., -3.]]])
Dimensions without coordinates: a, b, mom

For DataArray objects, you can specify a new dimension

>>> concat((dx, dy), dim="new")
<xarray.DataArray (new: 2, a: 2, b: 1, mom: 2)> Size: 64B
array([[[[ 0.,  1.]],

        [[ 2.,  3.]]],


       [[[-0., -1.]],

        [[-2., -3.]]]])
Dimensions without coordinates: new, a, b, mom

You can also concatenate CentralMoments and xCentralMoments objects

>>> cx = cmomy.CentralMoments(x)
>>> cy = cmomy.CentralMoments(y)
>>> concat((cx, cy), axis=1)
<CentralMoments(val_shape=(2, 2), mom=(1,))>
array([[[ 0.,  1.],
        [-0., -1.]],

       [[ 2.,  3.],
        [-2., -3.]]])
>>> dcx = cmomy.xCentralMoments(dx)
>>> dcy = cmomy.xCentralMoments(dy)
>>> concat((dcx, dcy), dim="new")
<xCentralMoments(val_shape=(2, 2, 1), mom=(1,))>
<xarray.DataArray (new: 2, a: 2, b: 1, mom: 2)> Size: 64B
array([[[[ 0.,  1.]],

        [[ 2.,  3.]]],


       [[[-0., -1.]],

        [[-2., -3.]]]])
Dimensions without coordinates: new, a, b, mom