Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
308 changes: 308 additions & 0 deletions lib/node_modules/@stdlib/stats/covarmtk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,308 @@
<!--

@license Apache-2.0

Copyright (c) 2026 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# covarmtk

> Compute the [covariance][covariance] of two ndarrays provided known means and using a one-pass textbook algorithm.

<section class="intro">

The population [covariance][covariance] of two finite size populations of size `N` is given by

<!-- <equation class="equation" label="eq:population_covariance" align="center" raw="\operatorname{\mathrm{cov_N}} = \frac{1}{N} \sum_{i=0}^{N-1} (x_i - \mu_x)(y_i - \mu_y)" alt="Equation for the population covariance."> -->

```math
\mathop{\mathrm{cov_N}} = \frac{1}{N} \sum_{i=0}^{N-1} (x_i - \mu_x)(y_i - \mu_y)
```

<!-- </equation> -->

where the population means are given by

<!-- <equation class="equation" label="eq:population_mean_for_x" align="center" raw="\mu_x = \frac{1}{N} \sum_{i=0}^{N-1} x_i" alt="Equation for the population mean for first array."> -->

```math
\mu_x = \frac{1}{N} \sum_{i=0}^{N-1} x_i
```

<!-- </equation> -->

and

<!-- <equation class="equation" label="eq:population_mean_for_y" align="center" raw="\mu_y = \frac{1}{N} \sum_{i=0}^{N-1} y_i" alt="Equation for the population mean for second array."> -->

```math
\mu_y = \frac{1}{N} \sum_{i=0}^{N-1} y_i
```

<!-- </equation> -->

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`,

<!-- <equation class="equation" label="eq:unbiased_sample_covariance" align="center" raw="\operatorname{\mathrm{cov_n}} = \frac{1}{n-1} \sum_{i=0}^{n-1} (x_i - \bar{x}_n)(y_i - \bar{y}_n)" alt="Equation for computing an unbiased sample covariance."> -->

```math
\mathop{\mathrm{cov_n}} = \frac{1}{n-1} \sum_{i=0}^{n-1} (x_i - \bar{x}_n)(y_i - \bar{y}_n)
```

<!-- </equation> -->

where sample means are given by

<!-- <equation class="equation" label="eq:sample_mean_for_x" align="center" raw="\bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i" alt="Equation for the sample mean for first array."> -->

```math
\bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i
```

<!-- </equation> -->

and

<!-- <equation class="equation" label="eq:sample_mean_for_y" align="center" raw="\bar{y} = \frac{1}{n} \sum_{i=0}^{n-1} y_i" alt="Equation for the sample mean for second array."> -->

```math
\bar{y} = \frac{1}{n} \sum_{i=0}^{n-1} y_i
```

<!-- </equation> -->

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.

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var covarmtk = require( '@stdlib/stats/covarmtk' );
```

#### covarmtk( x, y, correction, meanx, meany\[, options] )

Computes the [covariance][covariance] of two ndarrays provided known means and using a one-pass textbook algorithm.

```javascript
var Float64Array = require( '@stdlib/array/float64' );
var ndarray = require( '@stdlib/ndarray/ctor' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );

var xbuf = new Float64Array( [ 1.0, -2.0, 2.0 ] );
var ybuf = new Float64Array( [ 2.0, -2.0, 1.0 ] );

var x = new ndarray( 'float64', xbuf, [ 3 ], [ 1 ], 0, 'row-major' );
var y = new ndarray( 'float64', ybuf, [ 3 ], [ 1 ], 0, 'row-major' );

var correction = scalar2ndarray( 1.0, {
'dtype': 'float64'
});
var meanx = scalar2ndarray( 1.0/3.0, {
'dtype': 'float64'
});
var meany = scalar2ndarray( 1.0/3.0, {
'dtype': 'float64'
});

var out = covarmtk( x, y, correction, meanx, meany );
// returns <ndarray>[ ~3.8333 ]
```

The function has the following parameters:

- **x**: first input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes].
- **y**: second input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes].
- **correction**: zero-dimensional [ndarray][@stdlib/ndarray/ctor] specifying the degrees of freedom adjustment. Setting this parameter to a value other than `0` has the effect of adjusting the divisor during the calculation of the [covariance][covariance] according to `N-c` where `c` corresponds to the provided degrees of freedom adjustment and `N` corresponds to the number of elements in each input [ndarray][@stdlib/ndarray/ctor]. When computing the population [covariance][covariance], setting this parameter to `0` is the standard choice (i.e., the provided arrays contain data constituting entire populations). When computing the unbiased sample [covariance][covariance], setting this parameter to `1` is the standard choice (i.e., the provided arrays contain data sampled from larger populations; this is commonly referred to as Bessel's correction).
- **meanx**: zero-dimensional [ndarray][@stdlib/ndarray/ctor] specifying the mean of the first input [ndarray][@stdlib/ndarray/ctor].
- **meany**: zero-dimensional [ndarray][@stdlib/ndarray/ctor] specifying the mean of the second input [ndarray][@stdlib/ndarray/ctor].
- **options**: function options (_optional_).

The function accepts the following options:

- **dims**: list of dimensions over which to perform a reduction. If not provided, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor].
- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. Must be a real-valued floating-point or "generic" [data type][@stdlib/ndarray/dtypes].
- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [ndarray][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`.

By default, the function performs a reduction over all elements in the provided input ndarrays. To perform a reduction over specific dimensions, provide a `dims` option.

```javascript
var Float64Array = require( '@stdlib/array/float64' );
var ndarray = require( '@stdlib/ndarray/ctor' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );

var xbuf = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ] );
var ybuf = new Float64Array( [ 2.0, 1.0, 2.0, 1.0, -2.0, 2.0, 3.0, 4.0 ] );

