All files main.js

100% Statements 201/201
97.05% Branches 33/34
100% Functions 2/2
100% Lines 201/201

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 2021x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 132x 132x 132x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 16x 16x 24x 24x 24x 24x 40x 14x 14x 40x 9x 9x 15x 40x 40x 40x 40x 40x 40x 40x 102x 102x 15x 15x 15x 15x 15x 15x 15x 40x 87x 87x 87x 15x 15x 87x 87x 87x 87x 102x 102x 87x 15x 15x 87x 87x 15x 15x 40x 51x 51x 15x 15x 15x 15x 40x 10x 40x 5x 5x 15x 15x 15x 40x 10x 40x 5x 5x 5x 15x 15x 15x 40x 10x 40x 2x 5x 2x 3x 1x 1x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 14x 40x 1x 1x 1x 1x 1x  
/**
* @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 setReadOnly = require( '@stdlib/utils/define-read-only-property' );
var isNumberArray = require( '@stdlib/assert/is-number-array' );
var format = require( '@stdlib/string/format' );
var sqrt = require( '@stdlib/math/base/special/sqrt' );
var abs = require( '@stdlib/math/base/special/abs' );
var normalCDF = require( '@stdlib/stats/base/dists/normal/cdf' );
var min = require( '@stdlib/math/base/special/min' );
var validate = require( './validate.js' );
 
// eslint-disable-next-line stdlib/no-redeclare
var print = require( './print.js' );
 
 
// FUNCTIONS //
 
/**
* Comparator for ascending sort of value-index pairs.
*
* @private
* @param {Array} a - first [value, index] pair
* @param {Array} b - second [value, index] pair
* @returns {number} comparison result
*/
function sortPairs( a, b ) {
	return a[ 0 ] - b[ 0 ];
}
 
 
// MAIN //
 
/**
* Computes the Mann–Whitney U rank-sum test for two independent samples.
*
* @param {NumericArray} x - first sample
* @param {NumericArray} y - second sample
* @param {Options} [options] - function options
* @param {number} [options.alpha=0.05] - significance level
* @param {string} [options.alternative='two-sided'] - alternative hypothesis (`two-sided`, `less`, `greater`)
* @throws {TypeError} must provide two numeric arrays
* @throws {RangeError} alpha option must be a number in `[0,1]`
* @throws {TypeError} alternative option must be a string
* @throws {Error} alternative option must be `two-sided`, `less` or `greater`
* @returns {Object} test results
*/
function mannWhitneyU() {
	var values;
	var tieSum;
	var pValue;
	var alpha;
	var ranks;
	var pairs;
	var opts;
	var mean;
	var alt;
	var avg;
	var err;
	var out;
	var R1;
	var U1;
	var U2;
	var sd;
	var n1;
	var n2;
	var N;
	var x;
	var y;
	var k;
	var t;
	var U;
	var z;
	var i;
	var j;
 
	opts = {};
 
	x = arguments[ 0 ];
	y = arguments[ 1 ];
 
	if ( !isNumberArray( x ) || !isNumberArray( y ) ) {
		throw new TypeError( format( 'invalid argument. Must provide two numeric arrays.' ) );
	}
	n1 = x.length;
	n2 = y.length;
	N = n1 + n2;
 
	if ( arguments[ 2 ] ) {
		err = validate( opts, arguments[ 2 ] );
	}
	if ( err ) {
		throw err;
	}
 
	alpha = ( opts.alpha === void 0 ) ? 0.05 : opts.alpha;
	alt = opts.alternative || 'two-sided';
 
	values = x.concat( y );
 
	pairs = [];
	for ( i = 0; i < N; i++ ) {
		pairs.push( [ values[ i ], i ] );
	}
 
	pairs.sort( sortPairs );
 
	ranks = [];
	tieSum = 0.0;
 
	i = 0;
	while ( i < N ) {
		j = i + 1;
 
		while ( j < N && pairs[ j ][ 0 ] === pairs[ i ][ 0 ] ) {
			j += 1;
		}
		t = j - i;
		avg = ( i + j + 1 ) / 2.0;
 
		for ( k = i; k < j; k++ ) {
			ranks[ pairs[ k ][ 1 ] ] = avg;
		}
		if ( t > 1 ) {
			tieSum += ( t * t * t ) - t;
		}
		i = j;
	}
 
	R1 = 0.0;
	for ( i = 0; i < n1; i++ ) {
		R1 += ranks[ i ];
	}
 
	U1 = R1 - ( n1 * ( n1 + 1 ) * 0.5 );
	U2 = ( n1 * n2 ) - U1;
 
	if ( alt === 'two-sided' ) {
		U = min( U1, U2 );
	} else {
		U = U1;
	}
 
	mean = n1 * n2 * 0.5;
 
	if ( tieSum === 0.0 ) {
		sd = sqrt( ( n1 * n2 * ( N + 1 ) ) / 12.0 );
	} else {
		// eslint-disable-next-line max-len
		sd = sqrt( ( n1 * n2 / 12.0 ) * ( ( N + 1 ) - ( tieSum / ( N * ( N - 1 ) ) ) ) );
	}
 
	z = ( U - mean ) / sd;
 
	if ( alt === 'two-sided' ) {
		pValue = 2.0 * ( 1.0 - normalCDF( abs( z ), 0.0, 1.0 ) );
	} else if ( alt === 'greater' ) {
		pValue = 1.0 - normalCDF( z, 0.0, 1.0 );
	} else if ( alt === 'less' ) {
		pValue = normalCDF( z, 0.0, 1.0 );
	} else {
		throw new Error(format('invalid option. `%s` option must be one of `two-sided`, `less`, `greater`. Option: `%s`.', 'alternative', alt));
	}
 
	out = {};
	setReadOnly( out, 'U', U );
	setReadOnly( out, 'U1', U1 );
	setReadOnly( out, 'U2', U2 );
	setReadOnly( out, 'pValue', pValue );
	setReadOnly( out, 'rejected', pValue <= alpha );
	setReadOnly( out, 'alpha', alpha );
	setReadOnly( out, 'alternative', alt );
	setReadOnly( out, 'method', 'Mann–Whitney U test' );
	setReadOnly( out, 'print', print );
 
	return out;
}
 
 
// EXPORTS //
 
module.exports = mannWhitneyU;