time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

main.js (5672B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2018 The Stdlib Authors.
      5 *
      6 * Licensed under the Apache License, Version 2.0 (the "License");
      7 * you may not use this file except in compliance with the License.
      8 * You may obtain a copy of the License at
      9 *
     10 *    http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing, software
     13 * distributed under the License is distributed on an "AS IS" BASIS,
     14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15 * See the License for the specific language governing permissions and
     16 * limitations under the License.
     17 */
     18 
     19 'use strict';
     20 
     21 // MODULES //
     22 
     23 var isCollection = require( '@stdlib/assert/is-collection' );
     24 var isPlainObject = require( '@stdlib/assert/is-plain-object' );
     25 var setReadOnly = require( '@stdlib/utils/define-read-only-property' );
     26 var objectKeys = require( '@stdlib/utils/keys' );
     27 var qnorm = require( './../../base/dists/normal/quantile' );
     28 var chisqCDF = require( './../../base/dists/chisquare/cdf' );
     29 var group = require( '@stdlib/utils/group' );
     30 var ranks = require( './../../ranks' );
     31 var abs = require( '@stdlib/math/base/special/abs' );
     32 var pow = require( '@stdlib/math/base/special/pow' );
     33 var indexOf = require( '@stdlib/utils/index-of' );
     34 var median = require( './median.js' );
     35 var validate = require( './validate.js' );
     36 var print = require( './print.js' ); // eslint-disable-line stdlib/no-redeclare
     37 
     38 
     39 // FUNCTIONS //
     40 
     41 /**
     42 * Returns an array of a chosen length filled with the supplied value.
     43 *
     44 * @private
     45 * @param {*} val - value to repeat
     46 * @param {NonNegativeInteger} len - array length
     47 * @returns {Array} filled array
     48 */
     49 function repeat( val, len ) {
     50 	var out = new Array( len );
     51 	var i;
     52 
     53 	for ( i = 0; i < len; i++ ) {
     54 		out[ i ] = val;
     55 	}
     56 	return out;
     57 }
     58 
     59 
     60 // MAIN //
     61 
     62 /**
     63 * Computes the Fligner-Killeen test for equal variances.
     64 *
     65 * @param {...NumericArray} arguments - either two or more number arrays or a single numeric array if an array of group indicators is supplied as an option
     66 * @param {Options} [options] - function options
     67 * @param {number} [options.alpha=0.05] - significance level
     68 * @param {Array} [options.groups] - array of group indicators
     69 * @throws {TypeError} must provide array-like arguments
     70 * @throws {RangeError} alpha option has to be a number in the interval `[0,1]`
     71 * @throws {Error} must provide at least two array-like arguments if `groups` is not set
     72 * @throws {TypeError} options has to be simple object
     73 * @throws {TypeError} must provide valid options
     74 * @returns {Object} test results
     75 *
     76 * @example
     77 * // Data from Hollander & Wolfe (1973), p. 116:
     78 * var x = [ 2.9, 3.0, 2.5, 2.6, 3.2 ];
     79 * var y = [ 3.8, 2.7, 4.0, 2.4 ];
     80 * var z = [ 2.8, 3.4, 3.7, 2.2, 2.0 ];
     81 *
     82 * var out = fligner( x, y, z );
     83 * // returns {...}
     84 */
     85 function fligner() {
     86 	var variance;
     87 	var options;
     88 	var ngroups;
     89 	var levels;
     90 	var groups;
     91 	var scores;
     92 	var table;
     93 	var alpha;
     94 	var delta;
     95 	var args;
     96 	var mean;
     97 	var opts;
     98 	var pval;
     99 	var sums;
    100 	var xabs;
    101 	var stat;
    102 	var err;
    103 	var loc;
    104 	var out;
    105 	var df;
    106 	var M2;
    107 	var a;
    108 	var n;
    109 	var x;
    110 	var i;
    111 	var j;
    112 
    113 	args = [];
    114 	ngroups = arguments.length;
    115 	opts = {};
    116 	if ( isPlainObject( arguments[ ngroups - 1 ] ) ) {
    117 		options = arguments[ ngroups - 1 ];
    118 		ngroups -= 1;
    119 		err = validate( opts, options );
    120 		if ( err ) {
    121 			throw err;
    122 		}
    123 	}
    124 	if ( opts.groups ) {
    125 		groups = opts.groups;
    126 		table = group( arguments[ 0 ], groups );
    127 		levels = objectKeys( table );
    128 		ngroups = levels.length;
    129 		if ( ngroups < 2 ) {
    130 			throw new Error( 'invalid number of groups. `groups` array must contain at least two unique elements. Value: `' + levels + '`.' );
    131 		}
    132 		for ( i = 0; i < ngroups; i++ ) {
    133 			args.push( table[ levels[ i ] ] );
    134 		}
    135 	} else {
    136 		groups = [];
    137 		for ( i = 0; i < ngroups; i++ ) {
    138 			args.push( arguments[ i ] );
    139 			groups = groups.concat( repeat( i, arguments[ i ].length ) );
    140 		}
    141 	}
    142 	if ( opts.alpha === void 0 ) {
    143 		alpha = 0.05;
    144 	} else {
    145 		alpha = opts.alpha;
    146 	}
    147 	if ( alpha < 0.0 || alpha > 1.0 ) {
    148 		throw new RangeError( 'invalid argument. Option `alpha` must be a number in the range 0 to 1. Value: `' + alpha + '`.' );
    149 	}
    150 	x = [];
    151 	for ( i = 0; i < ngroups; i++ ) {
    152 		if ( !isCollection( args[ i ] ) ) {
    153 			throw new TypeError( 'invalid argument. Must provide array-like arguments. Value: `' + args[ i ] + '`.' );
    154 		}
    155 		if ( args[ i ].length === 0 ) {
    156 			throw new Error( 'invalid argument. Supplied arrays cannot be empty. Value: `' + args[ i ] + '`.' );
    157 		}
    158 		loc = median( args[ i ] );
    159 		for ( j = 0; j < args[ i ].length; j++ ) {
    160 			args[ i ][ j ] -= loc;
    161 		}
    162 		x = x.concat( args[ i ] );
    163 	}
    164 	n = x.length;
    165 	xabs = new Array( n );
    166 	for ( i = 0; i < n; i++ ) {
    167 		xabs[ i ] = abs( x[ i ] );
    168 	}
    169 	scores = ranks( xabs );
    170 	a = new Array( n );
    171 	mean = 0.0;
    172 	M2 = 0.0;
    173 	sums = repeat( 0.0, ngroups );
    174 	for ( i = 0; i < n; i++ ) {
    175 		a[ i ] = qnorm( ( 1.0 + ( scores[ i ]/(n+1) ) ) / 2.0, 0.0, 1.0 );
    176 		sums[ ( levels ) ? indexOf( levels, groups[i] ) : groups[i] ] += a[ i ];
    177 		delta = a[ i ] - mean;
    178 		mean += delta / ( i+1 );
    179 		M2 += delta * ( a[ i ] - mean );
    180 	}
    181 	variance = M2 / ( n - 1 );
    182 	stat = 0.0;
    183 	for ( i = 0; i < ngroups; i++ ) {
    184 		stat += pow( sums[ i ], 2 ) / args[ i ].length;
    185 	}
    186 	stat = ( stat - ( n * pow( mean, 2 ) ) ) / variance;
    187 	df = ngroups - 1;
    188 	pval = 1.0 - chisqCDF( stat, df );
    189 
    190 	out = {};
    191 	setReadOnly( out, 'rejected', pval <= alpha );
    192 	setReadOnly( out, 'alpha', alpha );
    193 	setReadOnly( out, 'pValue', pval );
    194 	setReadOnly( out, 'statistic', stat );
    195 	setReadOnly( out, 'df', df );
    196 	setReadOnly( out, 'method', 'Fligner-Killeen test of homogeneity of variances' );
    197 	setReadOnly( out, 'print', print );
    198 	return out;
    199 }
    200 
    201 
    202 // EXPORTS //
    203 
    204 module.exports = fligner;