All files / random/sample/lib vose.js

100% Statements 105/105
100% Branches 13/13
100% Functions 1/1
100% Lines 105/105

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 1062x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 16x 16x 11x 16x 5x 5x 16x 3x 3x 3x 12x 12x 12x 12x 12x 12x 2x 12x 10x 10x 12x 3x 3x 3x 3x 1x 1x 3x 3x 14x 14x 9x 14x 5x 5x 14x 3x 3x 2x 2x 2x 2x 2x  
/**
* @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 floor = require( '@stdlib/math/base/special/floor' );
 
 
// MAIN //
 
/**
* Samples with replacement and non-uniform probabilities using Vose's [alias method][alias-method].
*
* ## References
*
* -   Vose, Michael D. 1991. "A linear algorithm for generating random numbers with a given distribution." _IEEE Transactions on Software Engineering_ 17 (9): 972–75. doi:[10.1109/32.92917][@vose:1991].
*
* [alias-method]: http://keithschwarz.com/darts-dice-coins/
* [@vose:1991]: https://doi.org/10.1109/32.92917
*
* @private
* @param {ArrayLike} x - array-like object from which to sample
* @param {NonNegativeInteger} size - sample size
* @param {Function} rand - PRNG for generating uniformly distributed numbers
* @param {ProbabilityArray} probabilities - element probabilities
* @returns {Array} sample
*/
function vose( x, size, rand, probabilities ) {
	var small;
	var large;
	var probs;
	var alias;
	var out;
	var N;
	var p;
	var g;
	var i;
	var l;
 
	probs = probabilities.slice();
	N = x.length;
 
	small = [];
	large = [];
	for ( i = 0; i < N; i++ ) {
		probs[ i ] *= N;
		if ( probs[ i ] < 1.0 ) {
			small.push( i );
		} else {
			large.push( i );
		}
	}
	alias = new Array( N );
	p = new Array( N );
	while ( small.length !== 0 && large.length !== 0 ) {
		l = small.shift();
		g = large.shift();
		p[ l ] = probs[ l ];
		alias[ l ] = g;
		probs[ g ] = probs[ g ] + probs[ l ] - 1.0;
		if ( probs[ g ] < 1.0 ) {
			small.push( g );
		} else {
			large.push( g );
		}
	}
	for ( i = 0; i < large.length; i++ ) {
		p[ large[ i ] ] = 1.0;
	}
	for ( i = 0; i < small.length; i++ ) {
		p[ small[ i ] ] = 1.0;
	}
	out = new Array( size );
	for ( i = 0; i < size; i++ ) {
		l = floor( N*rand() );
		if ( rand() < p[ l ] ) {
			out[ i ] = x[ l ];
		} else {
			out[ i ] = x[ alias[ l ] ];
		}
	}
	return out;
}
 
 
// EXPORTS //
 
module.exports = vose;