diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dcovarmtk/README.md b/lib/node_modules/@stdlib/stats/base/ndarray/dcovarmtk/README.md new file mode 100644 index 000000000000..f9756931994e --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dcovarmtk/README.md @@ -0,0 +1,216 @@ + + +# dcovarmtk + +> Calculate the [covariance][covariance] of two one-dimensional double-precision floating-point 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 dcovarmtk = require( '@stdlib/stats/base/ndarray/dcovarmtk' ); +``` + +#### dcovarmtk( arrays ) + +Computes the covariance of two one-dimensional double-precision floating-point ndarrays provided known means and using a one-pass textbook algorithm. + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); + +var opts = { + 'dtype': 'float64' +}; + +var xbuf = new Float64Array( [ 1.0, -2.0, 2.0 ] ); +var x = new ndarray( opts.dtype, xbuf, [ 3 ], [ 1 ], 0, 'row-major' ); + +var ybuf = new Float64Array( [ 2.0, -2.0, 1.0 ] ); +var y = new ndarray( opts.dtype, ybuf, [ 3 ], [ 1 ], 0, 'row-major' ); + +var correction = scalar2ndarray( 1.0, opts ); +var meanx = scalar2ndarray( 1.0/3.0, opts ); +var meany = scalar2ndarray( 1.0/3.0, opts ); + +var v = dcovarmtk( [ x, y, correction, meanx, meany ] ); +// returns ~3.8333 +``` + +The function has the following parameters: + +- **arrays**: array-like object containing the following ndarrays in order: + + 1. first one-dimensional input ndarray. + 2. second one-dimensional input ndarray. + 3. a zero-dimensional ndarray 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. 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). + 4. a zero-dimensional ndarray specifying the mean of the first one-dimensional ndarray. + 5. a zero-dimensional ndarray specifying the mean of the second one-dimensional ndarray. + +
+ + + +
+ +## Notes + +- Both input ndarrays should have the same number of elements. +- If provided empty one-dimensional ndarrays, the function returns `NaN`. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var dcovarmtk = require( '@stdlib/stats/base/ndarray/dcovarmtk' ); + +// Define array options: +var opts = { + 'dtype': 'float64' +}; + +// Create one-dimensional ndarrays containing pseudorandom numbers: +var xbuf = discreteUniform( 10, -50, 50, opts ); +var x = new ndarray( opts.dtype, xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); +console.log( ndarray2array( x ) ); + +var ybuf = discreteUniform( 10, -50, 50, opts ); +var y = new ndarray( opts.dtype, ybuf, [ ybuf.length ], [ 1 ], 0, 'row-major' ); +console.log( ndarray2array( y ) ); + +// Specify the degrees of freedom adjustment: +var correction = scalar2ndarray( 1.0, opts ); + +// Specify the known means: +var meanx = scalar2ndarray( 0.0, opts ); +var meany = scalar2ndarray( 0.0, opts ); + +// Calculate the sample covariance: +var v = dcovarmtk( [ x, y, correction, meanx, meany ] ); +console.log( v ); +``` + +
+ + + + + + + + + + + + + + + +[covariance]: https://en.wikipedia.org/wiki/Covariance + + + + diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dcovarmtk/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/base/ndarray/dcovarmtk/benchmark/benchmark.js new file mode 100644 index 000000000000..b73104a7edfa --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dcovarmtk/benchmark/benchmark.js @@ -0,0 +1,115 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 uniform = require( '@stdlib/random/array/uniform' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var scalar2ndarray = require( '@stdlib/ndarray/base/from-scalar' ); +var pkg = require( './../package.json' ).name; +var dcovarmtk = 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, -10.0, 10.0, options ); + x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' ); + + ybuf = uniform( len, -10.0, 10.0, options ); + y = new ndarray( options.dtype, ybuf, [ len ], [ 1 ], 0, 'row-major' ); + + correction = scalar2ndarray( 1.0, options.dtype, 'row-major' ); + meanx = scalar2ndarray( 0.0, options.dtype, 'row-major' ); + meany = scalar2ndarray( 0.0, options.dtype, 'row-major' ); + + return benchmark; + + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = dcovarmtk( [ x, y, correction, meanx, meany ] ); + if ( isnan( v ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( isnan( v ) ) { + 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( pkg+':len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dcovarmtk/docs/repl.txt b/lib/node_modules/@stdlib/stats/base/ndarray/dcovarmtk/docs/repl.txt new file mode 100644 index 000000000000..8ce72f1b7055 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dcovarmtk/docs/repl.txt @@ -0,0 +1,66 @@ + +{{alias}}( arrays ) + Computes the covariance of two one-dimensional double-precision floating- + point ndarrays provided known means and using a one-pass textbook algorithm. + + Both input ndarrays should have the same number of elements. + + If provided empty one-dimensional ndarrays, the function returns `NaN`. + + Parameters + ---------- + arrays: ArrayLikeObject + The function expects the following ndarrays in order: + + - first one-dimensional input ndarray. + - second one-dimensional input ndarray. + - a zero-dimensional ndarray 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 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. When computing the population + 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, 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). + - a zero-dimensional ndarray specifying the mean of the first one- + dimensional ndarray. + - a zero-dimensional ndarray specifying the mean of the second one- + dimensional ndarray. + + Returns + ------- + out: number + The covariance. + + Examples + -------- + // Create the input ndarrays: + > var xbuf = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 2.0 ] ); + > var ybuf = new {{alias:@stdlib/array/float64}}( [ 2.0, -2.0, 1.0 ] ); + > var dt = 'float64'; + > var sh = [ xbuf.length ]; + > var st = [ 1 ]; + > var oo = 0; + > var ord = 'row-major'; + > var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, st, oo, ord ); + > var y = new {{alias:@stdlib/ndarray/ctor}}( dt, ybuf, sh, st, oo, ord ); + + // Specify the degrees of freedom adjustment: + > var opts = { 'dtype': dt }; + > var correction = new {{alias:@stdlib/ndarray/from-scalar}}( 1.0, opts ); + + // Specify the known means: + > var meanx = new {{alias:@stdlib/ndarray/from-scalar}}( 1.0/3.0, opts ); + > var meany = new {{alias:@stdlib/ndarray/from-scalar}}( 1.0/3.0, opts ); + + // Calculate the sample covariance: + > {{alias}}( [ x, y, correction, meanx, meany ] ) + ~3.8333 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dcovarmtk/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/base/ndarray/dcovarmtk/docs/types/index.d.ts new file mode 100644 index 000000000000..eeadddfde502 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dcovarmtk/docs/types/index.d.ts @@ -0,0 +1,68 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 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 { float64ndarray } from '@stdlib/types/ndarray'; + +/** +* Computes the covariance of two one-dimensional double-precision floating-point ndarrays provided known means and using a one-pass textbook algorithm. +* +* ## Notes +* +* - The function expects the following ndarrays in order: +* +* - first one-dimensional input ndarray. +* - second one-dimensional input ndarray. +* - a zero-dimensional ndarray 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 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. When computing the population 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, 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). +* - a zero-dimensional ndarray specifying the mean of the first one-dimensional ndarray. +* - a zero-dimensional ndarray specifying the mean of the second one-dimensional ndarray. +* +* @param arrays - array-like object containing input ndarrays +* @returns covariance +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* +* var opts = { +* 'dtype': 'float64' +* }; +* +* var xbuf = new Float64Array( [ 1.0, -2.0, 2.0 ] ); +* var x = new ndarray( opts.dtype, xbuf, [ 3 ], [ 1 ], 0, 'row-major' ); +* +* var ybuf = new Float64Array( [ 2.0, -2.0, 1.0 ] ); +* var y = new ndarray( opts.dtype, ybuf, [ 3 ], [ 1 ], 0, 'row-major' ); +* +* var correction = scalar2ndarray( 1.0, opts ); +* var meanx = scalar2ndarray( 1.0/3.0, opts ); +* var meany = scalar2ndarray( 1.0/3.0, opts ); +* +* var v = dcovarmtk( [ x, y, correction, meanx, meany ] ); +* // returns ~3.8333 +*/ +declare function dcovarmtk( arrays: [ float64ndarray, float64ndarray, float64ndarray, float64ndarray, float64ndarray ] ): number; + + +// EXPORTS // + +export = dcovarmtk; diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dcovarmtk/docs/types/test.ts b/lib/node_modules/@stdlib/stats/base/ndarray/dcovarmtk/docs/types/test.ts new file mode 100644 index 000000000000..89d0b12b302f --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dcovarmtk/docs/types/test.ts @@ -0,0 +1,57 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2025 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. +*/ + +/* eslint-disable space-in-parens */ + +import zeros = require( '@stdlib/ndarray/zeros' ); +import dcovarmtk = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + const x = zeros( [ 10 ], { + 'dtype': 'float64' + }); + + dcovarmtk( [ x, x, x, x, x ] ); // $ExpectType number +} + +// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays... +{ + dcovarmtk( '10' ); // $ExpectError + dcovarmtk( 10 ); // $ExpectError + dcovarmtk( true ); // $ExpectError + dcovarmtk( false ); // $ExpectError + dcovarmtk( null ); // $ExpectError + dcovarmtk( undefined ); // $ExpectError + dcovarmtk( [] ); // $ExpectError + dcovarmtk( {} ); // $ExpectError + dcovarmtk( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 10 ], { + 'dtype': 'float64' + }); + + dcovarmtk(); // $ExpectError + dcovarmtk( [ x, x, x, x, x ], {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dcovarmtk/examples/index.js b/lib/node_modules/@stdlib/stats/base/ndarray/dcovarmtk/examples/index.js new file mode 100644 index 000000000000..c06e5bc0572c --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dcovarmtk/examples/index.js @@ -0,0 +1,50 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var dcovarmtk = require( './../lib' ); + +// Define array options: +var opts = { + 'dtype': 'float64' +}; + +// Create one-dimensional ndarrays containing pseudorandom numbers: +var xbuf = discreteUniform( 10, -50, 50, opts ); +var x = new ndarray( opts.dtype, xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' ); +console.log( ndarray2array( x ) ); + +var ybuf = discreteUniform( 10, -50, 50, opts ); +var y = new ndarray( opts.dtype, ybuf, [ ybuf.length ], [ 1 ], 0, 'row-major' ); +console.log( ndarray2array( y ) ); + +// Specify the degrees of freedom adjustment: +var correction = scalar2ndarray( 1.0, opts ); + +// Specify the known means: +var meanx = scalar2ndarray( 0.0, opts ); +var meany = scalar2ndarray( 0.0, opts ); + +// Calculate the sample covariance: +var v = dcovarmtk( [ x, y, correction, meanx, meany ] ); +console.log( v ); diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dcovarmtk/lib/index.js b/lib/node_modules/@stdlib/stats/base/ndarray/dcovarmtk/lib/index.js new file mode 100644 index 000000000000..2d68c5efe6bf --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dcovarmtk/lib/index.js @@ -0,0 +1,57 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 one-dimensional double-precision floating-point ndarrays provided known means and using a one-pass textbook algorithm. +* +* @module @stdlib/stats/base/ndarray/dcovarmtk +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* var dcovarmtk = require( '@stdlib/stats/base/ndarray/dcovarmtk' ); +* +* var opts = { +* 'dtype': 'float64' +* }; +* +* var xbuf = new Float64Array( [ 1.0, -2.0, 2.0 ] ); +* var x = new ndarray( opts.dtype, xbuf, [ 3 ], [ 1 ], 0, 'row-major' ); +* +* var ybuf = new Float64Array( [ 2.0, -2.0, 1.0 ] ); +* var y = new ndarray( opts.dtype, ybuf, [ 3 ], [ 1 ], 0, 'row-major' ); +* +* var correction = scalar2ndarray( 1.0, opts ); +* var meanx = scalar2ndarray( 1.0/3.0, opts ); +* var meany = scalar2ndarray( 1.0/3.0, opts ); +* +* var v = dcovarmtk( [ x, y, correction, meanx, meany ] ); +* // returns ~3.8333 +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dcovarmtk/lib/main.js b/lib/node_modules/@stdlib/stats/base/ndarray/dcovarmtk/lib/main.js new file mode 100644 index 000000000000..66f89996c544 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dcovarmtk/lib/main.js @@ -0,0 +1,91 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 numelDimension = require( '@stdlib/ndarray/base/numel-dimension' ); +var getStride = require( '@stdlib/ndarray/base/stride' ); +var getOffset = require( '@stdlib/ndarray/base/offset' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' ); +var strided = require( '@stdlib/stats/strided/dcovarmtk' ).ndarray; + + +// MAIN // + +/** +* Computes the covariance of two one-dimensional double-precision floating-point ndarrays provided known means and using a one-pass textbook algorithm. +* +* ## Notes +* +* - The function expects the following ndarrays in order: +* +* - first one-dimensional input ndarray. +* - second one-dimensional input ndarray. +* - a zero-dimensional ndarray 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 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. When computing the population 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, 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). +* - a zero-dimensional ndarray specifying the mean of the first one-dimensional ndarray. +* - a zero-dimensional ndarray specifying the mean of the second one-dimensional ndarray. +* +* @param {ArrayLikeObject} arrays - array-like object containing input ndarrays +* @returns {number} covariance +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* +* var opts = { +* 'dtype': 'float64' +* }; +* +* var xbuf = new Float64Array( [ 1.0, -2.0, 2.0 ] ); +* var x = new ndarray( opts.dtype, xbuf, [ 3 ], [ 1 ], 0, 'row-major' ); +* +* var ybuf = new Float64Array( [ 2.0, -2.0, 1.0 ] ); +* var y = new ndarray( opts.dtype, ybuf, [ 3 ], [ 1 ], 0, 'row-major' ); +* +* var correction = scalar2ndarray( 1.0, opts ); +* var meanx = scalar2ndarray( 1.0/3.0, opts ); +* var meany = scalar2ndarray( 1.0/3.0, opts ); +* +* var v = dcovarmtk( [ x, y, correction, meanx, meany ] ); +* // returns ~3.8333 +*/ +function dcovarmtk( arrays ) { + var correction; + var meanx; + var meany; + var x; + var y; + + x = arrays[ 0 ]; + y = arrays[ 1 ]; + + correction = ndarraylike2scalar( arrays[ 2 ] ); + meanx = ndarraylike2scalar( arrays[ 3 ] ); + meany = ndarraylike2scalar( arrays[ 4 ] ); + + return strided( numelDimension( x, 0 ), correction, meanx, getData( x ), getStride( x, 0 ), getOffset( x ), meany, getData( y ), getStride( y, 0 ), getOffset( y ) ); // eslint-disable-line max-len +} + + +// EXPORTS // + +module.exports = dcovarmtk; diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dcovarmtk/package.json b/lib/node_modules/@stdlib/stats/base/ndarray/dcovarmtk/package.json new file mode 100644 index 000000000000..32030201d611 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dcovarmtk/package.json @@ -0,0 +1,67 @@ +{ + "name": "@stdlib/stats/base/ndarray/dcovarmtk", + "version": "0.0.0", + "description": "Compute the covariance of two one-dimensional double-precision floating-point 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", + "covar", + "sample covariance", + "unbiased", + "correlation", + "variance", + "ndarray" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/stats/base/ndarray/dcovarmtk/test/test.js b/lib/node_modules/@stdlib/stats/base/ndarray/dcovarmtk/test/test.js new file mode 100644 index 000000000000..dff8befd10ad --- /dev/null +++ b/lib/node_modules/@stdlib/stats/base/ndarray/dcovarmtk/test/test.js @@ -0,0 +1,262 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2025 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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); +var dcovarmtk = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Returns a one-dimensional ndarray. +* +* @private +* @param {Float64Array} buffer - underlying data buffer +* @param {NonNegativeInteger} length - number of indexed elements +* @param {integer} stride - stride length +* @param {NonNegativeInteger} offset - index offset +* @returns {ndarray} one-dimensional ndarray +*/ +function vector( buffer, length, stride, offset ) { + return new ndarray( 'float64', buffer, [ length ], [ stride ], offset, 'row-major' ); +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof dcovarmtk, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 1', function test( t ) { + t.strictEqual( dcovarmtk.length, 1, 'has expected arity' ); + t.end(); +}); + +tape( 'the function calculates the population covariance of two one-dimensional double-precision floating-point ndarrays provided known means', function test( t ) { + var correction; + var meanx; + var meany; + var x; + var y; + var v; + + correction = scalar2ndarray( 0.0, options ); + + x = new Float64Array( [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ] ); + y = new Float64Array( [ -2.0, 1.0, 5.0, -4.0, 3.0, 0.0 ] ); + meanx = scalar2ndarray( 0.5, options ); + meany = scalar2ndarray( 0.5, options ); + v = dcovarmtk( [ vector( x, 6, 1, 0 ), vector( y, 6, 1, 0 ), correction, meanx, meany ] ); // eslint-disable-line max-len + t.strictEqual( v, -45.5/x.length, 'returns expected value' ); + + x = new Float64Array( [ -4.0, -4.0 ] ); + meanx = scalar2ndarray( -4.0, options ); + v = dcovarmtk( [ vector( x, 2, 1, 0 ), vector( x, 2, 1, 0 ), correction, meanx, meanx ] ); // eslint-disable-line max-len + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = new Float64Array( [ NaN, 4.0 ] ); + meanx = scalar2ndarray( 4.0, options ); + v = dcovarmtk( [ vector( x, 2, 1, 0 ), vector( x, 2, 1, 0 ), correction, meanx, meanx ] ); // eslint-disable-line max-len + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function calculates the sample covariance of two one-dimensional double-precision floating-point ndarrays provided known means', function test( t ) { + var correction; + var meanx; + var meany; + var x; + var y; + var v; + + correction = scalar2ndarray( 1.0, options ); + + x = new Float64Array( [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ] ); + y = new Float64Array( [ -2.0, 1.0, 5.0, -4.0, 3.0, 0.0 ] ); + meanx = scalar2ndarray( 0.5, options ); + meany = scalar2ndarray( 0.5, options ); + v = dcovarmtk( [ vector( x, 6, 1, 0 ), vector( y, 6, 1, 0 ), correction, meanx, meany ] ); // eslint-disable-line max-len + t.strictEqual( v, -45.5/(x.length-1), 'returns expected value' ); + + x = new Float64Array( [ -4.0, -4.0 ] ); + meanx = scalar2ndarray( -4.0, options ); + v = dcovarmtk( [ vector( x, 2, 1, 0 ), vector( x, 2, 1, 0 ), correction, meanx, meanx ] ); // eslint-disable-line max-len + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = new Float64Array( [ NaN, 4.0 ] ); + meanx = scalar2ndarray( 4.0, options ); + v = dcovarmtk( [ vector( x, 2, 1, 0 ), vector( x, 2, 1, 0 ), correction, meanx, meanx ] ); // eslint-disable-line max-len + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an empty vector, the function returns `NaN`', function test( t ) { + var correction; + var meanx; + var x; + var v; + + x = new Float64Array( [] ); + meanx = scalar2ndarray( 0.5, options ); + correction = scalar2ndarray( 1.0, options ); + + v = dcovarmtk( [ vector( x, 0, 1, 0 ), vector( x, 0, 1, 0 ), correction, meanx, meanx ] ); // eslint-disable-line max-len + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays having non-unit strides', function test( t ) { + var correction; + var meanx; + var meany; + var x; + var y; + var v; + + correction = scalar2ndarray( 1.0, options ); + + x = new Float64Array([ + 1.0, // 0 + 2.0, + 2.0, // 1 + -7.0, + -2.0, // 2 + 3.0, + 4.0, // 3 + 2.0 + ]); + meanx = scalar2ndarray( 1.25, options ); + + y = new Float64Array([ + 2.0, // 0 + 2.0, + 1.0, // 1 + -7.0, + 4.0, // 2 + 3.0, + -2.0, // 3 + 2.0 + ]); + meany = scalar2ndarray( 1.25, options ); + + v = dcovarmtk( [ vector( x, 4, 2, 0 ), vector( y, 4, 2, 0 ), correction, meanx, meany ] ); // eslint-disable-line max-len + + t.strictEqual( v, -18.25/3, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays having negative strides', function test( t ) { + var correction; + var meanx; + var meany; + var x; + var y; + var v; + + correction = scalar2ndarray( 1.0, options ); + + x = new Float64Array([ + 1.0, // 3 + 2.0, + 2.0, // 2 + -7.0, + -2.0, // 1 + 3.0, + 4.0, // 0 + 2.0 + ]); + meanx = scalar2ndarray( 1.25, options ); + + y = new Float64Array([ + 2.0, // 3 + 2.0, + 1.0, // 2 + -7.0, + 4.0, // 1 + 3.0, + -2.0, // 0 + 2.0 + ]); + meany = scalar2ndarray( 1.25, options ); + + v = dcovarmtk( [ vector( x, 4, -2, 6 ), vector( y, 4, -2, 6 ), correction, meanx, meany ] ); // eslint-disable-line max-len + + t.strictEqual( v, -18.25/3, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports one-dimensional ndarrays having non-zero offsets', function test( t ) { + var correction; + var meanx; + var meany; + var x; + var y; + var v; + + correction = scalar2ndarray( 1.0, options ); + + x = new Float64Array([ + 2.0, + 1.0, // 0 + 2.0, + -2.0, // 1 + -2.0, + 2.0, // 2 + 3.0, + 4.0 // 3 + ]); + meanx = scalar2ndarray( 1.25, options ); + + y = new Float64Array([ + 2.0, + -2.0, // 0 + 2.0, + 1.0, // 1 + -2.0, + 4.0, // 2 + 3.0, + 2.0 // 3 + ]); + meany = scalar2ndarray( 1.25, options ); + + v = dcovarmtk( [ vector( x, 4, 2, 1 ), vector( y, 4, 2, 1 ), correction, meanx, meany ] ); // eslint-disable-line max-len + t.strictEqual( v, 5.75/3, 'returns expected value' ); + + t.end(); +});