weighted_sampler 1.0.1 → 1.0.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 484d62fd78bfc61f3f7955dba454d3ebbbe836ca2f05d6cf62c4a8b347b17760
4
- data.tar.gz: 744ec53a149444fb29f0ea5a7b4399848726d361bfca04ad9cd710bb7a61fe68
3
+ metadata.gz: 158c6fdf6276d125dceac092e5d11517f69145f1d729eeffadb253839abe3ea5
4
+ data.tar.gz: 6b292492376053962087caa4df673926c038ac9825cc6738a13c9343ead610d6
5
5
  SHA512:
6
- metadata.gz: '0818c61256774eebbc72865028b4ba6e975d044a72f669f5120da8cbddb776d06af5534be4ca5d23c2329792a01bde97155b6e617eadfe2ff0115dbe13c16714'
7
- data.tar.gz: d9d0b7efa00aa2f95bc6a54e8c018e6d7c5a70a9bfaf9935fe7b3c03959b15bfa028fce0810e8b1cca3b6c774061266cc3dfb48078f3320f51bc4aec781f50dc
6
+ metadata.gz: 306c3bff850c31a4c1ece1a4a58e4f9fd3f88206ebfd27041b65fc4338123148bf7cca0bfcaf1fe141cef08d67ee902c0e534857f45be482ffff79591b143297
7
+ data.tar.gz: 29834db848343e4fc07b57540ca64dd85bd2fb36a2443f1d6b7b42b4f7415ebd85dcf5f95106b19cfb9549fced2d0460117823331e280f0659358f2c197b7593
data/README.md CHANGED
@@ -110,20 +110,44 @@ Please, note that if `seed` is not provided, sampler will use generic `rand` fun
110
110
 
111
111
  ### Performance
112
112
 
113
+ Once initialized, solution complexity is `O(n)`. [`Array#index`](https://ruby-doc.org/core/Array.html#method-i-index) is used to find a `rand` match to intervals (see [Math](#Math) section below).
114
+
115
+ ### Math
116
+
117
+ Consider normalized probabilities `P0, P1, .., Pn, 𝚺Pi = 1, 0 <= i <= n`
118
+
119
+ To select random index `i` with these probabilities we convert them into half-open intervals `[0, P0)`, `[P0, P1)`, `[P0+P1, P2)` ... `[𝚺Pi, 1), 0 <= i < n` and place on a half-open interval `[0, 1)`
120
+
121
+ ```
122
+ [<- P1 ->)[<- P2 ->) ... [<- Pn ->)
123
+ [--------------------------------------------------)
124
+ 0 ^- rand (sample pick) 1
125
+ ```
126
+
127
+ As you can see any random value from `[0, 1)` will hit into one of the half-open intervals with probability equal to the "length" of the interval
128
+
129
+ `rand` (seeded or not) will does exactly what is needed and returns a value in the same half-open interval `[0, 1)`
130
+
131
+ *NOTE about Terminilogy and beauty in Ruby:* in ruby [Range](https://ruby-doc.org/core/Range.html) class with `...` definition is equivalent to [half-open interval in the end](https://en.wikipedia.org/wiki/Interval_(mathematics)#Terminology) (i.e. `[0...1]` in Ruby is `[0, 1)` in math
132
+
133
+ *NOTE about precision and computer algebra ugliness:* in programming we cannot be sure that `𝚺Pi` will end up exactly equivalent to 1. And I do not like idea to use Ruby's [Rational](https://ruby-doc.org/core/Rational.html) due to performance implications and not much benefitss here
134
+
135
+ That's why `ERROR_ALLOWANCE = 10**-8` is accepted in normalization logic in WeightedSampler
136
+
113
137
  ## Development
114
138
 
115
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
139
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment
116
140
 
117
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
141
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org)
118
142
 
119
143
  ## Contributing
120
144
 
121
- Bug reports and pull requests are welcome on GitHub at https://gitlab.com/[USERNAME]/weighted_sampler. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
145
+ Bug reports and pull requests are welcome on GitHub at https://gitlab.com/[USERNAME]/weighted_sampler. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct
122
146
 
123
147
  ## License
124
148
 
125
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
149
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT)
126
150
 
