Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion lib/node_modules/@stdlib/stats/incr/wmean/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@

'use strict';

// MODULES //

var isNonNegativeNumber = require( '@stdlib/assert/is-nonnegative-number' ).isPrimitive;
var format = require( '@stdlib/string/format' );


// MAIN //

/**
* Returns an accumulator function which incrementally computes a weighted arithmetic mean.
*
Expand Down Expand Up @@ -97,7 +105,8 @@ function incrwmean() {
*
* @private
* @param {number} [x] - value
* @param {number} [w] - weight
* @param {NonNegativeNumber} [w] - weight
* @throws {TypeError} must provide a nonnegative number as the weight
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 nonnegative number as the weight
* @throws {TypeError} second argument must be a nonnegative number

* @returns {(number|null)} weighted mean or null
*/
function accumulator( x, w ) {
Expand All @@ -107,6 +116,12 @@ function incrwmean() {
}
return mu;
}
if ( !isNonNegativeNumber( w ) ) {
throw new TypeError( format( 'invalid argument. Must provide a nonnegative weight. Weight: `%s`.', w ) );
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
throw new TypeError( format( 'invalid argument. Must provide a nonnegative weight. Weight: `%s`.', w ) );
throw new TypeError( format( 'invalid argument. Second argument must be a nonnegative number. Value: `%s`.', w ) );

Copy link
Member

Choose a reason for hiding this comment

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

We prefer general error messages in order to minimize the total number of unique error messages in the code base. See error/tools/* for additional context.

Copy link
Contributor Author

@anandkaranubc anandkaranubc Jan 28, 2025

Choose a reason for hiding this comment

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

Thanks Athan! It will help me in the further implementations.

Applied the suggestions!

}
if (w === 0.0) {
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
if (w === 0.0) {
if ( w === 0.0 ) {

return ( FLG === void 0 ) ? null : mu;
}
FLG = true;
wsum += w;
mu += ( w/wsum ) * ( x-mu );
Expand Down
58 changes: 42 additions & 16 deletions lib/node_modules/@stdlib/stats/incr/wmean/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,47 @@ tape( 'the initial accumulated value is `null`', function test( t ) {
t.end();
});

tape( 'the accumulator function ignores zero weights and only updates the mean after encountering a non-zero weight', function test( t ) {
var acc = incrwmean();
t.equal( acc(), null, 'returns expected value' );
t.equal( acc( 2.0, 0.0 ), null, 'returns the previous accumulated mean' );
t.equal( acc( 2.0, 2.0 ), 2.0, 'returns the current accumulated mean' );
t.equal( acc( 5.0, 0.0 ), 2.0, 'returns the previous accumulated 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
t.equal( acc( 2.0, 0.0 ), null, 'returns the previous accumulated mean' );
t.equal( acc( 2.0, 2.0 ), 2.0, 'returns the current accumulated mean' );
t.equal( acc( 5.0, 0.0 ), 2.0, 'returns the previous accumulated mean' );
t.equal( acc( 2.0, 0.0 ), null, 'returns expected value' );
t.equal( acc( 2.0, 2.0 ), 2.0, 'returns expected value' );
t.equal( acc( 5.0, 0.0 ), 2.0, 'returns expected value' );

t.end();
});

tape( 'the function throws an error if not provided a nonnegative number', function test( t ) {
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
tape( 'the function throws an error if not provided a nonnegative number', function test( t ) {
tape( 'the accumulator function throws an error if provided a second argument which is not a nonnegative number', function test( t ) {

var values;
var acc;
var i;

values = [
'5',
-5.0,
NaN,
undefined,
true,
false,
null,
void 0,
[],
{},
function noop() {}
];

for ( i = 0; i < values.length; i++ ) {
t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] );
}
t.end();

function badValue( value ) {
return function badValue() {
acc = incrwmean();
acc( 2.0, value );
};
}
});

tape( 'the accumulator function incrementally computes a weighted arithmetic mean', function test( t ) {
var expected;
var actual;
Expand Down Expand Up @@ -99,24 +140,9 @@ tape( 'if not provided arguments, the accumulator function returns the current w
t.end();
});

tape( 'if not provided a weight, the accumulator function returns `NaN`', function test( t ) {
tape( 'if provided `NaN` for a value, the accumulator function returns `NaN`', function test( t ) {
var acc = incrwmean();
t.equal( isnan( acc( 2.0 ) ), true, 'returns NaN' );
t.equal( isnan( acc( 3.14 ) ), true, 'returns NaN' );
t.end();
});

tape( 'if provided `NaN` for either a value or a weight, the accumulator function returns `NaN`', function test( t ) {
var acc = incrwmean();
t.equal( isnan( acc( 2.0, NaN ) ), true, 'returns NaN' );
t.equal( isnan( acc( 3.14, NaN ) ), true, 'returns NaN' );

acc = incrwmean();
t.equal( isnan( acc( NaN, 1.0 ) ), true, 'returns NaN' );
t.equal( isnan( acc( NaN, 1.0 ) ), true, 'returns NaN' );

acc = incrwmean();
t.equal( isnan( acc( NaN, NaN ) ), true, 'returns NaN' );
t.equal( isnan( acc( NaN, NaN ) ), true, 'returns NaN' );
t.end();
});
Loading