From 247fe5a79da5d57ea48f780719b9e9588e581665 Mon Sep 17 00:00:00 2001 From: Om-A-osc Date: Sun, 1 Mar 2026 01:35:27 +0530 Subject: [PATCH 1/2] feat: add `stats/covarmtk` --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../@stdlib/stats/covarmtk/README.md | 308 +++++++++++++++ .../covarmtk/benchmark/benchmark.assign.js | 126 ++++++ .../stats/covarmtk/benchmark/benchmark.js | 122 ++++++ .../@stdlib/stats/covarmtk/docs/repl.txt | 116 ++++++ .../stats/covarmtk/docs/types/index.d.ts | 193 +++++++++ .../@stdlib/stats/covarmtk/docs/types/test.ts | 294 ++++++++++++++ .../@stdlib/stats/covarmtk/examples/index.js | 44 +++ .../@stdlib/stats/covarmtk/lib/index.js | 70 ++++ .../@stdlib/stats/covarmtk/lib/main.js | 168 ++++++++ .../@stdlib/stats/covarmtk/package.json | 65 ++++ .../stats/covarmtk/test/test.assign.js | 227 +++++++++++ .../@stdlib/stats/covarmtk/test/test.js | 39 ++ .../@stdlib/stats/covarmtk/test/test.main.js | 368 ++++++++++++++++++ 13 files changed, 2140 insertions(+) create mode 100644 lib/node_modules/@stdlib/stats/covarmtk/README.md create mode 100644 lib/node_modules/@stdlib/stats/covarmtk/benchmark/benchmark.assign.js create mode 100644 lib/node_modules/@stdlib/stats/covarmtk/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/stats/covarmtk/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/stats/covarmtk/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/stats/covarmtk/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/stats/covarmtk/examples/index.js create mode 100644 lib/node_modules/@stdlib/stats/covarmtk/lib/index.js create mode 100644 lib/node_modules/@stdlib/stats/covarmtk/lib/main.js create mode 100644 lib/node_modules/@stdlib/stats/covarmtk/package.json create mode 100644 lib/node_modules/@stdlib/stats/covarmtk/test/test.assign.js create mode 100644 lib/node_modules/@stdlib/stats/covarmtk/test/test.js create mode 100644 lib/node_modules/@stdlib/stats/covarmtk/test/test.main.js diff --git a/lib/node_modules/@stdlib/stats/covarmtk/README.md b/lib/node_modules/@stdlib/stats/covarmtk/README.md new file mode 100644 index 000000000000..262d90243981 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/covarmtk/README.md @@ -0,0 +1,308 @@ + + +# covarmtk + +> Compute the [covariance][covariance] of two ndarrays provided known means and using a one-pass textbook algorithm. + +
+ +The population [covariance][covariance] of two finite size populations of size `N` is given by + + + +```math +\mathop{\mathrm{cov_N}} = \frac{1}{N} \sum_{i=0}^{N-1} (x_i - \mu_x)(y_i - \mu_y) +``` + + + +where the population means are given by + + + +```math +\mu_x = \frac{1}{N} \sum_{i=0}^{N-1} x_i +``` + + + +and + + + +```math +\mu_y = \frac{1}{N} \sum_{i=0}^{N-1} y_i +``` + + + +Often in the analysis of data, the true population [covariance][covariance] is not known _a priori_ and must be estimated from samples drawn from population distributions. If one attempts to use the formula for the population [covariance][covariance], the result is biased and yields a **biased sample covariance**. To compute an **unbiased sample covariance** for samples of size `n`, + + + +```math +\mathop{\mathrm{cov_n}} = \frac{1}{n-1} \sum_{i=0}^{n-1} (x_i - \bar{x}_n)(y_i - \bar{y}_n) +``` + + + +where sample means are given by + + + +```math +\bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i +``` + + + +and + + + +```math +\bar{y} = \frac{1}{n} \sum_{i=0}^{n-1} y_i +``` + + + +The use of the term `n-1` is commonly referred to as Bessel's correction. Depending on the characteristics of the population distributions, other correction factors (e.g., `n-1.5`, `n+1`, etc) can yield better estimators. + +
+ + + +
+ +## Usage + +```javascript +var covarmtk = require( '@stdlib/stats/covarmtk' ); +``` + +#### covarmtk( x, y, correction, meanx, meany\[, options] ) + +Computes the [covariance][covariance] of two ndarrays provided known means and using a one-pass textbook algorithm. + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); + +var xbuf = new Float64Array( [ 1.0, -2.0, 2.0 ] ); +var ybuf = new Float64Array( [ 2.0, -2.0, 1.0 ] ); + +var x = new ndarray( 'float64', xbuf, [ 3 ], [ 1 ], 0, 'row-major' ); +var y = new ndarray( 'float64', ybuf, [ 3 ], [ 1 ], 0, 'row-major' ); + +var correction = scalar2ndarray( 1.0, { + 'dtype': 'float64' +}); +var meanx = scalar2ndarray( 1.0/3.0, { + 'dtype': 'float64' +}); +var meany = scalar2ndarray( 1.0/3.0, { + 'dtype': 'float64' +}); + +var out = covarmtk( x, y, correction, meanx, meany ); +// returns [ ~3.8333 ] +``` + +The function has the following parameters: + +- **x**: first input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. +- **y**: second input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. +- **correction**: zero-dimensional [ndarray][@stdlib/ndarray/ctor] specifying the degrees of freedom adjustment. Setting this parameter to a value other than `0` has the effect of adjusting the divisor during the calculation of the [covariance][covariance] according to `N-c` where `c` corresponds to the provided degrees of freedom adjustment and `N` corresponds to the number of elements in each input [ndarray][@stdlib/ndarray/ctor]. When computing the population [covariance][covariance], setting this parameter to `0` is the standard choice (i.e., the provided arrays contain data constituting entire populations). When computing the unbiased sample [covariance][covariance], setting this parameter to `1` is the standard choice (i.e., the provided arrays contain data sampled from larger populations; this is commonly referred to as Bessel's correction). +- **meanx**: zero-dimensional [ndarray][@stdlib/ndarray/ctor] specifying the mean of the first input [ndarray][@stdlib/ndarray/ctor]. +- **meany**: zero-dimensional [ndarray][@stdlib/ndarray/ctor] specifying the mean of the second input [ndarray][@stdlib/ndarray/ctor]. +- **options**: function options (_optional_). + +The function accepts the following options: + +- **dims**: list of dimensions over which to perform a reduction. If not provided, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. +- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. Must be a real-valued floating-point or "generic" [data type][@stdlib/ndarray/dtypes]. +- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [ndarray][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`. + +By default, the function performs a reduction over all elements in the provided input ndarrays. To perform a reduction over specific dimensions, provide a `dims` option. + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); + +var xbuf = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ] ); +var ybuf = new Float64Array( [ 2.0, 1.0, 2.0, 1.0, -2.0, 2.0, 3.0, 4.0 ] ); + +var x = new ndarray( 'float64', xbuf, [ 2, 2, 2 ], [ 4, 2, 1 ], 0, 'row-major' ); +var y = new ndarray( 'float64', ybuf, [ 2, 2, 2 ], [ 4, 2, 1 ], 0, 'row-major' ); + +var correction = scalar2ndarray( 1.0, { + 'dtype': 'float64' +}); +var meanx = scalar2ndarray( 1.25, { + 'dtype': 'float64' +}); +var meany = scalar2ndarray( 1.25, { + 'dtype': 'float64' +}); + +var out = covarmtk( x, y, correction, meanx, meany, { + 'dims': [ 2 ] +}); +// returns +``` + +#### covarmtk.assign( x, y, correction, meanx, meany, out\[, options] ) + +Computes the [covariance][covariance] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions and assigns results to a provided output [ndarray][@stdlib/ndarray/ctor]. + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var zeros = require( '@stdlib/ndarray/zeros' ); + +var xbuf = new Float64Array( [ 1.0, -2.0, 2.0 ] ); +var ybuf = new Float64Array( [ 2.0, -2.0, 1.0 ] ); + +var x = new ndarray( 'float64', xbuf, [ 3 ], [ 1 ], 0, 'row-major' ); +var y = new ndarray( 'float64', ybuf, [ 3 ], [ 1 ], 0, 'row-major' ); + +var correction = scalar2ndarray( 1.0, { + 'dtype': 'float64' +}); +var meanx = scalar2ndarray( 1.0/3.0, { + 'dtype': 'float64' +}); +var meany = scalar2ndarray( 1.0/3.0, { + 'dtype': 'float64' +}); + +var z = zeros( [], { + 'dtype': 'float64' +}); + +var out = covarmtk.assign( x, y, correction, meanx, meany, z ); +// returns [ ~3.8333 ] + +var bool = ( out === z ); +// returns true +``` + +The method has the following parameters: + +- **x**: first input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. +- **y**: second input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. +- **correction**: zero-dimensional [ndarray][@stdlib/ndarray/ctor] specifying the degrees of freedom adjustment. +- **meanx**: zero-dimensional [ndarray][@stdlib/ndarray/ctor] specifying the mean of the first input [ndarray][@stdlib/ndarray/ctor]. +- **meany**: zero-dimensional [ndarray][@stdlib/ndarray/ctor] specifying the mean of the second input [ndarray][@stdlib/ndarray/ctor]. +- **out**: output [ndarray][@stdlib/ndarray/ctor]. +- **options**: function options (_optional_). + +The method accepts the following options: + +- **dims**: list of dimensions over which to perform a reduction. If not provided, the function performs a reduction over all elements in the provided input ndarrays. + +
+ + + +
+ +## Notes + +- Both input ndarrays must have the same shape. +- Setting the `keepdims` option to `true` can be useful when wanting to ensure that the output [ndarray][@stdlib/ndarray/ctor] is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with ndarrays having the same shape as the input ndarrays. +- The output data type [policy][@stdlib/ndarray/output-dtype-policies] only applies to the main function and specifies that, by default, the function must return an [ndarray][@stdlib/ndarray/ctor] having a real-valued floating-point or "generic" [data type][@stdlib/ndarray/dtypes]. For the `assign` method, the output [ndarray][@stdlib/ndarray/ctor] is allowed to have any supported output [data type][@stdlib/ndarray/dtypes]. + +
+ + + +
+ +## Examples + + + +```javascript +var uniform = require( '@stdlib/random/array/uniform' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var covarmtk = require( '@stdlib/stats/covarmtk' ); + +var opts = { + 'dtype': 'float64' +}; + +var xbuf = uniform( 40, -50.0, 50.0, opts ); +var x = new ndarray( opts.dtype, xbuf, [ 5, 2, 4 ], [ 8, 4, 1 ], 0, 'row-major' ); + +var ybuf = uniform( 40, -50.0, 50.0, opts ); +var y = new ndarray( opts.dtype, ybuf, [ 5, 2, 4 ], [ 8, 4, 1 ], 0, 'row-major' ); + +var correction = scalar2ndarray( 1.0, { + 'dtype': 'float64' +}); +var meanx = scalar2ndarray( 0.0, { + 'dtype': 'float64' +}); +var meany = scalar2ndarray( 0.0, { + 'dtype': 'float64' +}); + +var out = covarmtk( x, y, correction, meanx, meany, { + 'dims': [ 2 ] +}); +console.log( ndarray2array( out ) ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/covarmtk/benchmark/benchmark.assign.js b/lib/node_modules/@stdlib/stats/covarmtk/benchmark/benchmark.assign.js new file mode 100644 index 000000000000..eb3c1fec7573 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/covarmtk/benchmark/benchmark.assign.js @@ -0,0 +1,126 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var format = require( '@stdlib/string/format' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var zeros = require( '@stdlib/array/zeros' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var pkg = require( './../package.json' ).name; +var covarmtk = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var correction; + var meanx; + var meany; + var xbuf; + var ybuf; + var out; + var x; + var y; + + xbuf = uniform( len, -50.0, 50.0, options ); + x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' ); + + ybuf = uniform( len, -50.0, 50.0, options ); + y = new ndarray( options.dtype, ybuf, [ len ], [ 1 ], 0, 'row-major' ); + + correction = scalar2ndarray( 1.0, options ); + meanx = scalar2ndarray( 0.0, options ); + meany = scalar2ndarray( 0.0, options ); + + out = new ndarray( options.dtype, zeros( 1, options.dtype ), [], [ 0 ], 0, 'row-major' ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var o; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = covarmtk.assign( x, y, correction, meanx, meany, out ); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( isnan( o.get() ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( format( '%s:assign:dtype=%s,len=%d', pkg, options.dtype, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/stats/covarmtk/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/covarmtk/benchmark/benchmark.js new file mode 100644 index 000000000000..94c7b1198d53 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/covarmtk/benchmark/benchmark.js @@ -0,0 +1,122 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var format = require( '@stdlib/string/format' ); +var uniform = require( '@stdlib/random/array/uniform' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var pkg = require( './../package.json' ).name; +var covarmtk = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var correction; + var meanx; + var meany; + var xbuf; + var ybuf; + var x; + var y; + + xbuf = uniform( len, -50.0, 50.0, options ); + x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' ); + + ybuf = uniform( len, -50.0, 50.0, options ); + y = new ndarray( options.dtype, ybuf, [ len ], [ 1 ], 0, 'row-major' ); + + correction = scalar2ndarray( 1.0, options ); + meanx = scalar2ndarray( 0.0, options ); + meany = scalar2ndarray( 0.0, options ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var o; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + o = covarmtk( x, y, correction, meanx, meany ); + if ( typeof o !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( isnan( o.get() ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 6; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( format( '%s:dtype=%s,len=%d', pkg, options.dtype, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/stats/covarmtk/docs/repl.txt b/lib/node_modules/@stdlib/stats/covarmtk/docs/repl.txt new file mode 100644 index 000000000000..b71d01074791 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/covarmtk/docs/repl.txt @@ -0,0 +1,116 @@ + +{{alias}}( x, y, correction, meanx, meany[, options] ) + Computes the covariance of two ndarrays provided known means and using a + one-pass textbook algorithm. + + Parameters + ---------- + x: ndarray + First input array. Must have a real-valued or "generic" data type. + + y: ndarray + Second input array. Must have a real-valued or "generic" data type. + + correction: ndarray + Zero-dimensional ndarray specifying the degrees of freedom adjustment. + + meanx: ndarray + Zero-dimensional ndarray specifying the mean of the first input + ndarray. + + meany: ndarray + Zero-dimensional ndarray specifying the mean of the second input + ndarray. + + options: Object (optional) + Function options. + + options.dtype: string|DataType (optional) + Output array data type. Must be a real-valued floating-point or + "generic" data type. + + options.dims: Array (optional) + List of dimensions over which to perform a reduction. If not provided, + the function performs a reduction over all elements in provided input + ndarrays. + + options.keepdims: boolean (optional) + Boolean indicating whether the reduced dimensions should be included in + the returned ndarray as singleton dimensions. Default: false. + + Returns + ------- + out: ndarray + Output array. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ 1.0, -2.0, 2.0 ] ); + > var y = {{alias:@stdlib/ndarray/array}}( [ 2.0, -2.0, 1.0 ] ); + > var c = {{alias:@stdlib/ndarray/from-scalar}}( 1.0, { 'dtype': 'float64' } ); + > var mx = {{alias:@stdlib/ndarray/from-scalar}}( 1.0/3.0, { 'dtype': 'float64' } ); + > var my = {{alias:@stdlib/ndarray/from-scalar}}( 1.0/3.0, { 'dtype': 'float64' } ); + > var out = {{alias}}( x, y, c, mx, my ) + + + > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ); + > var y = {{alias:@stdlib/ndarray/array}}( [ [ 4.0, 3.0 ], [ 2.0, 1.0 ] ] ); + > var out = {{alias}}( x, y, c, mx, my, { 'dims': [ 0 ] } ) + + + +{{alias}}.assign( x, y, correction, meanx, meany, out[, options] ) + Computes the covariance along one or more ndarray dimensions and assigns + results to a provided output ndarray. + + Parameters + ---------- + x: ndarray + First input array. Must have a real-valued or "generic" data type. + + y: ndarray + Second input array. Must have a real-valued or "generic" data type. + + correction: ndarray + Zero-dimensional ndarray specifying the degrees of freedom adjustment. + + meanx: ndarray + Zero-dimensional ndarray specifying the mean of the first input + ndarray. + + meany: ndarray + Zero-dimensional ndarray specifying the mean of the second input + ndarray. + + out: ndarray + Output array. + + options: Object (optional) + Function options. + + options.dims: Array (optional) + List of dimensions over which to perform a reduction. If not provided, + the function performs a reduction over all elements in provided input + ndarrays. + + Returns + ------- + out: ndarray + Output array. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ 1.0, -2.0, 2.0 ] ); + > var y = {{alias:@stdlib/ndarray/array}}( [ 2.0, -2.0, 1.0 ] ); + > var c = {{alias:@stdlib/ndarray/from-scalar}}( 1.0, { 'dtype': 'float64' } ); + > var mx = {{alias:@stdlib/ndarray/from-scalar}}( 1.0/3.0, { 'dtype': 'float64' } ); + > var my = {{alias:@stdlib/ndarray/from-scalar}}( 1.0/3.0, { 'dtype': 'float64' } ); + > var out = {{alias:@stdlib/ndarray/zeros}}( [] ); + > var z = {{alias}}.assign( x, y, c, mx, my, out ) + + > var bool = ( out === z ) + true + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/covarmtk/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/covarmtk/docs/types/index.d.ts new file mode 100644 index 000000000000..a55970e7e529 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/covarmtk/docs/types/index.d.ts @@ -0,0 +1,193 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +import { ArrayLike } from '@stdlib/types/array'; +import { RealFloatingPointAndGenericDataType as DataType, typedndarray } from '@stdlib/types/ndarray'; + +/** +* Input array. +*/ +type InputArray = typedndarray; + +/** +* Output array. +*/ +type OutputArray = typedndarray; + +/** +* Interface defining "base" options. +*/ +interface BaseOptions { + /** + * List of dimensions over which to perform a reduction. + */ + dims?: ArrayLike; +} + +/** +* Interface defining options. +*/ +interface Options extends BaseOptions { + /** + * Output array data type. + */ + dtype?: DataType; + + /** + * Boolean indicating whether the reduced dimensions should be included in the returned array as singleton dimensions. Default: `false`. + */ + keepdims?: boolean; +} + +/** +* Interface for performing a reduction on two ndarrays. +*/ +interface Binary { + /** + * Computes the covariance of two ndarrays provided known means and using a one-pass textbook algorithm. + * + * @param x - first input ndarray + * @param y - second input ndarray + * @param correction - zero-dimensional ndarray specifying the degrees of freedom adjustment + * @param meanx - zero-dimensional ndarray specifying the mean of the first input ndarray + * @param meany - zero-dimensional ndarray specifying the mean of the second input ndarray + * @param options - function options + * @returns output ndarray + * + * @example + * var Float64Array = require( '@stdlib/array/float64' ); + * var ndarray = require( '@stdlib/ndarray/ctor' ); + * var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); + * + * var xbuf = new Float64Array( [ 1.0, -2.0, 2.0 ] ); + * var ybuf = new Float64Array( [ 2.0, -2.0, 1.0 ] ); + * + * var x = new ndarray( 'float64', xbuf, [ 3 ], [ 1 ], 0, 'row-major' ); + * var y = new ndarray( 'float64', ybuf, [ 3 ], [ 1 ], 0, 'row-major' ); + * + * var correction = scalar2ndarray( 1.0, { 'dtype': 'float64' } ); + * var meanx = scalar2ndarray( 1.0/3.0, { 'dtype': 'float64' } ); + * var meany = scalar2ndarray( 1.0/3.0, { 'dtype': 'float64' } ); + * + * var out = covarmtk( x, y, correction, meanx, meany ); + * // returns [ ~3.8333 ] + */ + ( x: InputArray, y: InputArray, correction: InputArray, meanx: InputArray, meany: InputArray, options?: Options ): OutputArray; + + /** + * Computes the covariance of two ndarrays provided known means and using a one-pass textbook algorithm and assigns results to a provided output ndarray. + * + * @param x - first input ndarray + * @param y - second input ndarray + * @param correction - zero-dimensional ndarray specifying the degrees of freedom adjustment + * @param meanx - zero-dimensional ndarray specifying the mean of the first input ndarray + * @param meany - zero-dimensional ndarray specifying the mean of the second input ndarray + * @param out - output ndarray + * @param options - function options + * @returns output ndarray + * + * @example + * var Float64Array = require( '@stdlib/array/float64' ); + * var ndarray = require( '@stdlib/ndarray/ctor' ); + * var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); + * var zeros = require( '@stdlib/ndarray/zeros' ); + * + * var xbuf = new Float64Array( [ 1.0, -2.0, 2.0 ] ); + * var ybuf = new Float64Array( [ 2.0, -2.0, 1.0 ] ); + * + * var x = new ndarray( 'float64', xbuf, [ 3 ], [ 1 ], 0, 'row-major' ); + * var y = new ndarray( 'float64', ybuf, [ 3 ], [ 1 ], 0, 'row-major' ); + * + * var correction = scalar2ndarray( 1.0, { 'dtype': 'float64' } ); + * var meanx = scalar2ndarray( 1.0/3.0, { 'dtype': 'float64' } ); + * var meany = scalar2ndarray( 1.0/3.0, { 'dtype': 'float64' } ); + * + * var z = zeros( [], { 'dtype': 'float64' } ); + * + * var out = covarmtk.assign( x, y, correction, meanx, meany, z ); + * // returns [ ~3.8333 ] + * + * var bool = ( out === z ); + * // returns true + */ + assign = OutputArray, V = unknown>( x: InputArray, y: InputArray, correction: InputArray, meanx: InputArray, meany: InputArray, out: U, options?: BaseOptions ): U; +} + +/** +* Computes the covariance of two ndarrays provided known means and using a one-pass textbook algorithm. +* +* @param x - first input ndarray +* @param y - second input ndarray +* @param correction - zero-dimensional ndarray specifying the degrees of freedom adjustment +* @param meanx - zero-dimensional ndarray specifying the mean of the first input ndarray +* @param meany - zero-dimensional ndarray specifying the mean of the second input ndarray +* @param options - function options +* @returns output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* +* var xbuf = new Float64Array( [ 1.0, -2.0, 2.0 ] ); +* var ybuf = new Float64Array( [ 2.0, -2.0, 1.0 ] ); +* +* var x = new ndarray( 'float64', xbuf, [ 3 ], [ 1 ], 0, 'row-major' ); +* var y = new ndarray( 'float64', ybuf, [ 3 ], [ 1 ], 0, 'row-major' ); +* +* var correction = scalar2ndarray( 1.0, { 'dtype': 'float64' } ); +* var meanx = scalar2ndarray( 1.0/3.0, { 'dtype': 'float64' } ); +* var meany = scalar2ndarray( 1.0/3.0, { 'dtype': 'float64' } ); +* +* var out = covarmtk( x, y, correction, meanx, meany ); +* // returns [ ~3.8333 ] +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* var zeros = require( '@stdlib/ndarray/zeros' ); +* +* var xbuf = new Float64Array( [ 1.0, -2.0, 2.0 ] ); +* var ybuf = new Float64Array( [ 2.0, -2.0, 1.0 ] ); +* +* var x = new ndarray( 'float64', xbuf, [ 3 ], [ 1 ], 0, 'row-major' ); +* var y = new ndarray( 'float64', ybuf, [ 3 ], [ 1 ], 0, 'row-major' ); +* +* var correction = scalar2ndarray( 1.0, { 'dtype': 'float64' } ); +* var meanx = scalar2ndarray( 1.0/3.0, { 'dtype': 'float64' } ); +* var meany = scalar2ndarray( 1.0/3.0, { 'dtype': 'float64' } ); +* +* var z = zeros( [], { 'dtype': 'float64' } ); +* +* var out = covarmtk.assign( x, y, correction, meanx, meany, z ); +* // returns [ ~3.8333 ] +* +* var bool = ( out === z ); +* // returns true +*/ +declare const covarmtk: Binary; + + +// EXPORTS // + +export = covarmtk; diff --git a/lib/node_modules/@stdlib/stats/covarmtk/docs/types/test.ts b/lib/node_modules/@stdlib/stats/covarmtk/docs/types/test.ts new file mode 100644 index 000000000000..4b49d5b1efc0 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/covarmtk/docs/types/test.ts @@ -0,0 +1,294 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +import ndarray = require( '@stdlib/ndarray/ctor' ); +import scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +import zeros = require( '@stdlib/ndarray/zeros' ); +import covarmtk = require( './index' ); + + +// TESTS // + +// The main function returns an ndarray... +{ + const xbuf = new Float64Array( [ 1.0, -2.0, 2.0 ] ); + const ybuf = new Float64Array( [ 2.0, -2.0, 1.0 ] ); + const x = new ndarray( 'float64', xbuf, [ 3 ], [ 1 ], 0, 'row-major' ); + const y = new ndarray( 'float64', ybuf, [ 3 ], [ 1 ], 0, 'row-major' ); + const correction = scalar2ndarray( 1.0, { 'dtype': 'float64' } ); + const meanx = scalar2ndarray( 1.0/3.0, { 'dtype': 'float64' } ); + const meany = scalar2ndarray( 1.0/3.0, { 'dtype': 'float64' } ); + + covarmtk( x, y, correction, meanx, meany ); // $ExpectType OutputArray + covarmtk( x, y, correction, meanx, meany, { 'dims': [ 0 ] } ); // $ExpectType OutputArray + covarmtk( x, y, correction, meanx, meany, { 'dtype': 'float64' } ); // $ExpectType OutputArray + covarmtk( x, y, correction, meanx, meany, { 'keepdims': true } ); // $ExpectType OutputArray +} + +// The main function throws an error if provided arguments which are not ndarrays... +{ + const xbuf = new Float64Array( [ 1.0, -2.0, 2.0 ] ); + const x = new ndarray( 'float64', xbuf, [ 3 ], [ 1 ], 0, 'row-major' ); + const correction = scalar2ndarray( 1.0, { 'dtype': 'float64' } ); + const meanx = scalar2ndarray( 1.0/3.0, { 'dtype': 'float64' } ); + const meany = scalar2ndarray( 1.0/3.0, { 'dtype': 'float64' } ); + + covarmtk( '5', x, correction, meanx, meany ); // $ExpectError + covarmtk( x, '5', correction, meanx, meany ); // $ExpectError + covarmtk( x, x, '5', meanx, meany ); // $ExpectError + covarmtk( x, x, correction, '5', meany ); // $ExpectError + covarmtk( x, x, correction, meanx, '5' ); // $ExpectError + + covarmtk( 5, x, correction, meanx, meany ); // $ExpectError + covarmtk( x, 5, correction, meanx, meany ); // $ExpectError + covarmtk( x, x, 5, meanx, meany ); // $ExpectError + covarmtk( x, x, correction, 5, meany ); // $ExpectError + covarmtk( x, x, correction, meanx, 5 ); // $ExpectError + + covarmtk( true, x, correction, meanx, meany ); // $ExpectError + covarmtk( x, true, correction, meanx, meany ); // $ExpectError + covarmtk( x, x, true, meanx, meany ); // $ExpectError + covarmtk( x, x, correction, true, meany ); // $ExpectError + covarmtk( x, x, correction, meanx, true ); // $ExpectError + + covarmtk( false, x, correction, meanx, meany ); // $ExpectError + covarmtk( x, false, correction, meanx, meany ); // $ExpectError + covarmtk( x, x, false, meanx, meany ); // $ExpectError + covarmtk( x, x, correction, false, meany ); // $ExpectError + covarmtk( x, x, correction, meanx, false ); // $ExpectError + + covarmtk( null, x, correction, meanx, meany ); // $ExpectError + covarmtk( x, null, correction, meanx, meany ); // $ExpectError + covarmtk( x, x, null, meanx, meany ); // $ExpectError + covarmtk( x, x, correction, null, meany ); // $ExpectError + covarmtk( x, x, correction, meanx, null ); // $ExpectError + + covarmtk( undefined, x, correction, meanx, meany ); // $ExpectError + covarmtk( x, undefined, correction, meanx, meany ); // $ExpectError + covarmtk( x, x, undefined, meanx, meany ); // $ExpectError + covarmtk( x, x, correction, undefined, meany ); // $ExpectError + covarmtk( x, x, correction, meanx, undefined ); // $ExpectError + + covarmtk( [ 1, 2, 3 ], x, correction, meanx, meany ); // $ExpectError + covarmtk( x, [ 1, 2, 3 ], correction, meanx, meany ); // $ExpectError + covarmtk( x, x, [ 1, 2, 3 ], meanx, meany ); // $ExpectError + covarmtk( x, x, correction, [ 1, 2, 3 ], meany ); // $ExpectError + covarmtk( x, x, correction, meanx, [ 1, 2, 3 ] ); // $ExpectError + + covarmtk( {}, x, correction, meanx, meany ); // $ExpectError + covarmtk( x, {}, correction, meanx, meany ); // $ExpectError + covarmtk( x, x, {}, meanx, meany ); // $ExpectError + covarmtk( x, x, correction, {}, meany ); // $ExpectError + covarmtk( x, x, correction, meanx, {} ); // $ExpectError + + covarmtk( ( x: number ) => x, x, correction, meanx, meany ); // $ExpectError + covarmtk( x, ( x: number ) => x, correction, meanx, meany ); // $ExpectError + covarmtk( x, x, ( x: number ) => x, meanx, meany ); // $ExpectError + covarmtk( x, x, correction, ( x: number ) => x, meany ); // $ExpectError + covarmtk( x, x, correction, meanx, ( x: number ) => x ); // $ExpectError +} + +// The main function throws an error if provided an options argument which is not an object... +{ + const xbuf = new Float64Array( [ 1.0, -2.0, 2.0 ] ); + const x = new ndarray( 'float64', xbuf, [ 3 ], [ 1 ], 0, 'row-major' ); + const correction = scalar2ndarray( 1.0, { 'dtype': 'float64' } ); + const meanx = scalar2ndarray( 1.0/3.0, { 'dtype': 'float64' } ); + const meany = scalar2ndarray( 1.0/3.0, { 'dtype': 'float64' } ); + + covarmtk( x, x, correction, meanx, meany, '5' ); // $ExpectError + covarmtk( x, x, correction, meanx, meany, 5 ); // $ExpectError + covarmtk( x, x, correction, meanx, meany, true ); // $ExpectError + covarmtk( x, x, correction, meanx, meany, false ); // $ExpectError + covarmtk( x, x, correction, meanx, meany, null ); // $ExpectError + covarmtk( x, x, correction, meanx, meany, [] ); // $ExpectError + covarmtk( x, x, correction, meanx, meany, ( x: number ) => x ); // $ExpectError +} + +// The main function throws an error if provided a `dims` option which is not an array-like object of numbers... +{ + const xbuf = new Float64Array( [ 1.0, -2.0, 2.0 ] ); + const x = new ndarray( 'float64', xbuf, [ 3 ], [ 1 ], 0, 'row-major' ); + const correction = scalar2ndarray( 1.0, { 'dtype': 'float64' } ); + const meanx = scalar2ndarray( 1.0/3.0, { 'dtype': 'float64' } ); + const meany = scalar2ndarray( 1.0/3.0, { 'dtype': 'float64' } ); + + covarmtk( x, x, correction, meanx, meany, { 'dims': '5' } ); // $ExpectError + covarmtk( x, x, correction, meanx, meany, { 'dims': 5 } ); // $ExpectError + covarmtk( x, x, correction, meanx, meany, { 'dims': true } ); // $ExpectError + covarmtk( x, x, correction, meanx, meany, { 'dims': false } ); // $ExpectError + covarmtk( x, x, correction, meanx, meany, { 'dims': null } ); // $ExpectError + covarmtk( x, x, correction, meanx, meany, { 'dims': {} } ); // $ExpectError + covarmtk( x, x, correction, meanx, meany, { 'dims': ( x: number ) => x } ); // $ExpectError +} + +// The main function throws an error if provided a `dtype` option which is not a supported data type... +{ + const xbuf = new Float64Array( [ 1.0, -2.0, 2.0 ] ); + const x = new ndarray( 'float64', xbuf, [ 3 ], [ 1 ], 0, 'row-major' ); + const correction = scalar2ndarray( 1.0, { 'dtype': 'float64' } ); + const meanx = scalar2ndarray( 1.0/3.0, { 'dtype': 'float64' } ); + const meany = scalar2ndarray( 1.0/3.0, { 'dtype': 'float64' } ); + + covarmtk( x, x, correction, meanx, meany, { 'dtype': '5' } ); // $ExpectError + covarmtk( x, x, correction, meanx, meany, { 'dtype': 5 } ); // $ExpectError + covarmtk( x, x, correction, meanx, meany, { 'dtype': true } ); // $ExpectError + covarmtk( x, x, correction, meanx, meany, { 'dtype': false } ); // $ExpectError + covarmtk( x, x, correction, meanx, meany, { 'dtype': null } ); // $ExpectError + covarmtk( x, x, correction, meanx, meany, { 'dtype': [] } ); // $ExpectError + covarmtk( x, x, correction, meanx, meany, { 'dtype': {} } ); // $ExpectError + covarmtk( x, x, correction, meanx, meany, { 'dtype': ( x: number ) => x } ); // $ExpectError +} + +// The main function throws an error if provided a `keepdims` option which is not a boolean... +{ + const xbuf = new Float64Array( [ 1.0, -2.0, 2.0 ] ); + const x = new ndarray( 'float64', xbuf, [ 3 ], [ 1 ], 0, 'row-major' ); + const correction = scalar2ndarray( 1.0, { 'dtype': 'float64' } ); + const meanx = scalar2ndarray( 1.0/3.0, { 'dtype': 'float64' } ); + const meany = scalar2ndarray( 1.0/3.0, { 'dtype': 'float64' } ); + + covarmtk( x, x, correction, meanx, meany, { 'keepdims': '5' } ); // $ExpectError + covarmtk( x, x, correction, meanx, meany, { 'keepdims': 5 } ); // $ExpectError + covarmtk( x, x, correction, meanx, meany, { 'keepdims': null } ); // $ExpectError + covarmtk( x, x, correction, meanx, meany, { 'keepdims': [] } ); // $ExpectError + covarmtk( x, x, correction, meanx, meany, { 'keepdims': {} } ); // $ExpectError + covarmtk( x, x, correction, meanx, meany, { 'keepdims': ( x: number ) => x } ); // $ExpectError +} + +// The `assign` method returns an ndarray... +{ + const xbuf = new Float64Array( [ 1.0, -2.0, 2.0 ] ); + const x = new ndarray( 'float64', xbuf, [ 3 ], [ 1 ], 0, 'row-major' ); + const correction = scalar2ndarray( 1.0, { 'dtype': 'float64' } ); + const meanx = scalar2ndarray( 1.0/3.0, { 'dtype': 'float64' } ); + const meany = scalar2ndarray( 1.0/3.0, { 'dtype': 'float64' } ); + const z = zeros( [], { 'dtype': 'float64' } ); + + covarmtk.assign( x, x, correction, meanx, meany, z ); // $ExpectType float64ndarray + covarmtk.assign( x, x, correction, meanx, meany, z, { 'dims': [ 0 ] } ); // $ExpectType float64ndarray +} + +// The `assign` method throws an error if provided arguments which are not ndarrays... +{ + const xbuf = new Float64Array( [ 1.0, -2.0, 2.0 ] ); + const x = new ndarray( 'float64', xbuf, [ 3 ], [ 1 ], 0, 'row-major' ); + const correction = scalar2ndarray( 1.0, { 'dtype': 'float64' } ); + const meanx = scalar2ndarray( 1.0/3.0, { 'dtype': 'float64' } ); + const meany = scalar2ndarray( 1.0/3.0, { 'dtype': 'float64' } ); + const z = zeros( [], { 'dtype': 'float64' } ); + + covarmtk.assign( '5', x, correction, meanx, meany, z ); // $ExpectError + covarmtk.assign( x, '5', correction, meanx, meany, z ); // $ExpectError + covarmtk.assign( x, x, '5', meanx, meany, z ); // $ExpectError + covarmtk.assign( x, x, correction, '5', meany, z ); // $ExpectError + covarmtk.assign( x, x, correction, meanx, '5', z ); // $ExpectError + covarmtk.assign( x, x, correction, meanx, meany, '5' ); // $ExpectError + + covarmtk.assign( 5, x, correction, meanx, meany, z ); // $ExpectError + covarmtk.assign( x, 5, correction, meanx, meany, z ); // $ExpectError + covarmtk.assign( x, x, 5, meanx, meany, z ); // $ExpectError + covarmtk.assign( x, x, correction, 5, meany, z ); // $ExpectError + covarmtk.assign( x, x, correction, meanx, 5, z ); // $ExpectError + covarmtk.assign( x, x, correction, meanx, meany, 5 ); // $ExpectError + + covarmtk.assign( true, x, correction, meanx, meany, z ); // $ExpectError + covarmtk.assign( x, true, correction, meanx, meany, z ); // $ExpectError + covarmtk.assign( x, x, true, meanx, meany, z ); // $ExpectError + covarmtk.assign( x, x, correction, true, meany, z ); // $ExpectError + covarmtk.assign( x, x, correction, meanx, true, z ); // $ExpectError + covarmtk.assign( x, x, correction, meanx, meany, true ); // $ExpectError + + covarmtk.assign( false, x, correction, meanx, meany, z ); // $ExpectError + covarmtk.assign( x, false, correction, meanx, meany, z ); // $ExpectError + covarmtk.assign( x, x, false, meanx, meany, z ); // $ExpectError + covarmtk.assign( x, x, correction, false, meany, z ); // $ExpectError + covarmtk.assign( x, x, correction, meanx, false, z ); // $ExpectError + covarmtk.assign( x, x, correction, meanx, meany, false ); // $ExpectError + + covarmtk.assign( null, x, correction, meanx, meany, z ); // $ExpectError + covarmtk.assign( x, null, correction, meanx, meany, z ); // $ExpectError + covarmtk.assign( x, x, null, meanx, meany, z ); // $ExpectError + covarmtk.assign( x, x, correction, null, meany, z ); // $ExpectError + covarmtk.assign( x, x, correction, meanx, null, z ); // $ExpectError + covarmtk.assign( x, x, correction, meanx, meany, null ); // $ExpectError + + covarmtk.assign( undefined, x, correction, meanx, meany, z ); // $ExpectError + covarmtk.assign( x, undefined, correction, meanx, meany, z ); // $ExpectError + covarmtk.assign( x, x, undefined, meanx, meany, z ); // $ExpectError + covarmtk.assign( x, x, correction, undefined, meany, z ); // $ExpectError + covarmtk.assign( x, x, correction, meanx, undefined, z ); // $ExpectError + covarmtk.assign( x, x, correction, meanx, meany, undefined ); // $ExpectError + + covarmtk.assign( [ 1, 2, 3 ], x, correction, meanx, meany, z ); // $ExpectError + covarmtk.assign( x, [ 1, 2, 3 ], correction, meanx, meany, z ); // $ExpectError + covarmtk.assign( x, x, [ 1, 2, 3 ], meanx, meany, z ); // $ExpectError + covarmtk.assign( x, x, correction, [ 1, 2, 3 ], meany, z ); // $ExpectError + covarmtk.assign( x, x, correction, meanx, [ 1, 2, 3 ], z ); // $ExpectError + covarmtk.assign( x, x, correction, meanx, meany, [ 1, 2, 3 ] ); // $ExpectError + + covarmtk.assign( {}, x, correction, meanx, meany, z ); // $ExpectError + covarmtk.assign( x, {}, correction, meanx, meany, z ); // $ExpectError + covarmtk.assign( x, x, {}, meanx, meany, z ); // $ExpectError + covarmtk.assign( x, x, correction, {}, meany, z ); // $ExpectError + covarmtk.assign( x, x, correction, meanx, {}, z ); // $ExpectError + covarmtk.assign( x, x, correction, meanx, meany, {} ); // $ExpectError + + covarmtk.assign( ( x: number ) => x, x, correction, meanx, meany, z ); // $ExpectError + covarmtk.assign( x, ( x: number ) => x, correction, meanx, meany, z ); // $ExpectError + covarmtk.assign( x, x, ( x: number ) => x, meanx, meany, z ); // $ExpectError + covarmtk.assign( x, x, correction, ( x: number ) => x, meany, z ); // $ExpectError + covarmtk.assign( x, x, correction, meanx, ( x: number ) => x, z ); // $ExpectError + covarmtk.assign( x, x, correction, meanx, meany, ( x: number ) => x ); // $ExpectError +} + +// The `assign` method throws an error if provided an options argument which is not an object... +{ + const xbuf = new Float64Array( [ 1.0, -2.0, 2.0 ] ); + const x = new ndarray( 'float64', xbuf, [ 3 ], [ 1 ], 0, 'row-major' ); + const correction = scalar2ndarray( 1.0, { 'dtype': 'float64' } ); + const meanx = scalar2ndarray( 1.0/3.0, { 'dtype': 'float64' } ); + const meany = scalar2ndarray( 1.0/3.0, { 'dtype': 'float64' } ); + const z = zeros( [], { 'dtype': 'float64' } ); + + covarmtk.assign( x, x, correction, meanx, meany, z, '5' ); // $ExpectError + covarmtk.assign( x, x, correction, meanx, meany, z, 5 ); // $ExpectError + covarmtk.assign( x, x, correction, meanx, meany, z, true ); // $ExpectError + covarmtk.assign( x, x, correction, meanx, meany, z, false ); // $ExpectError + covarmtk.assign( x, x, correction, meanx, meany, z, null ); // $ExpectError + covarmtk.assign( x, x, correction, meanx, meany, z, [] ); // $ExpectError + covarmtk.assign( x, x, correction, meanx, meany, z, ( x: number ) => x ); // $ExpectError +} + +// The `assign` method throws an error if provided a `dims` option which is not an array-like object of numbers... +{ + const xbuf = new Float64Array( [ 1.0, -2.0, 2.0 ] ); + const x = new ndarray( 'float64', xbuf, [ 3 ], [ 1 ], 0, 'row-major' ); + const correction = scalar2ndarray( 1.0, { 'dtype': 'float64' } ); + const meanx = scalar2ndarray( 1.0/3.0, { 'dtype': 'float64' } ); + const meany = scalar2ndarray( 1.0/3.0, { 'dtype': 'float64' } ); + const z = zeros( [], { 'dtype': 'float64' } ); + + covarmtk.assign( x, x, correction, meanx, meany, z, { 'dims': '5' } ); // $ExpectError + covarmtk.assign( x, x, correction, meanx, meany, z, { 'dims': 5 } ); // $ExpectError + covarmtk.assign( x, x, correction, meanx, meany, z, { 'dims': true } ); // $ExpectError + covarmtk.assign( x, x, correction, meanx, meany, z, { 'dims': false } ); // $ExpectError + covarmtk.assign( x, x, correction, meanx, meany, z, { 'dims': null } ); // $ExpectError + covarmtk.assign( x, x, correction, meanx, meany, z, { 'dims': {} } ); // $ExpectError + covarmtk.assign( x, x, correction, meanx, meany, z, { 'dims': ( x: number ) => x } ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/covarmtk/examples/index.js b/lib/node_modules/@stdlib/stats/covarmtk/examples/index.js new file mode 100644 index 000000000000..9819ffb31e1a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/covarmtk/examples/index.js @@ -0,0 +1,44 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var uniform = require( '@stdlib/random/array/uniform' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var covarmtk = require( './../lib' ); + +var opts = { + 'dtype': 'float64' +}; + +var xbuf = uniform( 40, -50.0, 50.0, opts ); +var x = new ndarray( opts.dtype, xbuf, [ 5, 2, 4 ], [ 8, 4, 1 ], 0, 'row-major' ); + +var ybuf = uniform( 40, -50.0, 50.0, opts ); +var y = new ndarray( opts.dtype, ybuf, [ 5, 2, 4 ], [ 8, 4, 1 ], 0, 'row-major' ); + +var correction = scalar2ndarray( 1.0, opts ); +var meanx = scalar2ndarray( 0.0, opts ); +var meany = scalar2ndarray( 0.0, opts ); + +var out = covarmtk( x, y, correction, meanx, meany, { + 'dims': [ 2 ] +}); +console.log( ndarray2array( out ) ); diff --git a/lib/node_modules/@stdlib/stats/covarmtk/lib/index.js b/lib/node_modules/@stdlib/stats/covarmtk/lib/index.js new file mode 100644 index 000000000000..2b330fb2215b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/covarmtk/lib/index.js @@ -0,0 +1,70 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* Compute the covariance of two ndarrays provided known means and using a one-pass textbook algorithm. +* +* @module @stdlib/stats/covarmtk +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* var covarmtk = require( '@stdlib/stats/covarmtk' ); +* +* // Create data buffers: +* var xbuf = new Float64Array( [ 1.0, -2.0, 2.0 ] ); +* var ybuf = new Float64Array( [ 2.0, -2.0, 1.0 ] ); +* +* // Define the shape of the input arrays: +* var sh = [ 3 ]; +* +* // Define the array strides: +* var sx = [ 1 ]; +* var sy = [ 1 ]; +* +* // Define the index offsets: +* var ox = 0; +* var oy = 0; +* +* // Create input ndarrays: +* var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); +* var y = new ndarray( 'float64', ybuf, sh, sx, ox, 'row-major' ); +* +* // Define correction and means: +* var correction = scalar2ndarray( 1.0, { 'dtype': 'float64' } ); +* var meanx = scalar2ndarray( 1.0/3.0, { 'dtype': 'float64' } ); +* var meany = scalar2ndarray( 1.0/3.0, { 'dtype': 'float64' } ); +* +* // Perform reduction: +* var out = covarmtk( x, y, correction, meanx, meany ); +* // returns [ ~3.8333 ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; + +// exports: { "assign": "main.assign" } diff --git a/lib/node_modules/@stdlib/stats/covarmtk/lib/main.js b/lib/node_modules/@stdlib/stats/covarmtk/lib/main.js new file mode 100644 index 000000000000..c523c5d1d06b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/covarmtk/lib/main.js @@ -0,0 +1,168 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var dtypes = require( '@stdlib/ndarray/dtypes' ); +var gcovarmtk = require( '@stdlib/stats/base/ndarray/covarmtk' ); +var dcovarmtk = require( '@stdlib/stats/base/ndarray/dcovarmtk' ); +var scovarmtk = require( '@stdlib/stats/base/ndarray/scovarmtk' ); +var factory = require( '@stdlib/ndarray/base/binary-reduce-strided1d-dispatch-factory' ); +var isObject = require( '@stdlib/assert/is-object' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var indicesComplement = require( '@stdlib/array/base/indices-complement' ); +var takeIndexed = require( '@stdlib/array/base/take-indexed' ); +var zeroTo = require( '@stdlib/array/base/zero-to' ); +var broadcast = require( '@stdlib/ndarray/base/broadcast-array' ); +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); + + +// VARIABLES // + +var idtypes = dtypes( 'real_and_generic' ); +var odtypes = dtypes( 'real_floating_point_and_generic' ); +var policies = { + 'output': 'real_floating_point_and_generic', + 'casting': 'none' +}; +var table = { + 'types': [ + 'float64', + 'float64', + 'float32', + 'float32' + ], + 'fcns': [ + dcovarmtk, + scovarmtk + ], + 'default': gcovarmtk +}; +var itypes = [ idtypes, idtypes, idtypes, idtypes, idtypes ]; +var dispatcher = factory( table, itypes, odtypes, policies ); + + +// FUNCTIONS // + +/** +* Broadcasts arguments to a loop shape. +* +* @private +* @param {Array} args - arguments to broadcast +* @param {ndarray} x - reference ndarray for shape +* @param {Object} [options] - function options +* @returns {Array} broadcasted arguments +*/ +function broadcastArgs( args, x, options ) { + var loopShape; + var sh; + var d; + var i; + + sh = getShape( x ); + if ( isObject( options ) && options.dims ) { + d = options.dims; + } else { + d = zeroTo( sh.length ); + } + loopShape = takeIndexed( sh, indicesComplement( sh.length, d ) ); + for ( i = 0; i < args.length; i++ ) { + args[ i ] = broadcast( args[ i ], loopShape ); + } + return args; +} + + +// MAIN // + +/** +* Computes the covariance of two ndarrays provided known means and using a one-pass textbook algorithm. +* +* @name covarmtk +* @type {Function} +* @param {ndarray} x - first input ndarray +* @param {ndarray} y - second input ndarray +* @param {ndarray} correction - zero-dimensional ndarray specifying the degrees of freedom adjustment +* @param {ndarray} meanx - zero-dimensional ndarray specifying the mean of the first input ndarray +* @param {ndarray} meany - zero-dimensional ndarray specifying the mean of the second input ndarray +* @param {Options} [options] - function options +* @param {IntegerArray} [options.dims] - list of dimensions over which to perform a reduction +* @param {boolean} [options.keepdims=false] - boolean indicating whether the reduced dimensions should be included in the returned ndarray as singleton dimensions +* @param {*} [options.dtype] - output ndarray data type +* @throws {TypeError} first argument must be an ndarray-like object +* @throws {TypeError} second argument must be an ndarray-like object +* @throws {TypeError} third argument must be an ndarray-like object +* @throws {TypeError} fourth argument must be an ndarray-like object +* @throws {TypeError} fifth argument must be an ndarray-like object +* @throws {TypeError} options argument must be an object +* @throws {RangeError} dimension indices must not exceed input ndarray bounds +* @throws {RangeError} number of dimension indices must not exceed the number of input ndarray dimensions +* @throws {Error} must provide valid options +* @returns {ndarray} output ndarray +*/ +function covarmtk( x, y, correction, meanx, meany, options ) { + var args = broadcastArgs( [ correction, meanx, meany ], x, options ); + if ( arguments.length < 6 ) { + return dispatcher( x, y, args[ 0 ], args[ 1 ], args[ 2 ] ); + } + return dispatcher( x, y, args[ 0 ], args[ 1 ], args[ 2 ], options ); +} + +/** +* Computes the covariance of two ndarrays provided known means and using a one-pass textbook algorithm and assigns results to a provided output ndarray. +* +* @private +* @name assign +* @memberof covarmtk +* @type {Function} +* @param {ndarray} x - first input ndarray +* @param {ndarray} y - second input ndarray +* @param {ndarray} correction - zero-dimensional ndarray specifying the degrees of freedom adjustment +* @param {ndarray} meanx - zero-dimensional ndarray specifying the mean of the first input ndarray +* @param {ndarray} meany - zero-dimensional ndarray specifying the mean of the second input ndarray +* @param {ndarray} out - output ndarray +* @param {Options} [options] - function options +* @throws {TypeError} first argument must be an ndarray-like object +* @throws {TypeError} second argument must be an ndarray-like object +* @throws {TypeError} third argument must be an ndarray-like object +* @throws {TypeError} fourth argument must be an ndarray-like object +* @throws {TypeError} fifth argument must be an ndarray-like object +* @throws {TypeError} sixth argument must be an ndarray-like object +* @throws {TypeError} options argument must be an object +* @throws {RangeError} dimension indices must not exceed input ndarray bounds +* @throws {RangeError} number of dimension indices must not exceed the number of input ndarray dimensions +* @throws {Error} must provide valid options +* @returns {ndarray} output ndarray +*/ +function assign( x, y, correction, meanx, meany, out, options ) { + var args = broadcastArgs( [ correction, meanx, meany ], x, options ); + if ( arguments.length < 7 ) { + return dispatcher.assign( x, y, args[ 0 ], args[ 1 ], args[ 2 ], out ); + } + return dispatcher.assign( x, y, args[ 0 ], args[ 1 ], args[ 2 ], out, options ); // eslint-disable-line max-len +} + +// Set the `assign` method: +setReadOnly( covarmtk, 'assign', assign ); + + +// EXPORTS // + +module.exports = covarmtk; diff --git a/lib/node_modules/@stdlib/stats/covarmtk/package.json b/lib/node_modules/@stdlib/stats/covarmtk/package.json new file mode 100644 index 000000000000..4269be7e22c1 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/covarmtk/package.json @@ -0,0 +1,65 @@ +{ + "name": "@stdlib/stats/covarmtk", + "version": "0.0.0", + "description": "Compute the covariance of two ndarrays provided known means and using a one-pass textbook algorithm.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "statistics", + "stats", + "mathematics", + "math", + "covariance", + "cov", + "textbook", + "one-pass", + "ndarray" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/stats/covarmtk/test/test.assign.js b/lib/node_modules/@stdlib/stats/covarmtk/test/test.assign.js new file mode 100644 index 000000000000..9a5705d837e6 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/covarmtk/test/test.assign.js @@ -0,0 +1,227 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var isAlmostSameValue = require( '@stdlib/assert/is-almost-same-value' ); +var covarmtk = require( './../lib' ).assign; + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof covarmtk, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object', function test( t ) { + var correction; + var values; + var meanx; + var meany; + var out; + var x; + var i; + + x = zeros( [ 3 ], { + 'dtype': 'float64' + }); + out = zeros( [], { + 'dtype': 'float64' + }); + correction = scalar2ndarray( 1.0, { + 'dtype': 'float64' + }); + meanx = scalar2ndarray( 0.0, { + 'dtype': 'float64' + }); + meany = scalar2ndarray( 0.0, { + 'dtype': 'float64' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + covarmtk( value, x, correction, meanx, meany, out ); + }; + } +}); + +tape( 'the function throws an error if provided a sixth argument which is not an ndarray-like object', function test( t ) { + var correction; + var values; + var meanx; + var meany; + var x; + var i; + + x = zeros( [ 3 ], { + 'dtype': 'float64' + }); + correction = scalar2ndarray( 1.0, { + 'dtype': 'float64' + }); + meanx = scalar2ndarray( 0.0, { + 'dtype': 'float64' + }); + meany = scalar2ndarray( 0.0, { + 'dtype': 'float64' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + covarmtk( x, x, correction, meanx, meany, value ); + }; + } +}); + +tape( 'the function performs a reduction on an ndarray (default, row-major)', function test( t ) { + var correction; + var expected; + var actual; + var meanx; + var meany; + var xbuf; + var ybuf; + var out; + var x; + var y; + + xbuf = new Float64Array( [ 1.0, -2.0, 2.0 ] ); + ybuf = new Float64Array( [ 2.0, -2.0, 1.0 ] ); + + x = new ndarray( 'float64', xbuf, [ 3 ], [ 1 ], 0, 'row-major' ); + y = new ndarray( 'float64', ybuf, [ 3 ], [ 1 ], 0, 'row-major' ); + + correction = scalar2ndarray( 1.0, { + 'dtype': 'float64' + }); + meanx = scalar2ndarray( 1.0/3.0, { + 'dtype': 'float64' + }); + meany = scalar2ndarray( 1.0/3.0, { + 'dtype': 'float64' + }); + + out = zeros( [], { + 'dtype': 'float64' + }); + + actual = covarmtk( x, y, correction, meanx, meany, out ); + expected = 3.8333333333333335; + + t.strictEqual( actual, out, 'returns expected value' ); + t.strictEqual( isAlmostSameValue( actual.get(), expected, 5 ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports specifying reduction dimensions (row-major)', function test( t ) { + var correction; + var expected; + var actual; + var meanx; + var meany; + var xbuf; + var ybuf; + var out; + var x; + var y; + var i; + var j; + + xbuf = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ] ); + ybuf = new Float64Array( [ 2.0, 1.0, 2.0, 1.0, -2.0, 2.0, 3.0, 4.0 ] ); + + x = new ndarray( 'float64', xbuf, [ 2, 2, 2 ], [ 4, 2, 1 ], 0, 'row-major' ); + y = new ndarray( 'float64', ybuf, [ 2, 2, 2 ], [ 4, 2, 1 ], 0, 'row-major' ); + + correction = scalar2ndarray( 1.0, { + 'dtype': 'float64' + }); + meanx = scalar2ndarray( 1.25, { + 'dtype': 'float64' + }); + meany = scalar2ndarray( 1.25, { + 'dtype': 'float64' + }); + + out = zeros( [ 2, 2 ], { + 'dtype': 'float64' + }); + + actual = covarmtk( x, y, correction, meanx, meany, out, { + 'dims': [ 2 ] + }); + + expected = [ + [ -0.375, 2.625 ], + [ 11.875, 6.875 ] + ]; + t.strictEqual( actual, out, 'returns output array' ); + + actual = ndarray2array( out ); + for ( i = 0; i < 2; i++ ) { + for ( j = 0; j < 2; j++ ) { + t.strictEqual( actual[ i ][ j ], expected[ i ][ j ], 'returns expected value' ); + } + } + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/covarmtk/test/test.js b/lib/node_modules/@stdlib/stats/covarmtk/test/test.js new file mode 100644 index 000000000000..9d5778c80f78 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/covarmtk/test/test.js @@ -0,0 +1,39 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isMethod = require( '@stdlib/assert/is-method' ); +var covarmtk = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof covarmtk, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is an `assign` method', function test( t ) { + t.strictEqual( isMethod( covarmtk, 'assign' ), true, 'returns expected value' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/covarmtk/test/test.main.js b/lib/node_modules/@stdlib/stats/covarmtk/test/test.main.js new file mode 100644 index 000000000000..1cc91927d932 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/covarmtk/test/test.main.js @@ -0,0 +1,368 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var empty = require( '@stdlib/ndarray/empty' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var getDType = require( '@stdlib/ndarray/dtype' ); +var getShape = require( '@stdlib/ndarray/shape' ); +var isAlmostSameValue = require( '@stdlib/assert/is-almost-same-value' ); +var covarmtk = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof covarmtk, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object', function test( t ) { + var correction; + var values; + var meanx; + var meany; + var x; + var i; + + x = zeros( [ 3 ], { + 'dtype': 'float64' + }); + correction = scalar2ndarray( 1.0, { + 'dtype': 'float64' + }); + meanx = scalar2ndarray( 0.0, { + 'dtype': 'float64' + }); + meany = scalar2ndarray( 0.0, { + 'dtype': 'float64' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + covarmtk( value, x, correction, meanx, meany ); + }; + } +}); + +tape( 'the function throws an error if provided a second argument which is not an ndarray-like object', function test( t ) { + var correction; + var values; + var meanx; + var meany; + var x; + var i; + + x = zeros( [ 3 ], { + 'dtype': 'float64' + }); + correction = scalar2ndarray( 1.0, { + 'dtype': 'float64' + }); + meanx = scalar2ndarray( 0.0, { + 'dtype': 'float64' + }); + meany = scalar2ndarray( 0.0, { + 'dtype': 'float64' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + {}, + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + covarmtk( x, value, correction, meanx, meany ); + }; + } +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray-like object having a supported data type', function test( t ) { + var correction; + var values; + var meanx; + var meany; + var x; + var i; + + x = zeros( [ 3 ], { + 'dtype': 'float64' + }); + correction = scalar2ndarray( 1.0, { + 'dtype': 'float64' + }); + meanx = scalar2ndarray( 0.0, { + 'dtype': 'float64' + }); + meany = scalar2ndarray( 0.0, { + 'dtype': 'float64' + }); + + values = [ + empty( [ 3 ], { + 'dtype': 'bool' + }) + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + covarmtk( value, x, correction, meanx, meany ); + }; + } +}); + +tape( 'the function throws an error if provided a sixth argument which is not an object', function test( t ) { + var correction; + var values; + var meanx; + var meany; + var x; + var i; + + x = zeros( [ 3 ], { + 'dtype': 'float64' + }); + correction = scalar2ndarray( 1.0, { + 'dtype': 'float64' + }); + meanx = scalar2ndarray( 0.0, { + 'dtype': 'float64' + }); + meany = scalar2ndarray( 0.0, { + 'dtype': 'float64' + }); + + values = [ + '5', + 5, + NaN, + true, + false, + null, + void 0, + [], + function noop() {} + ]; + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + covarmtk( x, x, correction, meanx, meany, value ); + }; + } +}); + +tape( 'the function computes the covariance of two ndarrays provided known means', function test( t ) { + var correction; + var expected; + var meanx; + var meany; + var xbuf; + var ybuf; + var out; + var x; + var y; + + xbuf = new Float64Array( [ 1.0, -2.0, 2.0 ] ); + ybuf = new Float64Array( [ 2.0, -2.0, 1.0 ] ); + + x = new ndarray( 'float64', xbuf, [ 3 ], [ 1 ], 0, 'row-major' ); + y = new ndarray( 'float64', ybuf, [ 3 ], [ 1 ], 0, 'row-major' ); + + correction = scalar2ndarray( 1.0, { + 'dtype': 'float64' + }); + meanx = scalar2ndarray( 1.0/3.0, { + 'dtype': 'float64' + }); + meany = scalar2ndarray( 1.0/3.0, { + 'dtype': 'float64' + }); + + out = covarmtk( x, y, correction, meanx, meany ); + + expected = 3.8333333333333335; + t.strictEqual( isndarrayLike( out ), true, 'returns an ndarray' ); + t.deepEqual( getShape( out ), [], 'returns zero-dim array' ); + t.strictEqual( isAlmostSameValue( out.get(), expected, 5 ), true, 'returns expected value'); + t.end(); +}); + +tape( 'the function computes the covariance of two ndarrays along a specific dimension', function test( t ) { + var correction; + var expected; + var actual; + var meanx; + var meany; + var xbuf; + var ybuf; + var out; + var x; + var y; + var i; + var j; + + xbuf = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ] ); + ybuf = new Float64Array( [ 2.0, 1.0, 2.0, 1.0, -2.0, 2.0, 3.0, 4.0 ] ); + + x = new ndarray( 'float64', xbuf, [ 2, 2, 2 ], [ 4, 2, 1 ], 0, 'row-major' ); + y = new ndarray( 'float64', ybuf, [ 2, 2, 2 ], [ 4, 2, 1 ], 0, 'row-major' ); + + correction = scalar2ndarray( 1.0, { + 'dtype': 'float64' + }); + meanx = scalar2ndarray( 1.25, { + 'dtype': 'float64' + }); + meany = scalar2ndarray( 1.25, { + 'dtype': 'float64' + }); + + out = covarmtk( x, y, correction, meanx, meany, { + 'dims': [ 2 ] + }); + + expected = [ + [ -0.375, 2.625 ], + [ 11.875, 6.875 ] + ]; + actual = ndarray2array( out ); + for ( i = 0; i < 2; i++ ) { + for ( j = 0; j < 2; j++ ) { + t.strictEqual( actual[ i ][ j ], expected[ i ][ j ], 'returns expected value' ); + } + } + t.end(); +}); + +tape( 'the function supports the `keepdims` option', function test( t ) { + var correction; + var meanx; + var meany; + var xbuf; + var ybuf; + var out; + var x; + var y; + + xbuf = new Float64Array( [ 1.0, -2.0, 2.0 ] ); + ybuf = new Float64Array( [ 2.0, -2.0, 1.0 ] ); + + x = new ndarray( 'float64', xbuf, [ 3 ], [ 1 ], 0, 'row-major' ); + y = new ndarray( 'float64', ybuf, [ 3 ], [ 1 ], 0, 'row-major' ); + + correction = scalar2ndarray( 1.0, { + 'dtype': 'float64' + }); + meanx = scalar2ndarray( 1.0/3.0, { + 'dtype': 'float64' + }); + meany = scalar2ndarray( 1.0/3.0, { + 'dtype': 'float64' + }); + + out = covarmtk( x, y, correction, meanx, meany, { + 'keepdims': true + }); + + t.deepEqual( getShape( out ), [ 1 ], 'returns singleton dimension' ); + t.strictEqual( isAlmostSameValue( out.get( 0 ), 3.8333333333333335, 5 ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports specifying the output array data type', function test( t ) { + var correction; + var meanx; + var meany; + var xbuf; + var ybuf; + var out; + var x; + var y; + + xbuf = new Float64Array( [ 1.0, -2.0, 2.0 ] ); + ybuf = new Float64Array( [ 2.0, -2.0, 1.0 ] ); + + x = new ndarray( 'float64', xbuf, [ 3 ], [ 1 ], 0, 'row-major' ); + y = new ndarray( 'float64', ybuf, [ 3 ], [ 1 ], 0, 'row-major' ); + + correction = scalar2ndarray( 1.0, { + 'dtype': 'float64' + }); + meanx = scalar2ndarray( 1.0/3.0, { + 'dtype': 'float64' + }); + meany = scalar2ndarray( 1.0/3.0, { + 'dtype': 'float64' + }); + + out = covarmtk( x, y, correction, meanx, meany, { + 'dtype': 'generic' + }); + + t.strictEqual( getDType( out ), 'generic', 'returns expected data type' ); + t.strictEqual( isAlmostSameValue( out.get(), 3.8333333333333335, 5 ), true, 'returns expected value' ); + t.end(); +}); From ab67d1c15657ed7ff266d26437cbb8a3c0ab8cf8 Mon Sep 17 00:00:00 2001 From: Om-A-osc Date: Thu, 5 Mar 2026 00:00:09 +0530 Subject: [PATCH 2/2] fix: addressing round one comments --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: na - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed --- --- lib/node_modules/@stdlib/stats/covarmtk/README.md | 4 ++-- lib/node_modules/@stdlib/stats/covarmtk/docs/repl.txt | 5 +++-- lib/node_modules/@stdlib/stats/covarmtk/lib/index.js | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/node_modules/@stdlib/stats/covarmtk/README.md b/lib/node_modules/@stdlib/stats/covarmtk/README.md index 262d90243981..91c25d2a787f 100644 --- a/lib/node_modules/@stdlib/stats/covarmtk/README.md +++ b/lib/node_modules/@stdlib/stats/covarmtk/README.md @@ -56,7 +56,7 @@ and Often in the analysis of data, the true population [covariance][covariance] is not known _a priori_ and must be estimated from samples drawn from population distributions. If one attempts to use the formula for the population [covariance][covariance], the result is biased and yields a **biased sample covariance**. To compute an **unbiased sample covariance** for samples of size `n`, - + ```math \mathop{\mathrm{cov_n}} = \frac{1}{n-1} \sum_{i=0}^{n-1} (x_i - \bar{x}_n)(y_i - \bar{y}_n) @@ -173,7 +173,7 @@ var out = covarmtk( x, y, correction, meanx, meany, { #### covarmtk.assign( x, y, correction, meanx, meany, out\[, options] ) -Computes the [covariance][covariance] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions and assigns results to a provided output [ndarray][@stdlib/ndarray/ctor]. +Computes the [covariance][covariance] of two ndarrays provided known means and using a one-pass textbook algorithm and assigns results to a provided output [ndarray][@stdlib/ndarray/ctor]. ```javascript var Float64Array = require( '@stdlib/array/float64' ); diff --git a/lib/node_modules/@stdlib/stats/covarmtk/docs/repl.txt b/lib/node_modules/@stdlib/stats/covarmtk/docs/repl.txt index b71d01074791..93a7a80a495f 100644 --- a/lib/node_modules/@stdlib/stats/covarmtk/docs/repl.txt +++ b/lib/node_modules/@stdlib/stats/covarmtk/docs/repl.txt @@ -60,8 +60,9 @@ {{alias}}.assign( x, y, correction, meanx, meany, out[, options] ) - Computes the covariance along one or more ndarray dimensions and assigns - results to a provided output ndarray. + Computes the covariance of two ndarrays provided known means and using a + one-pass textbook algorithm and assigns results to a provided output + ndarray. Parameters ---------- diff --git a/lib/node_modules/@stdlib/stats/covarmtk/lib/index.js b/lib/node_modules/@stdlib/stats/covarmtk/lib/index.js index 2b330fb2215b..4a92b896d9bf 100644 --- a/lib/node_modules/@stdlib/stats/covarmtk/lib/index.js +++ b/lib/node_modules/@stdlib/stats/covarmtk/lib/index.js @@ -46,7 +46,7 @@ * * // Create input ndarrays: * var x = new ndarray( 'float64', xbuf, sh, sx, ox, 'row-major' ); -* var y = new ndarray( 'float64', ybuf, sh, sx, ox, 'row-major' ); +* var y = new ndarray( 'float64', ybuf, sh, sy, oy, 'row-major' ); * * // Define correction and means: * var correction = scalar2ndarray( 1.0, { 'dtype': 'float64' } );