vose 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/vose.rb +9 -6
- data/lib/vose/version.rb +1 -1
- data/spec/vose_spec.rb +3 -3
- metadata +2 -2
data/lib/vose.rb
CHANGED
@@ -4,7 +4,7 @@ module Vose
|
|
4
4
|
class InvalidArgumentException < RuntimeError; end;
|
5
5
|
|
6
6
|
class AliasMethod
|
7
|
-
attr_reader :limit
|
7
|
+
attr_reader :limit
|
8
8
|
|
9
9
|
def initialize(probabilities)
|
10
10
|
raise InvalidArgumentException if probabilities.empty?
|
@@ -33,24 +33,27 @@ module Vose
|
|
33
33
|
|
34
34
|
0.upto(limit-1) do |j|
|
35
35
|
if scaled_probality[j] > 1
|
36
|
-
large_worklist[large_worklist_counter
|
36
|
+
large_worklist[large_worklist_counter] = j
|
37
|
+
large_worklist_counter+=1
|
37
38
|
else
|
38
|
-
small_worklist[small_worklist_counter
|
39
|
+
small_worklist[small_worklist_counter] = j
|
40
|
+
small_worklist_counter+=1
|
39
41
|
end
|
40
42
|
end
|
41
43
|
|
42
44
|
while small_worklist_counter != 0 && large_worklist_counter != 0
|
43
|
-
current_small_worklist_index = small_worklist[small_worklist_counter].to_i
|
44
|
-
current_large_worklist_index = large_worklist[large_worklist_counter].to_i
|
45
45
|
small_worklist_counter-=1
|
46
46
|
large_worklist_counter-=1
|
47
47
|
|
48
|
+
current_small_worklist_index = small_worklist[small_worklist_counter].to_i
|
49
|
+
current_large_worklist_index = large_worklist[large_worklist_counter].to_i
|
50
|
+
|
48
51
|
@prob[current_small_worklist_index] = scaled_probality[current_small_worklist_index]
|
49
52
|
@alias[current_small_worklist_index] = current_large_worklist_index
|
50
53
|
|
51
54
|
scaled_probality[current_large_worklist_index] = (scaled_probality[current_large_worklist_index] + scaled_probality[current_small_worklist_index]) - 1
|
52
55
|
if scaled_probality[current_large_worklist_index] > 1
|
53
|
-
large_worklist[large_worklist_counter] =
|
56
|
+
large_worklist[large_worklist_counter] = current_large_worklist_index
|
54
57
|
large_worklist_counter+=1
|
55
58
|
else
|
56
59
|
small_worklist[small_worklist_counter] = current_large_worklist_index
|
data/lib/vose/version.rb
CHANGED
data/spec/vose_spec.rb
CHANGED
@@ -8,14 +8,14 @@ describe Vose::AliasMethod do
|
|
8
8
|
}.must_raise(Vose::InvalidArgumentException)
|
9
9
|
end
|
10
10
|
|
11
|
-
it "
|
11
|
+
it "complains if the probabilities aren't positive" do
|
12
12
|
probabilities = [-0.5]
|
13
13
|
lambda {
|
14
14
|
Vose::AliasMethod.new probabilities
|
15
15
|
}.must_raise(Vose::InvalidArgumentException)
|
16
16
|
end
|
17
17
|
|
18
|
-
it "sets the limit to the
|
18
|
+
it "sets the limit to the length of probabilities" do
|
19
19
|
probabilities = [0.1, 0.9]
|
20
20
|
vose = Vose::AliasMethod.new probabilities
|
21
21
|
vose.limit.must_equal 2
|
@@ -36,7 +36,7 @@ describe Vose::AliasMethod do
|
|
36
36
|
results.must_match_probability expected_percentages
|
37
37
|
end
|
38
38
|
|
39
|
-
it "allows you choose the next
|
39
|
+
it "allows you choose the next probability given your inputs" do
|
40
40
|
probabilities = [0.1, 0.5, 0.4]
|
41
41
|
vose = Vose::AliasMethod.new probabilities
|
42
42
|
results = []
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vose
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-07-
|
12
|
+
date: 2012-07-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: pry
|