time-to-botec

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

factory.js (2313B)


      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 round = require( '@stdlib/math/base/special/round' );
     27 var isnan = require( '@stdlib/math/base/assert/is-nan' );
     28 var exp = require( '@stdlib/math/base/special/exp' );
     29 var LN2 = require( '@stdlib/constants/float64/ln-two' );
     30 var weights = require( './weights.js' );
     31 
     32 
     33 // MAIN //
     34 
     35 /**
     36 * Returns a function for evaluating the cumulative distribution function (CDF) for the distribution of the Wilcoxon signed rank test statistic with `n` observations.
     37 *
     38 * @param {PositiveInteger} n - number of observations
     39 * @returns {Function} CDF
     40 *
     41 * @example
     42 * var cdf = factory( 8 );
     43 * var y = cdf( 3.9 );
     44 * // returns ~0.027
     45 *
     46 * y = cdf( 17.0 );
     47 * // returns ~0.473
     48 */
     49 function factory( n ) {
     50 	var mlim;
     51 	var pui;
     52 
     53 	if ( !isPositiveInteger( n ) || !isfinite( n ) ) {
     54 		return constantFunction( NaN );
     55 	}
     56 	pui = exp( -n * LN2 );
     57 	mlim = n * ( n + 1 ) / 2;
     58 	return cdf;
     59 
     60 	/**
     61 	* Evaluates the cumulative distribution function (CDF) for the distribution of the Wilcoxon signed rank test statistic.
     62 	*
     63 	* @private
     64 	* @param {number} x - input value
     65 	* @returns {Probability} evaluated CDF
     66 	*
     67 	* @example
     68 	* var y = cdf( 2 );
     69 	* // returns <number>
     70 	*/
     71 	function cdf( x ) {
     72 		var i;
     73 		var p;
     74 		if ( isnan( x ) ) {
     75 			return NaN;
     76 		}
     77 		if ( x < 0.0 ) {
     78 			return 0.0;
     79 		}
     80 		x = round( x );
     81 		if ( x >= mlim ) {
     82 			return 1.0;
     83 		}
     84 		p = 0;
     85 		for ( i = 0; i <= x; i++ ) {
     86 			p += weights( i, n ) * pui;
     87 		}
     88 		return p;
     89 	}
     90 }
     91 
     92 
     93 // EXPORTS //
     94 
     95 module.exports = factory;