Skip to content

Commit 9960320

Browse files
committed
fix(stats): update logic
--- 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: passed - 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: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent f22b800 commit 9960320

File tree

6 files changed

+81
-40
lines changed

6 files changed

+81
-40
lines changed

lib/node_modules/@stdlib/stats/incr/nanmpcorr/docs/repl.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11

22
{{alias}}( W[, mx, my] )
33
Returns an accumulator function which incrementally computes a moving
4-
sample Pearson product-moment correlation coefficient.
4+
sample Pearson product-moment correlation coefficient,
5+
ignoring `NaN` values.
56

67
The `W` parameter defines the number of values over which to compute the
78
moving sample correlation coefficient.

lib/node_modules/@stdlib/stats/incr/nanmpcorr/docs/types/index.d.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
type accumulator = ( x?: number, y?: number ) => number | null;
3535

3636
/**
37-
* Returns an accumulator function which incrementally computes an updated moving sample correlation coefficient.
37+
* Returns an accumulator function which incrementally computes an updated moving sample correlation coefficient, ignoring `NaN` values.
3838
*
3939
* ## Notes
4040
*
@@ -53,7 +53,7 @@ type accumulator = ( x?: number, y?: number ) => number | null;
5353
declare function incrnanmpcorr( W: number, meanx: number, meany: number ): accumulator;
5454

5555
/**
56-
* Returns an accumulator function which incrementally computes an updated moving sample correlation coefficient.
56+
* Returns an accumulator function which incrementally computes an updated moving sample correlation coefficient, ignoring `NaN` values.
5757
*
5858
* ## Notes
5959
*
@@ -74,22 +74,22 @@ declare function incrnanmpcorr( W: number, meanx: number, meany: number ): accum
7474
* // returns 0.0
7575
*
7676
* r = accumulator( -2.0, NaN );
77-
* // returns NaN
77+
* // returns 0.0
7878
*
7979
* r = accumulator( -5.0, 3.14 );
80-
* // returns NaN
80+
* // returns ~-1.0
8181
*
8282
* r = accumulator( NaN, 2.71 );
83-
* // returns NaN
83+
* // returns ~-1.0
8484
*
8585
* r = accumulator( 3.0, -1.0 );
86-
* // returns NaN
86+
* // returns ~-0.925
8787
*
8888
* r = accumulator( 5.0, -9.5 );
89-
* // returns NaN
89+
* // returns ~-0.863
9090
*
9191
* r = accumulator();
92-
* // returns NaN
92+
* // returns ~-0.863
9393
*/
9494
declare function incrnanmpcorr( W: number ): accumulator;
9595

lib/node_modules/@stdlib/stats/incr/nanmpcorr/docs/types/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2019 The Stdlib Authors.
4+
* Copyright (c) 2025 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.

lib/node_modules/@stdlib/stats/incr/nanmpcorr/lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
'use strict';
2020

