Skip to content

Commit f475446

Browse files
nakul-krishnakumarkgrytestdlib-bot
authored
feat: add stats/strided/dpcorrwd
PR-URL: #10677 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Co-authored-by: stdlib-bot <noreply@stdlib.io>
1 parent abf8e78 commit f475446

33 files changed

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

0 commit comments

Comments
 (0)