Skip to content

Commit c2e1cf1

Browse files
fix: changes
1 parent 117fd8b commit c2e1cf1

File tree

6 files changed

+98
-272
lines changed

6 files changed

+98
-272
lines changed
Lines changed: 30 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,31 @@
1-
/*
2-
* @license Apache-2.0
3-
*
4-
* Copyright (c) 2025 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-
191
{{alias}}( [mean] )
20-
Returns an accumulator function which incrementally computes an unbiased sample variance, ignoring NaN values.
21-
22-
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.
23-
24-
If provided `NaN`, the `NaN` value is ignored and the current accumulated variance is returned.
25-
26-
27-
Parameters
28-
----------
29-
mean: number (optional)
30-
Known mean.
31-
32-
33-
Returns
34-
-------
35-
acc: Function
36-
Accumulator function.
37-
38-
39-
Examples
40-
--------
41-
> var accumulator = {{alias}}();
42-
> var s2 = accumulator()
43-
null
44-
> s2 = accumulator( 2.0 )
45-
0.0
46-
> s2 = accumulator( NaN )
47-
0.0
48-
> s2 = accumulator( -5.0 )
49-
24.5
50-
> s2 = accumulator()
51-
24.5
52-
53-
54-
See Also
55-
--------
56-
@stdlib/stats/incr/variance
57-
@stdlib/stats/incr/nanstdev
2+
Returns an accumulator function which incrementally computes an unbiased
3+
sample variance, ignoring NaN values.
4+
If provided a value, the accumulator function returns an updated unbiased
5+
sample variance. If not provided a value, the accumulator function returns
6+
the current unbiased sample variance.
7+
If provided `NaN`, the `NaN` value is ignored and the current accumulated
8+
variance is returned.
9+
Parameters
10+
----------
11+
mean: number (optional)
12+
Known mean.
13+
Returns
14+
-------
15+
acc: Function
16+
Accumulator function.
17+
Examples
18+
--------
19+
> var accumulator = {{alias}}();
20+
> var s2 = accumulator()
21+
null
22+
> s2 = accumulator( 2.0 )
23+
0.0
24+
> s2 = accumulator( NaN )
25+
0.0
26+
> s2 = accumulator( -5.0 )
27+
24.5
28+
> s2 = accumulator()
29+
24.5
30+
See Also
31+
--------

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

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
* limitations under the License.
1717
*/
1818

19-
import incrnanvariance from './index' ;
20-
19+
import incrnanvariance = require( './index' );
2120

2221
// TESTS //
2322

@@ -29,13 +28,13 @@ import incrnanvariance from './index' ;
2928

3029
// The compiler throws an error if the function is provided invalid arguments...
3130
{
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
31+
incrnanvariance( '5' ); // $ExpectError
32+
incrnanvariance( true ); // $ExpectError
33+
incrnanvariance( false ); // $ExpectError
34+
incrnanvariance( null ); // $ExpectError
35+
incrnanvariance( [] ); // $ExpectError
36+
incrnanvariance( {} ); // $ExpectError
37+
incrnanvariance( ( x: number ): number => x ); // $ExpectError
3938
}
4039

4140
// The function returns an accumulator function which returns an accumulated result...
@@ -48,11 +47,11 @@ import incrnanvariance from './index' ;
4847
// The compiler throws an error if the returned accumulator function is provided invalid arguments...
4948
{
5049
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-
}
50+
acc( '5' ); // $ExpectError
51+
acc( true ); // $ExpectError
52+
acc( false ); // $ExpectError
53+
acc( null ); // $ExpectError
54+
acc( [] ); // $ExpectError
55+
acc( {} ); // $ExpectError
56+
acc( ( x: number ): number => x ); // $ExpectError
57+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
* // returns null
5353
*
5454
* v = accumulator( 2.0 );
55-
* // returns 1.2996
55+
* // returns ~1.2996
5656
*
5757
* v = accumulator( NaN );
5858
* // returns 1.2996

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

Lines changed: 11 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,16 @@
2020

2121
// MODULES //
2222

23-
var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
2423
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25-
var format = require( '@stdlib/string/format' );
24+
var incrvariance = require( '@stdlib/stats/incr/variance' );
2625

2726

2827
// MAIN //
2928