var x = new ndarray( 'float64', xbuf, [ 2, 2, 2 ], [ 4, 2, 1 ], 0, 'row-major' );
var y = new ndarray( 'float64', ybuf, [ 2, 2, 2 ], [ 4, 2, 1 ], 0, 'row-major' );

var correction = scalar2ndarray( 1.0, {
'dtype': 'float64'
});
var meanx = scalar2ndarray( 1.25, {
'dtype': 'float64'
});
var meany = scalar2ndarray( 1.25, {
'dtype': 'float64'
});

var out = covarmtk( x, y, correction, meanx, meany, {
'dims': [ 2 ]
});
// returns <ndarray>
```

#### covarmtk.assign( x, y, correction, meanx, meany, out\[, options] )

Computes the [covariance][covariance] of two ndarrays provided known means and using a one-pass textbook algorithm and assigns results to a provided output [ndarray][@stdlib/ndarray/ctor].

```javascript
var Float64Array = require( '@stdlib/array/float64' );
var ndarray = require( '@stdlib/ndarray/ctor' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var zeros = require( '@stdlib/ndarray/zeros' );

var xbuf = new Float64Array( [ 1.0, -2.0, 2.0 ] );
var ybuf = new Float64Array( [ 2.0, -2.0, 1.0 ] );

var x = new ndarray( 'float64', xbuf, [ 3 ], [ 1 ], 0, 'row-major' );
var y = new ndarray( 'float64', ybuf, [ 3 ], [ 1 ], 0, 'row-major' );

var correction = scalar2ndarray( 1.0, {
'dtype': 'float64'
});
var meanx = scalar2ndarray( 1.0/3.0, {
'dtype': 'float64'
});
var meany = scalar2ndarray( 1.0/3.0, {
'dtype': 'float64'
});

var z = zeros( [], {
'dtype': 'float64'
});

var out = covarmtk.assign( x, y, correction, meanx, meany, z );
// returns <ndarray>[ ~3.8333 ]

var bool = ( out === z );
// returns true
```

The method has the following parameters:

- **x**: first input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes].
- **y**: second input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes].
- **correction**: zero-dimensional [ndarray][@stdlib/ndarray/ctor] specifying the degrees of freedom adjustment.
- **meanx**: zero-dimensional [ndarray][@stdlib/ndarray/ctor] specifying the mean of the first input [ndarray][@stdlib/ndarray/ctor].
- **meany**: zero-dimensional [ndarray][@stdlib/ndarray/ctor] specifying the mean of the second input [ndarray][@stdlib/ndarray/ctor].
- **out**: output [ndarray][@stdlib/ndarray/ctor].
- **options**: function options (_optional_).

The method accepts the following options:

- **dims**: list of dimensions over which to perform a reduction. If not provided, the function performs a reduction over all elements in the provided input ndarrays.

</section>

<!-- /.usage -->

<section class="notes">

## Notes

- Both input ndarrays must have the same shape.
- Setting the `keepdims` option to `true` can be useful when wanting to ensure that the output [ndarray][@stdlib/ndarray/ctor] is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with ndarrays having the same shape as the input ndarrays.
- The output data type [policy][@stdlib/ndarray/output-dtype-policies] only applies to the main function and specifies that, by default, the function must return an [ndarray][@stdlib/ndarray/ctor] having a real-valued floating-point or "generic" [data type][@stdlib/ndarray/dtypes]. For the `assign` method, the output [ndarray][@stdlib/ndarray/ctor] is allowed to have any supported output [data type][@stdlib/ndarray/dtypes].

</section>

<!-- /.notes -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var uniform = require( '@stdlib/random/array/uniform' );
var ndarray = require( '@stdlib/ndarray/ctor' );
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var covarmtk = require( '@stdlib/stats/covarmtk' );

var opts = {
'dtype': 'float64'
};

var xbuf = uniform( 40, -50.0, 50.0, opts );
var x = new ndarray( opts.dtype, xbuf, [ 5, 2, 4 ], [ 8, 4, 1 ], 0, 'row-major' );

var ybuf = uniform( 40, -50.0, 50.0, opts );
var y = new ndarray( opts.dtype, ybuf, [ 5, 2, 4 ], [ 8, 4, 1 ], 0, 'row-major' );

var correction = scalar2ndarray( 1.0, {
'dtype': 'float64'
});
var meanx = scalar2ndarray( 0.0, {
'dtype': 'float64'
});
var meany = scalar2ndarray( 0.0, {
'dtype': 'float64'
});

var out = covarmtk( x, y, correction, meanx, meany, {
'dims': [ 2 ]
});
console.log( ndarray2array( out ) );
```

</section>

<!-- /.examples -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[covariance]: https://en.wikipedia.org/wiki/Covariance

[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor

[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes

[@stdlib/ndarray/output-dtype-policies]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/output-dtype-policies

[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes

</section>

<!-- /.links -->
Loading