aliastable 3.0.3 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 64997be896934a0e70ba0c4776e26f08a68fae50
4
- data.tar.gz: 7e7d8a38a21f8355be9c0e83559fb3e5689ea556
2
+ SHA256:
3
+ metadata.gz: daff6ffd485a6eabe1a2341625be2ffd7b43e821b66d6b0a50363f5822f22cdd
4
+ data.tar.gz: cffa205ec028890811239ab49dfaf36111d53c749a8b90971fd658da5ad0fe5c
5
5
  SHA512:
6
- metadata.gz: e49595d9a6da320b2e22db5672f5fc4546a4919edc3915dbd6e9b1cec69beff947b16006f76a55727c147946dd08cec4f2aeaad4be31df6e9a606de27190ed6d
7
- data.tar.gz: f696a2b2f0b5de5eb7b9932f4a121f0b1e94e988284eb0123e79c45bef8e59dee1c3b089ab5e24ddd448ab17e22acfe367cbb52e8b3130e7745307c3bd4ae638
6
+ metadata.gz: f227b5d8d874414e8ff7ab1dea7295cf1cd212f9a7cdebd8df90cb2e74ff95ff07bda966df03ccc205abdf13397990de16988ccc011d9a025ab2964ac3e08a5e
7
+ data.tar.gz: 4b2742b75bc5e1d8c089bcf653bb7b37c2c0a6757a754d97cace15b1b13a1dc2744dbc294641bf4790d238a7e9832d3f01c031d0f74fad043086fef251cb1148
@@ -1,13 +1,13 @@
1
1
  # -*- ruby -*-
2
- _VERSION = "3.0.3"
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 = "2015-09-10"
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 = '>= 1.9.3'
25
+ s.required_ruby_version = '>= 2.0.0'
25
26
  s.license = 'LGPL'
26
27
  end
@@ -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.length != p_value.length
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(&:to_r)
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)
@@ -8,7 +8,7 @@
8
8
  8,8/253
9
9
  9,9/253
10
10
  10,10/253
11
- 11,1/23
11
+ 11,11/253
12
12
  12,12/253
13
13
  13,13/253
14
14
  14,14/253
@@ -19,4 +19,4 @@
19
19
  19,19/253
20
20
  20,20/253
21
21
  21,21/253
22
- 22,2/23
22
+ 22,22/253
@@ -0,0 +1,3 @@
1
+ 1,0.333333
2
+ 3,0.333333
3
+ 7,0.333334
@@ -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].to_r
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.3
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: 2015-09-10 00:00:00.000000000 Z
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, but in O(1) time.
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: 1.9.3
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.5.1
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.