random_value_sampler 0.1.2 → 0.1.3

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/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in random_value_sampler.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Brian Percival
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,100 @@
1
+ # RandomValueSampler
2
+
3
+ Class to allow sampling from very, very simple probability mass functions
4
+ (uniform and arbitrary non-uniform). Values can be any object;
5
+ for uniform distributions, a Range can be used to specify a range of
6
+ discrete values.
7
+
8
+ To specify a uniform distribution, only the values need to be specified, and
9
+ can be:
10
+ - an Array of values (it is assumed the values are distinct, but you may
11
+ insert duplicates if you know what you're doing and realize you're probably
12
+ no longer dealing with a truly uniform distribution anymore (but this could
13
+ be used to "cheat" to generate distributions that are 'nearly' uniform where
14
+ probability mass is quantized (e.g. a 1/3, 2/3 distribution). This may
15
+ prove to be a more efficient implementation in such cases as the non-uniform
16
+ pmf is more computationally demanding).
17
+ - a ruby Range object; RandomValueSampler honors the inclusion/exclusion of last/end
18
+ of the Range (as defined by exclude_end? method). the Range must be of
19
+ numeric type unless you REALLY know what you're doing (e.g. the Xs class
20
+ example in the Range rdoc won't work).
21
+ - a single numeric type specifying an upper bound (zero is assumed as
22
+ lower bound--both zero and upper bound are included in distribution)
23
+
24
+ To specify a non-uniform distribution, the values and probability mass
25
+ must be specified. It is not necessary for the probability mass to
26
+ represent a true probability distribution (needn't sum to 1), as the class
27
+ will normalize accordingly. The pmf may be specified as a Hash or an Array:
28
+ - Hash, where the hash keys are the possible values the random variable
29
+ can take on; the hash values are the 'frequency counts' or non-normalized
30
+ probability mass
31
+ - Array, each element of which is a two-element array. each two element
32
+ array's first element is the value; the last element is the frequency
33
+ count for that value
34
+
35
+ ## Installation
36
+
37
+ Add this line to your application's Gemfile:
38
+
39
+ gem 'random_value_sampler'
40
+
41
+ And then execute:
42
+
43
+ $ bundle
44
+
45
+ Or install it yourself as:
46
+
47
+ $ gem install random_value_sampler
48
+
49
+ ## Usage
50
+
51
+ require 'random_value_sampler'
52
+
53
+ uniform
54
+
55
+ # generate a uniform pmf over [1,5]
56
+ a = RandomValueSampler.new_uniform([1,2,3,4,5])
57
+
58
+ # generate a uniform pmf over some words
59
+ a = RandomValueSampler.new_uniform(["one", "two", "buckle", "my", "shoe"])
60
+
61
+ # generate a 'quantized' pmf by using duplicate entries
62
+ a = RandomValueSampler.new_uniform([1, 2, 2, 3, 3, 3])
63
+ a = RandomValueSampler.new_uniform(["the", "the", "a", "the", "and", "zyzzyva"])
64
+
65
+ # generate a uniform pmf over [1,5] using a Range
66
+ a = RandomValueSampler.new_uniform(1..5)
67
+ a = RandomValueSampler.new_uniform(1...6)
68
+
69
+ # generate a uniform pmf over [0,5] by specifying upper limit
70
+ a = RandomValueSampler.new_uniform(5)
71
+
72
+ non-uniform
73
+
74
+ # generate a non-uniform pmf using the Hash form:
75
+
76
+ # values are 5 and 10, with probability 0.4 and 0.6, respectively
77
+ a = RandomValueSampler.new_non_uniform( { 5 => 20, 10 => 30 } )
78
+
79
+ # values are "probable", "possible" and "not likely" with probability
80
+ # 0.75, 0.20 and 0.05, respectively.
81
+ a = RandomValueSampler.new_non_uniform( { "probable" => 75,
82
+ "possible" => 20,
83
+ "not likely" => 5 } )
84
+
85
+ # generate a non-uniform pmf using the Array form (same examples as above)
86
+ a = RandomValueSampler.new_non_uniform( [ [5,20], [10,30] )
87
+ a = RandomValueSampler.new_non_uniform( [ ["probable",75],
88
+ ["possible" => 20],
89
+ ["not likely" => 5 ] ] )
90
+
91
+ ## Contributing
92
+
93
+ 1. Fork it
94
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
95
+ 3. Write new tests and test:
96
+ bundle exec rake test
97
+ (NOTE: if you add new test files, please clean up the test rake test...it's a hack right now)
98
+ 4. Commit your changes (`git commit -am 'Added some feature'`)
99
+ 5. Push to the branch (`git push origin my-new-feature`)
100
+ 6. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ desc "run the unit tests"
5
+ task :test do
6
+ require File.join(File.dirname(File.expand_path(__FILE__)), 'test/random_value_sampler_test')
7
+ end
@@ -0,0 +1,3 @@
1
+ class RandomValueSampler
2
+ VERSION = "0.1.3"
3
+ end
@@ -1,3 +1,5 @@
1
+ require File.join(File.dirname(File.expand_path(__FILE__)), 'random_value_sampler/version')
2
+
1
3
  # simple class for generating and sampling from a probability distribution,
2
4
  # including implementation of sampling from uniform and arbitrary distributions
3
5
  # on discrete random variables, by passing in an object that represents
@@ -40,7 +42,6 @@
40
42
  # the sample_unique() method will likely run faster.
41
43
  #-------------------------------------------------------------------------------
42
44
  class RandomValueSampler
43
-
44
45
  # instantiate RandomValueSampler given a probability_function object. the
45
46
  # object must respond to:
46
47
  # - sample_from_distribution -> single value sampled from distribution
@@ -235,8 +236,8 @@ class RandomValueSampler
235
236
  end
236
237
 
237
238
  @num_values = vals.length
238
- @values = vals
239
- else
239
+ @values = vals.clone
240
+ else # (Range)
240
241
  @num_values = vals.last - vals.first + (vals.exclude_end? ? 0 : 1)
241
242
  @values = vals
242
243
 
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.join(File.dirname(File.expand_path(__FILE__)), 'lib/random_value_sampler/version')
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Brian Percival"]
6
+ gem.email = ["bpercival@goodreads.com"]
7
+ gem.description = %q{Class for sampling from arbitrary probability distributions}
8
+ gem.summary = %q{Class for sampling from arbitrary probability distributions, particular discrete random variables with lookup-table-like PMFs}
9
+ gem.homepage = "https://github.com/bmpercy/random_value_sampler"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "random_value_sampler"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = RandomValueSampler::VERSION
17
+ gem.add_development_dependency 'rake'
18
+ end
data/test/.svn/entries ADDED
@@ -0,0 +1,62 @@
1
+ 10
2
+
3
+ dir
4
+ 16712
5
+ svn+ssh://bpercival@svn/home/svn/repository/discovereads_archive/code/gems/random_value_sampler/test
6
+ svn+ssh://bpercival@svn/home/svn/repository
7
+
8
+
9
+
10
+ 2011-03-18T22:57:57.847457Z
11
+ 16598
12
+ bpercival
13
+
14
+
15
+
16
+
17
+
18
+
19
+
20
+
21
+
22
+
23
+
24
+
25
+
26
+
27
+ b7929305-2822-0410-b81b-d5ebcc0c7c9f
28
+
29
+ random_value_sampler_test.rb
30
+ file
31
+
32
+
33
+
34
+
35
+ 2011-03-23T18:17:45.000000Z
36
+ 57871a1c82580fe89b7e24f39c979bb4
37
+ 2011-03-18T22:57:57.847457Z
38
+ 16598
39
+ bpercival
40
+
41
+
42
+
43
+
44
+
45
+
46
+
47
+
48
+
49
+
50
+
51
+
52
+
53
+
54
+
55
+
56
+
57
+
58
+
59
+
60
+
61
+ 42257
62
+