Conversion routines (convert
)#
Functions:
|
Convert between central and raw moments type. |
|
Convert from moments to comoments data. |
|
Update weights of moments array. |
|
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]
: weightvalues_in[..., 1]
: meanvalues_in[..., k]
: kth central moment
Central comoments of variables a and b:
values_in[..., 0, 0]
: weightvalues_in[..., 1, 0]
: mean of avalues_in[....,0, 1]
: mean of bvalues_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]
: weightvalues_in[..., k]
: kth moment \(\langle a^k \rangle\)
Raw comoments array of variables a and b:
values_in[..., 0, 0]
: weightvalues_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 thevalues_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 frominput_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 withmom_ndim==1
. It is assumed that the last dimension is the moments dimension.mom (
tuple
ofint
) – 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 inmom = (2, -1)
, then this will be transformed tomom = (2, m - 2)
.mom_dims (hashable or
tuple
of hashable) – Name of moment dimensions. Defaults to("mom_0",)
formom_ndim==1
and(mom_0, mom_1)
formom_ndim==2
keep_attrs (
{"drop", "identical", "no_conflicts", "drop_conflicts", "override"}
orbool
, 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
orDataArray
) – Co-moments array. Same type asvalues
.
Notes
mom_dims
andkeep_attrs
are used only ifvalues
is aDataArray
.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:
weight (array-like, optional) – Optional weights. Can be scalar, 1d array of length
args[0].shape[axis]
or array of same form asargs[0]
.mom_ndim (
{1, 2}
) – Value indicates if moments (mom_ndim = 1
) or comoments (mom_ndim=2
).copy (
bool
, defaultTrue
) – IfTrue
(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:
arrays (
Iterable
ofndarray
orDataArray
orCentralMoments
orxCentralMoments
) – Central moments objects to combine.axis (
int
, optional) – Axis to concatenate along. If specify axis forDataArray
orxCentralMoments
input objects with outdim
, then determinedim
fromdim = first.dims[axis]
wherefirst
is the first item inarrays
.dim (
str
, optional) – Dimension to concatenate along (used forDataArray
andxCentralMoments
objects only)**kwargs (Any) – Extra arguments to
numpy.concatenate()
orxarray.concat()
.
- Returns:
output (
ndarray
orDataArray
orCentralMoments
orxCentralMoments
) – Concatenated object. Type is the same as the elements ofarrays
.
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
andxCentralMoments
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