diff --git a/lib/node_modules/@stdlib/stats/strided/covarmtk/README.md b/lib/node_modules/@stdlib/stats/strided/covarmtk/README.md new file mode 100644 index 000000000000..3948e3f20451 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/covarmtk/README.md @@ -0,0 +1,258 @@ + + + + +# covarmtk + +> Calculate the [covariance][covariance] of two strided arrays 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/strided/covarmtk' ); +``` + +#### covarmtk( N, correction, meanx, x, strideX, meany, y, strideY ) + +Computes the [covariance][covariance] of two strided arrays provided known means and using a one-pass textbook algorithm. + +```javascript +var x = [ 1.0, -2.0, 2.0 ]; +var y = [ 2.0, -2.0, 1.0 ]; + +var v = covarmtk( x.length, 1, 1.0/3.0, x, 1, 1.0/3.0, y, 1 ); +// returns ~3.8333 +``` + +The function has the following parameters: + +- **N**: number of indexed elements. +- **correction**: 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. 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**: mean of `x`. +- **x**: first input [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. +- **strideX**: stride length for `x`. +- **meany**: mean of `y`. +- **y**: second input [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. +- **strideY**: stride length for `y`. + +The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to compute the [covariance][covariance] of every other element in `x` and `y`, + +```javascript +var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ]; +var y = [ 2.0, 1.0, 2.0, 1.0, -2.0, 2.0, 3.0, 4.0 ]; + +var v = covarmtk( 4, 1, 1.25, x, 2, 1.25, y, 2 ); +// returns 5.25 +``` + +Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views. + + + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); + +var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); +var y0 = new Float64Array( [ 2.0, -2.0, 2.0, 1.0, -2.0, 4.0, 3.0, 2.0 ] ); + +var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element +var y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*1 ); // start at 2nd element + +var v = covarmtk( 4, 1, 1.25, x1, 2, 1.25, y1, 2 ); +// returns ~1.9167 +``` + +#### covarmtk.ndarray( N, correction, meanx, x, strideX, offsetX, meany, y, strideY, offsetY ) + +Computes the [covariance][covariance] of two strided arrays provided known means and using a one-pass textbook algorithm and alternative indexing semantics. + +```javascript +var x = [ 1.0, -2.0, 2.0 ]; +var y = [ 2.0, -2.0, 1.0 ]; + +var v = covarmtk.ndarray( x.length, 1, 1.0/3.0, x, 1, 0, 1.0/3.0, y, 1, 0 ); +// returns ~3.8333 +``` + +The function has the following additional parameters: + +- **offsetX**: starting index for `x`. +- **offsetY**: starting index for `y`. + +While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameters support indexing semantics based on starting indices. For example, to calculate the [covariance][covariance] for every other element in `x` and `y` starting from the second element + +```javascript +var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]; +var y = [ -7.0, 2.0, 2.0, 1.0, -2.0, 2.0, 3.0, 4.0 ]; + +var v = covarmtk.ndarray( 4, 1, 1.25, x, 2, 1, 1.25, y, 2, 1 ); +// returns 6.0 +``` + +
+ + + +
+ +## Notes + +- If `N <= 0`, both functions return `NaN`. +- If `N - c` is less than or equal to `0` (where `c` corresponds to the provided degrees of freedom adjustment), both functions return `NaN`. +- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]). +- Depending on the environment, the typed versions ([`dcovarmtk`][@stdlib/stats/strided/dcovarmtk], [`scovarmtk`][@stdlib/stats/strided/scovarmtk], etc.) are likely to be significantly more performant. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var covarmtk = require( '@stdlib/stats/strided/covarmtk' ); + +var opts = { + 'dtype': 'generic' +}; +var x = discreteUniform( 10, -50, 50, opts ); +console.log( x ); + +var y = discreteUniform( 10, -50, 50, opts ); +console.log( y ); + +var v = covarmtk( x.length, 1, 0.0, x, 1, 0.0, y, 1 ); +console.log( v ); +``` + +
+ + + +* * * + +
+ +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/stats/strided/covarmtk/benchmark/benchmark.js b/lib/node_modules/@stdlib/stats/strided/covarmtk/benchmark/benchmark.js new file mode 100644 index 000000000000..30e121615e27 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/covarmtk/benchmark/benchmark.js @@ -0,0 +1,96 @@ +/** +* @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 pkg = require( './../package.json' ).name; +var covarmtk = require( './../lib/main.js' ); + + +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = uniform( len, -10.0, 10.0, options ); + return benchmark; + + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = covarmtk( x.length, 1, 0.0, x, 1, 0.0, x, 1 ); + 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/strided/covarmtk/benchmark/benchmark.ndarray.js b/lib/node_modules/@stdlib/stats/strided/covarmtk/benchmark/benchmark.ndarray.js new file mode 100644 index 000000000000..c1ef80d6b40c --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/covarmtk/benchmark/benchmark.ndarray.js @@ -0,0 +1,96 @@ +/** +* @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 pkg = require( './../package.json' ).name; +var covarmtk = require( './../lib/ndarray.js' ); + + +// VARIABLES // + +var options = { + 'dtype': 'generic' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x = uniform( len, -10.0, 10.0, options ); + return benchmark; + + function benchmark( b ) { + var v; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = covarmtk( x.length, 1, 0.0, x, 1, 0, 0.0, x, 1, 0 ); + 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+':ndarray:len='+len, f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/stats/strided/covarmtk/docs/repl.txt b/lib/node_modules/@stdlib/stats/strided/covarmtk/docs/repl.txt new file mode 100644 index 000000000000..ebc14ae6b848 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/covarmtk/docs/repl.txt @@ -0,0 +1,141 @@ + +{{alias}}( N, correction, meanx, x, sx, meany, y, sy ) + Computes the covariance of two strided arrays provided known means and + using a one-pass textbook algorithm. + + The `N` and stride parameters determine which elements in the strided arrays + are accessed at runtime. + + Indexing is relative to the first index. To introduce an offset, use a typed + array view. + + If `N <= 0`, the function returns `NaN`. + + Parameters + ---------- + N: integer + Number of indexed elements. + + correction: number + 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. 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 array contains data sampled + from larger populations; this is commonly referred to as Bessel's + correction). + + meanx: number + Mean of `x`. + + x: Array|TypedArray + First input array. + + sx: integer + Stride length of `x`. + + meany: number + Mean of `y`. + + y: Array|TypedArray + Second input array. + + sy: integer + Stride length of `y`. + + Returns + ------- + out: number + The covariance. + + Examples + -------- + // Standard Usage: + > var x = [ 1.0, -2.0, 2.0 ]; + > {{alias}}( x.length, 1, 1.0/3.0, x, 1, 1.0/3.0, x, 1 ) + ~4.3333 + + // Using `N` and stride parameters: + > x = [ -2.0, 1.0, 1.0, -5.0, 2.0, -1.0 ]; + > {{alias}}( 3, 1, 1.0/3.0, x, 2, 1.0/3.0, x, 2 ) + ~4.3333 + + // Using view offsets: + > var x0 = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 3.0, 2.0, 5.0, 1.0 ] ); + > var x1 = new {{alias:@stdlib/array/float64}}( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); + > {{alias}}( 3, 1, 1.0/3.0, x1, 2, 1.0/3.0, x1, 2 ) + ~4.3333 + + +{{alias}}.ndarray( N, correction, meanx, x, sx, ox, meany, y, sy, oy ) + Computes the covariance of two strided arrays provided known means and + using a one-pass textbook algorithm and alternative indexing semantics. + + While typed array views mandate a view offset based on the underlying + buffer, offset parameters support indexing semantics based on starting + indices. + + Parameters + ---------- + N: integer + Number of indexed elements. + + correction: number + 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. 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 array contains data sampled + from larger populations; this is commonly referred to as Bessel's + correction). + + meanx: number + Mean of `x`. + + x: Array|TypedArray + First input array. + + sx: integer + Stride length of `x`. + + ox: integer + Starting index of `x`. + + meany: number + Mean of `y`. + + y: Array|TypedArray + Second input array. + + sy: integer + Stride length of `y`. + + oy: integer + Starting index of `y`. + + Returns + ------- + out: number + The covariance. + + Examples + -------- + // Standard Usage: + > var x = [ 1.0, -2.0, 2.0 ]; + > {{alias}}.ndarray( x.length, 1, 1.0/3.0, x, 1, 0, 1.0/3.0, x, 1, 0 ) + ~4.3333 + + // Using offset parameters: + > var x = [ 1.0, -2.0, 3.0, 2.0, 5.0, 1.0 ]; + > {{alias}}.ndarray( 3, 1, 1.0/3.0, x, 2, 1, 1.0/3.0, x, 2, 1 ) + ~4.3333 + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/stats/strided/covarmtk/docs/types/index.d.ts b/lib/node_modules/@stdlib/stats/strided/covarmtk/docs/types/index.d.ts new file mode 100644 index 000000000000..4fe34cd33d54 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/covarmtk/docs/types/index.d.ts @@ -0,0 +1,113 @@ +/* +* @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 { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array'; + +/** +* Input array. +*/ +type InputArray = NumericArray | Collection | AccessorArrayLike; + +/** +* Interface describing `covarmtk`. +*/ +interface Routine { + /** + * Computes the covariance of two strided arrays provided known means and using a one-pass textbook algorithm. + * + * @param N - number of indexed elements + * @param correction - degrees of freedom adjustment + * @param meanx - mean of `x` + * @param x - first input array + * @param strideX - stride length of `x` + * @param meany - mean of `y` + * @param y - second input array + * @param strideY - stride length of `y` + * @returns covariance + * + * @example + * var x = [ 1.0, -2.0, 2.0 ]; + * var y = [ 2.0, -2.0, 1.0 ]; + * + * var v = covarmtk( x.length, 1, 1.0/3.0, x, 1, 1.0/3.0, y, 1 ); + * // returns ~3.8333 + */ + ( N: number, correction: number, meanx: number, x: InputArray, strideX: number, meany: number, y: InputArray, strideY: number ): number; + + /** + * Computes the covariance of two strided arrays provided known means and using a one-pass textbook algorithm and alternative indexing semantics. + * + * @param N - number of indexed elements + * @param correction - degrees of freedom adjustment + * @param meanx - mean of `x` + * @param x - first input array + * @param strideX - stride length of `x` + * @param offsetX - starting index of `x` + * @param meany - mean of `y` + * @param y - second input array + * @param strideY - stride length of `y` + * @param offsetY - starting index of `y` + * @returns covariance + * + * @example + * var x = [ 1.0, -2.0, 2.0 ]; + * var y = [ 2.0, -2.0, 1.0 ]; + * + * var v = covarmtk.ndarray( x.length, 1, 1.0/3.0, x, 1, 0, 1.0/3.0, y, 1, 0 ); + * // returns ~3.8333 + */ + ndarray( N: number, correction: number, meanx: number, x: InputArray, strideX: number, offsetX: number, meany: number, y: InputArray, strideY: number, offsetY: number ): number; +} + +/** +* Computes the covariance of two strided arrays provided known means and using a one-pass textbook algorithm. +* +* @param N - number of indexed elements +* @param correction - degrees of freedom adjustment +* @param meanx - mean of `x` +* @param x - first input array +* @param strideX - stride length of `x` +* @param meany - mean of `y` +* @param y - second input array +* @param strideY - stride length of `y` +* @returns covariance +* +* @example +* var x = [ 1.0, -2.0, 2.0 ]; +* var y = [ 2.0, -2.0, 1.0 ]; +* +* var v = covarmtk( x.length, 1, 1.0/3.0, x, 1, 1.0/3.0, y, 1 ); +* // returns ~3.8333 +* +* @example +* var x = [ 1.0, -2.0, 2.0 ]; +* var y = [ 2.0, -2.0, 1.0 ]; +* +* var v = covarmtk.ndarray( x.length, 1, 1.0/3.0, x, 1, 0, 1.0/3.0, y, 1, 0 ); +* // returns ~3.8333 +*/ +declare var covarmtk: Routine; + + +// EXPORTS // + +export = covarmtk; diff --git a/lib/node_modules/@stdlib/stats/strided/covarmtk/docs/types/test.ts b/lib/node_modules/@stdlib/stats/strided/covarmtk/docs/types/test.ts new file mode 100644 index 000000000000..f1d9724759f3 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/covarmtk/docs/types/test.ts @@ -0,0 +1,327 @@ +/* +* @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. +*/ + +import AccessorArray = require( '@stdlib/array/base/accessor' ); +import covarmtk = require( './index' ); + + +// TESTS // + +// The function returns a number... +{ + const x = new Float64Array( 10 ); + + covarmtk( x.length, 1, 0.0, x, 1, 0.0, x, 1 ); // $ExpectType number + covarmtk( x.length, 1, 0.0, new AccessorArray( x ), 1, 0.0, new AccessorArray( x ), 1 ); // $ExpectType number +} + +// The compiler throws an error if the function is provided a first argument which is not a number... +{ + const x = new Float64Array( 10 ); + + covarmtk( '10', 1, 0.0, x, 1, 0.0, x, 1 ); // $ExpectError + covarmtk( true, 1, 0.0, x, 1, 0.0, x, 1 ); // $ExpectError + covarmtk( false, 1, 0.0, x, 1, 0.0, x, 1 ); // $ExpectError + covarmtk( null, 1, 0.0, x, 1, 0.0, x, 1 ); // $ExpectError + covarmtk( undefined, 1, 0.0, x, 1, 0.0, x, 1 ); // $ExpectError + covarmtk( [], 1, 0.0, x, 1, 0.0, x, 1 ); // $ExpectError + covarmtk( {}, 1, 0.0, x, 1, 0.0, x, 1 ); // $ExpectError + covarmtk( ( x: number ): number => x, 1, 0.0, x, 1, 0.0, x, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not a number... +{ + const x = new Float64Array( 10 ); + + covarmtk( x.length, '10', 0.0, x, 1, 0.0, x, 1 ); // $ExpectError + covarmtk( x.length, true, 0.0, x, 1, 0.0, x, 1 ); // $ExpectError + covarmtk( x.length, false, 0.0, x, 1, 0.0, x, 1 ); // $ExpectError + covarmtk( x.length, null, 0.0, x, 1, 0.0, x, 1 ); // $ExpectError + covarmtk( x.length, undefined, 0.0, x, 1, 0.0, x, 1 ); // $ExpectError + covarmtk( x.length, [], 0.0, x, 1, 0.0, x, 1 ); // $ExpectError + covarmtk( x.length, {}, 0.0, x, 1, 0.0, x, 1 ); // $ExpectError + covarmtk( x.length, ( x: number ): number => x, 0.0, x, 1, 0.0, x, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument which is not a number... +{ + const x = new Float64Array( 10 ); + + covarmtk( x.length, 1, '10', x, 1, 0.0, x, 1 ); // $ExpectError + covarmtk( x.length, 1, true, x, 1, 0.0, x, 1 ); // $ExpectError + covarmtk( x.length, 1, false, x, 1, 0.0, x, 1 ); // $ExpectError + covarmtk( x.length, 1, null, x, 1, 0.0, x, 1 ); // $ExpectError + covarmtk( x.length, 1, undefined, x, 1, 0.0, x, 1 ); // $ExpectError + covarmtk( x.length, 1, [], x, 1, 0.0, x, 1 ); // $ExpectError + covarmtk( x.length, 1, {}, x, 1, 0.0, x, 1 ); // $ExpectError + covarmtk( x.length, 1, ( x: number ): number => x, x, 1, 0.0, x, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a fourth argument which is not a numeric array... +{ + const x = new Float64Array( 10 ); + + covarmtk( x.length, 1, 0.0, 10, 1, 0.0, x, 1 ); // $ExpectError + covarmtk( x.length, 1, 0.0, '10', 1, 0.0, x, 1 ); // $ExpectError + covarmtk( x.length, 1, 0.0, true, 1, 0.0, x, 1 ); // $ExpectError + covarmtk( x.length, 1, 0.0, false, 1, 0.0, x, 1 ); // $ExpectError + covarmtk( x.length, 1, 0.0, null, 1, 0.0, x, 1 ); // $ExpectError + covarmtk( x.length, 1, 0.0, undefined, 1, 0.0, x, 1 ); // $ExpectError + covarmtk( x.length, 1, 0.0, [ '1' ], 1, 0.0, x, 1 ); // $ExpectError + covarmtk( x.length, 1, 0.0, {}, 1, 0.0, x, 1 ); // $ExpectError + covarmtk( x.length, 1, 0.0, ( x: number ): number => x, 1, 0.0, x, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a fifth argument which is not a number... +{ + const x = new Float64Array( 10 ); + + covarmtk( x.length, 1, 0.0, x, '10', 0.0, x, 1 ); // $ExpectError + covarmtk( x.length, 1, 0.0, x, true, 0.0, x, 1 ); // $ExpectError + covarmtk( x.length, 1, 0.0, x, false, 0.0, x, 1 ); // $ExpectError + covarmtk( x.length, 1, 0.0, x, null, 0.0, x, 1 ); // $ExpectError + covarmtk( x.length, 1, 0.0, x, undefined, 0.0, x, 1 ); // $ExpectError + covarmtk( x.length, 1, 0.0, x, [], 0.0, x, 1 ); // $ExpectError + covarmtk( x.length, 1, 0.0, x, {}, 0.0, x, 1 ); // $ExpectError + covarmtk( x.length, 1, 0.0, x, ( x: number ): number => x, 0.0, x, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a sixth argument which is not a number... +{ + const x = new Float64Array( 10 ); + + covarmtk( x.length, 1, 0.0, x, 1, '10', x, 1 ); // $ExpectError + covarmtk( x.length, 1, 0.0, x, 1, true, x, 1 ); // $ExpectError + covarmtk( x.length, 1, 0.0, x, 1, false, x, 1 ); // $ExpectError + covarmtk( x.length, 1, 0.0, x, 1, null, x, 1 ); // $ExpectError + covarmtk( x.length, 1, 0.0, x, 1, undefined, x, 1 ); // $ExpectError + covarmtk( x.length, 1, 0.0, x, 1, [], x, 1 ); // $ExpectError + covarmtk( x.length, 1, 0.0, x, 1, {}, x, 1 ); // $ExpectError + covarmtk( x.length, 1, 0.0, x, 1, ( x: number ): number => x, x, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided a seventh argument which is not a numeric array... +{ + const x = new Float64Array( 10 ); + + covarmtk( x.length, 1, 0.0, x, 1, 0.0, 10, 1 ); // $ExpectError + covarmtk( x.length, 1, 0.0, x, 1, 0.0, '10', 1 ); // $ExpectError + covarmtk( x.length, 1, 0.0, x, 1, 0.0, true, 1 ); // $ExpectError + covarmtk( x.length, 1, 0.0, x, 1, 0.0, false, 1 ); // $ExpectError + covarmtk( x.length, 1, 0.0, x, 1, 0.0, null, 1 ); // $ExpectError + covarmtk( x.length, 1, 0.0, x, 1, 0.0, undefined, 1 ); // $ExpectError + covarmtk( x.length, 1, 0.0, x, 1, 0.0, [ '1' ], 1 ); // $ExpectError + covarmtk( x.length, 1, 0.0, x, 1, 0.0, {}, 1 ); // $ExpectError + covarmtk( x.length, 1, 0.0, x, 1, 0.0, ( x: number ): number => x, 1 ); // $ExpectError +} + +// The compiler throws an error if the function is provided an eighth argument which is not a number... +{ + const x = new Float64Array( 10 ); + + covarmtk( x.length, 1, 0.0, x, 1, 0.0, x, '10' ); // $ExpectError + covarmtk( x.length, 1, 0.0, x, 1, 0.0, x, true ); // $ExpectError + covarmtk( x.length, 1, 0.0, x, 1, 0.0, x, false ); // $ExpectError + covarmtk( x.length, 1, 0.0, x, 1, 0.0, x, null ); // $ExpectError + covarmtk( x.length, 1, 0.0, x, 1, 0.0, x, undefined ); // $ExpectError + covarmtk( x.length, 1, 0.0, x, 1, 0.0, x, [] ); // $ExpectError + covarmtk( x.length, 1, 0.0, x, 1, 0.0, x, {} ); // $ExpectError + covarmtk( x.length, 1, 0.0, x, 1, 0.0, x, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = new Float64Array( 10 ); + + covarmtk(); // $ExpectError + covarmtk( x.length ); // $ExpectError + covarmtk( x.length, 1 ); // $ExpectError + covarmtk( x.length, 1, 0.0 ); // $ExpectError + covarmtk( x.length, 1, 0.0, x ); // $ExpectError + covarmtk( x.length, 1, 0.0, x, 1 ); // $ExpectError + covarmtk( x.length, 1, 0.0, x, 1, 0.0 ); // $ExpectError + covarmtk( x.length, 1, 0.0, x, 1, 0.0, x ); // $ExpectError + covarmtk( x.length, 1, 0.0, x, 1, 0.0, x, 1, 0 ); // $ExpectError +} + +// Attached to main export is an `ndarray` method which returns a number... +{ + const x = new Float64Array( 10 ); + + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, 0.0, x, 1, 0 ); // $ExpectType number + covarmtk.ndarray( x.length, 1, 0.0, new AccessorArray( x ), 1, 0, 0.0, new AccessorArray( x ), 1, 0 ); // $ExpectType number +} + +// The compiler throws an error if the `ndarray` method is provided a first argument which is not a number... +{ + const x = new Float64Array( 10 ); + + covarmtk.ndarray( '10', 1, 0.0, x, 1, 0, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( true, 1, 0.0, x, 1, 0, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( false, 1, 0.0, x, 1, 0, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( null, 1, 0.0, x, 1, 0, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( undefined, 1, 0.0, x, 1, 0, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( [], 1, 0.0, x, 1, 0, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( {}, 1, 0.0, x, 1, 0, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( ( x: number ): number => x, 1, 0.0, x, 1, 0, 0.0, x, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a second argument which is not a number... +{ + const x = new Float64Array( 10 ); + + covarmtk.ndarray( x.length, '10', 0.0, x, 1, 0, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, true, 0.0, x, 1, 0, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, false, 0.0, x, 1, 0, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, null, 0.0, x, 1, 0, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, undefined, 0.0, x, 1, 0, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, [], 0.0, x, 1, 0, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, {}, 0.0, x, 1, 0, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, ( x: number ): number => x, 0.0, x, 1, 0, 0.0, x, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a third argument which is not a number... +{ + const x = new Float64Array( 10 ); + + covarmtk.ndarray( x.length, 1, '10', x, 1, 0, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, true, x, 1, 0, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, false, x, 1, 0, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, null, x, 1, 0, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, undefined, x, 1, 0, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, [], x, 1, 0, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, {}, x, 1, 0, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, ( x: number ): number => x, x, 1, 0, 0.0, x, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a fourth argument which is not a numeric array... +{ + const x = new Float64Array( 10 ); + + covarmtk.ndarray( x.length, 1, 0.0, 10, 1, 0, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, '10', 1, 0, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, true, 1, 0, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, false, 1, 0, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, null, 1, 0, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, undefined, 1, 0, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, [ '1' ], 1, 0, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, {}, 1, 0, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, ( x: number ): number => x, 1, 0, 0.0, x, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a fifth argument which is not a number... +{ + const x = new Float64Array( 10 ); + + covarmtk.ndarray( x.length, 1, 0.0, x, '10', 0, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, true, 0, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, false, 0, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, null, 0, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, undefined, 0, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, [], 0, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, {}, 0, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, ( x: number ): number => x, 0, 0.0, x, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a sixth argument which is not a number... +{ + const x = new Float64Array( 10 ); + + covarmtk.ndarray( x.length, 1, 0.0, x, 1, '10', 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, true, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, false, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, null, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, undefined, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, [], 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, {}, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, ( x: number ): number => x, 0.0, x, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a seventh argument which is not a number... +{ + const x = new Float64Array( 10 ); + + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, '10', x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, true, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, false, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, null, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, undefined, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, [], x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, {}, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, ( x: number ): number => x, x, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided an eighth argument which is not a numeric array... +{ + const x = new Float64Array( 10 ); + + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, 0.0, 10, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, 0.0, '10', 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, 0.0, true, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, 0.0, false, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, 0.0, null, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, 0.0, undefined, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, 0.0, [ '1' ], 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, 0.0, {}, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, 0.0, ( x: number ): number => x, 1, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a ninth argument which is not a number... +{ + const x = new Float64Array( 10 ); + + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, 0.0, x, '10', 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, 0.0, x, true, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, 0.0, x, false, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, 0.0, x, null, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, 0.0, x, undefined, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, 0.0, x, [], 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, 0.0, x, {}, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, 0.0, x, ( x: number ): number => x, 0 ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided a tenth argument which is not a number... +{ + const x = new Float64Array( 10 ); + + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, 0.0, x, 1, '10' ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, 0.0, x, 1, true ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, 0.0, x, 1, false ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, 0.0, x, 1, null ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, 0.0, x, 1, undefined ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, 0.0, x, 1, [] ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, 0.0, x, 1, {} ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, 0.0, x, 1, ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the `ndarray` method is provided an unsupported number of arguments... +{ + const x = new Float64Array( 10 ); + + covarmtk.ndarray(); // $ExpectError + covarmtk.ndarray( x.length ); // $ExpectError + covarmtk.ndarray( x.length, 1 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, 0.0 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, 0.0, x ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, 0.0, x, 1 ); // $ExpectError + covarmtk.ndarray( x.length, 1, 0.0, x, 1, 0, 0.0, x, 1, 0, 0 ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/stats/strided/covarmtk/examples/index.js b/lib/node_modules/@stdlib/stats/strided/covarmtk/examples/index.js new file mode 100644 index 000000000000..286778b70066 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/covarmtk/examples/index.js @@ -0,0 +1,34 @@ +/** +* @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 covarmtk = require( './../lib' ); + +var opts = { + 'dtype': 'generic' +}; +var x = discreteUniform( 10, -50, 50, opts ); +console.log( x ); + +var y = discreteUniform( 10, -50, 50, opts ); +console.log( y ); + +var v = covarmtk( x.length, 1, 0.0, x, 1, 0.0, y, 1 ); +console.log( v ); diff --git a/lib/node_modules/@stdlib/stats/strided/covarmtk/lib/accessors.js b/lib/node_modules/@stdlib/stats/strided/covarmtk/lib/accessors.js new file mode 100644 index 000000000000..036076196a51 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/covarmtk/lib/accessors.js @@ -0,0 +1,86 @@ +/** +* @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'; + +// MAIN // + +/** +* Computes the covariance of two strided arrays provided known means and using a one-pass textbook algorithm. +* +* @private +* @param {PositiveInteger} N - number of indexed elements +* @param {number} correction - degrees of freedom adjustment +* @param {number} meanx - mean of `x` +* @param {Object} x - first input array object +* @param {Collection} x.data - first input array data +* @param {Array} x.accessors - array element accessors +* @param {integer} strideX - stride length of `x` +* @param {NonNegativeInteger} offsetX - starting index of `x` +* @param {number} meany - mean of `y` +* @param {Object} y - second input array object +* @param {Collection} y.data - second input array data +* @param {Array} y.accessors - array element accessors +* @param {integer} strideY - stride length of `y` +* @param {NonNegativeInteger} offsetY - starting index of `y` +* @returns {number} covariance +* +* @example +* var toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +* var arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +* +* var x = toAccessorArray( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] ); +* +* var v = covarmtk( 4, 1, 1.25, arraylike2object( x ), 2, 1, 1.25, arraylike2object( x ), 2, 1 ); +* // returns 6.25 +*/ +function covarmtk( N, correction, meanx, x, strideX, offsetX, meany, y, strideY, offsetY ) { // eslint-disable-line max-len + var xbuf; + var ybuf; + var xget; + var yget; + var ix; + var iy; + var C; + var n; + var i; + + // Cache references to array data: + xbuf = x.data; + ybuf = y.data; + + // Cache references to element accessors: + xget = x.accessors[ 0 ]; + yget = y.accessors[ 0 ]; + + n = N - correction; + ix = offsetX; + iy = offsetY; + C = 0.0; + for ( i = 0; i < N; i++ ) { + C += ( xget( xbuf, ix ) - meanx ) * ( yget( ybuf, iy ) - meany ); + ix += strideX; + iy += strideY; + } + return C / n; +} + + +// EXPORTS // + +module.exports = covarmtk; diff --git a/lib/node_modules/@stdlib/stats/strided/covarmtk/lib/index.js b/lib/node_modules/@stdlib/stats/strided/covarmtk/lib/index.js new file mode 100644 index 000000000000..dbece1b6e4bb --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/covarmtk/lib/index.js @@ -0,0 +1,59 @@ +/** +* @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 strided arrays provided known means and using a one-pass textbook algorithm. +* +* @module @stdlib/stats/strided/covarmtk +* +* @example +* var covarmtk = require( '@stdlib/stats/strided/covarmtk' ); +* +* var x = [ 1.0, -2.0, 2.0 ]; +* +* var v = covarmtk( x.length, 1, 1.0/3.0, x, 1, 1.0/3.0, x, 1 ); +* // returns ~4.3333 +* +* @example +* var covarmtk = require( '@stdlib/stats/strided/covarmtk' ); +* +* var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]; +* +* var v = covarmtk.ndarray( 4, 1, 1.25, x, 2, 1, 1.25, x, 2, 1 ); +* // returns 6.25 +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var main = require( './main.js' ); +var ndarray = require( './ndarray.js' ); + + +// MAIN // + +setReadOnly( main, 'ndarray', ndarray ); + + +// EXPORTS // + +module.exports = main; + +// exports: { "ndarray": "main.ndarray" } diff --git a/lib/node_modules/@stdlib/stats/strided/covarmtk/lib/main.js b/lib/node_modules/@stdlib/stats/strided/covarmtk/lib/main.js new file mode 100644 index 000000000000..9a2ec42f3c87 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/covarmtk/lib/main.js @@ -0,0 +1,55 @@ +/** +* @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 stride2offset = require( '@stdlib/strided/base/stride2offset' ); +var ndarray = require( './ndarray.js' ); + + +// MAIN // + +/** +* Computes the covariance of two strided arrays provided known means and using a one-pass textbook algorithm. +* +* @param {PositiveInteger} N - number of indexed elements +* @param {number} correction - degrees of freedom adjustment +* @param {number} meanx - mean of `x` +* @param {NumericArray} x - first input array +* @param {integer} strideX - stride length of `x` +* @param {number} meany - mean of `y` +* @param {NumericArray} y - second input array +* @param {integer} strideY - stride length of `y` +* @returns {number} covariance +* +* @example +* var x = [ 1.0, -2.0, 2.0 ]; +* +* var v = covarmtk( x.length, 1, 1.0/3.0, x, 1, 1.0/3.0, x, 1 ); +* // returns ~4.3333 +*/ +function covarmtk( N, correction, meanx, x, strideX, meany, y, strideY ) { + return ndarray( N, correction, meanx, x, strideX, stride2offset( N, strideX ), meany, y, strideY, stride2offset( N, strideY ) ); // eslint-disable-line max-len +} + + +// EXPORTS // + +module.exports = covarmtk; diff --git a/lib/node_modules/@stdlib/stats/strided/covarmtk/lib/ndarray.js b/lib/node_modules/@stdlib/stats/strided/covarmtk/lib/ndarray.js new file mode 100644 index 000000000000..9340ac5d618a --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/covarmtk/lib/ndarray.js @@ -0,0 +1,82 @@ +/** +* @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 arraylike2object = require( '@stdlib/array/base/arraylike2object' ); +var accessors = require( './accessors.js' ); + + +// MAIN // + +/** +* Computes the covariance of two strided arrays provided known means and using a one-pass textbook algorithm. +* +* @param {PositiveInteger} N - number of indexed elements +* @param {number} correction - degrees of freedom adjustment +* @param {number} meanx - mean of `x` +* @param {NumericArray} x - first input array +* @param {integer} strideX - stride length of `x` +* @param {NonNegativeInteger} offsetX - starting index of `x` +* @param {number} meany - mean of `y` +* @param {NumericArray} y - second input array +* @param {integer} strideY - stride length of `y` +* @param {NonNegativeInteger} offsetY - starting index of `y` +* @returns {number} covariance +* +* @example +* var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ]; +* +* var v = covarmtk( 4, 1, 1.25, x, 2, 1, 1.25, x, 2, 1 ); +* // returns 6.25 +*/ +function covarmtk( N, correction, meanx, x, strideX, offsetX, meany, y, strideY, offsetY ) { // eslint-disable-line max-len + var ox; + var oy; + var ix; + var iy; + var C; + var n; + var i; + + n = N - correction; + if ( N <= 0 || n <= 0.0 ) { + return NaN; + } + ox = arraylike2object( x ); + oy = arraylike2object( y ); + if ( ox.accessorProtocol || oy.accessorProtocol ) { + return accessors( N, correction, meanx, ox, strideX, offsetX, meany, oy, strideY, offsetY ); // eslint-disable-line max-len + } + ix = offsetX; + iy = offsetY; + C = 0.0; + for ( i = 0; i < N; i++ ) { + C += ( x[ ix ] - meanx ) * ( y[ iy ] - meany ); + ix += strideX; + iy += strideY; + } + return C / n; +} + + +// EXPORTS // + +module.exports = covarmtk; diff --git a/lib/node_modules/@stdlib/stats/strided/covarmtk/package.json b/lib/node_modules/@stdlib/stats/strided/covarmtk/package.json new file mode 100644 index 000000000000..f36867a4635b --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/covarmtk/package.json @@ -0,0 +1,70 @@ +{ + "name": "@stdlib/stats/strided/covarmtk", + "version": "0.0.0", + "description": "Calculate the covariance of two strided arrays 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", + "strided", + "strided array", + "typed", + "array" + ], + "__stdlib__": {} +} diff --git a/lib/node_modules/@stdlib/stats/strided/covarmtk/test/test.js b/lib/node_modules/@stdlib/stats/strided/covarmtk/test/test.js new file mode 100644 index 000000000000..1bfc232de244 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/covarmtk/test/test.js @@ -0,0 +1,38 @@ +/** +* @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 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 a method providing an ndarray interface', function test( t ) { + t.strictEqual( typeof covarmtk.ndarray, 'function', 'method is a function' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/strided/covarmtk/test/test.main.js b/lib/node_modules/@stdlib/stats/strided/covarmtk/test/test.main.js new file mode 100644 index 000000000000..6060a51104dd --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/covarmtk/test/test.main.js @@ -0,0 +1,397 @@ +/** +* @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 toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +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 has an arity of 8', function test( t ) { + t.strictEqual( covarmtk.length, 8, 'has expected arity' ); + t.end(); +}); + +tape( 'the function calculates the population covariance', function test( t ) { + var x; + var y; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + y = [ -2.0, 1.0, 5.0, -4.0, 3.0, 0.0 ]; + v = covarmtk( x.length, 0, 0.5, x, 1, 0.5, y, 1 ); + t.strictEqual( v, -45.5/x.length, 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = covarmtk( x.length, 0, -4.0, x, 1, -4.0, x, 1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = covarmtk( x.length, 0, 4.0, x, 1, 4.0, x, 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function calculates the population covariance (accessors)', function test( t ) { + var x; + var y; + var v; + + x = toAccessorArray( [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ] ); + y = toAccessorArray( [ -2.0, 1.0, 5.0, -4.0, 3.0, 0.0 ] ); + v = covarmtk( x.length, 0, 0.5, x, 1, 0.5, y, 1 ); + t.strictEqual( v, -45.5/x.length, 'returns expected value' ); + + x = toAccessorArray( [ -4.0, -4.0 ] ); + v = covarmtk( x.length, 0, -4.0, x, 1, -4.0, x, 1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = toAccessorArray( [ NaN, 4.0 ] ); + v = covarmtk( x.length, 0, 4.0, x, 1, 4.0, x, 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function calculates the sample covariance', function test( t ) { + var x; + var y; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + y = [ -2.0, 1.0, 5.0, -4.0, 3.0, 0.0 ]; + v = covarmtk( x.length, 1, 0.5, x, 1, 0.5, y, 1 ); + t.strictEqual( v, -45.5/(x.length-1), 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = covarmtk( x.length, 1, -4.0, x, 1, -4.0, x, 1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = covarmtk( x.length, 1, 4.0, x, 1, 4.0, x, 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function calculates the sample covariance (accessors)', function test( t ) { + var x; + var y; + var v; + + x = toAccessorArray( [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ] ); + y = toAccessorArray( [ -2.0, 1.0, 5.0, -4.0, 3.0, 0.0 ] ); + v = covarmtk( x.length, 1, 0.5, x, 1, 0.5, y, 1 ); + t.strictEqual( v, -45.5/(x.length-1), 'returns expected value' ); + + x = toAccessorArray( [ -4.0, -4.0 ] ); + v = covarmtk( x.length, 1, -4.0, x, 1, -4.0, x, 1 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = toAccessorArray( [ NaN, 4.0 ] ); + v = covarmtk( x.length, 1, 4.0, x, 1, 4.0, x, 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `NaN`', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = covarmtk( 0, 1, 0.6, x, 1, 0.6, x, 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = covarmtk( -1, 1, 0.6, x, 1, 0.6, x, 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `NaN` (accessors)', function test( t ) { + var x; + var v; + + x = toAccessorArray( [ 1.0, -2.0, -4.0, 5.0, 3.0 ] ); + + v = covarmtk( 0, 1, 0.6, x, 1, 0.6, x, 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = covarmtk( -1, 1, 0.6, x, 1, 0.6, x, 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided a `correction` parameter yielding `N-correction` less than or equal to `0`, the function returns `NaN`', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = covarmtk( x.length, x.length, 0.6, x, 1, 0.6, x, 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = covarmtk( x.length, x.length+1, 0.6, x, 1, 0.6, x, 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided a `correction` parameter yielding `N-correction` less than or equal to `0`, the function returns `NaN` (accessors)', function test( t ) { + var x; + var v; + + x = toAccessorArray( [ 1.0, -2.0, -4.0, 5.0, 3.0 ] ); + + v = covarmtk( x.length, x.length, 0.6, x, 1, 0.6, x, 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = covarmtk( x.length, x.length+1, 0.6, x, 1, 0.6, x, 1 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports stride parameters', function test( t ) { + var x; + var y; + var v; + + x = [ + 1.0, // 0 + 2.0, + 2.0, // 1 + -7.0, + -2.0, // 2 + 3.0, + 4.0, // 3 + 2.0 + ]; + + y = [ + 2.0, // 0 + 2.0, + 1.0, // 1 + -7.0, + 4.0, // 2 + 3.0, + -2.0, // 3 + 2.0 + ]; + + v = covarmtk( 4, 1, 1.25, x, 2, 1.25, y, 2 ); + + t.strictEqual( v, -18.25/3, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports stride parameters (accessors)', function test( t ) { + var x; + var y; + var v; + + x = toAccessorArray([ + 1.0, // 0 + 2.0, + 2.0, // 1 + -7.0, + -2.0, // 2 + 3.0, + 4.0, // 3 + 2.0 + ]); + + y = toAccessorArray([ + 2.0, // 0 + 2.0, + 1.0, // 1 + -7.0, + 4.0, // 2 + 3.0, + -2.0, // 3 + 2.0 + ]); + + v = covarmtk( 4, 1, 1.25, x, 2, 1.25, y, 2 ); + + t.strictEqual( v, -18.25/3, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports negative stride parameters', function test( t ) { + var x; + var y; + var v; + + x = [ + 1.0, // 3 + 2.0, + 2.0, // 2 + -7.0, + -2.0, // 1 + 3.0, + 4.0, // 0 + 2.0 + ]; + + y = [ + 2.0, // 3 + 2.0, + 1.0, // 2 + -7.0, + 4.0, // 1 + 3.0, + -2.0, // 0 + 2.0 + ]; + + v = covarmtk( 4, 1, 1.25, x, -2, 1.25, y, -2 ); + + t.strictEqual( v, -18.25/3, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports negative stride parameters (accessors)', function test( t ) { + var x; + var y; + var v; + + x = toAccessorArray([ + 1.0, // 3 + 2.0, + 2.0, // 2 + -7.0, + -2.0, // 1 + 3.0, + 4.0, // 0 + 2.0 + ]); + + y = toAccessorArray([ + 2.0, // 3 + 2.0, + 1.0, // 2 + -7.0, + 4.0, // 1 + 3.0, + -2.0, // 0 + 2.0 + ]); + + v = covarmtk( 4, 1, 1.25, x, -2, 1.25, y, -2 ); + + t.strictEqual( v, -18.25/3, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports view offsets', function test( t ) { + var x0; + var x1; + var y0; + var y1; + var v; + + x0 = new Float64Array([ + 2.0, + 1.0, // 0 + 2.0, + -2.0, // 1 + -2.0, + 2.0, // 2 + 3.0, + 4.0, // 3 + 6.0 + ]); + + y0 = new Float64Array([ + 2.0, + -2.0, // 0 + 2.0, + 1.0, // 1 + -2.0, + 4.0, // 2 + 3.0, + 2.0, // 3 + 6.0 + ]); + + x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element + y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*1 ); // start at 2nd element + + v = covarmtk( 4, 1, 1.25, x1, 2, 1.25, y1, 2 ); + t.strictEqual( v, 5.75/3, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports view offsets (accessors)', function test( t ) { + var x0; + var x1; + var y0; + var y1; + var v; + + x0 = new Float64Array([ + 2.0, + 1.0, // 0 + 2.0, + -2.0, // 1 + -2.0, + 2.0, // 2 + 3.0, + 4.0, // 3 + 6.0 + ]); + + y0 = new Float64Array([ + 2.0, + -2.0, // 0 + 2.0, + 1.0, // 1 + -2.0, + 4.0, // 2 + 3.0, + 2.0, // 3 + 6.0 + ]); + + x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element + y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*1 ); // start at 2nd element + + v = covarmtk( 4, 1, 1.25, toAccessorArray( x1 ), 2, 1.25, toAccessorArray( y1 ), 2 ); + t.strictEqual( v, 5.75/3, 'returns expected value' ); + + t.end(); +}); diff --git a/lib/node_modules/@stdlib/stats/strided/covarmtk/test/test.ndarray.js b/lib/node_modules/@stdlib/stats/strided/covarmtk/test/test.ndarray.js new file mode 100644 index 000000000000..2a39b6ca83a7 --- /dev/null +++ b/lib/node_modules/@stdlib/stats/strided/covarmtk/test/test.ndarray.js @@ -0,0 +1,382 @@ +/** +* @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 toAccessorArray = require( '@stdlib/array/base/to-accessor-array' ); +var covarmtk = require( './../lib/ndarray.js' ); + + +// 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 has an arity of 10', function test( t ) { + t.strictEqual( covarmtk.length, 10, 'has expected arity' ); + t.end(); +}); + +tape( 'the function calculates the population covariance', function test( t ) { + var x; + var y; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + y = [ -2.0, 1.0, 5.0, -4.0, 3.0, 0.0 ]; + v = covarmtk( x.length, 0, 0.5, x, 1, 0, 0.5, y, 1, 0 ); + t.strictEqual( v, -45.5/x.length, 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = covarmtk( x.length, 0, -4.0, x, 1, 0, -4.0, x, 1, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = covarmtk( x.length, 0, 4.0, x, 1, 0, 4.0, x, 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function calculates the population covariance (accessors)', function test( t ) { + var x; + var y; + var v; + + x = toAccessorArray( [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ] ); + y = toAccessorArray( [ -2.0, 1.0, 5.0, -4.0, 3.0, 0.0 ] ); + v = covarmtk( x.length, 0, 0.5, x, 1, 0, 0.5, y, 1, 0 ); + t.strictEqual( v, -45.5/x.length, 'returns expected value' ); + + x = toAccessorArray( [ -4.0, -4.0 ] ); + v = covarmtk( x.length, 0, -4.0, x, 1, 0, -4.0, x, 1, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = toAccessorArray( [ NaN, 4.0 ] ); + v = covarmtk( x.length, 0, 4.0, x, 1, 0, 4.0, x, 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function calculates the sample covariance', function test( t ) { + var x; + var y; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ]; + y = [ -2.0, 1.0, 5.0, -4.0, 3.0, 0.0 ]; + v = covarmtk( x.length, 1, 0.5, x, 1, 0, 0.5, y, 1, 0 ); + t.strictEqual( v, -45.5/(x.length-1), 'returns expected value' ); + + x = [ -4.0, -4.0 ]; + v = covarmtk( x.length, 1, -4.0, x, 1, 0, -4.0, x, 1, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = [ NaN, 4.0 ]; + v = covarmtk( x.length, 1, 4.0, x, 1, 0, 4.0, x, 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function calculates the sample covariance (accessors)', function test( t ) { + var x; + var y; + var v; + + x = toAccessorArray( [ 1.0, -2.0, -4.0, 5.0, 0.0, 3.0 ] ); + y = toAccessorArray( [ -2.0, 1.0, 5.0, -4.0, 3.0, 0.0 ] ); + v = covarmtk( x.length, 1, 0.5, x, 1, 0, 0.5, y, 1, 0 ); + t.strictEqual( v, -45.5/(x.length-1), 'returns expected value' ); + + x = toAccessorArray( [ -4.0, -4.0 ] ); + v = covarmtk( x.length, 1, -4.0, x, 1, 0, -4.0, x, 1, 0 ); + t.strictEqual( v, 0.0, 'returns expected value' ); + + x = toAccessorArray( [ NaN, 4.0 ] ); + v = covarmtk( x.length, 1, 4.0, x, 1, 0, 4.0, x, 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `NaN`', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = covarmtk( 0, 1, 0.6, x, 1, 0, 0.6, x, 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = covarmtk( -1, 1, 0.6, x, 1, 0, 0.6, x, 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided an `N` parameter less than or equal to `0`, the function returns `NaN` (accessors)', function test( t ) { + var x; + var v; + + x = toAccessorArray( [ 1.0, -2.0, -4.0, 5.0, 3.0 ] ); + + v = covarmtk( 0, 1, 0.6, x, 1, 0, 0.6, x, 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = covarmtk( -1, 1, 0.6, x, 1, 0, 0.6, x, 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided a `correction` parameter yielding `N-correction` less than or equal to `0`, the function returns `NaN`', function test( t ) { + var x; + var v; + + x = [ 1.0, -2.0, -4.0, 5.0, 3.0 ]; + + v = covarmtk( x.length, x.length, 0.6, x, 1, 0, 0.6, x, 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = covarmtk( x.length, x.length+1, 0.6, x, 1, 0, 0.6, x, 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'if provided a `correction` parameter yielding `N-correction` less than or equal to `0`, the function returns `NaN` (accessors)', function test( t ) { + var x; + var v; + + x = toAccessorArray( [ 1.0, -2.0, -4.0, 5.0, 3.0 ] ); + + v = covarmtk( x.length, x.length, 0.6, x, 1, 0, 0.6, x, 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + v = covarmtk( x.length, x.length+1, 0.6, x, 1, 0, 0.6, x, 1, 0 ); + t.strictEqual( isnan( v ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports stride parameters', function test( t ) { + var x; + var y; + var v; + + x = [ + 1.0, // 0 + 2.0, + 2.0, // 1 + -7.0, + -2.0, // 2 + 3.0, + 4.0, // 3 + 2.0 + ]; + + y = [ + 2.0, // 0 + 2.0, + 1.0, // 1 + -7.0, + 4.0, // 2 + 3.0, + -2.0, // 3 + 2.0 + ]; + + v = covarmtk( 4, 1, 1.25, x, 2, 0, 1.25, y, 2, 0 ); + + t.strictEqual( v, -18.25/3, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports stride parameters (accessors)', function test( t ) { + var x; + var y; + var v; + + x = toAccessorArray([ + 1.0, // 0 + 2.0, + 2.0, // 1 + -7.0, + -2.0, // 2 + 3.0, + 4.0, // 3 + 2.0 + ]); + + y = toAccessorArray([ + 2.0, // 0 + 2.0, + 1.0, // 1 + -7.0, + 4.0, // 2 + 3.0, + -2.0, // 3 + 2.0 + ]); + + v = covarmtk( 4, 1, 1.25, x, 2, 0, 1.25, y, 2, 0 ); + + t.strictEqual( v, -18.25/3, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports negative stride parameters', function test( t ) { + var x; + var y; + var v; + + x = [ + 1.0, // 3 + 2.0, + 2.0, // 2 + -7.0, + -2.0, // 1 + 3.0, + 4.0, // 0 + 2.0 + ]; + + y = [ + 2.0, // 3 + 2.0, + 1.0, // 2 + -7.0, + 4.0, // 1 + 3.0, + -2.0, // 0 + 2.0 + ]; + + v = covarmtk( 4, 1, 1.25, x, -2, 6, 1.25, y, -2, 6 ); + + t.strictEqual( v, -18.25/3, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports negative stride parameters (accessors)', function test( t ) { + var x; + var y; + var v; + + x = toAccessorArray([ + 1.0, // 3 + 2.0, + 2.0, // 2 + -7.0, + -2.0, // 1 + 3.0, + 4.0, // 0 + 2.0 + ]); + + y = toAccessorArray([ + 2.0, // 3 + 2.0, + 1.0, // 2 + -7.0, + 4.0, // 1 + 3.0, + -2.0, // 0 + 2.0 + ]); + + v = covarmtk( 4, 1, 1.25, x, -2, 6, 1.25, y, -2, 6 ); + + t.strictEqual( v, -18.25/3, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports offset parameters', function test( t ) { + var x; + var y; + var v; + + x = [ + 2.0, + 1.0, // 0 + 2.0, + -2.0, // 1 + -2.0, + 2.0, // 2 + 3.0, + 4.0 // 3 + ]; + + y = [ + 2.0, + 4.0, // 0 + 2.0, + -2.0, // 1 + -2.0, + 1.0, // 2 + 3.0, + 2.0 // 3 + ]; + + v = covarmtk( 4, 1, 1.25, x, 2, 1, 1.25, y, 2, 1 ); + t.strictEqual( v, 11.75/3, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports offset parameters (accessors)', function test( t ) { + var x; + var y; + var v; + + x = toAccessorArray([ + 2.0, + 1.0, // 0 + 2.0, + -2.0, // 1 + -2.0, + 2.0, // 2 + 3.0, + 4.0 // 3 + ]); + + y = toAccessorArray([ + 2.0, + 4.0, // 0 + 2.0, + -2.0, // 1 + -2.0, + 1.0, // 2 + 3.0, + 2.0 // 3 + ]); + + v = covarmtk( 4, 1, 1.25, x, 2, 1, 1.25, y, 2, 1 ); + t.strictEqual( v, 11.75/3, 'returns expected value' ); + + t.end(); +});