Skip to content

Commit a75d199

Browse files
committed
edit
1 parent 1a812f2 commit a75d199

File tree

5 files changed

+28
-116
lines changed

5 files changed

+28
-116
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
@license Apache-2.0
44
5-
Copyright (c) 2018 The Stdlib Authors.
5+
Copyright (c) 2025 The Stdlib Authors.
66
77
Licensed under the Apache License, Version 2.0 (the "License");
88
you may not use this file except in compliance with the License.
Lines changed: 20 additions & 111 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.
@@ -20,142 +20,51 @@
2020

2121
// MODULES //
2222

23-
var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
24-
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25-
var format = require( '@stdlib/string/format' );
26-
var sqrt = require( '@stdlib/math/base/special/sqrt' );
23+
var isnan = require('@stdlib/math/base/assert/is-nan');
24+
var incrcv = require('./../../cv/lib/main');
2725

2826

2927
// MAIN //
3028

3129
/**
32-
* Returns an accumulator function which incrementally computes the coefficient of variation (CV).
30+
* Returns an accumulator function which incrementally computes the coefficient of variation (CV), ignoring NaN values.
3331
*
34-
* ## Method
35-
*
36-
* - This implementation uses [Welford's method][algorithms-variance] for efficient computation, which can be derived as follows. Let
37-
*
38-
* ```tex
39-
* \begin{align*}
40-
* S_n &= n \sigma_n^2 \\
41-
* &= \sum_{i=1}^{n} (x_i - \mu_n)^2 \\
42-
* &= \biggl(\sum_{i=1}^{n} x_i^2 \biggr) - n\mu_n^2
43-
* \end{align*}
44-
* ```
45-
*
46-
* Accordingly,
47-
*
48-
* ```tex
49-
* \begin{align*}
50-
* S_n - S_{n-1} &= \sum_{i=1}^{n} x_i^2 - n\mu_n^2 - \sum_{i=1}^{n-1} x_i^2 + (n-1)\mu_{n-1}^2 \\
51-
* &= x_n^2 - n\mu_n^2 + (n-1)\mu_{n-1}^2 \\
52-
* &= x_n^2 - \mu_{n-1}^2 + n(\mu_{n-1}^2 - \mu_n^2) \\
53-
* &= x_n^2 - \mu_{n-1}^2 + n(\mu_{n-1} - \mu_n)(\mu_{n-1} + \mu_n) \\
54-
* &= x_n^2 - \mu_{n-1}^2 + (\mu_{n-1} - x_n)(\mu_{n-1} + \mu_n) \\
55-
* &= x_n^2 - \mu_{n-1}^2 + \mu_{n-1}^2 - x_n\mu_n - x_n\mu_{n-1} + \mu_n\mu_{n-1} \\
56-
* &= x_n^2 - x_n\mu_n - x_n\mu_{n-1} + \mu_n\mu_{n-1} \\
57-
* &= (x_n - \mu_{n-1})(x_n - \mu_n) \\
58-
* &= S_{n-1} + (x_n - \mu_{n-1})(x_n - \mu_n)
59-
* \end{align*}
60-
* ```
61-
*
62-
* where we use the identity
63-
*
64-
* ```tex
65-
* x_n - \mu_{n-1} = n (\mu_n - \mu_{n-1})
66-
* ```
67-
*
68-
* [algorithms-variance]: https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
69-
*
70-
* @param {number} [mean] - mean value
71-
* @throws {TypeError} must provide a number primitive
7232
* @returns {Function} accumulator function
7333
*
7434
* @example
75-
* var accumulator = incrcv();
35+
* var accumulator = incrnancv();
7636
*
7737
* var cv = accumulator();
7838
* // returns null
7939
*
80-
* cv = accumulator( 2.0 );
40+
* cv = accumulator(2.0);
41+
* // returns 0.0
42+
*
43+
* cv = accumulator(NaN);
8144
* // returns 0.0
8245
*
83-
* cv = accumulator( 1.0 );
46+
* cv = accumulator(1.0);
8447
* // returns ~0.47
8548
*
8649
* cv = accumulator();
8750
* // returns ~0.47
88-
*
89-
* @example
90-
* var accumulator = incrcv( 3.14 );
9151
*/
92-
function incrcv( mean ) {
93-
var delta;
94-
var mu;
95-
var M2;
96-
var N;
97-
98-
M2 = 0.0;
99-
N = 0;
100-
if ( arguments.length ) {
101-
if ( !isNumber( mean ) ) {
102-
throw new TypeError( format( 'invalid argument. Must provide a number. Value: `%s`.', mean ) );
52+
function incrnancv() {
53+
var cv = incrcv();
54+
function accumulator(x) {
55+
if (arguments.length === 0) {
56+
return cv();
10357
}
104-
mu = mean;
105-
return accumulator2;
106-
}
107-
mu = 0.0;
108-
return accumulator1;
109-
110-
/**
111-
* If provided a value, the accumulator function returns an updated accumulated value. If not provided a value, the accumulator function returns the current accumulated value.
112-
*
113-
* @private
114-
* @param {number} [x] - new value
115-
* @returns {(number|null)} accumulated value or null
116-
*/
117-
function accumulator1( x ) {
118-
if ( arguments.length === 0 ) {
119-
if ( N === 0 ) {
120-
return null;
121-
}
122-
if ( N === 1 ) {
123-
return ( isnan( M2 ) ) ? NaN : 0.0/mu;
124-
}
125-
return sqrt( M2/(N-1) ) / mu;
126-
}
127-
N += 1;
128-
delta = x - mu;
129-
mu += delta / N;
130-
M2 += delta * ( x - mu );
131-
if ( N < 2 ) {
132-
return ( isnan( M2 ) ) ? NaN : 0.0/mu;
58+
if (isnan(x)) {
59+
return cv();
13360
}
134-
return sqrt( M2/(N-1) ) / mu;
61+
return cv(x);
13562
}
13663

137-
/**
138-
* If provided a value, the accumulator function returns an updated accumulated value. If not provided a value, the accumulator function returns the current accumulated value.
139-
*
140-
* @private
141-
* @param {number} [x] - new value
142-
* @returns {(number|null)} accumulated value or null
143-
*/
144-
function accumulator2( x ) {
145-
if ( arguments.length === 0 ) {
146-
if ( N === 0 ) {
147-
return null;
148-
}
149-
return sqrt( M2/N ) / mu;
150-
}
151-
N += 1;
152-
delta = x - mu;
153-
M2 += delta * delta;
154-
return sqrt( M2/N ) / mu;
155-
}
64+
return accumulator;
15665
}
15766

15867

15968
// EXPORTS //
16069

161-
module.exports = incrcv;
70+
module.exports = incrnancv;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@stdlib/stats/incr/cv",
2+
"name": "@stdlib/stats/incr/nancv",
33
"version": "0.0.0",
44
"description": "Compute the coefficient of variation (CV) incrementally.",
55
"license": "Apache-2.0",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type accumulator = ( x?: number ) => number | null;
5050
* cv = accumulator( 1.0 );
5151
* // returns ~0.47
5252
*
53-
* * cv = accumulator( NaN );
53+
* cv = accumulator( NaN );
5454
* // returns ~0.47
5555
*
5656
* cv = accumulator();

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
'use strict';
2020

2121
// MODULES //
22-
var incrcv = require( './../../cv/lib/main' );
22+
2323
var isnan = require( '@stdlib/math/base/assert/is-nan' );
24+
var incrcv = require( './../../cv/lib/main' );
25+
2426

2527
// MAIN //
2628

@@ -38,7 +40,7 @@ var isnan = require( '@stdlib/math/base/assert/is-nan' );
3840
* cv = accumulator( 2.0 );
3941
* // returns 0.0
4042
*
41-
* cv = accumulator( NaN );
43+
* cv = accumulator( NaN );
4244
* // returns 0.0
4345
*
4446
* cv = accumulator( 1.0 );
@@ -61,6 +63,7 @@ function incrnancv() {
6163
};
6264
}
6365

66+
6467
// EXPORTS //
6568

6669
module.exports = incrnancv;

0 commit comments

Comments
 (0)