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
211 changes: 211 additions & 0 deletions lib/node_modules/@stdlib/stats/spearman-test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
<!--

@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.

-->

# Spearman Correlation Test

> Compute a Spearman rank correlation test between paired samples.

<section class="intro">

The [Spearman rank correlation coefficient][spearman-correlation] is a non-parametric measure of rank correlation that assesses how well the relationship between two variables can be described using a monotonic function. Unlike the [Pearson correlation][pearson-correlation], which assesses linear relationships, the Spearman correlation assesses monotonic relationships (whether linear or not).

For a sample of size `n`, the Spearman rank correlation coefficient is defined as the [Pearson correlation coefficient][pearson-correlation] between the rank variables.

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var spearmanTest = require( '@stdlib/stats/spearman-test' );
```

#### spearmanTest( x, y\[, opts] )

By default, the function performs a t-test for the null hypothesis that the paired data in [arrays][mdn-array] or [typed arrays][mdn-typed-array] `x` and `y` have a [Spearman correlation coefficient][spearman-correlation] of zero.

```javascript
var x = [ 0.7, -1.6, -0.2, -1.2, -0.1, 3.4, 3.7, 0.8, 0.0, 2.0 ];
var y = [ 1.9, 0.8, 1.1, 0.1, -0.1, 4.4, 5.5, 1.6, 4.6, 3.4 ];

var out = spearmanTest( x, y );
/* e.g., returns
{
'alpha': 0.05,
'rejected': true,
'pValue': ~0.014,
'statistic': ~2.98,
'ci': [ ~0.162, ~0.95 ],
'nullValue': 0,
'scorr': ~0.745,
// ...
}
*/
```

The returned object comes with a `.print()` method which when invoked will print a formatted output of the results of the hypothesis test. `print` accepts a `digits` option that controls the number of decimal digits displayed for the outputs and a `decision` option, which when set to `false` will hide the test decision.

<!-- run-disable -->

```javascript
console.log( out.print() );
/* e.g., =>
t-test for Spearman's rank correlation coefficient

Alternative hypothesis: True correlation coefficient is not equal to 0

pValue: 0.014
statistic: 2.9848
95% confidence interval: [0.1623,0.9504]

Test Decision: Reject null in favor of alternative at 5% significance level
*/
```

The function accepts the following `options`:

- **alpha**: `number` in the interval `[0,1]` giving the significance level of the hypothesis test. Default: `0.05`.
- **alternative**: Either `two-sided`, `less` or `greater`. Indicates whether the alternative hypothesis is that `x` has a larger (or smaller) Spearman rank correlation with `y` than `rho` (`two-sided`), that `x` has a larger rank correlation than `rho` (`greater`) or a smaller rank correlation than `rho` (`less`). Default: `two-sided`.
- **rho**: `number` denoting the correlation under the null hypothesis. Default: `0`.

By default, a two-sided test is performed. To perform either of the one-sided tests, set the `alternative` option.

```javascript
var x = [ 0.7, -1.6, -0.2, -1.2, -0.1, 3.4, 3.7, 0.8, 0.0, 2.0 ];
var y = [ 1.9, 0.8, 1.1, 0.1, -0.1, 4.4, 5.5, 1.6, 4.6, 3.4 ];

var out = spearmanTest( x, y, {
'alternative': 'less'
});
/* e.g., returns
{
'alpha': 0.05,
'rejected': false,
'pValue': ~0.993,
// ...
}
*/

out = spearmanTest( x, y, {
'alternative': 'greater'
});
/* e.g., returns
{
'alpha': 0.05,
'rejected': true,
'pValue': ~0.007,
// ...
}
*/
```

By default, the null hypothesis is that the correlation coefficient is `0`. To test against a different value, set the `rho` option.

```javascript
var x = [ 0.7, -1.6, -0.2, -1.2, -0.1, 3.4, 3.7, 0.8, 0.0, 2.0 ];
var y = [ 1.9, 0.8, 1.1, 0.1, -0.1, 4.4, 5.5, 1.6, 4.6, 3.4 ];

