@stdlib/stats-strided-dsvariancepn 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/LICENSE +177 -0
- package/NOTICE +1 -0
- package/README.md +465 -0
- package/SECURITY.md +5 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +7 -0
- package/docs/img/equation_population_mean.svg +42 -0
- package/docs/img/equation_population_variance.svg +54 -0
- package/docs/img/equation_sample_mean.svg +43 -0
- package/docs/img/equation_unbiased_sample_variance.svg +61 -0
- package/docs/types/index.d.ts +95 -0
- package/include/stdlib/stats/strided/dsvariancepn.h +45 -0
- package/lib/dsvariancepn.js +62 -0
- package/lib/dsvariancepn.native.js +52 -0
- package/lib/index.js +68 -0
- package/lib/main.js +35 -0
- package/lib/native.js +35 -0
- package/lib/ndarray.js +90 -0
- package/lib/ndarray.native.js +53 -0
- package/manifest.json +104 -0
- package/package.json +95 -0
- package/src/addon.c +64 -0
- package/src/main.c +89 -0
package/lib/native.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
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 setReadOnly = require( '@stdlib/utils-define-nonenumerable-read-only-property' );
|
|
24
|
+
var dsvariancepn = require( './dsvariancepn.native.js' );
|
|
25
|
+
var ndarray = require( './ndarray.native.js' );
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
// MAIN //
|
|
29
|
+
|
|
30
|
+
setReadOnly( dsvariancepn, 'ndarray', ndarray );
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
// EXPORTS //
|
|
34
|
+
|
|
35
|
+
module.exports = dsvariancepn;
|
package/lib/ndarray.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
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 dssum = require( '@stdlib/blas-ext-base-dssum' ).ndarray;
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
// MAIN //
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Computes the variance of a single-precision floating-point strided array using a two-pass algorithm with extended accumulation and returning an extended precision result.
|
|
30
|
+
*
|
|
31
|
+
* ## Method
|
|
32
|
+
*
|
|
33
|
+
* - This implementation uses a two-pass approach, as suggested by Neely (1966).
|
|
34
|
+
*
|
|
35
|
+
* ## References
|
|
36
|
+
*
|
|
37
|
+
* - Neely, Peter M. 1966. "Comparison of Several Algorithms for Computation of Means, Standard Deviations and Correlation Coefficients." _Communications of the ACM_ 9 (7). Association for Computing Machinery: 496–99. doi:[10.1145/365719.365958](https://doi.org/10.1145/365719.365958).
|
|
38
|
+
* - Schubert, Erich, and Michael Gertz. 2018. "Numerically Stable Parallel Computation of (Co-)Variance." In _Proceedings of the 30th International Conference on Scientific and Statistical Database Management_. New York, NY, USA: Association for Computing Machinery. doi:[10.1145/3221269.3223036](https://doi.org/10.1145/3221269.3223036).
|
|
39
|
+
*
|
|
40
|
+
* @param {PositiveInteger} N - number of indexed elements
|
|
41
|
+
* @param {number} correction - degrees of freedom adjustment
|
|
42
|
+
* @param {Float32Array} x - input array
|
|
43
|
+
* @param {integer} strideX - stride length
|
|
44
|
+
* @param {NonNegativeInteger} offsetX - starting index
|
|
45
|
+
* @returns {number} variance
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* var Float32Array = require( '@stdlib/array-float32' );
|
|
49
|
+
*
|
|
50
|
+
* var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
|
|
51
|
+
*
|
|
52
|
+
* var v = dsvariancepn( 4, 1, x, 2, 1 );
|
|
53
|
+
* // returns 6.25
|
|
54
|
+
*/
|
|
55
|
+
function dsvariancepn( N, correction, x, strideX, offsetX ) {
|
|
56
|
+
var mu;
|
|
57
|
+
var ix;
|
|
58
|
+
var M2;
|
|
59
|
+
var M;
|
|
60
|
+
var d;
|
|
61
|
+
var n;
|
|
62
|
+
var i;
|
|
63
|
+
|
|
64
|
+
n = N - correction;
|
|
65
|
+
if ( N <= 0 || n <= 0 ) {
|
|
66
|
+
return NaN;
|
|
67
|
+
}
|
|
68
|
+
if ( N === 1 || strideX === 0 ) {
|
|
69
|
+
return 0.0;
|
|
70
|
+
}
|
|
71
|
+
// Compute an estimate for the mean:
|
|
72
|
+
mu = dssum( N, x, strideX, offsetX ) / N;
|
|
73
|
+
|
|
74
|
+
// Compute the variance...
|
|
75
|
+
ix = offsetX;
|
|
76
|
+
M2 = 0.0;
|
|
77
|
+
M = 0.0;
|
|
78
|
+
for ( i = 0; i < N; i++ ) {
|
|
79
|
+
d = x[ ix ] - mu;
|
|
80
|
+
M2 += d * d;
|
|
81
|
+
M += d;
|
|
82
|
+
ix += strideX;
|
|
83
|
+
}
|
|
84
|
+
return (M2/n) - ((M/N)*(M/n));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
// EXPORTS //
|
|
89
|
+
|
|
90
|
+
module.exports = dsvariancepn;
|
|
@@ -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 a two-pass algorithm with extended accumulation and returning an extended precision result.
|
|
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 = dsvariancepn( 4, 1, x, 2, 1 );
|
|
44
|
+
* // returns 6.25
|
|
45
|
+
*/
|
|
46
|
+
function dsvariancepn( N, correction, x, strideX, offsetX ) {
|
|
47
|
+
return addon.ndarray( N, correction, x, strideX, offsetX );
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
// EXPORTS //
|
|
52
|
+
|
|
53
|
+
module.exports = dsvariancepn;
|
package/manifest.json
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
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-ext-base-dssum",
|
|
42
|
+
"@stdlib/blas-base-shared",
|
|
43
|
+
"@stdlib/strided-base-stride2offset",
|
|
44
|
+
"@stdlib/napi-export",
|
|
45
|
+
"@stdlib/napi-argv",
|
|
46
|
+
"@stdlib/napi-argv-int64",
|
|
47
|
+
"@stdlib/napi-argv-float",
|
|
48
|
+
"@stdlib/napi-argv-strided-float32array",
|
|
49
|
+
"@stdlib/napi-create-double"
|
|
50
|
+
]
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
"task": "benchmark",
|
|
54
|
+
"wasm": false,
|
|
55
|
+
"src": [
|
|
56
|
+
"./src/main.c"
|
|
57
|
+
],
|
|
58
|
+
"include": [
|
|
59
|
+
"./include"
|
|
60
|
+
],
|
|
61
|
+
"libraries": [],
|
|
62
|
+
"libpath": [],
|
|
63
|
+
"dependencies": [
|
|
64
|
+
"@stdlib/blas-ext-base-dssum",
|
|
65
|
+
"@stdlib/blas-base-shared",
|
|
66
|
+
"@stdlib/strided-base-stride2offset"
|
|
67
|
+
]
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
"task": "examples",
|
|
71
|
+
"wasm": false,
|
|
72
|
+
"src": [
|
|
73
|
+
"./src/main.c"
|
|
74
|
+
],
|
|
75
|
+
"include": [
|
|
76
|
+
"./include"
|
|
77
|
+
],
|
|
78
|
+
"libraries": [],
|
|
79
|
+
"libpath": [],
|
|
80
|
+
"dependencies": [
|
|
81
|
+
"@stdlib/blas-ext-base-dssum",
|
|
82
|
+
"@stdlib/blas-base-shared",
|
|
83
|
+
"@stdlib/strided-base-stride2offset"
|
|
84
|
+
]
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
"task": "build",
|
|
88
|
+
"wasm": true,
|
|
89
|
+
"src": [
|
|
90
|
+
"./src/main.c"
|
|
91
|
+
],
|
|
92
|
+
"include": [
|
|
93
|
+
"./include"
|
|
94
|
+
],
|
|
95
|
+
"libraries": [],
|
|
96
|
+
"libpath": [],
|
|
97
|
+
"dependencies": [
|
|
98
|
+
"@stdlib/blas-ext-base-dssum",
|
|
99
|
+
"@stdlib/blas-base-shared",
|
|
100
|
+
"@stdlib/strided-base-stride2offset"
|
|
101
|
+
]
|
|
102
|
+
}
|
|
103
|
+
]
|
|
104
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@stdlib/stats-strided-dsvariancepn",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Calculate the variance of a single-precision floating-point strided array using a two-pass algorithm with extended accumulation and returning an extended precision result.",
|
|
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-dsvariancepn.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.1.0",
|
|
39
|
+
"@stdlib/blas-ext-base-dssum": "^0.2.2",
|
|
40
|
+
"@stdlib/napi-argv": "^0.2.2",
|
|
41
|
+
"@stdlib/napi-argv-float": "^0.2.2",
|
|
42
|
+
"@stdlib/napi-argv-int64": "^0.2.2",
|
|
43
|
+
"@stdlib/napi-argv-strided-float32array": "^0.2.2",
|
|
44
|
+
"@stdlib/napi-create-double": "^0.0.2",
|
|
45
|
+
"@stdlib/napi-export": "^0.3.0",
|
|
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
|
+
"strided",
|
|
84
|
+
"strided array",
|
|
85
|
+
"typed",
|
|
86
|
+
"array",
|
|
87
|
+
"float32",
|
|
88
|
+
"double",
|
|
89
|
+
"float32array"
|
|
90
|
+
],
|
|
91
|
+
"funding": {
|
|
92
|
+
"type": "opencollective",
|
|
93
|
+
"url": "https://opencollective.com/stdlib"
|
|
94
|
+
}
|
|
95
|
+
}
|
package/src/addon.c
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
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
|
+
|
|
19
|
+
#include "stdlib/stats/strided/dsvariancepn.h"
|
|
20
|
+
#include "stdlib/blas/base/shared.h"
|
|
21
|
+
#include "stdlib/napi/export.h"
|
|
22
|
+
#include "stdlib/napi/argv.h"
|
|
23
|
+
#include "stdlib/napi/argv_int64.h"
|
|
24
|
+
#include "stdlib/napi/argv_strided_float32array.h"
|
|
25
|
+
#include "stdlib/napi/create_double.h"
|
|
26
|
+
#include "stdlib/napi/argv_float.h"
|
|
27
|
+
#include <node_api.h>
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Receives JavaScript callback invocation data.
|
|
31
|
+
*
|
|
32
|
+
* @param env environment under which the function is invoked
|
|
33
|
+
* @param info callback data
|
|
34
|
+
* @return Node-API value
|
|
35
|
+
*/
|
|
36
|
+
static napi_value addon( napi_env env, napi_callback_info info ) {
|
|
37
|
+
STDLIB_NAPI_ARGV( env, info, argv, argc, 4 );
|
|
38
|
+
STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 );
|
|
39
|
+
STDLIB_NAPI_ARGV_FLOAT( env, correction, argv, 1 );
|
|
40
|
+
STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 3 );
|
|
41
|
+
STDLIB_NAPI_ARGV_STRIDED_FLOAT32ARRAY( env, X, N, strideX, argv, 2 )
|
|
42
|
+
STDLIB_NAPI_CREATE_DOUBLE( env, API_SUFFIX(stdlib_strided_dsvariancepn)( N, correction, X, strideX ), v );
|
|
43
|
+
return v;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Receives JavaScript callback invocation data.
|
|
48
|
+
*
|
|
49
|
+
* @param env environment under which the function is invoked
|
|
50
|
+
* @param info callback data
|
|
51
|
+
* @return Node-API value
|
|
52
|
+
*/
|
|
53
|
+
static napi_value addon_method( napi_env env, napi_callback_info info ) {
|
|
54
|
+
STDLIB_NAPI_ARGV( env, info, argv, argc, 5 );
|
|
55
|
+
STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 );
|
|
56
|
+
STDLIB_NAPI_ARGV_FLOAT( env, correction, argv, 1 );
|
|
57
|
+
STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 3 );
|
|
58
|
+
STDLIB_NAPI_ARGV_STRIDED_FLOAT32ARRAY( env, X, N, strideX, argv, 2 );
|
|
59
|
+
STDLIB_NAPI_ARGV_INT64( env, offsetX, argv, 4 );
|
|
60
|
+
STDLIB_NAPI_CREATE_DOUBLE( env, API_SUFFIX(stdlib_strided_dsvariancepn_ndarray)( N, correction, X, strideX, offsetX ), v );
|
|
61
|
+
return v;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
STDLIB_NAPI_MODULE_EXPORT_FCN_WITH_METHOD( addon, "ndarray", addon_method )
|
package/src/main.c
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
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/dsvariancepn.h"
|
|
20
|
+
#include "stdlib/blas/ext/base/dssum.h"
|
|
21
|
+
#include "stdlib/blas/base/shared.h"
|
|
22
|
+
#include "stdlib/strided/base/stride2offset.h"
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Computes the variance of a single-precision floating-point strided array using a two-pass algorithm with extended accumulation and returning an extended precision result.
|
|
26
|
+
*
|
|
27
|
+
* ## Method
|
|
28
|
+
*
|
|
29
|
+
* - This implementation uses a two-pass approach, as suggested by Neely (1966).
|
|
30
|
+
*
|
|
31
|
+
* ## References
|
|
32
|
+
*
|
|
33
|
+
* - Neely, Peter M. 1966. "Comparison of Several Algorithms for Computation of Means, Standard Deviations and Correlation Coefficients." _Communications of the ACM_ 9 (7). Association for Computing Machinery: 496–99. doi:[10.1145/365719.365958](https://doi.org/10.1145/365719.365958).
|
|
34
|
+
* - Schubert, Erich, and Michael Gertz. 2018. "Numerically Stable Parallel Computation of (Co-)Variance." In _Proceedings of the 30th International Conference on Scientific and Statistical Database Management_. New York, NY, USA: Association for Computing Machinery. doi:[10.1145/3221269.3223036](https://doi.org/10.1145/3221269.3223036).
|
|
35
|
+
*
|
|
36
|
+
* @param N number of indexed elements
|
|
37
|
+
* @param correction degrees of freedom adjustment
|
|
38
|
+
* @param X input array
|
|
39
|
+
* @param strideX stride length
|
|
40
|
+
* @return output value
|
|
41
|
+
*/
|
|
42
|
+
double API_SUFFIX(stdlib_strided_dsvariancepn)( const CBLAS_INT N, const float correction, const float *X, const CBLAS_INT strideX ) {
|
|
43
|
+
const CBLAS_INT ox = stdlib_strided_stride2offset( N, strideX );
|
|
44
|
+
return API_SUFFIX(stdlib_strided_dsvariancepn_ndarray)( N, correction, X, strideX, ox );
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Computes the variance of a single-precision floating-point strided array using a two-pass algorithm with extended accumulation and returning an extended precision result.
|
|
49
|
+
*
|
|
50
|
+
* @param N number of indexed elements
|
|
51
|
+
* @param correction degrees of freedom adjustment
|
|
52
|
+
* @param X input array
|
|
53
|
+
* @param strideX stride length
|
|
54
|
+
* @param offsetX starting index for X
|
|
55
|
+
* @return output value
|
|
56
|
+
*/
|
|
57
|
+
double API_SUFFIX(stdlib_strided_dsvariancepn_ndarray)( const CBLAS_INT N, const float correction, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ) {
|
|
58
|
+
CBLAS_INT ix;
|
|
59
|
+
CBLAS_INT i;
|
|
60
|
+
double dN;
|
|
61
|
+
double mu;
|
|
62
|
+
double M2;
|
|
63
|
+
double M;
|
|
64
|
+
double n;
|
|
65
|
+
double d;
|
|
66
|
+
|
|
67
|
+
dN = (double)N;
|
|
68
|
+
n = dN - (double)correction;
|
|
69
|
+
if ( N <= 0 || n <= 0 ) {
|
|
70
|
+
return 0.0 / 0.0; // NaN
|
|
71
|
+
}
|
|
72
|
+
if ( N == 1 || strideX == 0 ) {
|
|
73
|
+
return 0.0;
|
|
74
|
+
}
|
|
75
|
+
// Compute an estimate for the mean:
|
|
76
|
+
mu = stdlib_strided_dssum_ndarray( N, X, strideX, offsetX ) / dN;
|
|
77
|
+
|
|
78
|
+
// Compute the variance...
|
|
79
|
+
ix = offsetX;
|
|
80
|
+
M2 = 0.0;
|
|
81
|
+
M = 0.0;
|
|
82
|
+
for ( i = 0; i < N; i++ ) {
|
|
83
|
+
d = (double)X[ ix ] - mu;
|
|
84
|
+
M2 += d * d;
|
|
85
|
+
M += d;
|
|
86
|
+
ix += strideX;
|
|
87
|
+
}
|
|
88
|
+
return (M2/n) - ((M/dN)*(M/n));
|
|
89
|
+
}
|