weighted_sample 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +3 -0
- data/LICENSE +22 -0
- data/README.md +35 -0
- data/Rakefile +2 -0
- data/lib/version.rb +3 -0
- data/lib/weighted_sample.rb +20 -0
- data/spec/weighted_sample_spec.rb +63 -0
- data/weighted_sample.gemspec +18 -0
- metadata +72 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 todesking
|
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,35 @@
|
|
1
|
+
# weighted_sample
|
2
|
+
|
3
|
+
This gem add Enumerable#weighted_sample_by mehtod.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'weighted_sample'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install weighted_sample
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
[1, 9].weighted_sample_by {|x| x}
|
22
|
+
=> 9(90%) or 1(10%)
|
23
|
+
|
24
|
+
### Restrictions
|
25
|
+
|
26
|
+
* Weight is zero or positive Integer(Negative or Float is not allowed).
|
27
|
+
* Sum of weights should > 0
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
1. Fork it
|
32
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
33
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
34
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
35
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/version.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
module Enumerable
|
2
|
+
def weighted_sample_by &block
|
3
|
+
return nil if self.empty?
|
4
|
+
|
5
|
+
weights = self.map(&block)
|
6
|
+
raise ArgumentError, 'Weight should Integer' unless weights.all? &Integer.method(:===)
|
7
|
+
|
8
|
+
total_weight = weights.reduce(:+)
|
9
|
+
raise ArgumentError, 'Sum of weights should > 0' unless total_weight > 0
|
10
|
+
|
11
|
+
r = rand(total_weight)
|
12
|
+
|
13
|
+
cumlative_weight = 0
|
14
|
+
self.zip(weights).each do|obj, weight|
|
15
|
+
cumlative_weight += weight
|
16
|
+
return obj if r < cumlative_weight
|
17
|
+
end
|
18
|
+
raise 'Should not reach here'
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
load File.join(File.dirname(__FILE__), '..', 'lib', 'weighted_sample.rb')
|
2
|
+
|
3
|
+
def samples(n, &block)
|
4
|
+
n.times.each_with_object(Hash.new{|_|0}) {|_,h|
|
5
|
+
sampled = block.call
|
6
|
+
h[sampled] += 1
|
7
|
+
}
|
8
|
+
end
|
9
|
+
|
10
|
+
# IDentity function
|
11
|
+
ID = ->(x){x}
|
12
|
+
|
13
|
+
describe 'Enumerable#weighted_sample_by' do
|
14
|
+
subject { [1, 9, 90] }
|
15
|
+
it 'returns its content value' do
|
16
|
+
subject.weighted_sample_by(&ID).should be_a(Integer)
|
17
|
+
subject.should be_include(subject.weighted_sample_by(&ID))
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'returns value with respect for its weight' do
|
21
|
+
s = samples(10000) { subject.weighted_sample_by(&ID) }
|
22
|
+
s[90].should be_within(5000).of(9000)
|
23
|
+
s[9].should be_within(500).of(900)
|
24
|
+
s[1].should be_within(50).of(100)
|
25
|
+
end
|
26
|
+
|
27
|
+
describe 'the Enumerable is empty' do
|
28
|
+
subject { [] }
|
29
|
+
it 'returns nil' do
|
30
|
+
subject.weighted_sample_by(&ID).should be_nil
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe 'weight is not Integer' do
|
35
|
+
subject { [0.1, 2] }
|
36
|
+
it 'raises ArgumentError' do
|
37
|
+
expect { subject.weighted_sample_by(&ID) }.to raise_error(ArgumentError)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe 'weight is negative' do
|
42
|
+
NEGATER = ->(x){-x}
|
43
|
+
it 'raises ArgumentError' do
|
44
|
+
expect { subject.weighted_sample_by(&NEGATER) }.to raise_error(ArgumentError)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe 'weight contains zero' do
|
49
|
+
subject { [0, 1, 0, 0, 1] }
|
50
|
+
it 'returns non-zero weighted element' do
|
51
|
+
10.times do
|
52
|
+
subject.weighted_sample_by(&ID).should == 1
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe 'weight is all zero' do
|
58
|
+
subject { [0, 0] }
|
59
|
+
it 'raises ArgumentError' do
|
60
|
+
expect { subject.weighted_sample_by(&ID) }.to raise_error(ArgumentError)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["todesking"]
|
6
|
+
gem.email = ["discommunicative@gmail.com"]
|
7
|
+
gem.summary = %q{This gem add Enumerable#weighted_sample_by method.}
|
8
|
+
gem.homepage = "https://github.com/todesking/weighted_sample"
|
9
|
+
|
10
|
+
gem.files = `git ls-files`.split($\)
|
11
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
12
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
13
|
+
gem.name = "weighted_sample"
|
14
|
+
gem.require_paths = ["lib"]
|
15
|
+
gem.version = WeightedSample::VERSION
|
16
|
+
|
17
|
+
gem.add_development_dependency 'rspec'
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: weighted_sample
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- todesking
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-21 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !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: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description:
|
31
|
+
email:
|
32
|
+
- discommunicative@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- lib/version.rb
|
43
|
+
- lib/weighted_sample.rb
|
44
|
+
- spec/weighted_sample_spec.rb
|
45
|
+
- weighted_sample.gemspec
|
46
|
+
homepage: https://github.com/todesking/weighted_sample
|
47
|
+
licenses: []
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
requirements: []
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 1.8.19
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: This gem add Enumerable#weighted_sample_by method.
|
70
|
+
test_files:
|
71
|
+
- spec/weighted_sample_spec.rb
|
72
|
+
has_rdoc:
|