Created
February 14, 2016 10:24
-
-
Save mathieujobin/9f30083f5cc49a7b9b50 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
module Enumerable | |
# weighted random sampling. | |
# | |
# Pavlos S. Efraimidis, Paul G. Spirakis | |
# Weighted random sampling with a reservoir | |
# Information Processing Letters | |
# Volume 97, Issue 5 (16 March 2006) | |
def wsample(n) | |
self.max_by(n) {|v| rand ** (1.0/yield(v)) } | |
end | |
end | |
e = (-20..20).to_a*10000 | |
a = e.wsample(20000) {|x| | |
Math.exp(-(x/5.0)**2) # normal distribution | |
} | |
# a is 20000 samples from e. | |
p a.length #=> 20000 | |
h = a.group_by {|x| x } | |
-10.upto(10) {|x| puts "*" * (h[x].length/30.0).to_i if h[x] } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment