magic_numbers 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +6 -0
- data/Gemfile +2 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/lib/magic_numbers.rb +6 -0
- data/lib/magic_numbers/summary.rb +113 -0
- data/lib/magic_numbers/summary/all.rb +3 -0
- data/lib/magic_numbers/summary/array.rb +5 -0
- data/lib/magic_numbers/summary/matrix.rb +6 -0
- data/lib/magic_numbers/summary/set.rb +6 -0
- data/lib/magic_numbers/version.rb +3 -0
- data/magic_numbers.gemspec +25 -0
- data/spec/magic_numbers/summary_spec.rb +61 -0
- data/spec/spec_helper.rb +0 -0
- metadata +130 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7b3ced15a7b66249d77826c9510e1aec7a734a77
|
4
|
+
data.tar.gz: ebc70856bfac4d0a725b6dd10220e265a50f7ed8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d1486a6fcc2272d534fc3df4079093b7f725a75b2187b8c92b1139fa7ac1188d8b2db4e72507fb73035971b7e23ea147e6646316499963d73de32db8eb0e80e6
|
7
|
+
data.tar.gz: b0a51eb9ba078e97dd161c3593fe0ab4048b7e6c7daf96773ba978ad50ec38fac9549026f828a808eb02f2308203a9ac757f7fed64838581eae7118bd383e032
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Fernando Rodrigues da Silva
|
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,29 @@
|
|
1
|
+
MagicNumbers
|
2
|
+
============
|
3
|
+
|
4
|
+
Install
|
5
|
+
=======
|
6
|
+
Add this line to your application's Gemfile:
|
7
|
+
```ruby
|
8
|
+
gem 'magic_numbers'
|
9
|
+
```
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
```bash
|
13
|
+
$ bundle
|
14
|
+
```
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
```bash
|
18
|
+
$ gem install magic_numbers
|
19
|
+
```
|
20
|
+
|
21
|
+
Usage
|
22
|
+
=====
|
23
|
+
|
24
|
+
Contributing
|
25
|
+
============
|
26
|
+
|
27
|
+
Licence
|
28
|
+
=======
|
29
|
+
MIT
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,113 @@
|
|
1
|
+
module MagicNumbers
|
2
|
+
module Summary
|
3
|
+
# Check if all elements are numbers.
|
4
|
+
#
|
5
|
+
# @example
|
6
|
+
# [1, 2, 3].numeric? #=> True
|
7
|
+
# ["a", 5].numeric? #=> False
|
8
|
+
def numeric?
|
9
|
+
all? { |x| x.is_a?(Numeric) }
|
10
|
+
end
|
11
|
+
|
12
|
+
# Sum of all elements.
|
13
|
+
#
|
14
|
+
# @example
|
15
|
+
# [1, 2, 3].sum #=> 6
|
16
|
+
# [].sum #=> nil
|
17
|
+
# ["a", 5].sum #=> TypeError
|
18
|
+
#
|
19
|
+
# @raise [TypeError] When collection includes a non Numeric type
|
20
|
+
# @return [Numeric] The sum of all objects
|
21
|
+
def sum
|
22
|
+
if numeric?
|
23
|
+
reduce(:+)
|
24
|
+
else
|
25
|
+
raise TypeError, "collection must include only Numeric types"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Arithmetic mean
|
30
|
+
#
|
31
|
+
# @example
|
32
|
+
# [1, 2, 3, 4].mean #=> 2.5
|
33
|
+
# [].mean #=> nil
|
34
|
+
# ["a", 5].mean #=> TypeError
|
35
|
+
#
|
36
|
+
# @see http://en.wikipedia.org/wiki/Arithmetic_mean Wikipedia: Arithmetic Mean
|
37
|
+
# @raise [TypeError] When collection includes a non Numeric type
|
38
|
+
# @return [Numeric] The mean
|
39
|
+
def mean
|
40
|
+
return nil unless any?
|
41
|
+
sum / size.to_f
|
42
|
+
end
|
43
|
+
|
44
|
+
# A measure of the expected deviation from the mean.
|
45
|
+
#
|
46
|
+
# @example
|
47
|
+
# [2, 3, 4, 5].var #=> 1.25
|
48
|
+
# [1, 4, 5, 8].var #=> 6.25
|
49
|
+
# [2, 3, 6, 12].var(1) #=> 20.25
|
50
|
+
# [].var #=> nil
|
51
|
+
# ["a", 5].var #=> TypeError
|
52
|
+
# [2, 3, 6, 12].var(-1) #=> ArgumentError
|
53
|
+
# [2, 3, 6, 12].var(4) #=> ArgumentError
|
54
|
+
#
|
55
|
+
# @see http://en.wikipedia.org/wiki/Variance Wikipedia: Variance
|
56
|
+
# @see http://en.wikipedia.org/wiki/Degrees_of_freedom_%28statistics%29 Wikipedia: Degrees of Freedom
|
57
|
+
# @raise [TypeError] When collection includes a non Numeric type
|
58
|
+
# @raise [ArgumentError] When degress of freedom is not between (0..collection_size - 1)
|
59
|
+
# @return [Numeric] The population variance
|
60
|
+
def variance(degrees_of_freedom = 0)
|
61
|
+
l_mean, l_size = mean, size
|
62
|
+
msg = "degrees_of_freedom must be greater or equal to zero and lesser than collection size"
|
63
|
+
|
64
|
+
return nil unless l_mean
|
65
|
+
raise ArgumentError, msg unless (0...l_size) === degrees_of_freedom
|
66
|
+
|
67
|
+
squares = map { |x| (x - mean) ** 2 }
|
68
|
+
squares.sum / (l_size - degrees_of_freedom)
|
69
|
+
end
|
70
|
+
alias :var :variance
|
71
|
+
|
72
|
+
# A statistic that measures how spread out a set of data is.
|
73
|
+
#
|
74
|
+
# @example
|
75
|
+
# [1, 4, 5, 8].sd #=> 2.5
|
76
|
+
# [3, 4, 7, 12].sd #=> 3.5
|
77
|
+
# [2, 3, 6, 12].sd(1) #=> 4.5
|
78
|
+
# [].sd #=> nil
|
79
|
+
# ["a", 5].sd #=> TypeError
|
80
|
+
# [2, 3, 6, 12].sd(-1) #=> ArgumentError
|
81
|
+
# [2, 3, 6, 12].sd(4) #=> ArgumentError
|
82
|
+
#
|
83
|
+
# @see http://en.wikipedia.org/wiki/Standard_deviation Wikipedia: Standard Deviation
|
84
|
+
# @see http://en.wikipedia.org/wiki/Degrees_of_freedom_%28statistics%29 Wikipedia: Degrees of Freedom
|
85
|
+
# @raise [TypeError] When collection includes a non Numeric type
|
86
|
+
# @raise [ArgumentError] When degress of freedom is not between (0..collection_size - 1)
|
87
|
+
# @return [Numeric] The population standard deviation
|
88
|
+
def standard_deviation(degrees_of_freedom = 0)
|
89
|
+
return nil unless l_variance = variance(degrees_of_freedom)
|
90
|
+
Math.sqrt(l_variance)
|
91
|
+
end
|
92
|
+
alias :sd :standard_deviation
|
93
|
+
|
94
|
+
# The difference between the minimum and the maximum values.
|
95
|
+
#
|
96
|
+
# @example
|
97
|
+
# [19, 4, 5, 8].range #=> 15
|
98
|
+
# [].range #=> nil
|
99
|
+
# ["a", 5].range #=> TypeError
|
100
|
+
#
|
101
|
+
# @see http://en.wikipedia.org/wiki/Range_(statistics) Wikipedia: Range
|
102
|
+
# @raise [TypeError] When collection includes a non Numeric type
|
103
|
+
# @return [Numeric] The range
|
104
|
+
def range
|
105
|
+
return nil unless any?
|
106
|
+
if numeric?
|
107
|
+
max - min
|
108
|
+
else
|
109
|
+
raise TypeError, "collection must include only Numeric types"
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'magic_numbers/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "magic_numbers"
|
8
|
+
spec.version = MagicNumbers::VERSION
|
9
|
+
spec.authors = ["Fernando Rodrigues"]
|
10
|
+
spec.email = ["fernandors87@gmail.com"]
|
11
|
+
spec.summary = %q{Ruby data analysis library}
|
12
|
+
spec.description = %q{Ruby data analysis library}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
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", "~> 1.5"
|
22
|
+
spec.add_development_dependency "rake", "~> 0"
|
23
|
+
spec.add_development_dependency "rspec", "~> 2.14", ">= 2.14.1"
|
24
|
+
spec.add_development_dependency "yard", "~> 0.8", ">= 0.8.7.3"
|
25
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'magic_numbers/summary/all'
|
2
|
+
|
3
|
+
describe MagicNumbers::Summary do
|
4
|
+
shared_examples_for 'a statistical summary' do
|
5
|
+
describe '#sum' do
|
6
|
+
it { expect(collection.sum).to eq 0 }
|
7
|
+
it { expect(empty.sum).to be_nil }
|
8
|
+
it { expect { nan.sum }.to raise_error(TypeError) }
|
9
|
+
end
|
10
|
+
|
11
|
+
describe '#numeric?' do
|
12
|
+
it { expect(collection).to be_numeric }
|
13
|
+
it { expect(nan).not_to be_numeric }
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '#mean' do
|
17
|
+
it { expect(collection.mean).to eq 0 }
|
18
|
+
it { expect(empty.mean).to be_nil }
|
19
|
+
it { expect { nan.mean }.to raise_error(TypeError) }
|
20
|
+
end
|
21
|
+
|
22
|
+
describe '#variance' do
|
23
|
+
it { expect { collection.variance(-1) }.to raise_error(ArgumentError) }
|
24
|
+
it { expect(collection.variance).to be_within(10.0).of(0.00000001) }
|
25
|
+
it { expect(collection.variance(2)).to be_within(12.22222222).of(0.00000001) }
|
26
|
+
it { expect { collection.variance(collection.size) }.to raise_error(ArgumentError) }
|
27
|
+
it { expect(empty.variance).to be_nil }
|
28
|
+
it { expect { nan.variance }.to raise_error(TypeError) }
|
29
|
+
end
|
30
|
+
|
31
|
+
describe '#standard_deviation' do
|
32
|
+
it { expect { collection.standard_deviation(-1) }.to raise_error(ArgumentError) }
|
33
|
+
it { expect(collection.standard_deviation).to be_within(3.16227766).of(0.00000001) }
|
34
|
+
it { expect(collection.standard_deviation(2)).to be_within(3.49602949).of(0.00000001) }
|
35
|
+
it { expect { collection.standard_deviation(collection.size) }.to raise_error(ArgumentError) }
|
36
|
+
it { expect(empty.standard_deviation).to be_nil }
|
37
|
+
it { expect { nan.standard_deviation }.to raise_error(TypeError) }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe Array do
|
42
|
+
let(:collection) { (-5..5).to_a }
|
43
|
+
let(:empty) { [] }
|
44
|
+
let(:nan) { [1, "2"] }
|
45
|
+
it_behaves_like 'a statistical summary'
|
46
|
+
end
|
47
|
+
|
48
|
+
describe Set do
|
49
|
+
let(:collection) { Set.new((-5..5).to_a) }
|
50
|
+
let(:empty) { Set.new }
|
51
|
+
let(:nan) { Set.new([1, "2"]) }
|
52
|
+
it_behaves_like 'a statistical summary'
|
53
|
+
end
|
54
|
+
|
55
|
+
describe Vector do
|
56
|
+
let(:collection) { Vector[*(-5..5).to_a] }
|
57
|
+
let(:empty) { Vector[] }
|
58
|
+
let(:nan) { Vector[*[1, "2"]] }
|
59
|
+
it_behaves_like 'a statistical summary'
|
60
|
+
end
|
61
|
+
end
|
data/spec/spec_helper.rb
ADDED
File without changes
|
metadata
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: magic_numbers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Fernando Rodrigues
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-02-24 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: '1.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
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: '2.14'
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: 2.14.1
|
51
|
+
type: :development
|
52
|
+
prerelease: false
|
53
|
+
version_requirements: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - "~>"
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '2.14'
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 2.14.1
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: yard
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0.8'
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: 0.8.7.3
|
71
|
+
type: :development
|
72
|
+
prerelease: false
|
73
|
+
version_requirements: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - "~>"
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0.8'
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: 0.8.7.3
|
81
|
+
description: Ruby data analysis library
|
82
|
+
email:
|
83
|
+
- fernandors87@gmail.com
|
84
|
+
executables: []
|
85
|
+
extensions: []
|
86
|
+
extra_rdoc_files: []
|
87
|
+
files:
|
88
|
+
- ".gitignore"
|
89
|
+
- Gemfile
|
90
|
+
- LICENSE.txt
|
91
|
+
- README.md
|
92
|
+
- Rakefile
|
93
|
+
- lib/magic_numbers.rb
|
94
|
+
- lib/magic_numbers/summary.rb
|
95
|
+
- lib/magic_numbers/summary/all.rb
|
96
|
+
- lib/magic_numbers/summary/array.rb
|
97
|
+
- lib/magic_numbers/summary/matrix.rb
|
98
|
+
- lib/magic_numbers/summary/set.rb
|
99
|
+
- lib/magic_numbers/version.rb
|
100
|
+
- magic_numbers.gemspec
|
101
|
+
- spec/magic_numbers/summary_spec.rb
|
102
|
+
- spec/spec_helper.rb
|
103
|
+
homepage: ''
|
104
|
+
licenses:
|
105
|
+
- MIT
|
106
|
+
metadata: {}
|
107
|
+
post_install_message:
|
108
|
+
rdoc_options: []
|
109
|
+
require_paths:
|
110
|
+
- lib
|
111
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project:
|
123
|
+
rubygems_version: 2.2.1
|
124
|
+
signing_key:
|
125
|
+
specification_version: 4
|
126
|
+
summary: Ruby data analysis library
|
127
|
+
test_files:
|
128
|
+
- spec/magic_numbers/summary_spec.rb
|
129
|
+
- spec/spec_helper.rb
|
130
|
+
has_rdoc:
|