Skip to content

Instantly share code, notes, and snippets.

@longouyang
Created April 25, 2017 23:39
Show Gist options
  • Save longouyang/43ee32ff846b8ce96b1b98fe30e1d11c to your computer and use it in GitHub Desktop.
Save longouyang/43ee32ff846b8ce96b1b98fe30e1d11c to your computer and use it in GitHub Desktop.
Uniformly sampling random subsets in webppl
// uses lodash, not underscore
var sampleSubset = function(xs) {
var n = xs.length;
var toBinary = function(dec){
// convert decimal to binary
var _s = (dec >>> 0).toString(2);
// do left padding
var k = _s.length;
var s = k < n ? ( _.repeat('0', n - k) + _s ) : _s;
var intArray = map(function(x) { return !!(x == '1') }, s);
return intArray;
}
var template = toBinary(randomInteger(Math.pow(2, n)), n);
var zipped = _.zip(xs, template)
return _.map(filter(function(pair) { return pair[1] }, zipped), '0')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment