@stdlib/stats-strided-svariancewd 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/ndarray.js ADDED
@@ -0,0 +1,117 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2020 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 float64ToFloat32 = require( '@stdlib/number-float64-base-to-float32' );
24
+
25
+
26
+ // MAIN //
27
+
28
+ /**
29
+ * Computes the variance of a single-precision floating-point strided array using Welford's algorithm.
30
+ *
31
+ * ## Method
32
+ *
33
+ * - This implementation uses Welford's algorithm for efficient computation, which can be derived as follows. Let
34
+ *
35
+ * ```tex
36
+ * \begin{align*}
37
+ * S_n &= n \sigma_n^2 \\
38
+ * &= \sum_{i=1}^{n} (x_i - \mu_n)^2 \\
39
+ * &= \biggl(\sum_{i=1}^{n} x_i^2 \biggr) - n\mu_n^2
40
+ * \end{align*}
41
+ * ```
42
+ *
43
+ * Accordingly,
44
+ *
45
+ * ```tex
46
+ * \begin{align*}
47
+ * 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 \\
48
+ * &= x_n^2 - n\mu_n^2 + (n-1)\mu_{n-1}^2 \\
49
+ * &= x_n^2 - \mu_{n-1}^2 + n(\mu_{n-1}^2 - \mu_n^2) \\
50
+ * &= x_n^2 - \mu_{n-1}^2 + n(\mu_{n-1} - \mu_n)(\mu_{n-1} + \mu_n) \\
51
+ * &= x_n^2 - \mu_{n-1}^2 + (\mu_{n-1} - x_n)(\mu_{n-1} + \mu_n) \\
52
+ * &= 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} \\
53
+ * &= x_n^2 - x_n\mu_n - x_n\mu_{n-1} + \mu_n\mu_{n-1} \\
54
+ * &= (x_n - \mu_{n-1})(x_n - \mu_n) \\
55
+ * &= S_{n-1} + (x_n - \mu_{n-1})(x_n - \mu_n)
56
+ * \end{align*}
57
+ * ```
58
+ *
59
+ * where we use the identity
60
+ *
61
+ * ```tex
62
+ * x_n - \mu_{n-1} = n (\mu_n - \mu_{n-1})
63
+ * ```
64
+ *
65
+ * ## References
66
+ *
67
+ * - Welford, B. P. 1962. "Note on a Method for Calculating Corrected Sums of Squares and Products." _Technometrics_ 4 (3). Taylor & Francis: 419–20. doi:[10.1080/00401706.1962.10490022](https://doi.org/10.1080/00401706.1962.10490022).
68
+ * - van Reeken, A. J. 1968. "Letters to the Editor: Dealing with Neely's Algorithms." _Communications of the ACM_ 11 (3): 149–50. doi:[10.1145/362929.362961](https://doi.org/10.1145/362929.362961).
69
+ *
70
+ * @param {PositiveInteger} N - number of indexed elements
71
+ * @param {number} correction - degrees of freedom adjustment
72
+ * @param {Float32Array} x - input array
73
+ * @param {integer} strideX - stride length
74
+ * @param {NonNegativeInteger} offsetX - starting index
75
+ * @returns {number} variance
76
+ *
77
+ * @example
78
+ * var Float32Array = require( '@stdlib/array-float32' );
79
+ *
80
+ * var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
81
+ *
82
+ * var v = svariancewd( 4, 1, x, 2, 1 );
83
+ * // returns 6.25
84
+ */
85
+ function svariancewd( N, correction, x, strideX, offsetX ) {
86
+ var delta;
87
+ var mu;
88
+ var M2;
89
+ var ix;
90
+ var v;
91
+ var n;
92
+ var i;
93
+
94
+ n = N - correction;
95
+ if ( N <= 0 || n <= 0.0 ) {
96
+ return NaN;
97
+ }
98
+ if ( N === 1 || strideX === 0 ) {
99
+ return 0.0;
100
+ }
101
+ ix = offsetX;
102
+ M2 = 0.0;
103
+ mu = 0.0;
104
+ for ( i = 0; i < N; i++ ) {
105
+ v = x[ ix ];
106
+ delta = float64ToFloat32( v - mu );
107
+ mu = float64ToFloat32( mu + float64ToFloat32( delta / (i+1) ) );
108
+ M2 = float64ToFloat32( M2 + float64ToFloat32( delta * float64ToFloat32( v - mu ) ) ); // eslint-disable-line max-len
109
+ ix += strideX;
110
+ }
111
+ return float64ToFloat32( M2 / n );
112
+ }
113
+
114
+
115
+ // EXPORTS //
116
+
117
+ module.exports = svariancewd;
@@ -0,0 +1,53 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2020 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 addon = require( './../src/addon.node' );
24
+
25
+
26
+ // MAIN //
27
+
28
+ /**
29
+ * Computes the variance of a single-precision floating-point strided array using Welford's algorithm.
30
+ *
31
+ * @param {PositiveInteger} N - number of indexed elements
32
+ * @param {number} correction - degrees of freedom adjustment
33
+ * @param {Float32Array} x - input array
34
+ * @param {integer} strideX - stride length
35
+ * @param {NonNegativeInteger} offsetX - starting index
36
+ * @returns {number} variance
37
+ *
38
+ * @example
39
+ * var Float32Array = require( '@stdlib/array-float32' );
40
+ *
41
+ * var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
42
+ *
43
+ * var v = svariancewd( 4, 1, x, 2, 1 );
44
+ * // returns 6.25
45
+ */
46
+ function svariancewd( N, correction, x, strideX, offsetX ) {
47
+ return addon.ndarray( N, correction, x, strideX, offsetX );
48
+ }
49
+
50
+
51
+ // EXPORTS //
52
+
53
+ module.exports = svariancewd;
@@ -0,0 +1,92 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2020 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 stride2offset = require( '@stdlib/strided-base-stride2offset' );
24
+ var ndarray = require( './ndarray.js' );
25
+
26
+
27
+ // MAIN //
28
+
29
+ /**
30
+ * Computes the variance of a single-precision floating-point strided array using Welford's algorithm.
31
+ *
32
+ * ## Method
33
+ *
34
+ * - This implementation uses Welford's algorithm for efficient computation, which can be derived as follows. Let
35
+ *
36
+ * ```tex
37
+ * \begin{align*}
38
+ * S_n &= n \sigma_n^2 \\
39
+ * &= \sum_{i=1}^{n} (x_i - \mu_n)^2 \\
40
+ * &= \biggl(\sum_{i=1}^{n} x_i^2 \biggr) - n\mu_n^2
41
+ * \end{align*}
42
+ * ```
43
+ *
44
+ * Accordingly,
45
+ *
46
+ * ```tex
47
+ * \begin{align*}
48
+ * 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 \\
49
+ * &= x_n^2 - n\mu_n^2 + (n-1)\mu_{n-1}^2 \\
50
+ * &= x_n^2 - \mu_{n-1}^2 + n(\mu_{n-1}^2 - \mu_n^2) \\
51
+ * &= x_n^2 - \mu_{n-1}^2 + n(\mu_{n-1} - \mu_n)(\mu_{n-1} + \mu_n) \\
52
+ * &= x_n^2 - \mu_{n-1}^2 + (\mu_{n-1} - x_n)(\mu_{n-1} + \mu_n) \\
53
+ * &= 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} \\
54
+ * &= x_n^2 - x_n\mu_n - x_n\mu_{n-1} + \mu_n\mu_{n-1} \\
55
+ * &= (x_n - \mu_{n-1})(x_n - \mu_n) \\
56
+ * &= S_{n-1} + (x_n - \mu_{n-1})(x_n - \mu_n)
57
+ * \end{align*}
58
+ * ```
59
+ *
60
+ * where we use the identity
61
+ *
62
+ * ```tex
63
+ * x_n - \mu_{n-1} = n (\mu_n - \mu_{n-1})
64
+ * ```
65
+ *
66
+ * ## References
67
+ *
68
+ * - Welford, B. P. 1962. "Note on a Method for Calculating Corrected Sums of Squares and Products." _Technometrics_ 4 (3). Taylor & Francis: 419–20. doi:[10.1080/00401706.1962.10490022](https://doi.org/10.1080/00401706.1962.10490022).
69
+ * - van Reeken, A. J. 1968. "Letters to the Editor: Dealing with Neely's Algorithms." _Communications of the ACM_ 11 (3): 149–50. doi:[10.1145/362929.362961](https://doi.org/10.1145/362929.362961).
70
+ *
71
+ * @param {PositiveInteger} N - number of indexed elements
72
+ * @param {number} correction - degrees of freedom adjustment
73
+ * @param {Float32Array} x - input array
74
+ * @param {integer} strideX - stride length
75
+ * @returns {number} variance
76
+ *
77
+ * @example
78
+ * var Float32Array = require( '@stdlib/array-float32' );
79
+ *
80
+ * var x = new Float32Array( [ 1.0, -2.0, 2.0 ] );
81
+ *
82
+ * var v = svariancewd( x.length, 1, x, 1 );
83
+ * // returns ~4.3333
84
+ */
85
+ function svariancewd( N, correction, x, strideX ) {
86
+ return ndarray( N, correction, x, strideX, stride2offset( N, strideX ) );
87
+ }
88
+
89
+
90
+ // EXPORTS //
91
+
92
+ module.exports = svariancewd;
@@ -0,0 +1,52 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2020 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 addon = require( './../src/addon.node' );
24
+
25
+
26
+ // MAIN //
27
+
28
+ /**
29
+ * Computes the variance of a single-precision floating-point strided array using Welford's algorithm.
30
+ *
31
+ * @param {PositiveInteger} N - number of indexed elements
32
+ * @param {number} correction - degrees of freedom adjustment
33
+ * @param {Float32Array} x - input array
34
+ * @param {integer} strideX - stride length
35
+ * @returns {number} variance
36
+ *
37
+ * @example
38
+ * var Float32Array = require( '@stdlib/array-float32' );
39
+ *
40
+ * var x = new Float32Array( [ 1.0, -2.0, 2.0 ] );
41
+ *
42
+ * var v = svariancewd( x.length, 1, x, 1 );
43
+ * // returns ~4.3333
44
+ */
45
+ function svariancewd( N, correction, x, strideX ) {
46
+ return addon( N, correction, x, strideX );
47
+ }
48
+
49
+
50
+ // EXPORTS //
51
+
52
+ module.exports = svariancewd;
package/manifest.json ADDED
@@ -0,0 +1,100 @@
1
+ {
2
+ "options": {
3
+ "task": "build",
4
+ "wasm": false
5
+ },
6
+ "fields": [
7
+ {
8
+ "field": "src",
9
+ "resolve": true,
10
+ "relative": true
11
+ },
12
+ {
13
+ "field": "include",
14
+ "resolve": true,
15
+ "relative": true
16
+ },
17
+ {
18
+ "field": "libraries",
19
+ "resolve": false,
20
+ "relative": false
21
+ },
22
+ {
23
+ "field": "libpath",
24
+ "resolve": true,
25
+ "relative": false
26
+ }
27
+ ],
28
+ "confs": [
29
+ {
30
+ "task": "build",
31
+ "wasm": false,
32
+ "src": [
33
+ "./src/main.c"
34
+ ],
35
+ "include": [
36
+ "./include"
37
+ ],
38
+ "libraries": [],
39
+ "libpath": [],
40
+ "dependencies": [
41
+ "@stdlib/blas-base-shared",
42
+ "@stdlib/strided-base-stride2offset",
43
+ "@stdlib/napi-export",
44
+ "@stdlib/napi-argv",
45
+ "@stdlib/napi-argv-int64",
46
+ "@stdlib/napi-argv-strided-float32array",
47
+ "@stdlib/napi-create-double",
48
+ "@stdlib/napi-argv-float"
49
+ ]
50
+ },
51
+ {
52
+ "task": "benchmark",
53
+ "wasm": false,
54
+ "src": [
55
+ "./src/main.c"
56
+ ],
57
+ "include": [
58
+ "./include"
59
+ ],
60
+ "libraries": [],
61
+ "libpath": [],
62
+ "dependencies": [
63
+ "@stdlib/blas-base-shared",
64
+ "@stdlib/strided-base-stride2offset"
65
+ ]
66
+ },
67
+ {
68
+ "task": "examples",
69
+ "wasm": false,
70
+ "src": [
71
+ "./src/main.c"
72
+ ],
73
+ "include": [
74
+ "./include"
75
+ ],
76
+ "libraries": [],
77
+ "libpath": [],
78
+ "dependencies": [
79
+ "@stdlib/blas-base-shared",
80
+ "@stdlib/strided-base-stride2offset"
81
+ ]
82
+ },
83
+ {
84
+ "task": "",
85
+ "wasm": true,
86
+ "src": [
87
+ "./src/main.c"
88
+ ],
89
+ "include": [
90
+ "./include"
91
+ ],
92
+ "libraries": [],
93
+ "libpath": [],
94
+ "dependencies": [
95
+ "@stdlib/blas-base-shared",
96
+ "@stdlib/strided-base-stride2offset"
97
+ ]
98
+ }
99
+ ]
100
+ }
package/package.json ADDED
@@ -0,0 +1,97 @@
1
+ {
2
+ "name": "@stdlib/stats-strided-svariancewd",
3
+ "version": "0.1.0",
4
+ "description": "Calculate the variance of a single-precision floating-point strided array using Welford's algorithm.",
5
+ "license": "Apache-2.0",
6
+ "author": {
7
+ "name": "The Stdlib Authors",
8
+ "url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
9
+ },
10
+ "contributors": [
11
+ {
12
+ "name": "The Stdlib Authors",
13
+ "url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
14
+ }
15
+ ],
16
+ "main": "./lib",
17
+ "browser": "./lib/main.js",
18
+ "gypfile": false,
19
+ "directories": {
20
+ "doc": "./docs",
21
+ "include": "./include",
22
+ "lib": "./lib",
23
+ "src": "./src",
24
+ "dist": "./dist"
25
+ },
26
+ "types": "./docs/types",
27
+ "scripts": {},
28
+ "homepage": "https://stdlib.io",
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "git://github.com/stdlib-js/stats-strided-svariancewd.git"
32
+ },
33
+ "bugs": {
34
+ "url": "https://github.com/stdlib-js/stdlib/issues"
35
+ },
36
+ "dependencies": {
37
+ "@stdlib/assert-is-error": "^0.2.2",
38
+ "@stdlib/blas-base-shared": "^0.2.0",
39
+ "@stdlib/napi-argv": "^0.2.2",
40
+ "@stdlib/napi-argv-float": "^0.2.2",
41
+ "@stdlib/napi-argv-int64": "^0.2.2",
42
+ "@stdlib/napi-argv-strided-float32array": "^0.2.2",
43
+ "@stdlib/napi-create-double": "^0.0.2",
44
+ "@stdlib/napi-export": "^0.3.0",
45
+ "@stdlib/number-float64-base-to-float32": "^0.2.2",
46
+ "@stdlib/strided-base-stride2offset": "^0.1.0",
47
+ "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2",
48
+ "@stdlib/utils-library-manifest": "^0.2.3",
49
+ "@stdlib/utils-try-require": "^0.2.2"
50
+ },
51
+ "devDependencies": {},
52
+ "engines": {
53
+ "node": ">=0.10.0",
54
+ "npm": ">2.7.0"
55
+ },
56
+ "os": [
57
+ "aix",
58
+ "darwin",
59
+ "freebsd",
60
+ "linux",
61
+ "macos",
62
+ "openbsd",
63
+ "sunos",
64
+ "win32",
65
+ "windows"
66
+ ],
67
+ "keywords": [
68
+ "stdlib",
69
+ "stdmath",
70
+ "statistics",
71
+ "stats",
72
+ "mathematics",
73
+ "math",
74
+ "variance",
75
+ "var",
76
+ "deviation",
77
+ "dispersion",
78
+ "sample variance",
79
+ "unbiased",
80
+ "stdev",
81
+ "std",
82
+ "standard deviation",
83
+ "welford",
84
+ "strided",
85
+ "strided array",
86
+ "typed",
87
+ "array",
88
+ "float32",
89
+ "float",
90
+ "single",
91
+ "float32array"
92
+ ],
93
+ "funding": {
94
+ "type": "opencollective",
95
+ "url": "https://opencollective.com/stdlib"
96
+ }
97
+ }
package/src/addon.c ADDED
@@ -0,0 +1,63 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2024 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
+ #include "stdlib/stats/strided/svariancewd.h"
20
+ #include "stdlib/napi/export.h"
21
+ #include "stdlib/napi/argv.h"
22
+ #include "stdlib/napi/argv_int64.h"
23
+ #include "stdlib/napi/argv_strided_float32array.h"
24
+ #include "stdlib/napi/create_double.h"
25
+ #include "stdlib/napi/argv_float.h"
26
+ #include <node_api.h>
27
+
28
+ /**
29
+ * Receives JavaScript callback invocation data.
30
+ *
31
+ * @param env environment under which the function is invoked
32
+ * @param info callback data
33
+ * @return Node-API value
34
+ */
35
+ static napi_value addon( napi_env env, napi_callback_info info ) {
36
+ STDLIB_NAPI_ARGV( env, info, argv, argc, 4 );
37
+ STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 );
38
+ STDLIB_NAPI_ARGV_FLOAT( env, correction, argv, 1 );
39
+ STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 3 );
40
+ STDLIB_NAPI_ARGV_STRIDED_FLOAT32ARRAY( env, X, N, strideX, argv, 2 )
41
+ STDLIB_NAPI_CREATE_DOUBLE( env, (double)stdlib_strided_svariancewd( N, correction, X, strideX ), v );
42
+ return v;
43
+ }
44
+
45
+ /**
46
+ * Receives JavaScript callback invocation data.
47
+ *
48
+ * @param env environment under which the function is invoked
49
+ * @param info callback data
50
+ * @return Node-API value
51
+ */
52
+ static napi_value addon_method( napi_env env, napi_callback_info info ) {
53
+ STDLIB_NAPI_ARGV( env, info, argv, argc, 5 );
54
+ STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 );
55
+ STDLIB_NAPI_ARGV_FLOAT( env, correction, argv, 1 );
56
+ STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 3 );
57
+ STDLIB_NAPI_ARGV_STRIDED_FLOAT32ARRAY( env, X, N, strideX, argv, 2 );
58
+ STDLIB_NAPI_ARGV_INT64( env, offsetX, argv, 4 );
59
+ STDLIB_NAPI_CREATE_DOUBLE( env, (double)stdlib_strided_svariancewd_ndarray( N, correction, X, strideX, offsetX ), v );
60
+ return v;
61
+ }
62
+
63
+ STDLIB_NAPI_MODULE_EXPORT_FCN_WITH_METHOD( addon, "ndarray", addon_method )
package/src/main.c ADDED
@@ -0,0 +1,113 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2020 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
+ #include "stdlib/stats/strided/svariancewd.h"
20
+ #include "stdlib/blas/base/shared.h"
21
+ #include "stdlib/strided/base/stride2offset.h"
22
+
23
+ /**
24
+ * Computes the variance of a single-precision floating-point strided array using Welford's algorithm.
25
+ *
26
+ * ## Method
27
+ *
28
+ * - This implementation uses Welford's algorithm for efficient computation, which can be derived as follows. Let
29
+ *
30
+ * ```tex
31
+ * \begin{align*}
32
+ * S_n &= n \sigma_n^2 \\
33
+ * &= \sum_{i=1}^{n} (x_i - \mu_n)^2 \\
34
+ * &= \biggl(\sum_{i=1}^{n} x_i^2 \biggr) - n\mu_n^2
35
+ * \end{align*}
36
+ * ```
37
+ *
38
+ * Accordingly,
39
+ *
40
+ * ```tex
41
+ * \begin{align*}
42
+ * 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 \\
43
+ * &= x_n^2 - n\mu_n^2 + (n-1)\mu_{n-1}^2 \\
44
+ * &= x_n^2 - \mu_{n-1}^2 + n(\mu_{n-1}^2 - \mu_n^2) \\
45
+ * &= x_n^2 - \mu_{n-1}^2 + n(\mu_{n-1} - \mu_n)(\mu_{n-1} + \mu_n) \\
46
+ * &= x_n^2 - \mu_{n-1}^2 + (\mu_{n-1} - x_n)(\mu_{n-1} + \mu_n) \\
47
+ * &= 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} \\
48
+ * &= x_n^2 - x_n\mu_n - x_n\mu_{n-1} + \mu_n\mu_{n-1} \\
49
+ * &= (x_n - \mu_{n-1})(x_n - \mu_n) \\
50
+ * &= S_{n-1} + (x_n - \mu_{n-1})(x_n - \mu_n)
51
+ * \end{align*}
52
+ * ```
53
+ *
54
+ * where we use the identity
55
+ *
56
+ * ```tex
57
+ * x_n - \mu_{n-1} = n (\mu_n - \mu_{n-1})
58
+ * ```
59
+ *
60
+ * ## References
61
+ *
62
+ * - Welford, B. P. 1962. "Note on a Method for Calculating Corrected Sums of Squares and Products." _Technometrics_ 4 (3). Taylor & Francis: 419–20. doi:[10.1080/00401706.1962.10490022](https://doi.org/10.1080/00401706.1962.10490022).
63
+ * - van Reeken, A. J. 1968. "Letters to the Editor: Dealing with Neely's Algorithms." _Communications of the ACM_ 11 (3): 149–50. doi:[10.1145/362929.362961](https://doi.org/10.1145/362929.362961).
64
+ *
65
+ * @param N number of indexed elements
66
+ * @param correction degrees of freedom adjustment
67
+ * @param X input array
68
+ * @param strideX stride length
69
+ * @return output value
70
+ */
71
+ float API_SUFFIX(stdlib_strided_svariancewd)( const CBLAS_INT N, const float correction, const float *X, const CBLAS_INT strideX ) {
72
+ const CBLAS_INT ox = stdlib_strided_stride2offset( N, strideX );
73
+ return API_SUFFIX(stdlib_strided_svariancewd_ndarray)( N, correction, X, strideX, ox );
74
+ }
75
+
76
+ /**
77
+ * Computes the variance of a single-precision floating-point strided array using Welford's algorithm and alternative indexing semantics.
78
+ *
79
+ * @param N number of indexed elements
80
+ * @param correction degrees of freedom adjustment
81
+ * @param X input array
82
+ * @param strideX stride length
83
+ * @param offsetX starting index of X
84
+ * @return output value
85
+ */
86
+ float API_SUFFIX(stdlib_strided_svariancewd_ndarray)( const CBLAS_INT N, const float correction, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ) {
87
+ float delta;
88
+ int64_t ix;
89
+ int64_t i;
90
+ double n;
91
+ float mu;
92
+ float M2;
93
+ float v;
94
+
95
+ n = (double)N - (double)correction;
96
+ if ( N <= 0 || n <= 0.0f ) {
97
+ return 0.0f / 0.0f; // NaN
98
+ }
99
+ if ( N == 1 || strideX == 0 ) {
100
+ return 0.0f;
101
+ }
102
+ ix = offsetX;
103
+ M2 = 0.0f;
104
+ mu = 0.0f;
105
+ for ( i = 0; i < N; i++ ) {
106
+ v = X[ ix ];
107
+ delta = v - mu;
108
+ mu += (float)((double)delta / (double)(i+1));
109
+ M2 += delta * ( v - mu );
110
+ ix += strideX;
111
+ }
112
+ return (double)M2 / n;
113
+ }