Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 8x 8x 18x 26x 15x 15x 8x 8x 15x 26x 26x 26x 26x 26x 26x 26x 61x 49x 49x 61x 10x 10x 10x 10x 10x 10x 26x 1x 5x 5x 26x 9x 9x 44x 44x 44x 35x 44x 36x 36x 4x 4x 36x 4x 4x 36x 4x 4x 4x 36x 36x 24x 24x 36x 36x 44x 44x 36x 44x 8x 8x 44x 9x 10x 26x 1x 1x 1x 1x 8x 3x 3x 8x 5x 5x 8x 1x 1x 26x 8x 8x 45x 6x 45x 39x 39x 45x 8x 8x 1x 1x 26x 1x 1x 1x 1x 1x | /**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
// MODULES //
var isCollection = require( '@stdlib/assert/is-collection' );
var contains = require( '@stdlib/assert/contains' );
var zeros = require( '@stdlib/array/base/zeros' );
var format = require( '@stdlib/string/format' );
var sum = require( './sum.js' );
var order = require( './order.js' );
var isMissing = require( './is_missing.js' );
var validate = require( './validate.js' );
// MAIN //
/**
* Computes the sample ranks for the values of an array-like object.
*
* @param {Collection} x - data array
* @param {Object} [options] - options object
* @param {string} [options.method='average'] - method name determining how ties are treated
* @param {string} [options.missing='last'] - determines where missing values go (`first`,`last`, or `remove`)
* @param {Array} [options.encoding=[null,NaN]] - array of values encoding missing values
* @throws {TypeError} first argument has to be an array-like object
* @throws {TypeError} options argument must be an object
* @throws {TypeError} must provide valid options
* @returns {Array} array containing the computed ranks for the elements of x
*
* @example
* var arr = [ 1.1, 2.0, 3.5, 0.0, 2.4 ];
* var out = ranks( arr );
* // returns [ 2, 3, 5, 1, 4 ]
*
* @example
* // Ties are averaged:
* arr = [ 2, 2, 1, 4, 3 ];
* out = ranks( arr );
* // returns [ 2.5, 2.5, 1, 5, 4 ]
*
* @example
* // Missing values are placed last:
* arr = [ null, 2, 2, 1, 4, 3, NaN, NaN ];
* out = ranks( arr );
* // returns [ 6, 2.5, 2.5, 1, 5, 4, 7 ,8 ]
*/
function ranks( x, options ) {
var missingIndices;
var noDuplicates;
var countMissing;
var totalNoTies;
var finalRanks;
var encoding;
var iPlusOne;
var ordered;
var missing;
var tieRank;
var method;
var ranks;
var opts;
var xnew;
var err;
var n;
var i;
var j;
if ( !isCollection( x ) ) {
throw new TypeError( format( 'invalid argument. First argument must be an array-like object. Value: `%s`.', x ) );
}
opts = {};
if ( arguments.length > 1 ) {
err = validate( opts, options );
if ( err ) {
throw err;
}
}
method = opts.method || 'average';
encoding = opts.encoding || [ null, NaN ];
missing = opts.missing || 'last';
n = x.length;
xnew = [];
for ( i = 0; i < n; i++ ) {
if ( !contains( encoding, x[ i ] ) ) {
xnew.push( x[ i ] );
}
}
missingIndices = isMissing( x, encoding );
n = xnew.length;
totalNoTies = 0;
ordered = order( xnew );
ranks = zeros( n );
if ( method === 'ordinal' ) {
for ( i = 0; i < n; i++ ) {
ranks[ ordered[ i ] ] = i + 1;
}
} else {
noDuplicates = 0;
for ( i = 0; i < n; i++ ) {
iPlusOne = i + 1;
if (
( i === n - 1 ) ||
( xnew[ ordered[i] ] !== xnew[ ordered[ iPlusOne ] ] )
) {
switch ( method ) {
case 'min':
tieRank = iPlusOne - noDuplicates;
break;
case 'max':
tieRank = iPlusOne;
break;
case 'dense':
tieRank = iPlusOne - noDuplicates - totalNoTies;
totalNoTies += noDuplicates;
break;
case 'average':
default:
tieRank = iPlusOne - ( 0.5 * noDuplicates );
break;
}
for ( j = i - noDuplicates; j < iPlusOne; j++ ) {
ranks[ ordered[ j ] ] = tieRank;
}
noDuplicates = 0;
} else {
noDuplicates += 1;
}
}
}
if ( missing === 'first' ) {
countMissing = sum( missingIndices );
j = 1;
finalRanks = [];
for ( i = 0; i < missingIndices.length; i++ ) {
if ( missingIndices[ i ] ) {
finalRanks.push( j );
j += 1;
} else {
finalRanks.push( ranks.shift() + countMissing );
}
}
return finalRanks;
}
if ( missing === 'last' ) {
finalRanks = [];
for ( i = 0; i < missingIndices.length; i++ ) {
if ( missingIndices[ i ] ) {
finalRanks.push( i + ranks.length + 1 );
} else {
finalRanks.push( ranks.shift() );
}
}
return finalRanks;
}
// Case: missing = 'remove'
return ranks;
}
// EXPORTS //
module.exports = ranks;
|