Skip to content

Commit 73653a2

Browse files
feat: add stats/strided/distances/dcorrelation
PR-URL: #11917 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 181b4cc commit 73653a2

33 files changed

Lines changed: 3829 additions & 0 deletions
Lines changed: 336 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,336 @@
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+
# dcorrelation
22+
23+
> Calculate the [correlation distance][correlation-distance] between two double-precision floating-point strided arrays.
24+
25+
<section class="intro">
26+
27+
The [correlation distance][correlation-distance] between random variables `X` and `Y` is defined as
28+
29+
<!-- <equation class="equation" label="eq:sample_correlation_distance" align="center" raw="D(X, Y) = 1 - \frac{\displaystyle\sum_{i=0}^{N-1} (x_i - \bar{x})(y_i - \bar{y})}{\displaystyle\sqrt{\sum_{i=0}^{N-1} (x_i - \bar{x})^2} \sqrt{\sum_{i=0}^{N-1} (y_i - \bar{y})^2}}" alt="Equation for the sample correlation distance."> -->
30+
31+
```math
32+
D(X, Y) = 1 - \frac{\displaystyle\sum_{i=0}^{N-1} (x_i - \bar{x})(y_i - \bar{y})}{\displaystyle\sqrt{\sum_{i=0}^{N-1} (x_i - \bar{x})^2} \sqrt{\sum_{i=0}^{N-1} (y_i - \bar{y})^2}}
33+
```
34+
35+
<!-- </equation> -->
36+
37+
where `x_i` and `y_i` are the _ith_ components of vectors **X** and **Y**, respectively.
38+
39+
<!-- </equation> -->
40+
41+
42+
</section>
43+
44+
<!-- /.intro -->
45+
46+
<section class="usage">
47+
48+
## Usage
49+
50+
```javascript
51+
var dcorrelation = require( '@stdlib/stats/strided/distances/dcorrelation' );
52+
```
53+
54+
#### dcorrelation( N, x, strideX, y, strideY )
55+
56+
Computes the [correlation distance][correlation-distance] of two double-precision floating-point strided arrays.
57+
58+
```javascript
59+
var Float64Array = require( '@stdlib/array/float64' );
60+
61+
var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
62+
var y = new Float64Array( [ 2.0, -2.0, 1.0 ] );
63+
64+
var c = dcorrelation( x.length, x, 1, y, 1 );
65+
// returns ~0.115
66+
```
67+
68+
The function has the following parameters:
69+
70+
- **N**: number of indexed elements.
71+
- **x**: first input [`Float64Array`][@stdlib/array/float64].
72+
- **strideX**: stride length for `x`.
73+
- **y**: second input [`Float64Array`][@stdlib/array/float64].
74+
- **strideY**: stride length for `y`.
75+
76+
The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to compute the [correlation distance][correlation-distance] of every other element in `x` and `y`,
77+
78+
```javascript
79+
var Float64Array = require( '@stdlib/array/float64' );
80+
81+
var x = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ] );
82+
var y = new Float64Array( [ 2.0, 1.0, 2.0, 1.0, -2.0, 2.0, 3.0, 4.0 ] );
83+
84+
var c = dcorrelation( 4, x, 2, y, 2 );
85+
// returns ~0.053
86+
```
87+
88+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
89+
90+
<!-- eslint-disable stdlib/capitalized-comments -->
91+
92+
```javascript
93+
var Float64Array = require( '@stdlib/array/float64' );
94+
95+
var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
96+
var y0 = new Float64Array( [ 2.0, -2.0, 2.0, 1.0, -2.0, 4.0, 3.0, 2.0 ] );
97+
98+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
99+
var y1 = new Float64Array( y0.buffer, y0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
100+
101+
var c = dcorrelation( 4, x1, 2, y1, 2 );
102+
// returns ~0.693
103+
```
104+
105+
#### dcorrelation.ndarray( N, x, strideX, offsetX, y, strideY, offsetY )
106+
107+
Computes the [correlation distance][correlation-distance] of two double-precision floating-point strided arrays using alternative indexing semantics.
108+
109+
```javascript
110+
var Float64Array = require( '@stdlib/array/float64' );
111+
112+
var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
113+
var y = new Float64Array( [ 2.0, -2.0, 1.0 ] );
114+
115+
var c = dcorrelation.ndarray( x.length, x, 1, 0, y, 1, 0 );
116+
// returns ~0.115
117+
```
118+
119+
The function has the following additional parameters:
120+
121+
- **offsetX**: starting index for `x`.
122+
- **offsetY**: starting index for `y`.
123+
124+
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 [correlation distance][correlation-distance] for every other element in `x` and `y` starting from the second element
125+
126+
```javascript
127+
var Float64Array = require( '@stdlib/array/float64' );
128+
129+
var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
130+
var y = new Float64Array( [ -7.0, 2.0, 2.0, 1.0, -2.0, 2.0, 3.0, 4.0 ] );
131+
132+
var c = dcorrelation.ndarray( 4, x, 2, 1, y, 2, 1 );
133+
// returns ~0.073
134+
```
135+
136+
</section>
137+
138+
<!-- /.usage -->
139+
140+
<section class="notes">
141+
142+
## Notes
143+
144+
- If `N <= 1`, both functions return `NaN`.
145+
- If all values in either `x` or `y` are constant, the [correlation distance][correlation-distance] is not defined.
146+
147+
</section>
148+
149+
<!-- /.notes -->
150+
151+
<section class="examples">
152+
153+
## Examples
154+
155+
<!-- eslint no-undef: "error" -->
156+
157+
```javascript
158+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
159+
var dcorrelation = require( '@stdlib/stats/strided/distances/dcorrelation' );
160+
161+
var opts = {
162+
'dtype': 'float64'
163+
};
164+
var x = discreteUniform( 10, -50, 50, opts );
165+
console.log( x );
166+
167+
var y = discreteUniform( 10, -50, 50, opts );
168+
console.log( y );
169+
170+
var c = dcorrelation( x.length, x, 1, y, 1 );
171+
console.log( c );
172+
```
173+
174+
</section>
175+
176+
<!-- /.examples -->
177+
178+
<!-- C interface documentation. -->
179+
180+
* * *
181+
182+
<section class="c">
183+
184+
## C APIs
185+
186+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
187+
188+
<section class="intro">
189+
190+
</section>
191+
192+
<!-- /.intro -->
193+
194+
<!-- C usage documentation. -->
195+
196+
<section class="usage">
197+
198+
### Usage
199+
200+
```c
201+
#include "stdlib/stats/strided/distances/dcorrelation.h"
202+
```
203+
204+
#### stdlib_strided_dcorrelation( N, \*X, strideX, \*Y, strideY )
205+
206+
Computes the [correlation distance][correlation-distance] between two double-precision floating-point strided arrays.
207+
208+
```c
209+
const double x[] = { 1.0, -2.0, 2.0 };
210+
const double y[] = { 2.0, -2.0, 1.0 };
211+
212+
double c = stdlib_strided_dcorrelation( 3, x, 1, y, 1 );
213+
// returns ~0.115
214+
```
215+
216+
The function accepts the following arguments:
217+
218+
- **N**: `[in] CBLAS_INT` number of indexed elements.
219+
- **X**: `[in] double*` first input array.
220+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
221+
- **Y**: `[in] double*` second input array.
222+
- **strideY**: `[in] CBLAS_INT` stride length for `Y`.
223+
224+
```c
225+
double stdlib_strided_dcorrelation( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const double *Y, const CBLAS_INT strideY );
226+
```
227+
228+
#### stdlib_strided_dcorrelation_ndarray( N, \*X, sx, ox, \*Y, sy, oy )
229+
230+
Computes the [correlation distance][correlation-distance] between two double-precision floating-point strided arrays using alternative indexing semantics.
231+
232+
```c
233+
const double x[] = { 1.0, -2.0, 2.0 };
234+
const double y[] = { 2.0, -2.0, 1.0 };
235+
236+
double c = stdlib_strided_dcorrelation_ndarray( 3, x, 1, 0, y, 1, 0 );
237+
// returns ~0.115
238+
```
239+
240+
The function accepts the following arguments:
241+
242+
- **N**: `[in] CBLAS_INT` number of indexed elements.
243+
- **X**: `[in] double*` first input array.
244+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
245+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
246+
- **Y**: `[in] double*` second input array.
247+
- **strideY**: `[in] CBLAS_INT` stride length for `Y`.
248+
- **offsetY**: `[in] CBLAS_INT` starting index for `Y`.
249+
250+
```c
251+
double stdlib_strided_dcorrelation_ndarray( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, const double *Y, const CBLAS_INT strideY, const CBLAS_INT offsetY );
252+
```
253+
254+
</section>
255+
256+
<!-- /.usage -->
257+
258+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
259+
260+
<section class="notes">
261+
262+
</section>
263+
264+
<!-- /.notes -->
265+
266+
<!-- C API usage examples. -->
267+
268+
<section class="examples">
269+
270+
### Examples
271+
272+
```c
273+
#include "stdlib/stats/strided/distances/dcorrelation.h"
274+
#include <stdio.h>
275+
276+
int main( void ) {
277+
// Create strided arrays:
278+
const double x[] = { 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0, -8.0 };
279+
const double y[] = { 1.0, -2.0, 3.0, -4.0, 5.0, -6.0, 7.0, -8.0 };
280+
281+
// Specify the number of elements:
282+
const int N = 8;
283+
284+
// Specify strides:
285+
const int strideX = 1;
286+
const int strideY = -1;
287+
288+
// Compute the correlation distance between `x` and `y`:
289+
double d = stdlib_strided_dcorrelation( N, x, strideX, y, strideY );
290+
291+
// Print the result:
292+
printf( "Correlation Distance: %lf\n", d );
293+
294+
// Compute the correlation distance between `x` and `y` with offsets:
295+
d = stdlib_strided_dcorrelation_ndarray( N, x, strideX, 0, y, strideY, N-1 );
296+
297+
// Print the result:
298+
printf( "Correlation Distance: %lf\n", d );
299+
}
300+
```
301+
302+
</section>
303+
304+
<!-- /.examples -->
305+
306+
</section>
307+
308+
<!-- /.c -->
309+
310+
<section class="references">
311+
312+
</section>
313+
314+
<!-- /.references -->
315+
316+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
317+
318+
<section class="related">
319+
320+
</section>
321+
322+
<!-- /.related -->
323+
324+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
325+
326+
<section class="links">
327+
328+
[correlation-distance]: https://en.wikipedia.org/wiki/Pearson_correlation_coefficient#Pearson's_distance
329+
330+
[@stdlib/array/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float64
331+
332+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
333+
334+
</section>
335+
336+
<!-- /.links -->

0 commit comments

Comments
 (0)