time-to-botec

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

bh.js (1604B)


      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 min = require( '@stdlib/math/base/special/min' );
     24 var cumin = require( './../../base/cumin' );
     25 var Float64Array = require( '@stdlib/array/float64' );
     26 var order = require( './order.js' );
     27 
     28 
     29 // MAIN //
     30 
     31 /**
     32 * Adjusts the p-values via the Benjamini-Hochberg procedure.
     33 *
     34 * @private
     35 * @param {ProbabilityArray} pvalues - p-values to be adjusted
     36 * @param {PositiveInteger} comparisons - number of comparisons
     37 * @returns {ProbabilityArray} adjusted p-values
     38 */
     39 function bh( pvalues, comparisons ) {
     40 	var indices;
     41 	var sorted;
     42 	var len;
     43 	var out;
     44 	var i;
     45 
     46 	len = pvalues.length;
     47 	indices = order( pvalues, true );
     48 	sorted = new Float64Array( len );
     49 	for ( i = 0; i < len; i++ ) {
     50 		sorted[ i ] = comparisons / ( len - i ) * pvalues[ indices[ i ] ];
     51 	}
     52 	sorted = cumin( len, sorted, 1, sorted, 1 );
     53 	out = new Array( len );
     54 	for ( i = 0; i < len; i++ ) {
     55 		out[ indices[ i ] ] = min( sorted[ i ], 1.0 );
     56 	}
     57 	return out;
     58 }
     59 
     60 
     61 // EXPORTS //
     62 
     63 module.exports = bh;