127
151
  ## Code of Conduct
128
152
 
129
- Everyone interacting in the WeightedSampler project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/weighted_sampler/blob/master/CODE_OF_CONDUCT.md).
153
+ Everyone interacting in the WeightedSampler project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/weighted_sampler/blob/master/CODE_OF_CONDUCT.md)
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'weighted_sampler/version'
4
- require 'pry'
5
4
  module WeightedSampler
6
5
 
7
6
  # sum of floats are never stable enough to guarantee exact equality to 1
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WeightedSampler
4
- VERSION = '1.0.1'.freeze
4
+ VERSION = '1.0.2'.freeze
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: weighted_sampler
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oleksiy Babich
@@ -53,30 +53,19 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
55
  description: "\n Weighted Sampler helps you to pick a random samples from a collection
56
- with defined probabilities or weights\n\n You can pass an Array or a Hash with
57
- desired probabilities\n and use Module or Class API to pick samples\n "
56
+ with defined probabilities or weights.\n You can pass an Array or a Hash with
57
+ desired probabilities\n and use Module or Class API to pick samples.\n\n Please,
58
+ see documentation in the repo https://gitlab.com/alexey_b/weighted_sampler\n "
58
59
  email:
59
60
  - oleksiy@oleksiy.od.ua
60
61
  executables: []
61
62
  extensions: []
62
63
  extra_rdoc_files: []
63
64
  files:
64
- - ".gitignore"
65
- - ".gitlab-ci.yml"
66
- - ".rspec"
67
- - ".rubocop.yml"
68
- - ".travis.yml"
69
- - CODE_OF_CONDUCT.md
70
- - Gemfile
71
- - Gemfile.lock
72
65
  - LICENSE.txt
73
66
  - README.md
74
- - Rakefile
75
- - bin/console
76
- - bin/setup
77
67
  - lib/weighted_sampler.rb
78
68
  - lib/weighted_sampler/version.rb
79
- - weighted_sampler.gemspec
80
69
  homepage: https://gitlab.com/alexey_b/weighted_sampler
81
70
  licenses:
82
71
  - MIT
