vose 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +9 -0
- data/LICENSE +22 -0
- data/README.md +42 -0
- data/Rakefile +9 -0
- data/lib/vose/version.rb +3 -0
- data/lib/vose.rb +81 -0
- data/spec/spec_helper.rb +15 -0
- data/spec/vose_spec.rb +34 -0
- data/vose.gemspec +23 -0
- metadata +105 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Thunderbolt Labs, LLC.
|
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,42 @@
|
|
1
|
+
# Vose: Sample random values from a discrete probability distribution.
|
2
|
+
|
3
|
+
Vose is a Ruby implementation of the Vose Alias Method. It allows for
|
4
|
+
sampling of random values from a discrite probability distribution or in
|
5
|
+
others rolling a loaded die. This implementation is runs in O(1) time after
|
6
|
+
after a O(n) initialization.
|
7
|
+
|
8
|
+
For more information on the theory, have a look at [Darts, Dice, and Coins: Sampling from a Discrete Distribution](http://www.keithschwarz.com/darts-dice-coins/)
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
To track the latest officially released gem:
|
13
|
+
|
14
|
+
gem 'vose' # for the latest officially released gem
|
15
|
+
|
16
|
+
*OR* To track the git repo
|
17
|
+
|
18
|
+
gem 'vose', :git => "git://github.com/thunderboltlabs/vose.git"
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
|
22
|
+
$ bundle
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
$ gem install vose
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
probabilities = [0.4, 0.1, 0.2, 0.3]
|
31
|
+
vose = Vose::AliasMethod.new probalities
|
32
|
+
choice = vose.next
|
33
|
+
|
34
|
+
The choice will be an index of the probalities array.
|
35
|
+
|
36
|
+
## Contributing
|
37
|
+
|
38
|
+
1. Fork it
|
39
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
40
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
41
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
42
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/vose/version.rb
ADDED
data/lib/vose.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
require "vose/version"
|
2
|
+
|
3
|
+
module Vose
|
4
|
+
class InvalidArgumentException < RuntimeError; end;
|
5
|
+
|
6
|
+
class AliasMethod
|
7
|
+
attr_reader :limit
|
8
|
+
|
9
|
+
def initialize(probabilities)
|
10
|
+
raise InvalidArgumentException if probabilities.empty?
|
11
|
+
raise InvalidArgumentException if probabilities.any?{|p| p < 0}
|
12
|
+
|
13
|
+
@limit = probabilities.length
|
14
|
+
sum = probabilities.reduce(:+)
|
15
|
+
|
16
|
+
scale = limit / sum.to_f
|
17
|
+
scaled_probality = []
|
18
|
+
probabilities.each do |p|
|
19
|
+
scaled_probality << (p * scale)
|
20
|
+
end
|
21
|
+
|
22
|
+
@prob = Array.new(limit) { 0 }
|
23
|
+
@alias = Array.new(limit) { 0 }
|
24
|
+
|
25
|
+
preprocess(scaled_probality)
|
26
|
+
end
|
27
|
+
|
28
|
+
def preprocess(scaled_probality)
|
29
|
+
small_worklist = []
|
30
|
+
large_worklist = []
|
31
|
+
small_worklist_counter = 0
|
32
|
+
large_worklist_counter = 0
|
33
|
+
|
34
|
+
scaled_probality.each_with_index do |p,i|
|
35
|
+
if p > 1
|
36
|
+
large_worklist[large_worklist_counter] = i
|
37
|
+
large_worklist_counter+=1
|
38
|
+
else
|
39
|
+
small_worklist[small_worklist_counter] = i
|
40
|
+
small_worklist_counter+=1
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
while small_worklist_counter != 0 && large_worklist_counter != 0
|
45
|
+
small_worklist_counter-=1
|
46
|
+
large_worklist_counter-=1
|
47
|
+
current_small_worklist_index = small_worklist[small_worklist_counter].to_i
|
48
|
+
current_large_worklist_index = large_worklist[large_worklist_counter].to_i
|
49
|
+
|
50
|
+
@prob[current_small_worklist_index] = scaled_probality[current_small_worklist_index]
|
51
|
+
@alias[current_small_worklist_index] = current_large_worklist_index
|
52
|
+
|
53
|
+
scaled_probality[current_large_worklist_index] = (scaled_probality[current_large_worklist_index] + scaled_probality[current_small_worklist_index]) - 1
|
54
|
+
if scaled_probality[current_large_worklist_index] > 1
|
55
|
+
large_worklist[large_worklist_counter] = k
|
56
|
+
large_worklist_counter+=1
|
57
|
+
else
|
58
|
+
small_worklist[small_worklist_counter] = current_large_worklist_index
|
59
|
+
small_worklist_counter+=1
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
while small_worklist_counter != 0
|
64
|
+
small_worklist_counter-=1
|
65
|
+
@prob[small_worklist[small_worklist_counter]] = 1
|
66
|
+
end
|
67
|
+
|
68
|
+
while large_worklist_counter != 0
|
69
|
+
large_worklist_counter-=1
|
70
|
+
@prob[large_worklist[large_worklist_counter]] = 1
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def next
|
75
|
+
fair_die_roll = @limit * rand
|
76
|
+
i = fair_die_roll.floor
|
77
|
+
p = fair_die_roll - i
|
78
|
+
p <= @prob[i] ? i : @alias[i]
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
require 'minitest/spec'
|
3
|
+
require 'minitest/autorun'
|
4
|
+
require 'vose'
|
5
|
+
|
6
|
+
module MiniTest::Assertions
|
7
|
+
def assert_vose_probability(probabilities, results)
|
8
|
+
probabilities.each_with_index do |probablity, i|
|
9
|
+
percentage = results.count(i) / results.size.to_f
|
10
|
+
assert_in_delta probablity, percentage, 0.01
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
Array.infect_an_assertion :assert_vose_probability, :must_match_probability
|
data/spec/vose_spec.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Vose::AliasMethod do
|
4
|
+
it "complains if the list of probabilities is empty" do
|
5
|
+
probabilities = []
|
6
|
+
lambda {
|
7
|
+
Vose::AliasMethod.new probabilities
|
8
|
+
}.must_raise(Vose::InvalidArgumentException)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "complinas if the probabilities aren't positive" do
|
12
|
+
probabilities = [-0.5]
|
13
|
+
lambda {
|
14
|
+
Vose::AliasMethod.new probabilities
|
15
|
+
}.must_raise(Vose::InvalidArgumentException)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "sets the limit to the lenght of probabilities" do
|
19
|
+
probabilities = [0.1, 0.9]
|
20
|
+
vose = Vose::AliasMethod.new probabilities
|
21
|
+
vose.limit.must_equal 2
|
22
|
+
end
|
23
|
+
|
24
|
+
# Yes, this is a slow test. Consider it an acceptance
|
25
|
+
# level test :) Patches are welcome. Testing rand is hard or
|
26
|
+
# brittle imho.
|
27
|
+
it "allows you choose the next probablity given your inputs" do
|
28
|
+
probabilities = [0.1, 0.5, 0.4]
|
29
|
+
vose = Vose::AliasMethod.new probabilities
|
30
|
+
results = []
|
31
|
+
10000.times { results << vose.next }
|
32
|
+
results.must_match_probability probabilities
|
33
|
+
end
|
34
|
+
end
|
data/vose.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/vose/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["bryanl"]
|
6
|
+
gem.email = ["bryan@osesm.com"]
|
7
|
+
gem.description = %q{Vose: Sample random values from a discrete probability distribution.
|
8
|
+
}
|
9
|
+
gem.summary = %q{Vose is a Ruby implementation of the Vose Alias Method. It allows for sampling of random values from a discrite probability distribution or in others words, rolling a loaded die.}
|
10
|
+
gem.homepage = "https://github.com/thunderboltlabs/vose"
|
11
|
+
|
12
|
+
gem.files = `git ls-files`.split($\)
|
13
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
14
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
15
|
+
gem.name = "vose"
|
16
|
+
gem.require_paths = ["lib"]
|
17
|
+
gem.version = Vose::VERSION
|
18
|
+
|
19
|
+
gem.add_development_dependency("pry")
|
20
|
+
gem.add_development_dependency("ruby_gntp")
|
21
|
+
gem.add_development_dependency("guard-minitest")
|
22
|
+
gem.add_development_dependency("guard-bundler")
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: vose
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- bryanl
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-12 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: pry
|
16
|
+
requirement: &70195541301040 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70195541301040
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: ruby_gntp
|
27
|
+
requirement: &70195541300280 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70195541300280
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: guard-minitest
|
38
|
+
requirement: &70195541299780 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70195541299780
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: guard-bundler
|
49
|
+
requirement: &70195541299320 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70195541299320
|
58
|
+
description: ! 'Vose: Sample random values from a discrete probability distribution.
|
59
|
+
|
60
|
+
'
|
61
|
+
email:
|
62
|
+
- bryan@osesm.com
|
63
|
+
executables: []
|
64
|
+
extensions: []
|
65
|
+
extra_rdoc_files: []
|
66
|
+
files:
|
67
|
+
- .gitignore
|
68
|
+
- Gemfile
|
69
|
+
- LICENSE
|
70
|
+
- README.md
|
71
|
+
- Rakefile
|
72
|
+
- lib/vose.rb
|
73
|
+
- lib/vose/version.rb
|
74
|
+
- spec/spec_helper.rb
|
75
|
+
- spec/vose_spec.rb
|
76
|
+
- vose.gemspec
|
77
|
+
homepage: https://github.com/thunderboltlabs/vose
|
78
|
+
licenses: []
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ! '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ! '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 1.8.11
|
98
|
+
signing_key:
|
99
|
+
specification_version: 3
|
100
|
+
summary: Vose is a Ruby implementation of the Vose Alias Method. It allows for sampling
|
101
|
+
of random values from a discrite probability distribution or in others words, rolling
|
102
|
+
a loaded die.
|
103
|
+
test_files:
|
104
|
+
- spec/spec_helper.rb
|
105
|
+
- spec/vose_spec.rb
|