hommel.js (2487B)
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 max = require( '@stdlib/math/base/special/max' ); 25 var Float64Array = require( '@stdlib/array/float64' ); 26 var PINF = require( '@stdlib/constants/float64/pinf' ); 27 var order = require( './order.js' ); 28 29 30 // VARIABLES // 31 32 var slice = Array.prototype.slice; 33 34 35 // MAIN // 36 37 /** 38 * Adjusts the p-values via Hommel's method. 39 * 40 * @private 41 * @param {ProbabilityArray} pvalues - p-values to be adjusted 42 * @param {PositiveInteger} comparisons - number of comparisons 43 * @returns {ProbabilityArray} adjusted p-values 44 */ 45 function hommel( pvalues, comparisons ) { 46 var indices; 47 var diff; 48 var adj; 49 var idx; 50 var len; 51 var out; 52 var mq; 53 var m; 54 var i; 55 var j; 56 var k; 57 var q; 58 var v; 59 60 len = pvalues.length; 61 diff = comparisons - len; 62 if ( diff > 0 ) { 63 pvalues = slice.call( pvalues ); 64 while ( diff > 0 ) { 65 pvalues.push( 1.0 ); 66 diff -= 1; 67 } 68 } 69 indices = order( pvalues ); 70 m = PINF; 71 for ( i = 0; i < comparisons; i++ ) { 72 v = comparisons * pvalues[ i ] / ( i+1 ); 73 if ( v < m ) { 74 m = v; 75 } 76 } 77 q = new Float64Array( comparisons ); 78 adj = new Float64Array( comparisons ); 79 for ( i = comparisons - 1; i > 1; i-- ) { 80 mq = PINF; 81 for ( k = comparisons - i + 1; k <= comparisons; k++ ) { 82 v = i * pvalues[ indices[ k ] ] / ( 2 + k - comparisons + i - 1 ); 83 if ( v < mq ) { 84 mq = v; 85 } 86 } 87 for ( j = 0; j < comparisons - i + 1; j++ ) { 88 q[ j ] = min( i * pvalues[ indices[ j ] ], mq ); 89 } 90 for ( k = comparisons - i + 1; k <= comparisons; k++ ) { 91 q[ k ] = q[ comparisons - i ]; 92 } 93 for ( j = 0; j < adj.length; j++ ) { 94 adj[ j ] = max( q[ j ], adj[ j ] ); 95 } 96 } 97 out = new Array( len ); 98 for ( i = 0; i < len; i++ ) { 99 idx = indices[ i ]; 100 v = max( adj[ i ], pvalues[ idx ] ); 101 out[ idx ] = v; 102 } 103 return out; 104 } 105 106 107 // EXPORTS // 108 109 module.exports = hommel;