2121
/**
22-
* Compute a moving sample Pearson product-moment correlation coefficient incrementally.
22+
* Compute a moving sample Pearson product-moment correlation coefficient incrementally, ignoring `NaN` values.
2323
*
2424
* @module @stdlib/stats/incr/nanmpcorr
2525
*

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ var isnan = require( '@stdlib/assert/is-nan' );
3030
// MAIN //
3131

3232
/**
33-
* Returns an accumulator function which incrementally computes a moving sample Pearson product-moment correlation coefficient.
33+
* Returns an accumulator function which incrementally computes a moving sample Pearson product-moment correlation coefficient, ignoring `NaN` values.
3434
*
3535
* @param {PositiveInteger} W - window size
3636
* @param {number} [meanx] - mean value
@@ -99,7 +99,7 @@ function incrnanmpcorr( W, meanx, meany ) {
9999
if ( arguments.length === 0 ) {
100100
return mpcorr();
101101
}
102-
if ( isnan( x ) || isnan( y ) ) {
102+
if ( !isNumber( x ) || !isNumber( y ) || isnan( x ) || isnan( y ) ) {
103103
return mpcorr();
104104
}
105105
return mpcorr( x, y );

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

Lines changed: 67 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @license Apache-2.0
33
*
4-
* Copyright (c) 2018 The Stdlib Authors.
4+
* Copyright (c) 2025 The Stdlib Authors.
55
*
66
* Licensed under the Apache License, Version 2.0 (the "License");
77
* you may not use this file except in compliance with the License.
@@ -172,6 +172,8 @@ function datasets( N, M, seed ) {
172172
var tmp;
173173
var i;
174174
var j;
175+
var x;
176+
var y;
175177

176178
rand = randu.factory({
177179
'seed': seed || ( randu()*pow( 2.0, 31 ) )|0
@@ -182,10 +184,9 @@ function datasets( N, M, seed ) {
182184
for ( i = 0; i < N; i++ ) {
183185
tmp = [];
184186
for ( j = 0; j < M; j++ ) {
185-
tmp.push([
186-
rand() * pow( 10.0, i ),
187-
rand() * pow( 10.0, i )
188-
]);
187+
x = ( rand() < 0.1 ) ? NaN : rand() * pow( 10.0, i );
188+
y = ( rand() < 0.1 ) ? NaN : rand() * pow( 10.0, i );
189+
tmp.push( [ x, y ] );
189190
}
190191
data.push( tmp );
191192
}
@@ -325,21 +326,24 @@ tape( 'the function returns an accumulator function (known means)', function tes
325326
t.end();
326327
});
327328

328-
tape( 'the accumulator function computes a moving sample Pearson product-moment correlation coefficient incrementally', function test( t ) {
329+
tape( 'the accumulator function computes a moving sample Pearson product-moment correlation coefficient incrementally and skip NaN updates', function test( t ) {
330+
var validData;
329331
var expected;
330332
var actual;
331333
var delta;
332334
var means;
335+
var prev;
333336
var data;
334337
var acc;
335-
var arr;
336338
var tol;
337339
var d;
338340
var N;
339341
var M;
340342
var W;
341343
var i;
342344
var j;
345+
var x;
346+
var y;
343347

344348
N = 10;
345349
M = 100;
@@ -351,18 +355,29 @@ tape( 'the accumulator function computes a moving sample Pearson product-moment
351355
// For each dataset, compute the actual and expected correlation coefficients...
352356
for ( i = 0; i < N; i++ ) {
353357
d = data[ i ];
354-
355358
acc = incrnanmpcorr( W );
359+
validData = [];
360+
prev = null;
356361
for ( j = 0; j < M; j++ ) {
357-
actual = acc( d[j][0], d[j][1] );
358-
if ( j < W ) {
359-
arr = d.slice( 0, j+1 );
362+
x = d[ j ][ 0 ];
363+
y = d[ j ][ 1 ];
364+
if ( !isnan( x ) && !isnan( y ) ) {
365+
validData.push( [ x, y ] );
366+
if ( validData.length > W ) {
367+
validData.shift();
368+
}
369+
}
370+
actual = acc( x, y );
371+
if ( isnan( x ) || isnan( y ) ) {
372+
expected = prev;
360373
} else {
361-
arr = d.slice( j-W+1, j+1 );
374+
means = mean( [ 0.0, 0.0 ], validData );
375+
expected = pcorr( validData, means[0], means[1], false );
376+
prev = expected;
362377
}
363-
means = mean( [ 0.0, 0.0 ], arr );
364-
expected = pcorr( arr, means[ 0 ], means[ 1 ], false );
365-
if ( actual === expected ) {
378+
if ( isnan( expected ) ) {
379+
t.equal( isnan( actual ), true, 'returns NaN for window: '+i );
380+
} else if ( actual === expected ) {
366381
t.equal( actual, expected, 'returns expected value. dataset: '+i+'. window: '+j+'.' );
367382
} else {
368383
delta = abs( actual - expected );
@@ -374,43 +389,68 @@ tape( 'the accumulator function computes a moving sample Pearson product-moment
374389
t.end();
375390
});
376391

377-
tape( 'the accumulator function computes a moving sample Pearson product-moment correlation coefficient incrementally (known means)', function test( t ) {
392+
tape( 'the accumulator function computes a moving sample Pearson product-moment correlation coefficient incrementally (known means) and skip NaN updates', function test( t ) {
393+
var validData;
378394
var expected;
379395
var actual;
380396
var means;
381397
var delta;
382398
var data;
399+
var prev;
383400
var acc;
384-
var arr;
385401
var tol;
386402
var d;
387403
var N;
388404
var M;
389405
var W;
390406
var i;
391407
var j;
408+
var x;
409+
var y;
392410

393411
N = 10;
394412
M = 100;
395413
data = datasets( N, M, randu.seed );
396-
397-
// Define the window size:
414+
validData = [];
415+
for ( i = 0; i < N; i++ ) {
416+
d = data[ i ];
417+
for ( j = 0; j < M; j++ ) {
418+
x = d[ j ][ 0 ];
419+
y = d[ j ][ 1 ];
420+
if ( !isnan( x ) && !isnan( y ) ) {
421+
validData.push( [ x, y ] );
422+
}
423+
}
424+
}
425+
means = mean([0.0, 0.0], validData);
398426
W = 10;
399427

400428
// For each dataset, compute the actual and expected correlation coefficients...
401429
for ( i = 0; i < N; i++ ) {
402430
d = data[ i ];
403431
means = mean( [ 0.0, 0.0 ], d );
404432
acc = incrnanmpcorr( W, means[ 0 ], means[ 1 ] );
433+
validData = [];
434+
prev = null;
405435
for ( j = 0; j < M; j++ ) {
406-
actual = acc( d[j][0], d[j][1] );
407-
if ( j < W ) {
408-
arr = d.slice( 0, j+1 );
436+
x = d[ j ][ 0 ];
437+
y = d[ j ][ 1 ];
438+
if ( !isnan( x ) && !isnan( y ) ) {
439+
validData.push( [ x, y ] );
440+
if ( validData.length > W ) {
441+
validData.shift();
442+
}
443+
}
444+
actual = acc( x, y );
445+
if ( isnan( x ) || isnan( y ) ) {
446+
expected = prev;
409447
} else {
410-
arr = d.slice( j-W+1, j+1 );
448+
expected = pcorr(validData, means[0], means[1], true);
449+
prev = expected;
411450
}
412-
expected = pcorr( arr, means[ 0 ], means[ 1 ], true );
413-
if ( actual === expected ) {
451+
if ( isnan( expected ) ) {
452+
t.equal( isnan( actual ), true, 'dataset: '+i+', window: '+j+' returns NaN as expected' );
453+
} else if ( actual === expected ) {
414454
t.equal( actual, expected, 'returns expected value. dataset: '+i+'. window: '+j+'.' );
415455
} else {
416456
delta = abs( actual - expected );
@@ -505,8 +545,8 @@ tape( 'if not provided an input value, the accumulator function returns the curr
505545
N = data.length;
506546
validData = [];
507547
for ( i = 0; i < N; i++ ) {
508-
x = data[i][0];
509-
y = data[i][1];
548+
x = data[ i ][ 0 ];
549+
y = data[ i ][ 1 ];
510550
if ( !isNaN( x ) && !isNaN( y ) ) {
511551
validData.push( [ x, y ] );
512552
}

0 commit comments

Comments
 (0)