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

@license Apache-2.0

Copyright (c) 2018 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.

-->

# Mann-Whitney U Test

> Nonparametric hypothesis test comparing two independent samples.

<section class="usage">

## Usage

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

#### mannWhitneyU( x, y\[, options] )

Given two independent samples x and y, the function computes the Mann–Whitney U rank-sum test to assess whether the distributions differ.

```javascript
var out = mannWhitneyU( [ 7, 8, 9 ], [ 1, 2, 3 ] );
/* returns
{
'U': 0,
'pValue': 0.025,
'rejected': true
}
*/

out = mannWhitneyU( [ 10, 11, 10, 12 ], [ 9, 11, 10, 10 ] );
/* returns
{
'U': 4.5,
'pValue': 0.312,
'rejected': false
}
*/
```

<!-- run-disable -->

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 the direction of the hypothesis test. `'greater'` tests whether values in `x` tend to be larger than those in `y`, `'less'` tests whether values in `x` tend to be smaller than those in `y`, and `'two-sided'` tests for any difference in distributions.

By default, the hypothesis test is carried out at a significance level of `0.05`. To choose a different significance level, set the `alpha` option.

```javascript
var out = mannWhitneyU( [ 7, 8, 9 ], [ 1, 2, 3 ], {
'alpha': 0.05,
'alternative': 'greater'
});
/* returns
{
'U': 0,
'pValue': 0.025,
'rejected': true
}
*/
```

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

```javascript
var out = mannWhitneyU( [ 5, 6, 7, 8 ], [ 1, 2, 3, 4 ], {
'alternative': 'greater',
'alpha': 0.05
});
/* returns
{
'U': 0,
'pValue': 0.0104,
'rejected': true
}
*/

out = mannWhitneyU( [ 5, 6, 7, 8 ], [ 1, 2, 3, 4 ], {
'alternative': 'less',
'alpha': 0.05
});
/* returns
{
'U': 0,
'pValue': 0.9896,
'rejected': false
}
*/
```

</section>

<!-- /.usage -->

<section class="examples">

## Examples

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

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

var x = [ 12, 15, 14, 10, 13 ];
var y = [ 7, 9, 6, 8, 7 ];

var out = mannWhitneyU( x, y );
/* returns
{
'U': 0,
'pValue': 0.009,
'rejected': true
}
*/

out = mannWhitneyU( [ 5, 6, 7, 8 ], [ 1, 2, 3, 4 ] );
/* returns
{
'U': 0,
'pValue': 0.0208,
'rejected': true
}
*/

out = mannWhitneyU( [ 5, 6, 7, 8 ], [ 1, 2, 3, 4 ], {
'alternative': 'greater'
});
/* returns
{
'U': 0,
'pValue': 0.0104,
'rejected': true
}
*/

out = mannWhitneyU( [ 1, 2, 2, 3 ], [ 2, 3, 4, 5 ], {
'alternative': 'greater',
'alpha': 0.05
});
/* returns
{
'U': 2.5,
'pValue': 0.057,
'rejected': false
}
*/
```

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

</section>

<!-- /.links -->
131 changes: 131 additions & 0 deletions lib/node_modules/@stdlib/stats/mannwhitneyu/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2018 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 discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
var pkg = require( './../package.json' ).name;
var mannWhitneyU = require( './../lib' );


// MAIN //

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

x = [];
y = [];
for ( i = 0; i < 100; i++ ) {
x.push( 0 );
y.push( 0 );
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
fillArray( x );
fillArray( y );
result = mannWhitneyU( 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 x;
var y;
var i;

opts = {
'alternative': 'greater'
};
x = [];
y = [];
for ( i = 0; i < 100; i++ ) {
x.push( 0 );
y.push( 0 );
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
fillArray( x );
fillArray( y );
result = mannWhitneyU( 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 i;

result = mannWhitneyU( [ 7, 8, 9 ], [ 1, 2, 3 ] );

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();
});


// FUNCTIONS //

function fillArray( arr ) {
var i;
for ( i = 0; i < arr.length; i++ ) {
arr[ i ] = discreteUniform( 0, 100 );
}
}
77 changes: 77 additions & 0 deletions lib/node_modules/@stdlib/stats/mannwhitneyu/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
{{alias}}( x, y[, options] )
Computes the Mann–Whitney U rank-sum test for the difference between two
independent samples.

The returned object comes with a `.print()` method which when invoked will
print a formatted output of the results of the hypothesis test.

Parameters
----------
x: Array<number>
First independent sample.

y: Array<number>
Second independent sample.

options: Object (optional)
Options.

options.alpha: number (optional)
Number in the interval `[0,1]` giving the significance level of the
hypothesis test. Default: `0.05`.

options.alternative: string (optional)
Indicates whether the alternative hypothesis is that the distribution of
`x` is shifted to the right of `y` (`greater`), to the left of `y`
(`less`) or that they differ (`two-sided`). Default: `'two-sided'`.

Returns
-------
out: Object
Test result object.

out.alpha: number
Used significance level.

out.rejected: boolean
Test decision.

out.pValue: number
p-value of the test.

out.U: number
Mann–Whitney U statistic.

out.U1: number
U value for sample `x`.

out.U2: number
U value for sample `y`.

out.alternative: string
Alternative hypothesis (`two-sided`, `less` or `greater`).

out.method: string
Name of the test.

out.print: Function
Function to print formatted output.

Examples
--------
> var out = {{alias}}( [ 7, 8, 9 ], [ 1, 2, 3 ] )
{ 'U': 0, 'pValue': ~0.025, ... }

> out = {{alias}}( [ 5, 6, 7, 8 ], [ 1, 2, 3, 4 ], {
... 'alternative': 'greater'
... })
{ 'U': 0, 'pValue': ~0.0104, ... }

> out = {{alias}}( [ 1, 2, 2, 3 ], [ 2, 3, 4, 5 ], {
... 'alternative': 'greater',
... 'alpha': 0.05
... })
{ 'U': 2.5, 'pValue': ~0.057, ... }

See Also
--------
Loading
Loading