var out = spearmanTest( x, y, {
'rho': 0.8
});
```

To change the significance level, set the `alpha` option.

```javascript
var x = [ 0.7, -1.6, -0.2, -1.2, -0.1, 3.4, 3.7, 0.8, 0.0, 2.0 ];
var y = [ 1.9, 0.8, 1.1, 0.1, -0.1, 4.4, 5.5, 1.6, 4.6, 3.4 ];

var out = spearmanTest( x, y, {
'alpha': 0.01
});
```

</section>

<!-- /.usage -->

<section class="examples">

## Examples

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

```javascript
var rnorm = require( '@stdlib/random/base/normal' );
var sqrt = require( '@stdlib/math/base/special/sqrt' );
var spearmanTest = require( '@stdlib/stats/spearman-test' );

var table;
var out;
var rho;
var x;
var y;
var i;

rho = 0.5;
x = [];
y = [];
for ( i = 0; i < 300; i++ ) {
x.push( rnorm( 0.0, 1.0 ) );
y.push( ( rho * x[ i ] ) + rnorm( 0.0, sqrt( 1.0 - (rho*rho) ) ) );
}

out = spearmanTest( x, y );
table = out.print();
console.log( table );

out = spearmanTest( x, y, {
'rho': 0.5
});
table = out.print();
console.log( table );
```

</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">

[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays

[spearman-correlation]: https://en.wikipedia.org/wiki/Spearman%27s_rank_correlation_coefficient

[pearson-correlation]: https://en.wikipedia.org/wiki/Pearson_correlation_coefficient

</section>

<!-- /.links -->
138 changes: 138 additions & 0 deletions lib/node_modules/@stdlib/stats/spearman-test/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/**
* @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.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var isObject = require( '@stdlib/assert/is-object' );
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
var rnorm = require( '@stdlib/random/base/normal' );
var sqrt = require( '@stdlib/math/base/special/sqrt' );
var pkg = require( './../package.json' ).name;
var spearmanTest = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var result;
var rho;
var idx;
var y;
var x;
var i;

rho = 0.5;
x = [];
y = [];
for ( i = 0; i < 300; i++ ) {
x.push( rnorm( 0.0, 1.0 ) );
y.push( ( rho * x[ i ] ) + rnorm( 0.0, sqrt( 1.0 - (rho*rho) ) ) );
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
idx = i % x.length;
y[ idx ] = ( rho * x[ i ] ) + rnorm( 0.0, sqrt( 1.0 - (rho*rho) ) );
result = spearmanTest( x, y );
if ( typeof result !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( !isObject( result ) ) {
b.fail( 'should return an object' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::one-sided', function benchmark( b ) {
var result;
var opts;
var rho;
var idx;
var y;
var x;
var i;

rho = 0.5;
x = [];
y = [];
for ( i = 0; i < 300; i++ ) {
x.push( rnorm( 0.0, 1.0 ) );
y.push( ( rho * x[ i ] ) + rnorm( 0.0, sqrt( 1.0 - (rho*rho) ) ) );
}
opts = {
'alternative': 'less'
};

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
idx = i % x.length;
y[ idx ] = ( rho * x[ i ] ) + rnorm( 0.0, sqrt( 1.0 - (rho*rho) ) );
result = spearmanTest( x, y, opts );
if ( typeof result !== 'object' ) {
b.fail( 'should return an object' );
}
}
b.toc();
if ( !isObject( result ) ) {
b.fail( 'should return an object' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+':print', function benchmark( b ) {
var digits;
var result;
var output;
var rho;
var y;
var x;
var i;

rho = 0.5;
x = [];
y = [];
for ( i = 0; i < 300; i++ ) {
x.push( rnorm( 0.0, 1.0 ) );
y.push( ( rho * x[ i ] ) + rnorm( 0.0, sqrt( 1.0 - (rho*rho) ) ) );
}
result = spearmanTest( x, y );

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
digits = ( i % 8 ) + 1;
output = result.print({
'digits': digits
});
if ( typeof output !== 'string' ) {
b.fail( 'should return a string' );
}
}
b.toc();
if ( !isString( output ) ) {
b.fail( 'should return a string' );
}
b.pass( 'benchmark finished' );
b.end();
});
Loading
Loading