aliastable 3.0.3 → 3.1.0
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.
- checksums.yaml +5 -5
- data/aliastable.gemspec +5 -4
- data/lib/aliastable.rb +2 -4
- data/test/infile.good.3 +2 -2
- data/test/infile.good.4 +3 -0
- data/test/test_alias.rb +3 -1
- metadata +7 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: daff6ffd485a6eabe1a2341625be2ffd7b43e821b66d6b0a50363f5822f22cdd
|
4
|
+
data.tar.gz: cffa205ec028890811239ab49dfaf36111d53c749a8b90971fd658da5ad0fe5c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f227b5d8d874414e8ff7ab1dea7295cf1cd212f9a7cdebd8df90cb2e74ff95ff07bda966df03ccc205abdf13397990de16988ccc011d9a025ab2964ac3e08a5e
|
7
|
+
data.tar.gz: 4b2742b75bc5e1d8c089bcf653bb7b37c2c0a6757a754d97cace15b1b13a1dc2744dbc294641bf4790d238a7e9832d3f01c031d0f74fad043086fef251cb1148
|
data/aliastable.gemspec
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
# -*- ruby -*-
|
2
|
-
_VERSION = "3.0
|
2
|
+
_VERSION = "3.1.0"
|
3
3
|
|
4
4
|
Gem::Specification.new do |s|
|
5
5
|
s.name = "aliastable"
|
6
6
|
s.version = _VERSION
|
7
|
-
s.date = "
|
7
|
+
s.date = "2018-01-16"
|
8
8
|
s.summary = "Efficiently generate random outcomes from an arbitrary categorical distribution."
|
9
9
|
s.email = "pjs@alum.mit.edu"
|
10
|
-
s.description = "If a categorical distribution has k distinct values, traditional approaches will require O(k) work to pick an outcome with the correct probabilities. This algorithm uses conditional probability to construct a table which will yield outcomes with the correct probabilities, but in O(1) time."
|
10
|
+
s.description = "If a categorical distribution has k distinct values, traditional approaches will require O(k) work to pick an outcome with the correct probabilities. This algorithm uses conditional probability to construct a table which will yield outcomes with the correct probabilities. Table generation requires O(k) time, but subsequent generation is done in O(1) time."
|
11
11
|
s.author = "Paul J Sanchez"
|
12
12
|
s.files = %w[
|
13
13
|
aliastable.gemspec
|
@@ -19,8 +19,9 @@ Gem::Specification.new do |s|
|
|
19
19
|
test/infile.good.1
|
20
20
|
test/infile.good.2
|
21
21
|
test/infile.good.3
|
22
|
+
test/infile.good.4
|
22
23
|
test/test_alias.rb
|
23
24
|
]
|
24
|
-
s.required_ruby_version = '>=
|
25
|
+
s.required_ruby_version = '>= 2.0.0'
|
25
26
|
s.license = 'LGPL'
|
26
27
|
end
|
data/lib/aliastable.rb
CHANGED
@@ -26,11 +26,9 @@ class AliasTable
|
|
26
26
|
# - RuntimeError if +p_value+s don't sum to one. Rationals will avoid this.
|
27
27
|
#
|
28
28
|
def initialize(x_set, p_value)
|
29
|
-
if x_set.
|
30
|
-
fail 'Args to AliasTable must be vectors of the same length.'
|
31
|
-
end
|
29
|
+
fail 'x_set & p_value must have same length.' if x_set.size != p_value.size
|
32
30
|
fail 'p_values must be positive' unless p_value.all? { |value| value > 0 }
|
33
|
-
@p_primary = p_value.map(&:
|
31
|
+
@p_primary = p_value.map(&:rationalize)
|
34
32
|
fail 'p_values must sum to 1' unless @p_primary.reduce(:+) == Rational(1)
|
35
33
|
@x = x_set.clone.freeze
|
36
34
|
@alias = Array.new(@x.length)
|
data/test/infile.good.3
CHANGED
data/test/infile.good.4
ADDED
data/test/test_alias.rb
CHANGED
@@ -20,12 +20,14 @@ Dir['test/infile.*'].each do |f_name|
|
|
20
20
|
inputs = line.strip.split(/[\s,;:]+/)
|
21
21
|
x << inputs[0]
|
22
22
|
counts[inputs[0]] = 0
|
23
|
-
probs << inputs[1]
|
23
|
+
probs << Rational(inputs[1])
|
24
24
|
n_hat = probs[-1] * nvars
|
25
25
|
half_width = 2.5 * Math.sqrt(n_hat * (1.0 - probs[-1])) if n_hat > 0
|
26
26
|
expected_counts[inputs[0]] = [n_hat, half_width]
|
27
27
|
end
|
28
28
|
f.close
|
29
|
+
p x
|
30
|
+
p probs
|
29
31
|
begin
|
30
32
|
at = AliasTable.new(x, probs)
|
31
33
|
nvars.times { counts[at.generate] += 1 }
|
metadata
CHANGED
@@ -1,19 +1,20 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: aliastable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0
|
4
|
+
version: 3.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paul J Sanchez
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-01-16 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: If a categorical distribution has k distinct values, traditional approaches
|
14
14
|
will require O(k) work to pick an outcome with the correct probabilities. This
|
15
15
|
algorithm uses conditional probability to construct a table which will yield outcomes
|
16
|
-
with the correct probabilities
|
16
|
+
with the correct probabilities. Table generation requires O(k) time, but subsequent
|
17
|
+
generation is done in O(1) time.
|
17
18
|
email: pjs@alum.mit.edu
|
18
19
|
executables: []
|
19
20
|
extensions: []
|
@@ -28,6 +29,7 @@ files:
|
|
28
29
|
- test/infile.good.1
|
29
30
|
- test/infile.good.2
|
30
31
|
- test/infile.good.3
|
32
|
+
- test/infile.good.4
|
31
33
|
- test/test_alias.rb
|
32
34
|
homepage:
|
33
35
|
licenses:
|
@@ -41,7 +43,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
41
43
|
requirements:
|
42
44
|
- - ">="
|
43
45
|
- !ruby/object:Gem::Version
|
44
|
-
version:
|
46
|
+
version: 2.0.0
|
45
47
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
48
|
requirements:
|
47
49
|
- - ">="
|
@@ -49,7 +51,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
49
51
|
version: '0'
|
50
52
|
requirements: []
|
51
53
|
rubyforge_project:
|
52
|
-
rubygems_version: 2.4
|
54
|
+
rubygems_version: 2.7.4
|
53
55
|
signing_key:
|
54
56
|
specification_version: 4
|
55
57
|
summary: Efficiently generate random outcomes from an arbitrary categorical distribution.
|