random_value_sampler 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +100 -0
- data/Rakefile +7 -0
- data/lib/random_value_sampler/version.rb +3 -0
- data/lib/random_value_sampler.rb +4 -3
- data/random_value_sampler.gemspec +18 -0
- data/test/.svn/entries +62 -0
- data/test/.svn/text-base/random_value_sampler_test.rb.svn-base +890 -0
- data/test/random_value_sampler_test.rb +2 -5
- metadata +52 -38
- data/README +0 -79
@@ -1,6 +1,5 @@
|
|
1
1
|
require 'set'
|
2
2
|
require 'test/unit'
|
3
|
-
|
4
3
|
require 'random_value_sampler'
|
5
4
|
|
6
5
|
#
|
@@ -138,11 +137,9 @@ class RandomValueSamplerTest < Test::Unit::TestCase
|
|
138
137
|
end
|
139
138
|
end
|
140
139
|
|
141
|
-
def
|
140
|
+
def test_negative_num_samples
|
142
141
|
assert_raises(RuntimeError) { RandomValueSampler.new_uniform([1,2,3,4]).sample(-1) }
|
143
142
|
assert_raises(RuntimeError) { RandomValueSampler.new_uniform([1,2,3,4]).sample_unique(-1) }
|
144
|
-
assert_raises(RuntimeError) { RandomValueSampler.new_uniform([1,2,3,4]).sample(0) }
|
145
|
-
assert_raises(RuntimeError) { RandomValueSampler.new_uniform([1,2,3,4]).sample_unique(0) }
|
146
143
|
end
|
147
144
|
|
148
145
|
###################################################
|
@@ -780,7 +777,7 @@ class RandomValueSamplerTest < Test::Unit::TestCase
|
|
780
777
|
|
781
778
|
# valid inputs
|
782
779
|
|
783
|
-
@uniform_set_single_string = Set.new("one")
|
780
|
+
@uniform_set_single_string = Set.new(["one"])
|
784
781
|
@uniform_set_10_string = Set.new(array_of_ten_string)
|
785
782
|
@uniform_set_10_numeric = Set.new(3..12)
|
786
783
|
|
metadata
CHANGED
@@ -1,57 +1,71 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: random_value_sampler
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.3
|
5
|
+
prerelease:
|
5
6
|
platform: ruby
|
6
|
-
authors:
|
7
|
-
-
|
7
|
+
authors:
|
8
|
+
- Brian Percival
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
12
|
+
date: 2012-05-01 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: &2165317380 !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: *2165317380
|
25
|
+
description: Class for sampling from arbitrary probability distributions
|
26
|
+
email:
|
27
|
+
- bpercival@goodreads.com
|
18
28
|
executables: []
|
19
|
-
|
20
29
|
extensions: []
|
21
|
-
|
22
30
|
extra_rdoc_files: []
|
23
|
-
|
24
|
-
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- LICENSE
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
25
37
|
- lib/random_value_sampler.rb
|
26
|
-
-
|
38
|
+
- lib/random_value_sampler/version.rb
|
39
|
+
- random_value_sampler.gemspec
|
40
|
+
- test/.svn/entries
|
41
|
+
- test/.svn/text-base/random_value_sampler_test.rb.svn-base
|
27
42
|
- test/random_value_sampler_test.rb
|
28
|
-
|
29
|
-
homepage:
|
43
|
+
homepage: https://github.com/bmpercy/random_value_sampler
|
30
44
|
licenses: []
|
31
|
-
|
32
45
|
post_install_message:
|
33
46
|
rdoc_options: []
|
34
|
-
|
35
|
-
require_paths:
|
47
|
+
require_paths:
|
36
48
|
- lib
|
37
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
49
61
|
requirements: []
|
50
|
-
|
51
62
|
rubyforge_project:
|
52
|
-
rubygems_version: 1.
|
63
|
+
rubygems_version: 1.8.15
|
53
64
|
signing_key:
|
54
65
|
specification_version: 3
|
55
|
-
summary: Class for sampling from arbitrary probability distributions
|
56
|
-
|
66
|
+
summary: Class for sampling from arbitrary probability distributions, particular discrete
|
67
|
+
random variables with lookup-table-like PMFs
|
68
|
+
test_files:
|
69
|
+
- test/.svn/entries
|
70
|
+
- test/.svn/text-base/random_value_sampler_test.rb.svn-base
|
57
71
|
- test/random_value_sampler_test.rb
|
data/README
DELETED
@@ -1,79 +0,0 @@
|
|
1
|
-
RandomValueSampler
|
2
|
-
========
|
3
|
-
|
4
|
-
Class to allow sampling from very, very simple probability mass functions
|
5
|
-
(uniform and arbitrary non-uniform). Values can be any object;
|
6
|
-
for uniform distributions, a Range can be used to specify a range of
|
7
|
-
discrete values.
|
8
|
-
|
9
|
-
To specify a uniform distribution, only the values need to be specified, and
|
10
|
-
can be:
|
11
|
-
- an Array of values (it is assumed the values are distinct, but you may
|
12
|
-
insert duplicates if you know what you're doing and realize you're probably
|
13
|
-
no longer dealing with a truly uniform distribution anymore (but this could
|
14
|
-
be used to "cheat" to generate distributions that are 'nearly' uniform where
|
15
|
-
probability mass is quantized (e.g. a 1/3, 2/3 distribution). This may
|
16
|
-
prove to be a more efficient implementation in such cases as the non-uniform
|
17
|
-
pmf is more computationally demanding).
|
18
|
-
- a ruby Range object; RandomValueSampler honors the inclusion/exclusion of last/end
|
19
|
-
of the Range (as defined by exclude_end? method). the Range must be of
|
20
|
-
numeric type unless you REALLY know what you're doing (e.g. the Xs class
|
21
|
-
example in the Range rdoc won't work).
|
22
|
-
- a single numeric type specifying an upper bound (zero is assumed as
|
23
|
-
lower bound--both zero and upper bound are included in distribution)
|
24
|
-
|
25
|
-
To specify a non-uniform distribution, the values and probability mass
|
26
|
-
must be specified. It is not necessary for the probability mass to
|
27
|
-
represent a true probability distribution (needn't sum to 1), as the class
|
28
|
-
will normalize accordingly. The pmf may be specified as a Hash or an Array:
|
29
|
-
- Hash, where the hash keys are the possible values the random variable
|
30
|
-
can take on; the hash values are the 'frequency counts' or non-normalized
|
31
|
-
probability mass
|
32
|
-
- Array, each element of which is a two-element array. each two element
|
33
|
-
array's first element is the value; the last element is the frequency
|
34
|
-
count for that value
|
35
|
-
|
36
|
-
Examples
|
37
|
-
=========
|
38
|
-
|
39
|
-
require 'random_value_sampler'
|
40
|
-
|
41
|
-
uniform
|
42
|
-
-------
|
43
|
-
|
44
|
-
# generate a uniform pmf over [1,5]
|
45
|
-
a = RandomValueSampler.new_uniform([1,2,3,4,5])
|
46
|
-
|
47
|
-
# generate a uniform pmf over some words
|
48
|
-
a = RandomValueSampler.new_uniform(["one", "two", "buckle", "my", "shoe"])
|
49
|
-
|
50
|
-
# generate a 'quantized' pmf by using duplicate entries
|
51
|
-
a = RandomValueSampler.new_uniform([1, 2, 2, 3, 3, 3])
|
52
|
-
a = RandomValueSampler.new_uniform(["the", "the", "a", "the", "and", "zyzzyva"])
|
53
|
-
|
54
|
-
# generate a uniform pmf over [1,5] using a Range
|
55
|
-
a = RandomValueSampler.new_uniform(1..5)
|
56
|
-
a = RandomValueSampler.new_uniform(1...6)
|
57
|
-
|
58
|
-
# generate a uniform pmf over [0,5] by specifying upper limit
|
59
|
-
a = RandomValueSampler.new_uniform(5)
|
60
|
-
|
61
|
-
non-uniform
|
62
|
-
-----------
|
63
|
-
|
64
|
-
# generate a non-uniform pmf using the Hash form:
|
65
|
-
|
66
|
-
# values are 5 and 10, with probability 0.4 and 0.6, respectively
|
67
|
-
a = RandomValueSampler.new_non_uniform( { 5 => 20, 10 => 30 } )
|
68
|
-
|
69
|
-
# values are "probable", "possible" and "not likely" with probability
|
70
|
-
# 0.75, 0.20 and 0.05, respectively.
|
71
|
-
a = RandomValueSampler.new_non_uniform( { "probable" => 75,
|
72
|
-
"possible" => 20,
|
73
|
-
"not likely" => 5 } )
|
74
|
-
|
75
|
-
# generate a non-uniform pmf using the Array form (same examples as above)
|
76
|
-
a = RandomValueSampler.new_non_uniform( [ [5,20], [10,30] )
|
77
|
-
a = RandomValueSampler.new_non_uniform( [ ["probable",75],
|
78
|
-
["possible" => 20],
|
79
|
-
["not likely" => 5 ] ] )
|