time-to-botec

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

factory.js (2244B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2020 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 isPositiveInteger = require( '@stdlib/math/base/assert/is-positive-integer' );
     24 var constantFunction = require( '@stdlib/utils/constant-function' );
     25 var isfinite = require( '@stdlib/math/base/assert/is-finite' );
     26 var isnan = require( '@stdlib/math/base/assert/is-nan' );
     27 var exp = require( '@stdlib/math/base/special/exp' );
     28 var LN2 = require( '@stdlib/constants/float64/ln-two' );
     29 var weights = require( './weights.js' );
     30 
     31 
     32 // MAIN //
     33 
     34 /**
     35 * Returns a function for evaluating the quantile function of the Wilcoxon signed rank test statistic with `n` observations.
     36 *
     37 * @param {PositiveInteger} n - number of observations
     38 * @returns {Function} quantile function
     39 *
     40 * @example
     41 * var quantile = factory( 10 );
     42 * var y = quantile( 0.5 );
     43 * // returns 27
     44 *
     45 * y = quantile( 0.8 );
     46 * // returns 36
     47 */
     48 function factory( n ) {
     49 	var pui;
     50 	if ( isnan( n ) || !isPositiveInteger( n ) || !isfinite( n ) ) {
     51 		return constantFunction( NaN );
     52 	}
     53 	pui = exp( -n * LN2 );
     54 	return quantile;
     55 
     56 	/**
     57 	* Evaluates the quantile function of the Wilcoxon signed rank test statistic.
     58 	*
     59 	* @private
     60 	* @param {Probability} p - input value
     61 	* @returns {NonNegativeInteger} evaluated quantile function
     62 	*
     63 	* @example
     64 	* var y = quantile( 0.3 );
     65 	* // returns <number>
     66 	*/
     67 	function quantile( p ) {
     68 		var r;
     69 		var q;
     70 		if ( isnan( p ) || p < 0.0 || p > 1.0 ) {
     71 			return NaN;
     72 		}
     73 		if ( p === 0.0 ) {
     74 			return 0.0;
     75 		}
     76 		if ( p === 1.0 ) {
     77 			return ( n * ( n + 1 ) ) / 2;
     78 		}
     79 		r = 0;
     80 		q = -1;
     81 		while ( r < p ) {
     82 			q += 1;
     83 			r += pui * weights( q, n );
     84 		}
     85 		return q;
     86 	}
     87 }
     88 
     89 
     90 // EXPORTS //
     91 
     92 module.exports = factory;