Skip to content

Conversation

@SergeantQuickscoper
Copy link

Resolves #5567.

Description

What is the purpose of this pull request?

This pull request:

  • This PR adds the nanmcovariance module to the stats/incr/* namespace, providing a way to compute a moving unbiased sample covariance incrementally, while handling NaN values. This commit was made to address Issue [RFC]: add stats/incr/nanmcovariance #5567 and as suggested in the issue, it is based on a thin wrapper around mcovariance, similar to the relationship between nansum and sum, maintaining API consistency and design. This commit includes appropriate documentation and tests for the new purpose of the package, styles of which are consistent to the stats/incr/* namespace.

Related Issues

Does this pull request have any related issues?

This pull request:

Questions

Any questions for reviewers of this pull request?

No.

Other

Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.

No.

Checklist

Please ensure the following tasks are completed before submitting this pull request.


@stdlib-js/reviewers

…/* namespace

This commit adds the `nanmcovariance` module to the `stats/incr/*` namespace, providing a way to compute a moving unbiased sample covariance incrementally,
while handling NaN values. This commit was made to address Issue stdlib-js#5567 and as suggested in the issue, it is based on a thin wrapper around wmean, similar
to the relationship between nansum and sum, mainting API consistency and design. This commit includes appropriate documentation and tests
for the new purpose of the package, styles of which are consistent to the stats/incr/* namespace.

Fixes: stdlib-js#5567 [RFC]
Private-ref: stdlib-js#5567
Authored-by: Don Chacko <[email protected]>
@stdlib-bot stdlib-bot added Statistics Issue or pull request related to statistical functionality. First-time Contributor A pull request from a contributor who has never previously committed to the project repository. Needs Review A pull request which needs code review. Good First PR A pull request resolving a Good First Issue. labels Apr 29, 2025
@stdlib-bot
Copy link
Contributor

stdlib-bot commented Apr 29, 2025

Coverage Report

Package Statements Branches Functions Lines
stats/incr/nanmcovariance $\color{green}297/297$
$\color{green}+100.00\%$
$\color{green}10/10$
$\color{green}+100.00\%$
$\color{green}2/2$
$\color{green}+100.00\%$
$\color{green}297/297$
$\color{green}+100.00\%$

The above coverage report was generated for the changes in this PR.

…cases

This commit fixes a coverage issue with the previous commit where the NaN branch of the main.js file of the nanmcovariance package, is not adequetly tested.
It now ensures that the branch for known and unknown means are tested and benchmarked, for full coverage.

Authored by: Don Chacko <[email protected]>
Copy link
Member

@Planeshifter Planeshifter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay of getting to this PR; left some initial comments.

* @private
* @param {number} [x] - input value
* @param {number} [y] - input value
* @returns {(number)} unbiased sample covariance
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return type should include null since mcovariance() returns null when no data has been provided yet. See the reference in @stdlib/stats/incr/mcovariance which uses @returns {(number|null)}.

Suggested change
* @returns {(number)} unbiased sample covariance
* @returns {(number|null)} unbiased sample covariance or null

* @returns {(number)} unbiased sample covariance
*/
function accumulator( x, y ) {
if ( arguments.length === 0 || isnan( x ) || isnan( y )) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space before the closing parenthesis. Per stdlib style conventions (see @stdlib/stats/incr/mcovariance for reference), it should be isnan( y ) ) with a space before the final ).

Suggested change
if ( arguments.length === 0 || isnan( x ) || isnan( y )) {
if ( arguments.length === 0 || isnan( x ) || isnan( y ) ) {

type accumulator = ( x?: number, y?: number ) => number | null;

/**
* Returns an accumulator function which incrementally computes a moving unbiased sample covariance.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This overload's description doesn't mention NaN handling, but the second overload at line 52 does. Since NaN handling is the key differentiator of this package, both overloads should mention it for consistency. Consider updating to:

"Returns an accumulator function which incrementally computes a moving unbiased sample covariance, while handling NaN values."

Comment on lines +162 to +170
* * *

## See Also

- <span class="package-name">[`@stdlib/stats/incr/covariance`][@stdlib/stats/incr/covariance]</span><span class="delimiter">: </span><span class="description">compute an unbiased sample covariance incrementally.</span>
- <span class="package-name">[`@stdlib/stats/incr/mpcorr`][@stdlib/stats/incr/mpcorr]</span><span class="delimiter">: </span><span class="description">compute a moving sample Pearson product-moment correlation coefficient incrementally.</span>
- <span class="package-name">[`@stdlib/stats/incr/mcovariance`][@stdlib/stats/incr/mcovariance]</span><span class="delimiter">: </span><span class="description">compute a moving unbiased sample variance incrementally.</span>

</section>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "related" section is auto-populated.

Suggested change
* * *
## See Also
- <span class="package-name">[`@stdlib/stats/incr/covariance`][@stdlib/stats/incr/covariance]</span><span class="delimiter">: </span><span class="description">compute an unbiased sample covariance incrementally.</span>
- <span class="package-name">[`@stdlib/stats/incr/mpcorr`][@stdlib/stats/incr/mpcorr]</span><span class="delimiter">: </span><span class="description">compute a moving sample Pearson product-moment correlation coefficient incrementally.</span>
- <span class="package-name">[`@stdlib/stats/incr/mcovariance`][@stdlib/stats/incr/mcovariance]</span><span class="delimiter">: </span><span class="description">compute a moving unbiased sample variance incrementally.</span>
</section>

Comment on lines +182 to +186
[@stdlib/stats/incr/covariance]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/covariance

[@stdlib/stats/incr/mpcorr]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mpcorr

[@stdlib/stats/incr/mcovariance]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mcovariance
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also auto-populated:

Suggested change
[@stdlib/stats/incr/covariance]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/covariance
[@stdlib/stats/incr/mpcorr]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mpcorr
[@stdlib/stats/incr/mcovariance]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/incr/mcovariance

@Planeshifter Planeshifter added Needs Changes Pull request which needs changes before being merged. and removed Needs Review A pull request which needs code review. labels Jan 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

First-time Contributor A pull request from a contributor who has never previously committed to the project repository. Good First PR A pull request resolving a Good First Issue. Needs Changes Pull request which needs changes before being merged. Statistics Issue or pull request related to statistical functionality.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[RFC]: add stats/incr/nanmcovariance

3 participants