@stdlib/stats-strided-dvarmtk 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/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 dvarmtk = require( './dvarmtk.native.js' );
25
+ var ndarray = require( './ndarray.native.js' );
26
+
27
+
28
+ // MAIN //
29
+
30
+ setReadOnly( dvarmtk, 'ndarray', ndarray );
31
+
32
+
33
+ // EXPORTS //
34
+
35
+ module.exports = dvarmtk;
package/lib/ndarray.js ADDED
@@ -0,0 +1,69 @@
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
+ // MAIN //
22
+
23
+ /**
24
+ * Computes the variance of a double-precision floating-point strided array provided a known mean and using a one-pass textbook algorithm.
25
+ *
26
+ * @param {PositiveInteger} N - number of indexed elements
27
+ * @param {number} correction - degrees of freedom adjustment
28
+ * @param {number} mean - mean
29
+ * @param {Float64Array} x - input array
30
+ * @param {integer} strideX - stride length
31
+ * @param {NonNegativeInteger} offsetX - starting index
32
+ * @returns {number} variance
33
+ *
34
+ * @example
35
+ * var Float64Array = require( '@stdlib/array-float64' );
36
+ *
37
+ * var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
38
+ *
39
+ * var v = dvarmtk( 4, 1, 1.25, x, 2, 1 );
40
+ * // returns 6.25
41
+ */
42
+ function dvarmtk( N, correction, mean, x, strideX, offsetX ) {
43
+ var ix;
44
+ var M2;
45
+ var d;
46
+ var n;
47
+ var i;
48
+
49
+ n = N - correction;
50
+ if ( N <= 0 || n <= 0.0 ) {
51
+ return NaN;
52
+ }
53
+ if ( N === 1 || strideX === 0 ) {
54
+ return 0.0;
55
+ }
56
+ ix = offsetX;
57
+ M2 = 0.0;
58
+ for ( i = 0; i < N; i++ ) {
59
+ d = x[ ix ] - mean;
60
+ M2 += d * d;
61
+ ix += strideX;
62
+ }
63
+ return M2 / n;
64
+ }
65
+
66
+
67
+ // EXPORTS //
68
+
69
+ module.exports = dvarmtk;
@@ -0,0 +1,54 @@
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 double-precision floating-point strided array provided a known mean and using a one-pass textbook algorithm.
30
+ *
31
+ * @param {PositiveInteger} N - number of indexed elements
32
+ * @param {number} correction - degrees of freedom adjustment
33
+ * @param {number} mean - mean
34
+ * @param {Float64Array} x - input array
35
+ * @param {integer} strideX - stride length
36
+ * @param {NonNegativeInteger} offsetX - starting index
37
+ * @returns {number} variance
38
+ *
39
+ * @example
40
+ * var Float64Array = require( '@stdlib/array-float64' );
41
+ *
42
+ * var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
43
+ *
44
+ * var v = dvarmtk( 4, 1, 1.25, x, 2, 1 );
45
+ * // returns 6.25
46
+ */
47
+ function dvarmtk( N, correction, mean, x, strideX, offsetX ) {
48
+ return addon.ndarray( N, correction, mean, x, strideX, offsetX );
49
+ }
50
+
51
+
52
+ // EXPORTS //
53
+
54
+ module.exports = dvarmtk;
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-double",
47
+ "@stdlib/napi-argv-strided-float64array",
48
+ "@stdlib/napi-create-double"
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,94 @@
1
+ {
2
+ "name": "@stdlib/stats-strided-dvarmtk",
3
+ "version": "0.1.0",
4
+ "description": "Calculate the variance of a double-precision floating-point strided array provided a known mean and using a one-pass textbook 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-dvarmtk.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/napi-argv": "^0.2.2",
40
+ "@stdlib/napi-argv-double": "^0.2.1",
41
+ "@stdlib/napi-argv-int64": "^0.2.2",
42
+ "@stdlib/napi-argv-strided-float64array": "^0.2.2",
43
+ "@stdlib/napi-create-double": "^0.0.2",
44
+ "@stdlib/napi-export": "^0.3.0",
45
+ "@stdlib/strided-base-stride2offset": "^0.1.0",
46
+ "@stdlib/utils-define-nonenumerable-read-only-property": "^0.2.2",
47
+ "@stdlib/utils-library-manifest": "^0.2.3",
48
+ "@stdlib/utils-try-require": "^0.2.2"
49
+ },
50
+ "devDependencies": {},
51
+ "engines": {
52
+ "node": ">=0.10.0",
53
+ "npm": ">2.7.0"
54
+ },
55
+ "os": [
56
+ "aix",
57
+ "darwin",
58
+ "freebsd",
59
+ "linux",
60
+ "macos",
61
+ "openbsd",
62
+ "sunos",
63
+ "win32",
64
+ "windows"
65
+ ],
66
+ "keywords": [
67
+ "stdlib",
68
+ "stdmath",
69
+ "statistics",
70
+ "stats",
71
+ "mathematics",
72
+ "math",
73
+ "variance",
74
+ "var",
75
+ "deviation",
76
+ "dispersion",
77
+ "sample variance",
78
+ "unbiased",
79
+ "stdev",
80
+ "std",
81
+ "standard deviation",
82
+ "strided",
83
+ "strided array",
84
+ "typed",
85
+ "array",
86
+ "float64",
87
+ "double",
88
+ "float64array"
89
+ ],
90
+ "funding": {
91
+ "type": "opencollective",
92
+ "url": "https://opencollective.com/stdlib"
93
+ }
94
+ }
package/src/addon.c ADDED
@@ -0,0 +1,66 @@
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/dvarmtk.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_float64array.h"
25
+ #include "stdlib/napi/create_double.h"
26
+ #include "stdlib/napi/argv_double.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, 5 );
38
+ STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 );
39
+ STDLIB_NAPI_ARGV_DOUBLE( env, correction, argv, 1 );
40
+ STDLIB_NAPI_ARGV_DOUBLE( env, mean, argv, 2 );
41
+ STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 4 );
42
+ STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, X, N, strideX, argv, 3 )
43
+ STDLIB_NAPI_CREATE_DOUBLE( env, API_SUFFIX(stdlib_strided_dvarmtk)( N, correction, mean, X, strideX ), v );
44
+ return v;
45
+ }
46
+
47
+ /**
48
+ * Receives JavaScript callback invocation data.
49
+ *
50
+ * @param env environment under which the function is invoked
51
+ * @param info callback data
52
+ * @return Node-API value
53
+ */
54
+ static napi_value addon_method( napi_env env, napi_callback_info info ) {
55
+ STDLIB_NAPI_ARGV( env, info, argv, argc, 6 );
56
+ STDLIB_NAPI_ARGV_INT64( env, N, argv, 0 );
57
+ STDLIB_NAPI_ARGV_DOUBLE( env, correction, argv, 1 );
58
+ STDLIB_NAPI_ARGV_DOUBLE( env, mean, argv, 2 );
59
+ STDLIB_NAPI_ARGV_INT64( env, strideX, argv, 4 );
60
+ STDLIB_NAPI_ARGV_STRIDED_FLOAT64ARRAY( env, X, N, strideX, argv, 3 );
61
+ STDLIB_NAPI_ARGV_INT64( env, offsetX, argv, 5 );
62
+ STDLIB_NAPI_CREATE_DOUBLE( env, API_SUFFIX(stdlib_strided_dvarmtk_ndarray)( N, correction, mean, X, strideX, offsetX ), v );
63
+ return v;
64
+ }
65
+
66
+ STDLIB_NAPI_MODULE_EXPORT_FCN_WITH_METHOD( addon, "ndarray", addon_method )
package/src/main.c ADDED
@@ -0,0 +1,71 @@
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/dvarmtk.h"
20
+ #include "stdlib/blas/base/shared.h"
21
+ #include "stdlib/strided/base/stride2offset.h"
22
+
23
+ /**
24
+ * Computes the variance of a double-precision floating-point strided array provided a known mean and using a one-pass textbook algorithm.
25
+ *
26
+ * @param N number of indexed elements
27
+ * @param correction degrees of freedom adjustment
28
+ * @param mean mean
29
+ * @param X input array
30
+ * @param strideX stride length
31
+ * @return output value
32
+ */
33
+ double API_SUFFIX(stdlib_strided_dvarmtk)( const CBLAS_INT N, const double correction, const double mean, const double *X, const CBLAS_INT strideX ) {
34
+ const CBLAS_INT ox = stdlib_strided_stride2offset( N, strideX );
35
+ return API_SUFFIX(stdlib_strided_dvarmtk_ndarray)( N, correction, mean, X, strideX, ox );
36
+ }
37
+
38
+ /**
39
+ * Computes the variance of a double-precision floating-point strided array provided a known mean and using a one-pass textbook algorithm and alternative indexing semantics.
40
+ *
41
+ * @param N number of indexed elements
42
+ * @param correction degrees of freedom adjustment
43
+ * @param mean mean
44
+ * @param X input array
45
+ * @param strideX stride length
46
+ * @param offsetX starting index for X
47
+ * @return output value
48
+ */
49
+ double API_SUFFIX(stdlib_strided_dvarmtk_ndarray)( const CBLAS_INT N, const double correction, const double mean, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX ) {
50
+ CBLAS_INT ix;
51
+ CBLAS_INT i;
52
+ double M2;
53
+ double n;
54
+ double d;
55
+
56
+ n = (double)N - correction;
57
+ if ( N <= 0 || n <= 0.0 ) {
58
+ return 0.0 / 0.0; // NaN
59
+ }
60
+ if ( N == 1 || strideX == 0 ) {
61
+ return 0.0;
62
+ }
63
+ ix = offsetX;
64
+ M2 = 0.0;
65
+ for ( i = 0; i < N; i++ ) {
66
+ d = X[ ix ] - mean;
67
+ M2 += d * d;
68
+ ix += strideX;
69
+ }
70
+ return M2 / n;
71
+ }