Skip to content

Conversation

Harsh-Mathur-1503
Copy link

@Harsh-Mathur-1503 Harsh-Mathur-1503 commented Nov 10, 2024

Resolves #47

Description

What is the purpose of this pull request?

This pull request:

  • Adds an incremental weighted standard deviation function at @stdlib/stats/incr/wstdev to support efficient, real-time computation of weighted standard deviations.

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

Copy link
Contributor

@stdlib-bot stdlib-bot left a comment

Choose a reason for hiding this comment

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

👋 Hi there! 👋

And thank you for opening your first pull request! We will review it shortly. 🏃 💨

@Harsh-Mathur-1503
Copy link
Author

@Planeshifter ready for review !!

@@ -0,0 +1,31 @@
{{alias}}( [mean] )
Copy link
Member

Choose a reason for hiding this comment

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

This file does not follow our REPL text conventions and should be updated accordingly. See the REPL text style guide.

> s = accumulator( 2.0, 1.5 )
0.0
> s = accumulator( -5.0, 2.0 )
updated weighted standard deviation
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 not a valid return annotation.

* var accumulator = incrwstdev( 3.14 );
*/
function incrwstdev( mean ) {
var delta;
Copy link
Member

Choose a reason for hiding this comment

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

We use TAB indentation, not space. Please ensure you have EditorConfig setup in your IDE.

if ( W === 0 ) {
return null;
}
return Math.sqrt( M2 / W );
Copy link
Member

Choose a reason for hiding this comment

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

Use the stdlib package equivalent @stdlib/math/base/special/sqrt.

return Math.sqrt( M2 / W );
}
if ( !isNumber( x ) || !isNumber( w ) || w < 0 ) {
throw new TypeError( 'Both value and non-negative weight are required' );
Copy link
Member

Choose a reason for hiding this comment

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

This error message does not follow our error message conventions.

* // returns 0.0
*
* s = accumulator( -5.0, 2.0 );
* // returns updated weighted standard deviation
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 not a valid return annotation.

* Returns an accumulator function which incrementally computes a weighted standard deviation.
*
* @param {number} [mean] - known mean value
* @throws {TypeError} must provide a number primitive for mean
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* @throws {TypeError} must provide a number primitive for mean
* @throws {TypeError} must provide a number for mean

return Math.sqrt( M2 / W );
}
if ( !isNumber( x ) || !isNumber( w ) || w < 0 ) {
throw new TypeError( 'Both value and non-negative weight are required' );
Copy link
Member

Choose a reason for hiding this comment

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

Same comment.

This function provides an incremental approach to calculating the weighted standard deviation. It computes the value iteratively, updating the result with each new data point and weight, which is useful for scenarios where data is streamed or updated over time.

<!-- /.intro -->
<section class="usage>
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
<section class="usage>
<section class="usage>

@kgryte kgryte added Feature Issue or pull request for adding a new feature. Statistics Issue or pull request related to statistical functionality. Needs Changes Pull request which needs changes before being merged. labels Nov 17, 2024
throw new TypeError( 'Both value and non-negative weight are required' );
}
W += w;
N += 1;
Copy link
Member

Choose a reason for hiding this comment

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

Is N needed in this implementation?

*/
function accumulatorWithoutMean( x, w ) {
if ( arguments.length < 2 ) {
if ( W === 0 ) {
Copy link
Member

Choose a reason for hiding this comment

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

What happens if I provide all weights equal to 0? Should the output standard deviation be null or 0?

* @returns {(number|null)} weighted standard deviation or null
*/
function accumulatorWithoutMean( x, w ) {
if ( arguments.length < 2 ) {
Copy link
Member

Choose a reason for hiding this comment

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

What if I provide only x? That seems like a user error. Did they forget to provide a weight or did they erroneously provide an x?

* @returns {(number|null)} weighted standard deviation or null
*/
function accumulatorWithMean( x, w ) {
if ( arguments.length < 2 ) {
Copy link
Member

Choose a reason for hiding this comment

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

Same comments as above.

@kgryte kgryte changed the title feat(stats/incr): add incremental weighted standard deviation calculation feat: add stats/incr/wstdev Nov 17, 2024
@Planeshifter Planeshifter added the Potential Duplicate There might be another pull request resolving the same issue. label Mar 15, 2025
@kgryte kgryte added the autoclose: Stale Pull request which should be auto-closed as considered stale. label Apr 30, 2025
@stdlib-bot
Copy link
Contributor

This pull request has been automatically closed because it has been inactive for an extended period after changes were requested. If you still wish to pursue this contribution, feel free to reopen the pull request or submit a new one.

We appreciate your interest in contributing to stdlib!

@stdlib-bot stdlib-bot closed this Apr 30, 2025
@stdlib-bot stdlib-bot removed the Potential Duplicate There might be another pull request resolving the same issue. label May 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

autoclose: Stale Pull request which should be auto-closed as considered stale. Feature Issue or pull request for adding a new feature. 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/wstdev

4 participants