-
-
Notifications
You must be signed in to change notification settings - Fork 908
refactor: update stats/incr/wmean
to handle nonnegative weights
#4920
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 2 commits
c9a6ad4
6d48a4b
12552c5
f85aedd
29d359f
a0a1d52
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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. | ||||||
* | ||||||
|
@@ -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 | ||||||
* @returns {(number|null)} weighted mean or null | ||||||
*/ | ||||||
function accumulator( x, w ) { | ||||||
|
@@ -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 ) ); | ||||||
|
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 ) ); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (w === 0.0) { | |
if ( w === 0.0 ) { |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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' ); | ||||||||||||||
|
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' ); |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.