Skip to content
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

[RFC]: add stats/incr/nanvariance #5626 #5755

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var pkg = require( './../package.json' ).name;
var incrnanvariance = require( './../lib' );

// MAIN //

bench( pkg, function benchmark( b ) {
var f;
var i;
b.tic();
for ( i = 0; i < b.iterations; i++ ) {
f = incrnanvariance();
if ( typeof f !== 'function' ) {
b.fail( 'should return a function' );
}
}
b.toc();
if ( typeof f !== 'function' ) {
b.fail( 'should return a function' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::accumulator', function benchmark( b ) {
var acc;
var v;
var i;
acc = incrnanvariance();
b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = acc( randu() );
if ( v !== v ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( v !== v ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::accumulator,known_mean', function benchmark( b ) {
var acc;
var v;
var i;
acc = incrnanvariance( 3.14 );
b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = acc( randu() );
if ( v !== v ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( v !== v ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});
40 changes: 40 additions & 0 deletions lib/node_modules/@stdlib/stats/incr/nanvariance/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{{alias}}( [mean] )
Returns an accumulator function which incrementally computes an unbiased
sample variance, ignoring NaN values.

If provided a value, the accumulator function returns an updated unbiased
sample variance. If not provided a value, the accumulator function returns
the current unbiased sample variance.

If provided `NaN`, the `NaN` value is ignored and the current accumulated
variance is returned.

Parameters
----------
mean: number (optional)
Known mean.

Returns
-------
acc: Function
Accumulator function.

Examples
--------
> var accumulator = {{alias}}();
> var s2 = accumulator()
null
> s2 = accumulator( 2.0 )
0.0
> s2 = accumulator( NaN )
0.0
> s2 = accumulator( -5.0 )
24.5
> s2 = accumulator()
24.5

See Also
--------
@stdlib/stats/incr/variance
@stdlib/stats/incr/nanstdev

Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// TypeScript Version: 4.1

// <reference types="@stdlib/types"/>

/**
* If provided a value, returns an updated unbiased sample variance; otherwise, returns the current unbiased sample variance.
*
* ## Notes
*
* - If provided `NaN`, the `NaN` value is ignored and the current accumulated variance is returned.
*
* @param x - value
* @returns unbiased sample variance
*/
type accumulator = ( x?: number ) => number | null;

/**
* Returns an accumulator function which incrementally computes an unbiased sample variance, ignoring NaN values.
*
* @param mu - known mean
* @returns accumulator function
*
* @example
* var accumulator = incrnanvariance();
*
* var s2 = accumulator();
* // returns null
*
* s2 = accumulator( 2.0 );
* // returns 0.0
*
* s2 = accumulator( NaN );
* // returns 0.0
*
* s2 = accumulator( -5.0 );
* // returns 24.5
*
* s2 = accumulator();
* // returns 24.5
*/
declare function incrnanvariance( mu?: number ): accumulator;


// EXPORTS //

export = incrnanvariance;
58 changes: 58 additions & 0 deletions lib/node_modules/@stdlib/stats/incr/nanvariance/docs/types/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import incrnanvariance from './index' ;


// TESTS //

// The function returns an accumulator function...
{
incrnanvariance(); // $ExpectType accumulator
incrnanvariance( 0.0 ); // $ExpectType accumulator
}

// The compiler throws an error if the function is provided invalid arguments...
{
incrnanvariance( '5' as any ); // $ExpectError
incrnanvariance( true as any ); // $ExpectError
incrnanvariance( false as any ); // $ExpectError
incrnanvariance( null as any ); // $ExpectError
incrnanvariance( [] as any ); // $ExpectError
incrnanvariance( {} as any ); // $ExpectError
incrnanvariance( ((x: number): number => x) as any ); // $ExpectError
}

// The function returns an accumulator function which returns an accumulated result...
{
const acc = incrnanvariance();
acc(); // $ExpectType number | null
acc( 3.14 ); // $ExpectType number | null
}

// The compiler throws an error if the returned accumulator function is provided invalid arguments...
{
const acc = incrnanvariance();
acc( '5' as any ); // $ExpectError
acc( true as any ); // $ExpectError
acc( false as any ); // $ExpectError
acc( null as any ); // $ExpectError
acc( [] as any ); // $ExpectError
acc( {} as any ); // $ExpectError
acc( ((x: number): number => x) as any ); // $ExpectError
}
74 changes: 74 additions & 0 deletions lib/node_modules/@stdlib/stats/incr/nanvariance/lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

/**
* Compute an unbiased sample variance incrementally, ignoring NaN values.
*
* @module @stdlib/stats/incr/nanvariance
*
* @example
* var incrnanvariance = require( '@stdlib/stats/incr/nanvariance' );
*
* var accumulator = incrnanvariance();
*
* var v = accumulator();
* // returns null
*
* v = accumulator( 2.0 );
* // returns 0.0
*
* v = accumulator( NaN );
* // returns 0.0
*
* v = accumulator( -5.0 );
* // returns 24.5
*
* v = accumulator();
* // returns 24.5
*
* @example
* var incrnanvariance = require( '@stdlib/stats/incr/nanvariance' );
*
* var accumulator = incrnanvariance( 3.14 );
*
* var v = accumulator();
* // returns null
*
* v = accumulator( 2.0 );
* // returns 1.2996
*
* v = accumulator( NaN );
* // returns 1.2996
*
* v = accumulator( -5.0 );
* // returns 24.4402
*
* v = accumulator();
* // returns 24.4402
*/

// MODULES //

var main = require( './main.js' );


// EXPORTS //

module.exports = main;
Loading
Loading