ENH: expose correction and weights parameters in cov#690
Open
bruAristimunha wants to merge 1 commit intodata-apis:mainfrom
Open
ENH: expose correction and weights parameters in cov#690bruAristimunha wants to merge 1 commit intodata-apis:mainfrom
bruAristimunha wants to merge 1 commit intodata-apis:mainfrom
Conversation
Resolves data-apis#688. Adds `axis`, `correction`, `frequency_weights`, and `weights` to `cov`, giving users control over the degrees-of-freedom correction and the observation-axis / weighted variants that `numpy.cov` and `torch.cov` already support. Naming follows array-api conventions (`axis`, `correction`) rather than numpy's (`rowvar`, `bias`, `ddof`); the docstring includes a one-to-one mapping. The delegation moves observations to the last axis via `xp.moveaxis`, collapsing `rowvar` out of the backend dispatch — only `ddof` vs `correction` differs between branches. Dask's native `cov` forces `.compute()` on a lazy scalar when any weights are given, so weighted dask inputs fall through to the generic implementation, which is fully lazy.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves #688.
Summary
axis,correction,frequency_weights, andweightsparameters toxpx.cov, unlocking the degrees-of-freedom and weighted variants thatnumpy.covandtorch.covalready support.axis,correction) used elsewhere in this library rather than numpy's (rowvar,bias,ddof). The docstring includes a one-to-one mapping for users migrating fromnumpy.cov.Design
The delegation moves observations to the last axis via
xp.moveaxis, which collapsesrowvarout of backend dispatch entirely — onlyddof(numpy/cupy/dask/jax) vscorrection(torch) differs between branches.Fallbacks to the generic implementation (
_funcs.cov):m.ndim > 2(batched input, not supported by any native).correction(rejected bynumpy.cov'sddof).dask.array.covforces.compute()on a lazy 0-D scalar via its internalif fact <= 0check. The generic path stays fully lazy because its weighted branch doesn't comparefactto zero (noted in docstring).Weighted formula in
_funcs.covmatches numpy's (algebraically):c = (m_c · w) @ m_c.T / (v1 - correction · v2 / v1).Tests
New
TestCovcases validate againstnp.covas reference:test_correction(integer ddof)test_correction_float(generic-path-only, hand-computed reference)test_axis/test_axis_with_weights/test_axis_out_of_boundstest_frequency_weights/test_weights/test_both_weightstest_batch_with_weightsTest plan
pytest tests/test_funcs.py::TestCov— 126 passed across numpy, torch, jax, dask, array-api-strictpytest tests/test_funcs.pyfull — 4263 passed, 0 failedlefthook run pre-commit— ruff, numpydoc, mypy, pyright, typos all greenlazy_xp_function(cov)asserts 0.compute()calls, holds for weighted path via the fallback