Skip to content

Commit 1bfafba

Browse files
incr nanvariance implementation
1 parent befe02c commit 1bfafba

File tree

9 files changed

+888
-0
lines changed

9 files changed

+888
-0
lines changed

lib/node_modules/@stdlib/stats/incr/nanvariance/README.md

Whitespace-only changes.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var randu = require( '@stdlib/random/base/randu' );
25+
var pkg = require( './../package.json' ).name;
26+
var incrnanvariance = require( './../lib' );
27+
28+
// MAIN //
29+
30+
bench( pkg, function benchmark( b ) {
31+
var f;
32+
var i;
33+
b.tic();
34+
for ( i = 0; i < b.iterations; i++ ) {
35+
f = incrnanvariance();
36+
if ( typeof f !== 'function' ) {
37+
b.fail( 'should return a function' );
38+
}
39+
}
40+
b.toc();
41+
if ( typeof f !== 'function' ) {
42+
b.fail( 'should return a function' );
43+
}
44+
b.pass( 'benchmark finished' );
45+
b.end();
46+
});
47+
48+
bench( pkg+'::accumulator', function benchmark( b ) {
49+
var acc;
50+
var v;
51+
var i;
52+
acc = incrnanvariance();
53+
b.tic();
54+
for ( i = 0; i < b.iterations; i++ ) {
55+
v = acc( randu() );
56+
if ( v !== v ) {
57+
b.fail( 'should not return NaN' );
58+
}
59+
}
60+
b.toc();
61+
if ( v !== v ) {
62+
b.fail( 'should not return NaN' );
63+
}
64+
b.pass( 'benchmark finished' );
65+
b.end();
66+
});
67+
68+
bench( pkg+'::accumulator,known_mean', function benchmark( b ) {
69+
var acc;
70+
var v;
71+
var i;
72+
acc = incrnanvariance( 3.14 );
73+
b.tic();
74+
for ( i = 0; i < b.iterations; i++ ) {
75+
v = acc( randu() );
76+
if ( v !== v ) {
77+
b.fail( 'should not return NaN' );
78+
}
79+
}
80+
b.toc();
81+
if ( v !== v ) {
82+
b.fail( 'should not return NaN' );
83+
}
84+
b.pass( 'benchmark finished' );
85+
b.end();
86+
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{{alias}}( [mean] )
2+
Returns an accumulator function which incrementally computes an unbiased
3+
sample variance, ignoring NaN values.
4+
5+
If provided a value, the accumulator function returns an updated unbiased
6+
sample variance. If not provided a value, the accumulator function returns
7+
the current unbiased sample variance.
8+
9+
If provided `NaN`, the `NaN` value is ignored and the current accumulated
10+
variance is returned.
11+
12+
Parameters
13+
----------
14+
mean: number (optional)
15+
Known mean.
16+
17+
Returns
18+
-------
19+
acc: Function
20+
Accumulator function.
21+
22+
Examples
23+
--------
24+
> var accumulator = {{alias}}();
25+
> var s2 = accumulator()
26+
null
27+
> s2 = accumulator( 2.0 )
28+
0.0
29+
> s2 = accumulator( NaN )
30+
0.0
31+
> s2 = accumulator( -5.0 )
32+
24.5
33+
> s2 = accumulator()
34+
24.5
35+
36+
See Also
37+
--------
38+
@stdlib/stats/incr/variance
39+
@stdlib/stats/incr/nanstdev
40+
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 4.1
20+
21+
//<reference types="@stdlib/types"/>
22+
23+
/**
24+
* If provided a value, returns an updated unbiased sample variance; otherwise, returns the current unbiased sample variance.
25+
*
26+
* ## Notes
27+
*
28+
* - If provided `NaN`, the `NaN` value is ignored and the current accumulated variance is returned.
29+
*
30+
* @param x - value
31+
* @returns unbiased sample variance
32+
*/
33+
type accumulator = ( x?: number ) => number | null;
34+
35+
/**
36+
* Returns an accumulator function which incrementally computes an unbiased sample variance, ignoring NaN values.
37+
*
38+
* @param mu - known mean
39+
* @returns accumulator function
40+
*
41+
* @example
42+
* var accumulator = incrnanvariance();
43+
*
44+
* var s2 = accumulator();
45+
* // returns null
46+
*
47+
* s2 = accumulator( 2.0 );
48+
* // returns 0.0
49+
*
50+
* s2 = accumulator( NaN );
51+
* // returns 0.0
52+
*
53+
* s2 = accumulator( -5.0 );
54+
* // returns 24.5
55+
*
56+
* s2 = accumulator();
57+
* // returns 24.5
58+
*/
59+
declare function incrnanvariance( mu?: number ): accumulator;
60+
61+
62+
// EXPORTS //
63+
64+
export = incrnanvariance;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import incrnanvariance from './index' ;
20+
21+
22+
// TESTS //
23+
24+
// The function returns an accumulator function...
25+
{
26+
incrnanvariance(); // $ExpectType accumulator
27+
incrnanvariance( 0.0 ); // $ExpectType accumulator
28+
}
29+
30+
// The compiler throws an error if the function is provided invalid arguments...
31+
{
32+
incrnanvariance( '5' as any ); // $ExpectError
33+
incrnanvariance( true as any ); // $ExpectError
34+
incrnanvariance( false as any ); // $ExpectError
35+
incrnanvariance( null as any ); // $ExpectError
36+
incrnanvariance( [] as any ); // $ExpectError
37+
incrnanvariance( {} as any ); // $ExpectError
38+
incrnanvariance( ((x: number): number => x) as any ); // $ExpectError
39+
}
40+
41+
// The function returns an accumulator function which returns an accumulated result...
42+
{
43+
const acc = incrnanvariance();
44+
acc(); // $ExpectType number | null
45+
acc( 3.14 ); // $ExpectType number | null
46+
}
47+
48+
// The compiler throws an error if the returned accumulator function is provided invalid arguments...
49+
{
50+
const acc = incrnanvariance();
51+
acc( '5' as any ); // $ExpectError
52+
acc( true as any ); // $ExpectError
53+
acc( false as any ); // $ExpectError
54+
acc( null as any ); // $ExpectError
55+
acc( [] as any ); // $ExpectError
56+
acc( {} as any ); // $ExpectError
57+
acc( ((x: number): number => x) as any ); // $ExpectError
58+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
/**
22+
* Compute an unbiased sample variance incrementally, ignoring NaN values.
23+
*
24+
* @module @stdlib/stats/incr/nanvariance
25+
*
26+
* @example
27+
* var incrnanvariance = require( '@stdlib/stats/incr/nanvariance' );
28+
*
29+
* var accumulator = incrnanvariance();
30+
*
31+
* var v = accumulator();
32+
* // returns null
33+
*
34+
* v = accumulator( 2.0 );
35+
* // returns 0.0
36+
*
37+
* v = accumulator( NaN );
38+
* // returns 0.0
39+
*
40+
* v = accumulator( -5.0 );
41+
* // returns 24.5
42+
*
43+
* v = accumulator();
44+
* // returns 24.5
45+
*
46+
* @example
47+
* var incrnanvariance = require( '@stdlib/stats/incr/nanvariance' );
48+
*
49+
* var accumulator = incrnanvariance( 3.14 );
50+
*
51+
* var v = accumulator();
52+
* // returns null
53+
*
54+
* v = accumulator( 2.0 );
55+
* // returns 1.2996
56+
*
57+
* v = accumulator( NaN );
58+
* // returns 1.2996
59+
*
60+
* v = accumulator( -5.0 );
61+
* // returns 24.4402
62+
*
63+
* v = accumulator();
64+
* // returns 24.4402
65+
*/
66+
67+
// MODULES //
68+
69+
var main = require( './main.js' );
70+
71+
72+
// EXPORTS //
73+
74+
module.exports = main;

0 commit comments

Comments
 (0)