Skip to content

Add bias keyword argument to cov - #691

Closed
betatim wants to merge 2 commits into
data-apis:mainfrom
betatim:add-cov-ddof
Closed

Add bias keyword argument to cov#691
betatim wants to merge 2 commits into
data-apis:mainfrom
betatim:add-cov-ddof

Conversation

@betatim

@betatim betatim commented Apr 17, 2026

Copy link
Copy Markdown
Member

I'd like to use xpx.cov in scikit-learn but we need the bias keyword argument.

That is all :D

@bruAristimunha

Copy link
Copy Markdown
Contributor

Hey @betatim!

I think we have small overlap here: #690

@bruAristimunha

Copy link
Copy Markdown
Contributor

Maybe you can review my PR instead:)

@betatim

betatim commented Apr 17, 2026

Copy link
Copy Markdown
Member Author

I hadn't seen your PR :D I'll take a look

bruAristimunha added a commit to bruAristimunha/array-api-extra that referenced this pull request Apr 20, 2026
@betatim

betatim commented Apr 20, 2026

Copy link
Copy Markdown
Member Author

Closing for #690

@betatim betatim closed this Apr 20, 2026
lucascolley pushed a commit to bruAristimunha/array-api-extra that referenced this pull request Jul 26, 2026
lucascolley pushed a commit to bruAristimunha/array-api-extra that referenced this pull request Aug 12, 2026
lucascolley pushed a commit to bruAristimunha/array-api-extra that referenced this pull request Aug 12, 2026
lucascolley added a commit that referenced this pull request Aug 12, 2026
* ENH: expose correction and weights parameters in cov

Resolves #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.

* MNT: drop device= in cov weights

* STY: formatter

* TST: add bias tests from #691

* Update _funcs.py

Co-authored-by: Quentin Barthélemy <q.barthelemy@gmail.com>

* Update _delegation.py

Co-authored-by: Quentin Barthélemy <q.barthelemy@gmail.com>

* MNT: rename weights params to fweights/aweights

* ENH: validate weights shape in cov

* MNT: address lucascolley review

* MNT: move weights validation to generic cov

* Update _funcs.py

Co-authored-by: Quentin Barthélemy <q.barthelemy@gmail.com>

* DOC: explain non-integer correction use cases in cov

Addresses review feedback (kgryte, betatim) that the motivation for
allowing non-integer correction was not obvious from the docstring:
weighted unbiased correction and autocorrelated data both require
fractional values.

* TST: cover weight validation error paths in cov

Adds tests for the 1-D shape and length checks in the generic cov
path. Raises the diff coverage for this PR from 93.33% to 100%.

* Preserve torch autograd in the batched cov path

The generic `cov` implementation called `xp.asarray(m)` on its input. For
torch this detaches gradients and mutates the caller's tensor in place, so
`cov` on a batched tensor (ndim > 2, which routes to the generic path) with
`requires_grad=True` returned a detached result and silently zeroed the
input's grad.

The call is unnecessary: the delegation layer already guarantees `m` is an
array (it calls `array_namespace(m)` and reads `m.ndim`). Drop it, and add
a torch autograd regression test.

* MNT: address cov review feedback

* TYP: clarify array cast in cov warning test

* lint

* Apply suggestions from code review

Co-authored-by: Lucas Colley <lucas.colley8@gmail.com>

* MNT: address cov review comments

- link numpy/torch functions with intersphinx in the cov docstring
  (torch added to the intersphinx mapping)
- add canonical examples for correction, fweights and aweights
- explain the int(correction) cast: torch.cov rejects integer-valued
  floats such as 1.0 at runtime, so typing.cast is not enough
- simplify the integer-correction check to float(correction).is_integer()
- comment that the NaN shape checks account for Dask reporting unknown
  dimensions as NaN instead of None

---------

Co-authored-by: Quentin Barthélemy <q.barthelemy@gmail.com>
Co-authored-by: Lucas Colley <lucas.colley8@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants