zipfian 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
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 zipfian.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,7 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :test do
5
+ watch(%r{^test/test_.+\.rb$})
6
+ end
7
+
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Junegunn Choi
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,51 @@
1
+ # Zipfian
2
+
3
+ [Zipfian distribution](http://en.wikipedia.org/wiki/Zipf's_law) implementation.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'zipfian'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install zipfian
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ # 1000: Number of elements
23
+ # 1.0: Exponent
24
+ z = Zipfian.new 1000, 1.0
25
+
26
+ (1..1000).each do |i|
27
+ puts [z.pmf(i), z.cdf(i)].join ' - '
28
+ end
29
+
30
+ puts z.sample
31
+ ```
32
+
33
+ ## Disclaimer
34
+
35
+ On initialization, Zipfian precalculates and stores the values of cumulative distribution function for every integer in the range.
36
+ As the number gets bigger, it will take more time and memory.
37
+
38
+ ```ruby
39
+ # A workaround of memory limitation
40
+ z = Zipfian.new 1000000, 0.5
41
+
42
+ puts z.sample * 1000 - rand(1000)
43
+ ```
44
+
45
+ ## Contributing
46
+
47
+ 1. Fork it
48
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
49
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
50
+ 4. Push to the branch (`git push origin my-new-feature`)
51
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rake/testtask'
4
+ Rake::TestTask.new(:test) do |test|
5
+ test.libs << 'lib' << 'test'
6
+ test.pattern = 'test/**/test_*.rb'
7
+ test.verbose = true
8
+ end
data/lib/zipfian.rb ADDED
@@ -0,0 +1,66 @@
1
+ require "zipfian/version"
2
+
3
+ class Zipfian
4
+ def initialize n, s
5
+ unless n > 0 && n.is_a?(Integer)
6
+ raise ArgumentError.new("Number of elements must be a positive integer")
7
+ end
8
+ unless s > 0
9
+ raise ArgumentError.new("Exponent must be a positive number")
10
+ end
11
+
12
+ @n = n
13
+ @s = s
14
+ sums = [0]
15
+ @h = (1..@n).inject(0) { |sum, i| sums[i] = sum + 1.0 / (i ** @s) }
16
+ @cdf = (0..@n).map { |i| sums[i] / @h }
17
+
18
+ class << @cdf
19
+ def binary_search_index v
20
+ l = 0
21
+ r = self.length - 2
22
+
23
+ while (c = (l + r) / 2) && l < r
24
+ if v < self[c]
25
+ r = c - 1
26
+ elsif v > self[c]
27
+ l = c + 1
28
+ else
29
+ return c
30
+ end
31
+ end
32
+
33
+ v < self[c] ? c : c + 1
34
+ end
35
+ end
36
+ end
37
+
38
+ def inspect
39
+ {
40
+ :N => @n,
41
+ :s => @s
42
+ }.inspect
43
+ end
44
+
45
+ def pmf k
46
+ check_rank k
47
+ 1.0 / ((k ** @s) * @h)
48
+ end
49
+
50
+ def cdf k
51
+ check_rank k
52
+ @cdf[k]
53
+ end
54
+
55
+ def sample
56
+ @cdf.binary_search_index rand
57
+ end
58
+
59
+ private
60
+ def check_rank k
61
+ unless k.is_a?(Integer) && k >= 1 && k <= @n
62
+ raise ArgumentError.new("Rank must be a positive integer (max: #{@n})")
63
+ end
64
+ end
65
+ end
66
+
@@ -0,0 +1,3 @@
1
+ class Zipfian
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '../lib')
5
+ require 'zipfian'
6
+ require 'test-unit'
7
+
8
+ class TestZipfian < Test::Unit::TestCase
9
+ def test_init
10
+ assert_raise(ArgumentError) { z = Zipfian.new }
11
+
12
+ [0.9, 0, -1].each do |n|
13
+ assert_raise(ArgumentError) { z = Zipfian.new n, 1 }
14
+ end
15
+
16
+ assert_raise(ArgumentError) { z = Zipfian.new 100, 0 }
17
+ assert_raise(ArgumentError) { z = Zipfian.new 100, -1 }
18
+ end
19
+
20
+ def test_invalid_rank
21
+ z = Zipfian.new 1000, 0.1
22
+
23
+ [0.1, 0, 2000].each do |k|
24
+ assert_raise(ArgumentError) { z.pmf k }
25
+ assert_raise(ArgumentError) { z.cdf k }
26
+ end
27
+ end
28
+
29
+ def test_pmf_cdf
30
+ z = Zipfian.new 1000, 1
31
+
32
+ sum = 0
33
+ (1..1000).each do |i|
34
+ assert z.pmf(i) <= z.cdf(i)
35
+ assert z.pmf(i) < z.pmf(i - 1) if i > 1
36
+ end
37
+ assert_equal z.pmf(1), z.cdf(1)
38
+ assert_equal 1, z.cdf(1000)
39
+ end
40
+
41
+ def test_sample
42
+ z = Zipfian.new 100, 1
43
+
44
+ 10000.times do |i|
45
+ assert (1..100).include?(z.sample)
46
+ end
47
+ end
48
+ end
data/zipfian.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/zipfian/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Junegunn Choi"]
6
+ gem.email = ["junegunn.c@gmail.com"]
7
+ gem.description = %q{Zipfian distribution}
8
+ gem.summary = %q{Zipfian distribution}
9
+ gem.homepage = "https://github.com/junegunn/zipfian"
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 = "zipfian"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Zipfian::VERSION
17
+
18
+ gem.add_development_dependency 'test-unit'
19
+ gem.add_development_dependency 'guard'
20
+ gem.add_development_dependency 'guard-test'
21
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zipfian
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Junegunn Choi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: test-unit
16
+ requirement: &2153866200 !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: *2153866200
25
+ - !ruby/object:Gem::Dependency
26
+ name: guard
27
+ requirement: &2153865760 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *2153865760
36
+ - !ruby/object:Gem::Dependency
37
+ name: guard-test
38
+ requirement: &2153865340 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2153865340
47
+ description: Zipfian distribution
48
+ email:
49
+ - junegunn.c@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - Guardfile
57
+ - LICENSE
58
+ - README.md
59
+ - Rakefile
60
+ - lib/zipfian.rb
61
+ - lib/zipfian/version.rb
62
+ - test/test_zipfian.rb
63
+ - zipfian.gemspec
64
+ homepage: https://github.com/junegunn/zipfian
65
+ licenses: []
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 1.8.11
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Zipfian distribution
88
+ test_files:
89
+ - test/test_zipfian.rb