Created
April 25, 2017 23:39
-
-
Save longouyang/43ee32ff846b8ce96b1b98fe30e1d11c to your computer and use it in GitHub Desktop.
Uniformly sampling random subsets in webppl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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