3029
/**
3130
* Returns an accumulator function which incrementally computes an unbiased sample variance, ignoring NaN values.
3231
*
33-
* ## Method
34-
*
35-
* - This implementation uses Welford's algorithm for efficient computation, which is a numerically stable version of the following recurrence relation:
36-
*
37-
* ```tex
38-
* \begin{align}
39-
* S_n &= \sum_{i=0}^{n} (x_i - \bar{x}_n)^2\\
40-
* \end{align}
41-
* ```
42-
*
43-
* where $S_n$ is the sum of squared deviations from the mean.
44-
*
45-
* ## References
46-
*
47-
* - Welford, B. P. (1962). "Note on a Method for Calculating Corrected Sums of Squares and Products." _Technometrics_ 4(3):419–20. <https://doi.org/10.1080/00401706.1962.10490022>.
48-
* - van Reeken, A. J. (1968). "Letters to the Editor: Dealing with Neely's Algorithms." _Communications of the ACM_ 11(3):149–50. <https://doi.org/10.1145/362929.362961>.
49-
*
5032
* @param {number} [mean] - mean value
51-
* @throws {TypeError} must provide a number primitive
5233
* @returns {Function} accumulator function
5334
*
5435
* @example
@@ -88,59 +69,15 @@ var format = require( '@stdlib/string/format' );
8869
* // returns 33.7796
8970
*/
9071
function incrnanvariance( mean ) {
91-
var delta;
92-
var mu;
93-
var M2;
94-
var N;
95-
96-
M2 = 0.0;
97-
N = 0;
72+
var variance;
9873

99-
if ( arguments.length ) {
100-
if ( !isNumber( mean ) ) {
101-
throw new TypeError( format( 'invalid argument. Must provide a number. Value: `%s`.', mean ) );
102-
}
103-
mu = mean;
104-
return accumulator2;
74+
if ( arguments.length === 0 ) {
75+
variance = incrvariance();
76+
} else {
77+
variance = incrvariance( mean );
10578
}
106-
mu = 0.0;
107-
return accumulator1;
10879

109-
/**
110-
* 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.
111-
*
112-
* @private
113-
* @param {number} [x] - new value
114-
* @returns {(number|null)} unbiased sample variance or null
115-
*/
116-
function accumulator1( x ) {
117-
if ( arguments.length === 0 ) {
118-
if ( N === 0 ) {
119-
return null;
120-
}
121-
if ( N === 1 ) {
122-
return 0.0;
123-
}
124-
return M2 / (N-1);
125-
}
126-
if ( isnan( x ) ) {
127-
if ( N === 0 ) {
128-
return null;
129-
}
130-
if ( N === 1 ) {
131-
return 0.0;
132-
}
133-
return M2 / (N-1);
134-
}
135-
N += 1;
136-
delta = x - mu;
137-
mu += delta / N;
138-
M2 += delta * ( x - mu );
139-
if ( N < 2 ) {
140-
return 0.0;
141-
}
142-
return M2 / (N-1);
143-
}
80+
return accumulator;
14481

14582
/**
14683
* 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.
@@ -149,24 +86,11 @@ function incrnanvariance( mean ) {
14986
* @param {number} [x] - new value
15087
* @returns {(number|null)} unbiased sample variance or null
15188
*/
152-
function accumulator2( x ) {
153-
if ( arguments.length === 0 ) {
154-
if ( N === 0 ) {
155-
return null;
156-
}
157-
return M2 / N;
158-
}
159-
if ( isnan( x ) ) {
160-
if ( N === 0 ) {
161-
return null;
162-
}
163-
return M2 / N;
89+
function accumulator( x ) {
90+
if ( arguments.length === 0 || isnan( x ) ) {
91+
return variance();
16492
}
165-
// Important: For the known mean case, we're simply summing squared deviations
166-
delta = x - mu;
167-
M2 += delta * delta;
168-
N += 1;
169-
return M2 / N;
93+
return variance( x );
17094
}
17195
}
17296

lib/node_modules/@stdlib/stats/incr/nanvariance/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@
6666
"incremental",
6767
"accumulator",
6868
"nan",
69-
"missing values",
70-
"ignore",
71-
"ignoring"
69+
"missing values"
7270
]
7371
}

0 commit comments

Comments
 (0)