data/.gitignore DELETED
@@ -1,13 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /_yardoc/
4
- /coverage/
5
- /doc/
6
- /pkg/
7
- /spec/reports/
8
- /tmp/
9
-
10
- # rspec failure tracking
11
- .rspec_status
12
-
13
- *.gem
data/.gitlab-ci.yml DELETED
@@ -1,23 +0,0 @@
1
- image: ruby
2
-
3
- stages:
4
- - test
5
-
6
- before_script:
7
- - bundle install
8
-
9
- ruby2.3:
10
- image: ruby:2.3.7
11
- script: rspec
12
-
13
- ruby2.4:
14
- image: ruby:2.4.4
15
- script: rspec
16
-
17
- ruby2.5:
18
- image: ruby:2.5.1
19
- script: rspec
20
-
21
- ruby2.6-rc:
22
- image: ruby:2.6.0-preview2
23
- script: rspec
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --format progress
2
- --color
3
- --require spec_helper
data/.rubocop.yml DELETED
@@ -1,60 +0,0 @@
1
- AllCops:
2
- Exclude:
3
- - tmp/**/*
4
-
5
- Rails:
6
- Enabled: true
7
-
8
- # We prefer having assignments inside if-s
9
- Lint/AssignmentInCondition:
10
- Enabled: false
11
-
12
- # Longer lines is awesome
13
- Metrics/LineLength:
14
- Max: 160
15
-
16
- # Having every class documented is too much
17
- Style/Documentation:
18
- Enabled: false
19
-
20
- # We do love empty lines
21
- Layout/EmptyLines:
22
- Enabled: false
23
-
24
- Layout/EmptyLinesAroundArguments:
25
- Enabled: false
26
-
27
- Layout/EmptyLinesAroundClassBody:
28
- Enabled: false
29
-
30
- Layout/EmptyLinesAroundBlockBody:
31
- Enabled: false
32
-
33
- Layout/EmptyLinesAroundModuleBody:
34
- Enabled: false
35
-
36
- # We do love complex code
37
- Metrics/AbcSize:
38
- Enabled: false
39
-
40
- Metrics/CyclomaticComplexity:
41
- Enabled: false
42
-
43
- Metrics/ClassLength:
44
- Enabled: false
45
-
46
- Metrics/MethodLength:
47
- Enabled: false
48
-
49
- Metrics/PerceivedComplexity:
50
- Enabled: false
51
-
52
- Metrics/BlockLength:
53
- Enabled: false
54
-
55
- # This is AR-only cop
56
- Rails/FindEach:
57
- Enabled: false
58
-
59
- Rails/CreateTableWithTimestamps:
60
- Enabled: false
data/.travis.yml DELETED
@@ -1,8 +0,0 @@
1
- sudo: false
2
- language: ruby
3
- rvm:
4
- - 2.3.7
5
- - 2.4.4
6
- - 2.5.1
7
- script:
8
- - bundle exec rspec
data/CODE_OF_CONDUCT.md DELETED
@@ -1,74 +0,0 @@
1
- # Contributor Covenant Code of Conduct
2
-
3
- ## Our Pledge
4
-
5
- In the interest of fostering an open and welcoming environment, we as
6
- contributors and maintainers pledge to making participation in our project and
7
- our community a harassment-free experience for everyone, regardless of age, body
8
- size, disability, ethnicity, gender identity and expression, level of experience,
9
- nationality, personal appearance, race, religion, or sexual identity and
10
- orientation.
11
-
12
- ## Our Standards
13
-
14
- Examples of behavior that contributes to creating a positive environment
15
- include:
16
-
17
- * Using welcoming and inclusive language
18
- * Being respectful of differing viewpoints and experiences
19
- * Gracefully accepting constructive criticism
20
- * Focusing on what is best for the community
21
- * Showing empathy towards other community members
22
-
23
- Examples of unacceptable behavior by participants include:
24
-
25
- * The use of sexualized language or imagery and unwelcome sexual attention or
26
- advances
27
- * Trolling, insulting/derogatory comments, and personal or political attacks
28
- * Public or private harassment
29
- * Publishing others' private information, such as a physical or electronic
30
- address, without explicit permission
31
- * Other conduct which could reasonably be considered inappropriate in a
32
- professional setting
33
-
34
- ## Our Responsibilities
35
-
36
- Project maintainers are responsible for clarifying the standards of acceptable
37
- behavior and are expected to take appropriate and fair corrective action in
38
- response to any instances of unacceptable behavior.
39
-
40
- Project maintainers have the right and responsibility to remove, edit, or
41
- reject comments, commits, code, wiki edits, issues, and other contributions
42
- that are not aligned to this Code of Conduct, or to ban temporarily or
43
- permanently any contributor for other behaviors that they deem inappropriate,
44
- threatening, offensive, or harmful.
45
-
46
- ## Scope
47
-
48
- This Code of Conduct applies both within project spaces and in public spaces
49
- when an individual is representing the project or its community. Examples of
50
- representing a project or community include using an official project e-mail
51
- address, posting via an official social media account, or acting as an appointed
52
- representative at an online or offline event. Representation of a project may be
53
- further defined and clarified by project maintainers.
54
-
55
- ## Enforcement
56
-
57
- Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
- reported by contacting the project team at Alexey_Babich2@comcast.com. All
59
- complaints will be reviewed and investigated and will result in a response that
60
- is deemed necessary and appropriate to the circumstances. The project team is
61
- obligated to maintain confidentiality with regard to the reporter of an incident.
62
- Further details of specific enforcement policies may be posted separately.
63
-
64
- Project maintainers who do not follow or enforce the Code of Conduct in good
65
- faith may face temporary or permanent repercussions as determined by other
66
- members of the project's leadership.
67
-
68
- ## Attribution
69
-
70
- This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
- available at [http://contributor-covenant.org/version/1/4][version]
72
-
73
- [homepage]: http://contributor-covenant.org
74
- [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile DELETED
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- source 'https://rubygems.org'
4
-
5
- git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
-
7
- gem 'pry-byebug'
8
- gem 'simplecov'
9
-
10
- # Specify your gem's dependencies in weighted_sampler.gemspec
11
- gemspec
data/Gemfile.lock DELETED
@@ -1,53 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- weighted_sampler (1.0.1)
5
-
6
- GEM
7
- remote: https://rubygems.org/
8
- specs:
9
- byebug (10.0.2)
10
- coderay (1.1.2)
11
- diff-lcs (1.3)
12
- docile (1.3.1)
13
- json (2.1.0)
14
- method_source (0.9.0)
15
- pry (0.11.3)
16
- coderay (~> 1.1.0)
17
- method_source (~> 0.9.0)
18
- pry-byebug (3.6.0)
19
- byebug (~> 10.0)
20
- pry (~> 0.10)
21
- rake (10.5.0)
22
- rspec (3.7.0)
23
- rspec-core (~> 3.7.0)
24
- rspec-expectations (~> 3.7.0)
25
- rspec-mocks (~> 3.7.0)
26
- rspec-core (3.7.1)
27
- rspec-support (~> 3.7.0)
28
- rspec-expectations (3.7.0)
29
- diff-lcs (>= 1.2.0, < 2.0)
30
- rspec-support (~> 3.7.0)
31
- rspec-mocks (3.7.0)
32
- diff-lcs (>= 1.2.0, < 2.0)
33
- rspec-support (~> 3.7.0)
34
- rspec-support (3.7.1)
35
- simplecov (0.16.1)
36
- docile (~> 1.1)
37
- json (>= 1.8, < 3)
38
- simplecov-html (~> 0.10.0)
39
- simplecov-html (0.10.2)
40
-
41
- PLATFORMS
42
- ruby
43
-
44
- DEPENDENCIES
45
- bundler (~> 1.16)
46
- pry-byebug
47
- rake (~> 10.0)
48
- rspec (~> 3.0)
49
- simplecov
50
- weighted_sampler!
51
-
52
- BUNDLED WITH
53
- 1.16.1
data/Rakefile DELETED
@@ -1,8 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'bundler/gem_tasks'
4
- require 'rspec/core/rake_task'
5
-
6
- RSpec::Core::RakeTask.new(:spec)
7
-
8
- task default: :spec
data/bin/console DELETED
@@ -1,15 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
-
4
- require 'bundler/setup'
5
- require 'weighted_sampler'
6
-
7
- # You can add fixtures and/or initialization code here to make experimenting
8
- # with your gem easier. You can also use a different console, if you like.
9
-
10
- # (If you use this, don't forget to add pry to your Gemfile!)
11
- # require "pry"
12
- # Pry.start
13
-
14
- require 'irb'
15
- IRB.start(__FILE__)
data/bin/setup DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
@@ -1,34 +0,0 @@
1
-
2
- # frozen_string_literal: true
3
-
4
- lib = File.expand_path('lib', __dir__)
5
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
- require 'weighted_sampler/version'
7
-
8
- Gem::Specification.new do |spec|
9
- spec.name = 'weighted_sampler'
10
- spec.version = WeightedSampler::VERSION
11
- spec.authors = ['Oleksiy Babich']
12
- spec.email = ['oleksiy@oleksiy.od.ua']
13
-
14
- spec.summary = 'Weighted Sampler helps you to pick a random samples from a collection with defined probabilities or weights'
15
- spec.description = %(
16
- Weighted Sampler helps you to pick a random samples from a collection with defined probabilities or weights
17
-
18
- You can pass an Array or a Hash with desired probabilities
19
- and use Module or Class API to pick samples
20
- )
21
- spec.homepage = 'https://gitlab.com/alexey_b/weighted_sampler'
22
- spec.license = 'MIT'
23
-
24
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
25
- f.match(%r{^(test|spec|features)/})
26
- end
27
- spec.bindir = 'exe'
28
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
- spec.require_paths = ['lib']
30
-
31
- spec.add_development_dependency 'bundler', '~> 1.16'
32
- spec.add_development_dependency 'rake', '~> 10.0'
33
- spec.add_development_dependency 'rspec', '~> 3.0'
34
- end