cmomy.wrap_raw

Contents

cmomy.wrap_raw#

cmomy.wrap_raw(raw, *, mom_ndim=None, mom_axes=None, mom_params=None, out=None, dtype=None, casting='same_kind', order=None, keep_attrs=None, mom_dims=None, apply_ufunc_kwargs=None)[source]#

Create object from raw moment data.

raw[…, i, j] = <x**i y**j>. raw[…, 0, 0] = weight

Parameters:
  • raw (array-like or DataArray or Dataset) – Raw moment array.

  • mom_ndim ({1, 2}, optional) – Value indicates if moments (mom_ndim = 1) or comoments (mom_ndim=2). If not specified and data is an xarray object attempt to infer mom_ndim from mom_dims. Otherwise, default to mom_ndim = 1.

  • out (ndarray) – Optional output array. If specified, output will be a reference to this array. Note that if the output if method returns a Dataset, then this option is ignored.

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

  • casting ({'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional) –

    Controls what kind of data casting may occur.

    • ’no’ means the data types should not be cast at all.

    • ’equiv’ means only byte-order changes are allowed.

    • ’safe’ means only casts which can preserve values are allowed.

    • ’same_kind’ means only safe casts or casts within a kind, like float64 to float32, are allowed.

    • ’unsafe’ (default) means any data conversions may be done.

  • order ({"C", "F", "A", "K"}, optional) – Order argument. See numpy.asarray().

  • 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.

  • 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

  • apply_ufunc_kwargs (dict-like) – Extra parameters to xarray.apply_ufunc(). One useful option is on_missing_core_dim, which can take the value "copy" (the default), "raise", or "drop" and controls what to do with variables of a Dataset missing core dimensions. Other options are join, dataset_join, dataset_fill_value, and dask_gufunc_kwargs. Unlisted options are handled internally.

Returns:

wrapped (CentralMomentsArray or CentralMomentsData) – Wrapped object. If input data is an xarray object, then return CentralMomentsData instance. Otherwise, return CentralMomentsArray instance.

Notes

Weights are taken from raw[...,0, 0]. Using raw moments can result in numerical issues, especially for higher moments. Use with care.

Examples

>>> import cmomy
>>> rng = cmomy.default_rng(0)
>>> x = rng.random(10)
>>> raw_x = (x[:, None] ** np.arange(5)).mean(axis=0)
>>> dx_raw = cmomy.wrap_raw(raw_x, mom_ndim=1)
>>> print(dx_raw.mean())
0.5505105129032412
>>> dx_raw.cmom()
array([ 1.    ,  0.    ,  0.1014, -0.0178,  0.02  ])

Which is equivalent to creating raw moments from values >>> dx_cen = cmomy.wrap_reduce_vals(x, axis=0, mom=4) >>> print(dx_cen.mean()) 0.5505105129032413 >>> dx_cen.cmom() array([ 1. , 0. , 0.1014, -0.0178, 0.02 ])

But note that calculating using wrap_raw can lead to numerical issues. For example

>>> y = x + 10000
>>> raw_y = (y[:, None] ** np.arange(5)).mean(axis=0)
>>> dy_raw = cmomy.wrap_raw(raw_y, mom_ndim=1)
>>> print(dy_raw.mean() - 10000)
0.5505105129050207

Note that the central moments don’t match!

>>> np.isclose(dy_raw.cmom(), dx_raw.cmom())
array([ True,  True,  True, False, False])
>>> dy_cen = cmomy.wrap_reduce_vals(y, axis=0, mom=4)
>>> print(dy_cen.mean() - 10000)
0.5505105129032017
>>> dy_cen.cmom()  # this matches above
array([ 1.    ,  0.    ,  0.1014, -0.0178,  0.02  ])