walker_method 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +19 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +22 -0
- data/README.md +51 -0
- data/Rakefile +7 -0
- data/lib/walker_method.rb +47 -0
- data/lib/walker_method/version.rb +3 -0
- data/spec/spec_helper.rb +7 -0
- data/spec/walker_method_spec.rb +11 -0
- data/walker_method.gemspec +19 -0
- metadata +58 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Andrew Cantino
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# WalkerMethod
|
2
|
+
|
3
|
+
Walker's Alias Method is an O(1) algorithm for selecting elements from an array given a weighted distribution.
|
4
|
+
|
5
|
+
For example, let's say you want to return `:win` 80% of the time and `:lose` 20% of the time. You could fill an array with
|
6
|
+
8 examples of `:win` and 2 examples `:lose`, then pick a random element. You could also use ranges, picking a random number
|
7
|
+
between 0.0 and 1.0 and returning `:win` when the number is below 0.8, `:lose` otherwise. But, these algorithms are still O(n).
|
8
|
+
You can do better by using a heap or binary search tree. Walker's Alias Method is better still, with a constant runtime once an O(n)
|
9
|
+
pre-computation phase has completed. For this example, you could do:
|
10
|
+
|
11
|
+
selector = WalkerMethod.new([:win, :lose], [80, 20])
|
12
|
+
selector.random
|
13
|
+
|
14
|
+
This implementation is a port of http://code.activestate.com/recipes/576564-walkers-alias-method-for-random-objects-with-diffe/ from Python to Ruby. There is also a [Node.js implementation](https://github.com/ThoughtLeadr/Walker-Random-Node).
|
15
|
+
|
16
|
+
## Speed
|
17
|
+
|
18
|
+
It's FAST and simple. It samples from a 50,000 word frequency dictionary 10,000 times in 1.2 miliseconds.
|
19
|
+
|
20
|
+
## Installation
|
21
|
+
|
22
|
+
Add this line to your application's Gemfile:
|
23
|
+
|
24
|
+
gem 'walker_method'
|
25
|
+
|
26
|
+
And then execute:
|
27
|
+
|
28
|
+
$ bundle
|
29
|
+
|
30
|
+
Or install it yourself as:
|
31
|
+
|
32
|
+
$ gem install walker_method
|
33
|
+
|
34
|
+
## Usage
|
35
|
+
|
36
|
+
# Sampling from an English word frequency dictionary care of http://invokeit.wordpress.com/frequency-word-lists
|
37
|
+
|
38
|
+
words = ["you", "the", "i", "to", "a", "and", "it", "of", "that", "in", "is", "me", "what", "this", "for", "my", "on", "your", "we", "have", "do", "no", "don't", "are", "be"]
|
39
|
+
freqs = [4621939, 3957465, 3476773, 2873389, 2551033, 1775393, 1693042, 1531878, 1323823, 1295198, 1242191, 1208959, 1071825, 961194, 898671, 877684, 867296, 834953, 819499, 812625, 799991, 788200, 764177, 743194, 743014]
|
40
|
+
|
41
|
+
selector = WalkerMethod.new(words, weights)
|
42
|
+
selector.random
|
43
|
+
# => "and"
|
44
|
+
|
45
|
+
## Contributing
|
46
|
+
|
47
|
+
1. Fork it
|
48
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
49
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
50
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
51
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require "walker_method/version"
|
2
|
+
|
3
|
+
class WalkerMethod
|
4
|
+
attr_accessor :prob, :inx, :keys, :weights, :sumw, :length
|
5
|
+
|
6
|
+
def initialize(keys, weights)
|
7
|
+
self.keys = keys
|
8
|
+
self.weights = weights
|
9
|
+
self.sumw = weights.reduce(&:+).to_f
|
10
|
+
self.prob = []
|
11
|
+
self.inx = []
|
12
|
+
self.length = weights.length
|
13
|
+
short = []
|
14
|
+
long = []
|
15
|
+
weights.each do |w|
|
16
|
+
inx << -1
|
17
|
+
prob << w * length / sumw
|
18
|
+
end
|
19
|
+
|
20
|
+
prob.each.with_index do |p, index|
|
21
|
+
short << index if p < 1
|
22
|
+
long << index if p > 1
|
23
|
+
end
|
24
|
+
|
25
|
+
while short.length > 0 && long.length > 0
|
26
|
+
j = short.pop
|
27
|
+
k = long[-1]
|
28
|
+
inx[j] = k
|
29
|
+
prob[k] -= (1 - prob[j])
|
30
|
+
if prob[k] < 1
|
31
|
+
short << k
|
32
|
+
long.pop
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def random
|
37
|
+
u = rand
|
38
|
+
j = (rand * length).to_i
|
39
|
+
if u <= prob[j]
|
40
|
+
keys[j]
|
41
|
+
else
|
42
|
+
keys[inx[j]]
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
describe WalkerMethod do
|
3
|
+
it "should work with two arrays" do
|
4
|
+
sampler = WalkerMethod.new(%w[hello world], [8, 2])
|
5
|
+
samples = Hash.new(0.0)
|
6
|
+
10_000.times do
|
7
|
+
samples[sampler.random] += 1
|
8
|
+
end
|
9
|
+
(samples["hello"] / samples["world"]).should be_within(0.2).of(4.0)
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'walker_method/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "walker_method"
|
8
|
+
gem.version = WalkerMethod::VERSION
|
9
|
+
gem.authors = ["Andrew Cantino"]
|
10
|
+
gem.email = ["cantino@gmail.com"]
|
11
|
+
gem.description = %q{Ruby implementation of Walker's Alias Method for quickly sampling objects with a given probability distribution}
|
12
|
+
gem.summary = %q{}
|
13
|
+
gem.homepage = "https://github.com/cantino/walker_method"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: walker_method
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Andrew Cantino
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-05 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Ruby implementation of Walker's Alias Method for quickly sampling objects
|
15
|
+
with a given probability distribution
|
16
|
+
email:
|
17
|
+
- cantino@gmail.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- lib/walker_method.rb
|
28
|
+
- lib/walker_method/version.rb
|
29
|
+
- spec/spec_helper.rb
|
30
|
+
- spec/walker_method_spec.rb
|
31
|
+
- walker_method.gemspec
|
32
|
+
homepage: https://github.com/cantino/walker_method
|
33
|
+
licenses: []
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
requirements: []
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.8.25
|
53
|
+
signing_key:
|
54
|
+
specification_version: 3
|
55
|
+
summary: ''
|
56
|
+
test_files:
|
57
|
+
- spec/spec_helper.rb
|
58
|
+
- spec/walker_method_spec.rb
|