random_bell 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a642dbc691a26cf7c63cbeec3df54322c221e2e5
4
+ data.tar.gz: 6afe3bc00805d3dca7bf613e44feac36123a851f
5
+ SHA512:
6
+ metadata.gz: 64283a0a0fd759d0ceb355a6bc4f1ddba4307b872a543b18447f46de7037df12205663be849e2e9d466e4e15e56be192728c4b2c568331c95f365d973034125c
7
+ data.tar.gz: 331fc647a8a1e5f71fcb78914e611517338abe7402b9402e03ee02b6a5b19edcf438a5ca2570ade3b4df370a42c6f90eceac4851973842f0e6c07d94d9bca6a6
data/.gitignore ADDED
@@ -0,0 +1,22 @@
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
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 2.1.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in random_bell.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 OSA Shunsuke
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 OSA Shunsuke
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
+ # RandomBell
2
+
3
+ RandomBell is nomal(Gaussian) random number generator.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'random_bell'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```
22
+ $ gem install random_bell
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ TODO: Write usage instructions here
28
+
29
+ ## Contributing
30
+
31
+ 1. Fork it ( https://github.com/s-osa/random_bell/fork )
32
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
33
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
34
+ 4. Push to the branch (`git push origin my-new-feature`)
35
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ # Exclude histogram specs by default
5
+ RSpec::Core::RakeTask.new("spec") do |task|
6
+ task.rspec_opts = "--tag ~histogram"
7
+ end
8
+
9
+ RSpec::Core::RakeTask.new("spec:histogram")
10
+
11
+ task default: [:spec]
12
+
13
+
@@ -0,0 +1,3 @@
1
+ class RandomBell
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,59 @@
1
+ require "random_bell/version"
2
+
3
+ class RandomBell
4
+ def initialize(mu: 0.5, sigma: 0.2, range: 0.0..1.0, method: :box_muller)
5
+ @mu = mu
6
+ @sigma = sigma
7
+ @range = range
8
+ @method = method
9
+ end
10
+
11
+ def standard
12
+ send(@method)
13
+ end
14
+
15
+ def normal
16
+ @mu + standard * @sigma
17
+ end
18
+
19
+ def rand
20
+ loop do
21
+ num = normal
22
+ return num if @range.include?(num)
23
+ end
24
+ end
25
+
26
+ def to_histogram(rows: 20, size: 10 ** 4)
27
+ histogram = ""
28
+
29
+ numbers = (1..size).to_a.map{ rand }
30
+
31
+ klasses, klass_width = [], (@range.max - @range.min).to_f / rows
32
+ (0..rows).each_cons(2) do |min, max|
33
+ klasses << Range.new(klass_width * min, klass_width * max)
34
+ end
35
+
36
+ divider = (klasses.map{|klass| numbers.select{|num| klass.include?(num) }.size }.max / 50.0).ceil
37
+ klasses.each do |klass|
38
+ n = numbers.select{|num| klass.include?(num) }.size / divider
39
+ histogram << sprintf("%+3.3f", klass.max) << ": " << "*" * n << "\n"
40
+ end
41
+
42
+ histogram
43
+ end
44
+
45
+ private
46
+
47
+ def box_muller
48
+ theta = 2 * Math::PI * Random.rand
49
+ r = Math.sqrt( -2 * Math.log(Random.rand) )
50
+ z1, z2 = r * Math.cos(theta), r * Math.sin(theta)
51
+ [z1, z2].sample
52
+ end
53
+
54
+ def central_limit
55
+ arr = []
56
+ 12.times{ arr << Random.rand }
57
+ arr.reduce(0.0, &:+) - 6
58
+ end
59
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'random_bell/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "random_bell"
8
+ spec.version = RandomBell::VERSION
9
+ spec.authors = ["OSA Shunsuke"]
10
+ spec.email = ["hhelibebcnofnenamg@gmail.com"]
11
+ spec.summary = %q{Normal(Gaussian) random number generator.}
12
+ spec.description = %q{Normal(Gaussian) random number generator.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,129 @@
1
+ require 'spec_helper'
2
+
3
+ describe RandomBell do
4
+ REPEAT = 10 ** 5
5
+ WITHIN = 0.1
6
+
7
+ describe "#standard" do
8
+ before :all do
9
+ @bell = RandomBell.new
10
+ end
11
+
12
+ describe "results" do
13
+ before :all do
14
+ @results = []
15
+ REPEAT.times{ @results << @bell.standard }
16
+ end
17
+
18
+ it "should be 0 as average" do
19
+ expect(@results.average).to be_within(WITHIN).of(0)
20
+ end
21
+
22
+ it "should be 1 as variance" do
23
+ expect(@results.variance).to be_within(WITHIN).of(1)
24
+ end
25
+
26
+ it "should be 1 as standard diviation" do
27
+ expect(@results.standard_diviation).to be_within(WITHIN).of(1)
28
+ end
29
+
30
+ it "drow histgram", histogram: true do
31
+ puts @results.to_histogram
32
+ end
33
+ end
34
+ end
35
+
36
+ describe "#normal" do
37
+ before :all do
38
+ @mu, @sigma = 1, 0.5
39
+ @bell = RandomBell.new(mu: @mu, sigma: @sigma)
40
+ end
41
+
42
+ describe "results" do
43
+ before :all do
44
+ @results = []
45
+ REPEAT.times{ @results << @bell.normal }
46
+ end
47
+
48
+ it "should be mu as average" do
49
+ expect(@results.average).to be_within(WITHIN).of(@mu)
50
+ end
51
+
52
+ it "should be square sigma as variance" do
53
+ expect(@results.variance).to be_within(WITHIN).of(@sigma ** 2)
54
+ end
55
+
56
+ it "should be sigma as normal diviation" do
57
+ expect(@results.standard_diviation).to be_within(WITHIN).of(@sigma)
58
+ end
59
+
60
+ it "drow histgram", histogram: true do
61
+ puts @results.to_histogram
62
+ end
63
+ end
64
+ end
65
+
66
+ describe "#rand" do
67
+ context "range not given" do
68
+ before :all do
69
+ @bell = RandomBell.new(mu: 1, sigma: 0.5)
70
+ end
71
+
72
+ it "should return float" do
73
+ expect(@bell.rand).to be_an_instance_of Float
74
+ end
75
+
76
+ describe "results" do
77
+ before :all do
78
+ @results = []
79
+ REPEAT.times{ @results << @bell.rand }
80
+ end
81
+
82
+ it "should be 1 as max" do
83
+ expect(@results.max).to be <= 1
84
+ end
85
+
86
+ it "should be 1 as min" do
87
+ expect(@results.min).to be >= 0
88
+ end
89
+
90
+ it "drow histgram", histogram: true do
91
+ puts @results.to_histogram
92
+ end
93
+ end
94
+ end
95
+
96
+ context "range given" do
97
+ before :all do
98
+ @mu, @sigma, @range = 50, 20, 40..100
99
+ @bell = RandomBell.new(mu: @mu, sigma: @sigma, range: @range)
100
+ end
101
+
102
+ describe "results" do
103
+ before :all do
104
+ @results = []
105
+ REPEAT.times{ @results << @bell.rand }
106
+ end
107
+
108
+ it "should be 100 as max" do
109
+ expect(@results.max).to be <= 100
110
+ end
111
+
112
+ it "should be 40 as min" do
113
+ expect(@results.min).to be >= 40
114
+ end
115
+
116
+ it "drow histgram", histogram: true do
117
+ puts @results.to_histogram
118
+ end
119
+ end
120
+ end
121
+ end
122
+
123
+ describe "#to_histogram" do
124
+ it "should return histogram string" do
125
+ bell = RandomBell.new(mu: 0.7)
126
+ expect(bell.to_histogram).to match(/\*+/)
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,19 @@
1
+ project_root = File.join(File.dirname(__FILE__), '..')
2
+ $: << project_root
3
+
4
+ require 'lib/random_bell'
5
+
6
+ class Array
7
+ def average
8
+ reduce(0.0, &:+) / size
9
+ end
10
+
11
+ def variance
12
+ avg = average
13
+ reduce(0.0){|sum, f| sum += ( f - avg ) ** 2 } / size
14
+ end
15
+
16
+ def standard_diviation
17
+ Math.sqrt(variance)
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: random_bell
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - OSA Shunsuke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Normal(Gaussian) random number generator.
56
+ email:
57
+ - hhelibebcnofnenamg@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - lib/random_bell.rb
71
+ - lib/random_bell/version.rb
72
+ - random_bell.gemspec
73
+ - spec/random_bell_spec.rb
74
+ - spec/spec_helper.rb
75
+ homepage: ''
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.2.2
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Normal(Gaussian) random number generator.
99
+ test_files:
100
+ - spec/random_bell_spec.rb
101
+ - spec/spec_helper.rb