Skip to content

Commit 247fe5a

Browse files
committed
feat: add stats/covarmtk
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 086d2d6 commit 247fe5a

13 files changed

Lines changed: 2140 additions & 0 deletions

File tree

Lines changed: 308 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,308 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# covarmtk
22+
23+
> Compute the [covariance][covariance] of two ndarrays provided known means and using a one-pass textbook algorithm.
24+
25+
<section class="intro">
26+
27+
The population [covariance][covariance] of two finite size populations of size `N` is given by
28+
29+
<!-- <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."> -->
30+
31+
```math
32+
\mathop{\mathrm{cov_N}} = \frac{1}{N} \sum_{i=0}^{N-1} (x_i - \mu_x)(y_i - \mu_y)
33+
```
34+
35+
<!-- </equation> -->
36+
37+
where the population means are given by
38+
39+
<!-- <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."> -->
40+
41+
```math
42+
\mu_x = \frac{1}{N} \sum_{i=0}^{N-1} x_i
43+
```
44+
45+
<!-- </equation> -->
46+
47+
and
48+
49+
<!-- <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."> -->
50+
51+
```math
52+
\mu_y = \frac{1}{N} \sum_{i=0}^{N-1} y_i
53+
```
54+
55+
<!-- </equation> -->
56+
57+
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`,
58+
59+
<!-- <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 variance."> -->
60+
61+
```math
62+
\mathop{\mathrm{cov_n}} = \frac{1}{n-1} \sum_{i=0}^{n-1} (x_i - \bar{x}_n)(y_i - \bar{y}_n)
63+
```
64+
65+
<!-- </equation> -->
66+
67+
where sample means are given by
68+
69+
<!-- <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."> -->
70+
71+
```math
72+
\bar{x} = \frac{1}{n} \sum_{i=0}^{n-1} x_i
73+
```
74+
75+
<!-- </equation> -->
76+
77+
and
78+
79+
<!-- <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."> -->
80+
81+
```math
82+
\bar{y} = \frac{1}{n} \sum_{i=0}^{n-1} y_i
83+
```
84+
85+
<!-- </equation> -->
86+
87+
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.
88+
89+
</section>
90+
91+
<!-- /.intro -->
92+
93+
<section class="usage">
94+
95+
## Usage
96+
97+
```javascript
98+
var covarmtk = require( '@stdlib/stats/covarmtk' );
99+
```
100+
101+
#### covarmtk( x, y, correction, meanx, meany\[, options] )
102+
103+
Computes the [covariance][covariance] of two ndarrays provided known means and using a one-pass textbook algorithm.
104+
105+
```javascript
106+
var Float64Array = require( '@stdlib/array/float64' );
107+
var ndarray = require( '@stdlib/ndarray/ctor' );
108+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
109+
110+
var xbuf = new Float64Array( [ 1.0, -2.0, 2.0 ] );
111+
var ybuf = new Float64Array( [ 2.0, -2.0, 1.0 ] );
112+
113+
var x = new ndarray( 'float64', xbuf, [ 3 ], [ 1 ], 0, 'row-major' );
114+
var y = new ndarray( 'float64', ybuf, [ 3 ], [ 1 ], 0, 'row-major' );
115+
116+
var correction = scalar2ndarray( 1.0, {
117+
'dtype': 'float64'
118+
});
119+
var meanx = scalar2ndarray( 1.0/3.0, {
120+
'dtype': 'float64'
121+
});
122+
var meany = scalar2ndarray( 1.0/3.0, {
123+
'dtype': 'float64'
124+
});
125+
126+
var out = covarmtk( x, y, correction, meanx, meany );
127+
// returns <ndarray>[ ~3.8333 ]
128+
```
129+
130+
The function has the following parameters:
131+
132+
- **x**: first input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes].
133+
- **y**: second input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes].
134+
- **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).
135+
- **meanx**: zero-dimensional [ndarray][@stdlib/ndarray/ctor] specifying the mean of the first input [ndarray][@stdlib/ndarray/ctor].
136+
- **meany**: zero-dimensional [ndarray][@stdlib/ndarray/ctor] specifying the mean of the second input [ndarray][@stdlib/ndarray/ctor].
137+
- **options**: function options (_optional_).
138+
139+
The function accepts the following options:
140+
141+
- **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].
142+
- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. Must be a real-valued floating-point or "generic" [data type][@stdlib/ndarray/dtypes].
143+
- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [ndarray][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`.
144+
145+
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.
146+
147+
```javascript
148+
var Float64Array = require( '@stdlib/array/float64' );
149+
var ndarray = require( '@stdlib/ndarray/ctor' );
150+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
151+
152+
var xbuf = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ] );
153+
var ybuf = new Float64Array( [ 2.0, 1.0, 2.0, 1.0, -2.0, 2.0, 3.0, 4.0 ] );
154+
155+
var x = new ndarray( 'float64', xbuf, [ 2, 2, 2 ], [ 4, 2, 1 ], 0, 'row-major' );
156+
var y = new ndarray( 'float64', ybuf, [ 2, 2, 2 ], [ 4, 2, 1 ], 0, 'row-major' );
157+
158+
var correction = scalar2ndarray( 1.0, {
159+
'dtype': 'float64'
160+
});
161+
var meanx = scalar2ndarray( 1.25, {
162+
'dtype': 'float64'
163+
});
164+
var meany = scalar2ndarray( 1.25, {
165+
'dtype': 'float64'
166+
});
167+
168+
var out = covarmtk( x, y, correction, meanx, meany, {
169+
'dims': [ 2 ]
170+
});
171+
// returns <ndarray>
172+
```
173+
174+
#### covarmtk.assign( x, y, correction, meanx, meany, out\[, options] )
175+
176+
Computes the [covariance][covariance] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions and assigns results to a provided output [ndarray][@stdlib/ndarray/ctor].
177+
178+
```javascript
179+
var Float64Array = require( '@stdlib/array/float64' );
180+
var ndarray = require( '@stdlib/ndarray/ctor' );
181+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
182+
var zeros = require( '@stdlib/ndarray/zeros' );
183+
184+
var xbuf = new Float64Array( [ 1.0, -2.0, 2.0 ] );
185+
var ybuf = new Float64Array( [ 2.0, -2.0, 1.0 ] );
186+
187+
var x = new ndarray( 'float64', xbuf, [ 3 ], [ 1 ], 0, 'row-major' );
188+
var y = new ndarray( 'float64', ybuf, [ 3 ], [ 1 ], 0, 'row-major' );
189+
190+
var correction = scalar2ndarray( 1.0, {
191+
'dtype': 'float64'
192+
});
193+
var meanx = scalar2ndarray( 1.0/3.0, {
194+
'dtype': 'float64'
195+
});
196+
var meany = scalar2ndarray( 1.0/3.0, {
197+
'dtype': 'float64'
198+
});
199+
200+
var z = zeros( [], {
201+
'dtype': 'float64'
202+
});
203+
204+
var out = covarmtk.assign( x, y, correction, meanx, meany, z );
205+
// returns <ndarray>[ ~3.8333 ]
206+
207+
var bool = ( out === z );
208+
// returns true
209+
```
210+
211+
The method has the following parameters:
212+
213+
- **x**: first input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes].
214+
- **y**: second input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes].
215+
- **correction**: zero-dimensional [ndarray][@stdlib/ndarray/ctor] specifying the degrees of freedom adjustment.
216+
- **meanx**: zero-dimensional [ndarray][@stdlib/ndarray/ctor] specifying the mean of the first input [ndarray][@stdlib/ndarray/ctor].
217+
- **meany**: zero-dimensional [ndarray][@stdlib/ndarray/ctor] specifying the mean of the second input [ndarray][@stdlib/ndarray/ctor].
218+
- **out**: output [ndarray][@stdlib/ndarray/ctor].
219+
- **options**: function options (_optional_).
220+
221+
The method accepts the following options:
222+
223+
- **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.
224+
225+
</section>
226+
227+
<!-- /.usage -->
228+
229+
<section class="notes">
230+
231+
## Notes
232+
233+
- Both input ndarrays must have the same shape.
234+
- 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.
235+
- 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].
236+
237+
</section>
238+
239+
<!-- /.notes -->
240+
241+
<section class="examples">
242+
243+
## Examples
244+
245+
<!-- eslint no-undef: "error" -->
246+
247+
```javascript
248+
var uniform = require( '@stdlib/random/array/uniform' );
249+
var ndarray = require( '@stdlib/ndarray/ctor' );
250+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
251+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
252+
var covarmtk = require( '@stdlib/stats/covarmtk' );
253+
254+
var opts = {
255+
'dtype': 'float64'
256+
};
257+
258+
var xbuf = uniform( 40, -50.0, 50.0, opts );
259+
var x = new ndarray( opts.dtype, xbuf, [ 5, 2, 4 ], [ 8, 4, 1 ], 0, 'row-major' );
260+
261+
var ybuf = uniform( 40, -50.0, 50.0, opts );
262+
var y = new ndarray( opts.dtype, ybuf, [ 5, 2, 4 ], [ 8, 4, 1 ], 0, 'row-major' );
263+
264+
var correction = scalar2ndarray( 1.0, {
265+
'dtype': 'float64'
266+
});
267+
var meanx = scalar2ndarray( 0.0, {
268+
'dtype': 'float64'
269+
});
270+
var meany = scalar2ndarray( 0.0, {
271+
'dtype': 'float64'
272+
});
273+
274+
var out = covarmtk( x, y, correction, meanx, meany, {
275+
'dims': [ 2 ]
276+
});
277+
console.log( ndarray2array( out ) );
278+
```
279+
280+
</section>
281+
282+
<!-- /.examples -->
283+
284+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
285+
286+
<section class="related">
287+
288+
</section>
289+
290+
<!-- /.related -->
291+
292+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
293+
294+
<section class="links">
295+
296+
[covariance]: https://en.wikipedia.org/wiki/Covariance
297+
298+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
299+
300+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
301+
302+
[@stdlib/ndarray/output-dtype-policies]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/output-dtype-policies
303+
304+
[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes
305+
306+
</section>
307+
308+
<!-- /.links -->

0 commit comments

Comments
 (0)