cmomy#
A Python package to calculate and manipulate Central (co)moments. The main
features of cmomy
are as follows:
Numba accelerated computation of central moments and co-moments
Routines to combine, and resample central moments.
Supports numpy array and xarray DataArray or Dataset based data.
Routines to convert between central and raw moments.
Overview#
cmomy
is an open source package to calculate central moments and co-moments in
a numerical stable and direct way. Behind the scenes, cmomy
makes use of
Numba to rapidly calculate moments. A good introduction to the type of
formulas used can be found
here.
Features#
Fast calculation of central moments and central co-moments with weights
Support for scalar or vector inputs
numpy and xarray api’s
bootstrap resampling
Status#
This package is actively used by the author. Please feel free to create a pull request for wanted features and suggestions!
Quick start#
Use one of the following
pip install cmomy
or
conda install -c conda-forge cmomy
Example usage#
>>> import numpy as np
>>> import cmomy
>>> rng = cmomy.default_rng(seed=0)
>>> x = rng.random(100)
>>> m = x.mean()
>>> mom = np.array([((x - m) ** i).mean() for i in range(4)])
>>> c = cmomy.wrap_reduce_vals(x, mom=3, axis=0)
>>> np.testing.assert_allclose(c.cmom(), mom, atol=1e-8)
>>> c.cmom()
array([ 1. , 0. , 0.0919, -0.0061])
# break up into chunks
>>> c = cmomy.wrap_reduce_vals(x.reshape(-1, 2), mom=3, axis=0)
>>> c
<CentralMomentsArray(mom_ndim=1)>
array([[ 5.0000e+01, 5.3019e-01, 8.0115e-02, -4.3748e-03],
[ 5.0000e+01, 5.6639e-01, 1.0297e-01, -8.9911e-03]])
# Reduce along an axis
>>> c.reduce(axis=0).cmom()
array([ 1. , 0. , 0.0919, -0.0061])
# unequal chunks
>>> x0, x1, x2 = x[:20], x[20:60], x[60:]
>>> cs = [cmomy.wrap_reduce_vals(_, mom=3, axis=0) for _ in (x0, x1, x2)]
>>> c = cs[0] + cs[1] + cs[2]
>>> np.testing.assert_allclose(c.cmom(), mom, atol=1e-8)
>>> c.cmom()
array([ 1. , 0. , 0.0919, -0.0061])
Note on caching#
This code makes extensive use of the numba python package. This uses a jit compiler to speed up vital code sections. This means that the first time a function called, it has to compile the underlying code. However, caching has been implemented. Therefore, the very first time you run a function, it may be slow. But all subsequent uses (including other sessions) will be already compiled.
- Installation
- User guide
- API reference
- License
- Contributing
- Types of Contributions
- Making a contribution
- Pull Request Guidelines
- Using pre-commit
- Shared numba cache
- Using nox
- ipykernel
- Building the docs
- Testing with nox
- Building distribution for conda
- Building distribution for pypi
- Testing pypi or conda installs
- Testing notebooks with nbval
- Type checking
- Setup development environment
- Package version
- Credits
- Changelog
- Unreleased
- v0.23.0 — 2024-10-30
- v0.22.0 — 2024-09-28
- v0.21.0 — 2024-09-25
- v0.20.0 — 2024-09-21
- v0.19.0 — 2024-09-20
- v0.18.0 — 2024-09-18
- v0.17.0 — 2024-09-16
- v0.16.0 — 2024-08-06
- v0.15.0 — 2024-06-21
- v0.14.0 — 2024-06-20
- v0.13.0 — 2024-06-18
- v0.12.0 — 2024-06-13
- v0.11.0 — 2024-06-12
- v0.9.0 — 2024-04-10
- v0.8.0 — 2024-02-20
- v0.7.0 — 2023-08-11
- v0.5.0 — 2023-06-14
- v0.4.0 — 2023-05-02
- v0.3.0 - 2023-04-24
- v0.2.2 - 2023-04-05
- v0.2.1 - 2023-04-05
- v0.2.0 - 2023-03-22
- v0.1.9 - 2023-02-15
- v0.1.8 - 2022-12-02
- v0.1.7 - 2022-09-28
- v0.1.6 - 2022-09-27
- v0.1.5 - 2022-09-26
- v0.1.4 - 2022-09-15
- v0.1.3 - 2022-09-15
- v0.1.2 - 2022-09-13
- v0.1.1 - 2022-09-13
- v0.1.0 - 2022-09-13
- v0.0.7 - 2021-05-18
- v0.0.6 - 2021-02-03
- v0.0.4 - 2020-12-21
- Indices and tables