Skip to content

Commit c7a50f0

Browse files
committed
refactor: update tests to handle nonnegative weights
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: na - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- type: pre_push_report description: Results of running various checks prior to pushing changes. report: - task: run_javascript_examples status: na - task: run_c_examples status: na - task: run_cpp_examples status: na - task: run_javascript_readme_examples status: passed - task: run_c_benchmarks status: na - task: run_cpp_benchmarks status: na - task: run_fortran_benchmarks status: na - task: run_javascript_benchmarks status: na - task: run_julia_benchmarks status: na - task: run_python_benchmarks status: na - task: run_r_benchmarks status: na - task: run_javascript_tests status: passed ---
1 parent a0e8770 commit c7a50f0

File tree

2 files changed

+38
-21
lines changed

2 files changed

+38
-21
lines changed

lib/node_modules/@stdlib/stats/incr/wstdev/lib/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ var format = require( '@stdlib/string/format' );
8888
* \sigma_n = \sqrt{\frac{M2_n}{W_n}}
8989
* ```
9090
*
91-
* - Incrementally updating the weighted mean:
91+
* - Incrementally updating the weighted sample mean:
9292
*
9393
* ```tex
9494
* \begin{align*}
@@ -160,7 +160,7 @@ function incrwstdev( mean ) {
160160
throw new TypeError( format( 'invalid argument. Second argument must be a nonnegative number. Value: `%s`.', w ) );
161161
}
162162
if ( w === 0.0 ) {
163-
return ( FLG === void 0 ) ? null : mu;
163+
return ( FLG === void 0 ) ? null : sqrt( M2 / wsum );
164164
}
165165
FLG = true;
166166
delta = x - mu;

lib/node_modules/@stdlib/stats/incr/wstdev/test/test.js

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,46 @@ tape( 'the function throws an error if provided a non-numeric value', function t
7272
}
7373
});
7474

75-
tape( 'the weighted sample standard deviation is `null` until at least 1 datum has been provided (known mean)', function test( t ) {
76-
var acc;
77-
var s;
78-
79-
acc = incrwstdev( 3.0 );
80-
81-
s = acc();
82-
t.equal( s, null, 'returns null' );
75+
tape( 'the accumulator function ignores zero weights and only updates the weighted sample standard deviation after encountering a non-zero weight', function test( t ) {
76+
var acc = incrwstdev();
77+
t.equal( acc(), null, 'returns expected value' );
78+
t.equal( acc( 2.0, 0.0 ), null, 'returns expected value' );
79+
t.equal( acc( 2.0, 2.0 ), 0.0, 'returns expected value' );
80+
t.equal( acc( 14.0, 2.0 ), 6.0, 'returns expected value' );
81+
t.equal( acc( 25.0, 0.0 ), 6.0, 'returns expected value' );
82+
t.end();
83+
});
8384

84-
s = acc( 3.0 );
85-
t.notEqual( s, null, 'does not return null' );
85+
tape( 'the accumulator function throws an error if provided a second argument which is not a nonnegative number', function test( t ) {
86+
var values;
87+
var acc;
88+
var i;
8689

87-
s = acc();
88-
t.notEqual( s, null, 'does not return null' );
90+
values = [
91+
'5',
92+
-5.0,
93+
NaN,
94+
undefined,
95+
true,
96+
false,
97+
null,
98+
void 0,
99+
[],
100+
{},
101+
function noop() {}
102+
];
89103

104+
for ( i = 0; i < values.length; i++ ) {
105+
t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+values[i] );
106+
}
90107
t.end();
108+
109+
function badValue( value ) {
110+
return function badValue() {
111+
acc = incrwstdev();
112+
acc( 2.0, value );
113+
};
114+
}
91115
});
92116

93117
tape( 'the accumulator function incrementally computes a weighted sample standard deviation', function test( t ) {
@@ -176,13 +200,6 @@ tape( 'if not provided an input value, the accumulator function returns the curr
176200
t.end();
177201
});
178202

179-
tape( 'if not provided a weight, the accumulator function returns `NaN`', function test( t ) {
180-
var acc = incrwstdev();
181-
t.equal( isnan( acc( 2.0 ) ), true, 'returns NaN' );
182-
t.equal( isnan( acc( 3.14 ) ), true, 'returns NaN' );
183-
t.end();
184-
});
185-
186203
tape( 'if provided `NaN` for a value, the accumulator function returns `NaN`', function test( t ) {
187204
var acc = incrwstdev();
188205
t.equal( isnan( acc( NaN, 1.0 ) ), true, 'returns NaN' );

0 commit comments

Comments
 (0)