roulette_wheel_selection 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in roulette_wheel_selection.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Yoshimoto
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.
@@ -0,0 +1,36 @@
1
+ # RouletteWheelSelection
2
+
3
+ Hash#sample method is implemented with roulette wheel selection algorithm. Probability distribution is given by hash values.
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'roulette_wheel_selection'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install roulette_wheel_selection
19
+
20
+ ## Usage
21
+
22
+ Set a hash for probability each keys. Hash#sample method returns random keys by probability distributions.
23
+
24
+ > { a: 50, b: 30, c: 20 }.sample
25
+ => :a # 50%
26
+ => :b # 30%
27
+ => :c # 20%
28
+
29
+
30
+ ## Contributing
31
+
32
+ 1. Fork it
33
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
34
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
35
+ 4. Push to the branch (`git push origin my-new-feature`)
36
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require "roulette_wheel_selection"
@@ -0,0 +1,3 @@
1
+ require "roulette_wheel_selection/version"
2
+ require "roulette_wheel_selection/array/roulette_wheel_selection"
3
+ require "roulette_wheel_selection/hash/sample"
@@ -0,0 +1,21 @@
1
+ class Array
2
+ def roulette_wheel_selection n=nil
3
+ return nil if self.empty? && n.nil?
4
+ return [] if self.empty?
5
+
6
+ n1 = n
7
+ n1 = 1 if n.nil?
8
+ n1 = n1.to_int
9
+ rescue Exception => e
10
+ raise TypeError, "Coercion error: #{n.inspect}.to_int => Integer failed:\n(#{e.message})"
11
+ else
12
+ cumulative = self.inject( [0] ) { |s,i| s << s.last + i.to_i }
13
+ cumulative.shift
14
+ result = Array.new(n1).map! {
15
+ r = rand( cumulative.last )
16
+ cumulative.find_index { |t| r < t }
17
+ }
18
+ return result.first if n.nil?
19
+ result
20
+ end
21
+ end
@@ -0,0 +1,10 @@
1
+ class Hash
2
+ def roulette_wheel_selection n=nil
3
+ indexes = self.values.roulette_wheel_selection n
4
+
5
+ return nil if indexes.nil?
6
+ return self.keys[ indexes ] unless Array === indexes
7
+ self.keys.values_at *indexes
8
+ end
9
+ alias :sample :roulette_wheel_selection
10
+ end
@@ -0,0 +1,3 @@
1
+ module RouletteWheelSelection
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/roulette_wheel_selection/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Beyond"]
6
+ gem.email = ["beyond@be.to"]
7
+ gem.description = %q{ Hash#sample method is implemented with roulette wheel selection algorithm. Probability distribution is given by hash values. }
8
+ gem.summary = %q{ Hash#sample method implementation like Array#sample }
9
+ gem.homepage = "https://github.com/beyond/roulette_wheel_selection"
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 = "roulette_wheel_selection"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = RouletteWheelSelection::VERSION
17
+
18
+ gem.add_development_dependency "rspec"
19
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ describe Array do
4
+ describe "#roulette_wheel_selection" do
5
+ context "empty" do
6
+ subject { Array.new }
7
+
8
+ it do
9
+ subject.roulette_wheel_selection.should == nil
10
+ end
11
+
12
+ it do
13
+ subject.roulette_wheel_selection(1).should == []
14
+ end
15
+
16
+ it do
17
+ subject.roulette_wheel_selection(5).should == []
18
+ end
19
+ end
20
+
21
+ context "one value" do
22
+ subject { [10] }
23
+
24
+ it do
25
+ subject.roulette_wheel_selection.should == 0
26
+ end
27
+
28
+ it do
29
+ subject.roulette_wheel_selection(1).should == [0]
30
+ end
31
+
32
+ it do
33
+ subject.roulette_wheel_selection(2).should == [ 0, 0 ]
34
+ end
35
+
36
+ it do
37
+ o = Object.new
38
+ def o.to_int; 1; end
39
+ subject.roulette_wheel_selection(o).should == [0]
40
+ end
41
+
42
+ it do
43
+ o = Object.new
44
+ lambda { subject.roulette_wheel_selection(o) }.should raise_error TypeError
45
+ end
46
+
47
+ it do
48
+ o = Object.new
49
+ def o.to_int; ''; end
50
+ lambda { subject.roulette_wheel_selection(o) }.should raise_error TypeError
51
+ end
52
+
53
+ it do
54
+ lambda { subject.roulette_wheel_selection -7 }.should raise_error ArgumentError
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,92 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hash do
4
+ describe "#sample" do
5
+ context "empty hash" do
6
+ subject { Hash.new }
7
+
8
+ it do
9
+ subject.sample.should == nil
10
+ end
11
+
12
+ it do
13
+ subject.sample(1).should == []
14
+ end
15
+
16
+ it do
17
+ subject.sample(5).should == []
18
+ end
19
+ end
20
+
21
+ context "one key" do
22
+ subject { { a: 10 } }
23
+
24
+ it do
25
+ subject.sample.should == :a
26
+ end
27
+
28
+ it do
29
+ subject.sample(1).should == [:a]
30
+ end
31
+
32
+ it do
33
+ subject.sample(2).should == [ :a, :a ]
34
+ end
35
+
36
+ it do
37
+ o = Object.new
38
+ def o.to_int; 1; end
39
+ subject.sample(o).should == [:a]
40
+ end
41
+
42
+ it do
43
+ o = Object.new
44
+ lambda { subject.sample(o) }.should raise_error TypeError
45
+ end
46
+
47
+ it do
48
+ o = Object.new
49
+ def o.to_int; ''; end
50
+ lambda { subject.sample(o) }.should raise_error TypeError
51
+ end
52
+
53
+ it do
54
+ lambda { subject.sample -7 }.should raise_error ArgumentError
55
+ end
56
+ end
57
+
58
+ context "Chi-square test" do
59
+ before do
60
+ @h = { a: 10, b: 10, c: 25, d: 1, e: 2, f: 2, g: 5, h: 10, i: 20, j: 15 }
61
+ end
62
+
63
+ def calc_chi2 dist, excepted_freq
64
+ excepted_freq.inject( 0.0 ) { |r,x|
65
+ k,v = *x
66
+ r += ((dist[k] - v) ** 2).to_f / v
67
+ r
68
+ }
69
+ end
70
+
71
+ it "should be same probability" do
72
+ chi2_9 = [
73
+ 14.684, 12.242, 10.656, 9.414, 8.343,
74
+ 7.357, 6.393, 5.380, 4.168, 0.000,
75
+ ]
76
+
77
+ f1 = Hash[ @h.map { |k,x| [ k, 1.0 * x / @h.values.inject(&:+) * 1000 ] } ]
78
+ count = Hash.new(0)
79
+ 2000.times {
80
+ stats = @h.sample(1000).inject( Hash.new(0) ) { |h,k| h[k]+=1; h }
81
+ chi2 = calc_chi2 stats, f1
82
+ i = chi2_9.find_index { |x| chi2 > x }
83
+ count[i] += 1
84
+ }
85
+ f2 = Hash[ (0..9).map { |x| [ x, 200 ] } ]
86
+
87
+ chi2 = calc_chi2 count, f2
88
+ chi2.should < 21.666 # 1%
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,19 @@
1
+ require File.expand_path("../init", File.dirname(__FILE__))
2
+
3
+ # This file was generated by the `rspec --init` command. Conventionally, all
4
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
5
+ # Require this file using `require "spec_helper"` to ensure that it is only
6
+ # loaded once.
7
+ #
8
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
9
+ RSpec.configure do |config|
10
+ config.treat_symbols_as_metadata_keys_with_true_values = true
11
+ config.run_all_when_everything_filtered = true
12
+ config.filter_run :focus
13
+
14
+ # Run specs in random order to surface order dependencies. If you find an
15
+ # order dependency and want to debug it, you can fix the order by providing
16
+ # the seed, which is printed after each run.
17
+ # --seed 1234
18
+ config.order = 'random'
19
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: roulette_wheel_selection
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Beyond
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &9862680 !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: *9862680
25
+ description: ! ' Hash#sample method is implemented with roulette wheel selection algorithm.
26
+ Probability distribution is given by hash values. '
27
+ email:
28
+ - beyond@be.to
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - .gitignore
34
+ - .rspec
35
+ - Gemfile
36
+ - LICENSE
37
+ - README.md
38
+ - Rakefile
39
+ - init.rb
40
+ - lib/roulette_wheel_selection.rb
41
+ - lib/roulette_wheel_selection/array/roulette_wheel_selection.rb
42
+ - lib/roulette_wheel_selection/hash/sample.rb
43
+ - lib/roulette_wheel_selection/version.rb
44
+ - roulette_wheel_selection.gemspec
45
+ - spec/array/roulette_wheel_selection_spec.rb
46
+ - spec/hash/sample_spec.rb
47
+ - spec/spec_helper.rb
48
+ homepage: https://github.com/beyond/roulette_wheel_selection
49
+ licenses: []
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 1.8.10
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: Hash#sample method implementation like Array#sample
72
+ test_files:
73
+ - spec/array/roulette_wheel_selection_spec.rb
74
+ - spec/hash/sample_spec.rb
75
+ - spec/spec_helper.rb