statistical_methods 1.0.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 +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.rubocop.yml +6 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +4 -0
- data/Gemfile +15 -0
- data/Guardfile +13 -0
- data/LICENSE.txt +22 -0
- data/README.md +49 -0
- data/Rakefile +1 -0
- data/lib/statistical_methods.rb +5 -0
- data/lib/statistical_methods/computer.rb +67 -0
- data/lib/statistical_methods/core.rb +1 -0
- data/lib/statistical_methods/core/array.rb +34 -0
- data/lib/statistical_methods/leader.rb +32 -0
- data/lib/statistical_methods/summary_statistic.rb +10 -0
- data/lib/statistical_methods/summary_statistic/dependence.rb +6 -0
- data/lib/statistical_methods/summary_statistic/dependence/.keep +0 -0
- data/lib/statistical_methods/summary_statistic/location.rb +10 -0
- data/lib/statistical_methods/summary_statistic/location/.keep +0 -0
- data/lib/statistical_methods/summary_statistic/location/average.rb +63 -0
- data/lib/statistical_methods/summary_statistic/location/median.rb +20 -0
- data/lib/statistical_methods/summary_statistic/location/mode.rb +18 -0
- data/lib/statistical_methods/summary_statistic/shape.rb +6 -0
- data/lib/statistical_methods/summary_statistic/shape/.keep +0 -0
- data/lib/statistical_methods/summary_statistic/spread.rb +6 -0
- data/lib/statistical_methods/summary_statistic/spread/.keep +0 -0
- data/lib/statistical_methods/summary_statistic/spread/statistical_dispersion.rb +6 -0
- data/lib/statistical_methods/version.rb +4 -0
- data/spec/core/array_spec.rb +47 -0
- data/spec/spec_helper.rb +14 -0
- data/spec/statistical_methods/computer_spec.rb +28 -0
- data/spec/statistical_methods/leader_spec.rb +37 -0
- data/spec/statistical_methods/summary_statistic/dependence/.keep +0 -0
- data/spec/statistical_methods/summary_statistic/location/.keep +0 -0
- data/spec/statistical_methods/summary_statistic/location/average_spec.rb +195 -0
- data/spec/statistical_methods/summary_statistic/location/median_spec.rb +36 -0
- data/spec/statistical_methods/summary_statistic/location/mode_spec.rb +35 -0
- data/spec/statistical_methods/summary_statistic/shape/.keep +0 -0
- data/spec/statistical_methods/summary_statistic/spread/.keep +0 -0
- data/statistical_methods.gemspec +22 -0
- metadata +125 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 52562f83299094e8c979afd65e4cc8de2e444015
|
4
|
+
data.tar.gz: cf453ebef0932eef9d48b17f2201dc5d3f658e2a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fd81a32c7f8ac2330d32d0e099f1ce75b387a8c43d11358894b957a28ef34df6e64cd74f8b5a3fe3d889aa1f15c56ddd238dbf7c8f1d6408448ae5c3b7b02151
|
7
|
+
data.tar.gz: 098f8f0b83f0c7ffe19ac2400c2c1b986cf3e3ed0df5166dba0c1889d1e67910efe12037bc56e3ed9a333077673b46ef6cdb394eac252cfca3f3adfd4aa2b257
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
statistical_methods
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.2.1
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in statistical_methods.gemspec
|
4
|
+
gemspec
|
5
|
+
|
6
|
+
group :development, :test do
|
7
|
+
gem 'codeclimate-test-reporter', require: false
|
8
|
+
gem 'coveralls', require: false
|
9
|
+
gem 'guard'
|
10
|
+
gem 'guard-bundler'
|
11
|
+
gem 'guard-rspec'
|
12
|
+
gem 'reek'
|
13
|
+
gem 'rspec'
|
14
|
+
gem 'simplecov', require: false
|
15
|
+
end
|
data/Guardfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
guard 'bundler' do
|
2
|
+
watch('Gemfile')
|
3
|
+
watch(/^.+\.gemspec/)
|
4
|
+
end
|
5
|
+
|
6
|
+
guard :rspec do
|
7
|
+
watch(%r{^spec/.+_spec\.rb$})
|
8
|
+
watch(%r{^lib/(.+)\.rb$}) do |array|
|
9
|
+
name = array.last
|
10
|
+
"spec/#{name}_spec.rb"
|
11
|
+
end
|
12
|
+
watch('spec/spec_helper.rb') { 'spec' }
|
13
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Fractal Soft
|
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,49 @@
|
|
1
|
+
# StatisticalMethods
|
2
|
+
|
3
|
+
Statistical methods - easy and simple.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'statistical_methods'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install statistical_methods
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
StatisticalMethods::Computer.sum(array)
|
23
|
+
StatisticalMethods::Computer.mean(array)
|
24
|
+
StatisticalMethods::Computer.range(array)
|
25
|
+
StatisticalMethods::Computer.standard_deviation(array)
|
26
|
+
StatisticalMethods::Computer.median(array)
|
27
|
+
```
|
28
|
+
|
29
|
+
You can include methods into Array class:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
Array.send(:include, StatisticalMethods::Array)
|
33
|
+
```
|
34
|
+
|
35
|
+
and use
|
36
|
+
```ruby
|
37
|
+
[1,2,3].sum #=> 6.0
|
38
|
+
[1,2,3].mean #=> 2.0
|
39
|
+
[1,2,3].range #=> 1.0, 3.0
|
40
|
+
[1,2,3].median #=> 2.0
|
41
|
+
```
|
42
|
+
|
43
|
+
## Contributing
|
44
|
+
|
45
|
+
1. Fork it
|
46
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
47
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
48
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
49
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
@@ -0,0 +1,67 @@
|
|
1
|
+
module StatisticalMethods
|
2
|
+
# Calculation class
|
3
|
+
class Computer
|
4
|
+
def initialize
|
5
|
+
@average = StatisticalMethods::SummaryStatistic::Location::Average
|
6
|
+
@median = StatisticalMethods::SummaryStatistic::Location::Median
|
7
|
+
@mode = StatisticalMethods::SummaryStatistic::Location::Mode
|
8
|
+
end
|
9
|
+
|
10
|
+
def sum(array)
|
11
|
+
array.addition
|
12
|
+
end
|
13
|
+
|
14
|
+
def mean(array)
|
15
|
+
@average.new(array).arithmetic_mean
|
16
|
+
end
|
17
|
+
|
18
|
+
def min(array)
|
19
|
+
array.min
|
20
|
+
end
|
21
|
+
|
22
|
+
def max(array)
|
23
|
+
array.max
|
24
|
+
end
|
25
|
+
|
26
|
+
# http://en.wikipedia.org/wiki/Variance
|
27
|
+
def variance(array)
|
28
|
+
arithmetic_mean = mean(array)
|
29
|
+
mean(array.map { |value| (arithmetic_mean - value)**2 })
|
30
|
+
end
|
31
|
+
|
32
|
+
# http://en.wikipedia.org/wiki/Standard_deviation
|
33
|
+
def standard_deviation(array)
|
34
|
+
Math.sqrt(variance array)
|
35
|
+
end
|
36
|
+
|
37
|
+
# http://en.wikipedia.org/wiki/Median
|
38
|
+
def median(array)
|
39
|
+
@median.new(array).find
|
40
|
+
end
|
41
|
+
|
42
|
+
def harmonic_mean(array)
|
43
|
+
@average.new(array).harmonic_mean
|
44
|
+
end
|
45
|
+
|
46
|
+
def geometric_mean(array)
|
47
|
+
@average.new(array).geometric_mean
|
48
|
+
end
|
49
|
+
|
50
|
+
# http://en.wikipedia.org/wiki/Mode_(statistics)
|
51
|
+
def mode(array)
|
52
|
+
@mode.new(array).find
|
53
|
+
end
|
54
|
+
|
55
|
+
def quadratic_mean(array)
|
56
|
+
@average.new(array).quadratic_mean
|
57
|
+
end
|
58
|
+
|
59
|
+
def power_mean(array, exponent)
|
60
|
+
@average.new(array).power_mean(exponent)
|
61
|
+
end
|
62
|
+
|
63
|
+
def leader(array)
|
64
|
+
Leader.new(array).find
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'statistical_methods/core/array'
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module StatisticalMethods
|
2
|
+
module Core
|
3
|
+
# Extending class
|
4
|
+
module Array
|
5
|
+
def addition
|
6
|
+
reduce(:+)
|
7
|
+
end
|
8
|
+
|
9
|
+
def multiplication
|
10
|
+
reduce(:*)
|
11
|
+
end
|
12
|
+
|
13
|
+
def power(exponent)
|
14
|
+
map { |value| value**exponent }
|
15
|
+
end
|
16
|
+
|
17
|
+
def harmonic
|
18
|
+
map { |value| 1.0 / value }
|
19
|
+
end
|
20
|
+
|
21
|
+
def group_by_count
|
22
|
+
reduce(Hash.new(0)) do |hash, item|
|
23
|
+
hash[item] += 1
|
24
|
+
hash
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Core class Array
|
30
|
+
class ::Array
|
31
|
+
include StatisticalMethods::Core::Array
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module StatisticalMethods
|
2
|
+
# Finding a leader in the collection
|
3
|
+
class Leader
|
4
|
+
def initialize(array)
|
5
|
+
@array = array
|
6
|
+
@candidate = array.first
|
7
|
+
@counter = 0
|
8
|
+
end
|
9
|
+
|
10
|
+
def find
|
11
|
+
@array.each { |value| compare(value) }
|
12
|
+
@candidate if @array.count(@candidate) > @array.size / 2
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def compare(value)
|
18
|
+
@candidate == value ? increment : decrement(value)
|
19
|
+
end
|
20
|
+
|
21
|
+
def increment
|
22
|
+
@counter += 1
|
23
|
+
end
|
24
|
+
|
25
|
+
def decrement(value)
|
26
|
+
@counter -= 1
|
27
|
+
return if @counter == 0
|
28
|
+
@candidate = value
|
29
|
+
@counter = 1
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'statistical_methods/summary_statistic/location'
|
2
|
+
require 'statistical_methods/summary_statistic/spread'
|
3
|
+
require 'statistical_methods/summary_statistic/shape'
|
4
|
+
require 'statistical_methods/summary_statistic/dependence'
|
5
|
+
|
6
|
+
# http://en.wikipedia.org/wiki/Summary_statistic
|
7
|
+
module StatisticalMethods
|
8
|
+
module SummaryStatistic
|
9
|
+
end
|
10
|
+
end
|
File without changes
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'statistical_methods/summary_statistic/location/average'
|
2
|
+
require 'statistical_methods/summary_statistic/location/median'
|
3
|
+
require 'statistical_methods/summary_statistic/location/mode'
|
4
|
+
|
5
|
+
module StatisticalMethods
|
6
|
+
module SummaryStatistic
|
7
|
+
module Location
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
File without changes
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module StatisticalMethods
|
2
|
+
module SummaryStatistic
|
3
|
+
module Location
|
4
|
+
class Average
|
5
|
+
def initialize(array)
|
6
|
+
@array = array
|
7
|
+
@number = @array.size.to_f
|
8
|
+
@empty = @number < 1
|
9
|
+
end
|
10
|
+
|
11
|
+
# http://en.wikipedia.org/wiki/Arithmetic_mean
|
12
|
+
def arithmetic_mean
|
13
|
+
process { @array.addition / @number }
|
14
|
+
end
|
15
|
+
|
16
|
+
# http://en.wikipedia.org/wiki/Geometric_mean
|
17
|
+
def geometric_mean
|
18
|
+
process { @array.multiplication**(1.0 / @number) }
|
19
|
+
end
|
20
|
+
|
21
|
+
# http://en.wikipedia.org/wiki/Harmonic_mean
|
22
|
+
def harmonic_mean
|
23
|
+
process { @number / @array.harmonic.addition } unless include_zero?
|
24
|
+
end
|
25
|
+
|
26
|
+
# http://en.wikipedia.org/wiki/Root_mean_square
|
27
|
+
def quadratic_mean
|
28
|
+
process { Math.sqrt(@array.power(2).addition / @number) }
|
29
|
+
end
|
30
|
+
|
31
|
+
# http://en.wikipedia.org/wiki/Generalized_mean
|
32
|
+
def power_mean(exponent)
|
33
|
+
process do
|
34
|
+
return exception(exponent) if exceptions.include? exponent
|
35
|
+
(@array.power(exponent).addition / @number)**(1.0 / exponent)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def process(&block)
|
42
|
+
block.call unless @empty
|
43
|
+
end
|
44
|
+
|
45
|
+
def include_zero?
|
46
|
+
@array.include?(0.0)
|
47
|
+
end
|
48
|
+
|
49
|
+
def exceptions
|
50
|
+
[-Float::INFINITY, 0, +Float::INFINITY]
|
51
|
+
end
|
52
|
+
|
53
|
+
def exception(exponent)
|
54
|
+
case exponent
|
55
|
+
when 0 then geometric_mean
|
56
|
+
when -Float::INFINITY then @array.min
|
57
|
+
when +Float::INFINITY then @array.max
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module StatisticalMethods
|
2
|
+
module SummaryStatistic
|
3
|
+
module Location
|
4
|
+
class Median
|
5
|
+
def initialize(array)
|
6
|
+
@array = array
|
7
|
+
@sorted = @array.sort
|
8
|
+
@number = @array.size
|
9
|
+
end
|
10
|
+
|
11
|
+
def find
|
12
|
+
return if @array.empty?
|
13
|
+
upper = @number / 2
|
14
|
+
lower = @number.odd? ? upper : upper - 1
|
15
|
+
(@sorted[lower] + @sorted[upper]) / 2.0
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module StatisticalMethods
|
2
|
+
module SummaryStatistic
|
3
|
+
module Location
|
4
|
+
class Mode
|
5
|
+
def initialize(array)
|
6
|
+
@array = array
|
7
|
+
end
|
8
|
+
|
9
|
+
def find
|
10
|
+
return if @array.empty?
|
11
|
+
hash = @array.group_by_count
|
12
|
+
max = hash.values.max
|
13
|
+
hash.keep_if { |_key, value| value == max }.keys
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
File without changes
|
File without changes
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe StatisticalMethods::Core::Array do
|
4
|
+
let(:delta) { 0.001 }
|
5
|
+
|
6
|
+
describe '#addition' do
|
7
|
+
it { expect([].addition).to eq(nil) }
|
8
|
+
it { expect([0].addition).to be_within(delta).of(0) }
|
9
|
+
it { expect([0, 1].addition).to be_within(delta).of(1) }
|
10
|
+
it { expect([0, 1, 2].addition).to be_within(delta).of(3) }
|
11
|
+
it { expect([0, 1, 2, 3].addition).to be_within(delta).of(6) }
|
12
|
+
it { expect([0, 1, 2, 3, 4].addition).to be_within(delta).of(10) }
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '#multiplication' do
|
16
|
+
it { expect([].multiplication).to eq(nil) }
|
17
|
+
it { expect([0].multiplication).to be_within(delta).of(0) }
|
18
|
+
it { expect([1].multiplication).to be_within(delta).of(1) }
|
19
|
+
it { expect([1, 2].multiplication).to be_within(delta).of(2) }
|
20
|
+
it { expect([1, 2, 3].multiplication).to be_within(delta).of(6) }
|
21
|
+
it { expect([1, 2, 3, 4].multiplication).to be_within(delta).of(24) }
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '#power' do
|
25
|
+
it { expect([0].power(1)).to eq([0]) }
|
26
|
+
it { expect([0, 1].power(1)).to eq([0, 1]) }
|
27
|
+
it { expect([0, 1, 2].power(2)).to eq([0, 1, 4]) }
|
28
|
+
it { expect([0, 1, 2, 3].power(3)).to eq([0, 1, 8, 27]) }
|
29
|
+
it { expect([0, 1, 2, 3, 4].power(4)).to eq([0, 1, 16, 81, 256]) }
|
30
|
+
end
|
31
|
+
|
32
|
+
describe '#harmonic' do
|
33
|
+
it { expect([].harmonic).to eq([]) }
|
34
|
+
it { expect([1].harmonic).to eq([1]) }
|
35
|
+
it { expect([1, 2].harmonic).to eq([1, 0.5]) }
|
36
|
+
it { expect([1, 2, 3].harmonic).to eq([1, 0.5, 1.0 / 3.0]) }
|
37
|
+
it { expect([1, 2, 3, 4].harmonic).to eq([1, 0.5, 1.0 / 3.0, 0.25]) }
|
38
|
+
end
|
39
|
+
|
40
|
+
describe '#group_by_count' do
|
41
|
+
it { expect([].group_by_count).to eq({}) }
|
42
|
+
it { expect([0].group_by_count).to eq(0 => 1) }
|
43
|
+
it { expect([0, 1].group_by_count).to eq(0 => 1, 1 => 1) }
|
44
|
+
it { expect([0, 1, 2].group_by_count).to eq(0 => 1, 1 => 1, 2 => 1) }
|
45
|
+
it { expect([0, 1, 1, 1].group_by_count).to eq(0 => 1, 1 => 3) }
|
46
|
+
end
|
47
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'statistical_methods'
|
2
|
+
require 'coveralls'
|
3
|
+
require 'simplecov'
|
4
|
+
require 'codeclimate-test-reporter'
|
5
|
+
|
6
|
+
# Coveralls.wear!
|
7
|
+
# SimpleCov.start
|
8
|
+
# CodeClimate::TestReporter.start
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
config.run_all_when_everything_filtered = true
|
12
|
+
config.filter_run :focus
|
13
|
+
config.order = 'random'
|
14
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe StatisticalMethods::Computer do
|
4
|
+
let(:delta) { 0.001 }
|
5
|
+
|
6
|
+
context '#sum' do
|
7
|
+
it { expect(subject.sum [0]).to be_within(delta).of(0.0) }
|
8
|
+
it { expect(subject.sum [0, 1]).to be_within(delta).of(1.0) }
|
9
|
+
it { expect(subject.sum [0, 1, 2]).to be_within(delta).of(3.0) }
|
10
|
+
it { expect(subject.sum []).to be_nil }
|
11
|
+
end
|
12
|
+
|
13
|
+
context '#variance' do
|
14
|
+
it { expect(subject.variance [0]).to be_within(delta).of(0.0) }
|
15
|
+
it do
|
16
|
+
array = [2, 4, 4, 4, 5, 5, 7, 9]
|
17
|
+
expect(subject.variance array).to be_within(delta).of(4.0)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context '#standard_deviation' do
|
22
|
+
it { expect(subject.standard_deviation [0]).to be_within(delta).of(0.0) }
|
23
|
+
it do
|
24
|
+
array = [2, 4, 4, 4, 5, 5, 7, 9]
|
25
|
+
expect(subject.standard_deviation array).to be_within(delta).of(2.0)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe StatisticalMethods::Leader do
|
4
|
+
shared_examples_for :find do
|
5
|
+
subject { StatisticalMethods::Leader.new(array) }
|
6
|
+
it do
|
7
|
+
expect(subject.find).to eq result
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
context '#find' do
|
12
|
+
it_behaves_like :find do
|
13
|
+
let(:array) { [] }
|
14
|
+
let(:result) { nil }
|
15
|
+
end
|
16
|
+
|
17
|
+
it_behaves_like :find do
|
18
|
+
let(:array) { [0] }
|
19
|
+
let(:result) { 0 }
|
20
|
+
end
|
21
|
+
|
22
|
+
it_behaves_like :find do
|
23
|
+
let(:array) { [1] }
|
24
|
+
let(:result) { 1 }
|
25
|
+
end
|
26
|
+
|
27
|
+
it_behaves_like :find do
|
28
|
+
let(:array) { [0, 1] }
|
29
|
+
let(:result) { nil }
|
30
|
+
end
|
31
|
+
|
32
|
+
it_behaves_like :find do
|
33
|
+
let(:result) { rand(9) }
|
34
|
+
let(:array) { ([result] * 5 + [0, 1, 2, 3]).shuffle }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
File without changes
|
File without changes
|
@@ -0,0 +1,195 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe StatisticalMethods::SummaryStatistic::Location::Average do
|
4
|
+
context '#arithmetic_mean' do
|
5
|
+
shared_examples_for :calculation do
|
6
|
+
let(:delta) { 0.001 }
|
7
|
+
subject { described_class.new(array) }
|
8
|
+
it { expect(subject.arithmetic_mean).to be_within(delta).of(result) }
|
9
|
+
end
|
10
|
+
|
11
|
+
it do
|
12
|
+
subject = described_class.new([])
|
13
|
+
expect(subject.arithmetic_mean).to be_nil
|
14
|
+
end
|
15
|
+
|
16
|
+
it_behaves_like :calculation do
|
17
|
+
let(:array) { [0] }
|
18
|
+
let(:result) { 0 }
|
19
|
+
end
|
20
|
+
|
21
|
+
it_behaves_like :calculation do
|
22
|
+
let(:array) { [0, 1] }
|
23
|
+
let(:result) { 0.5 }
|
24
|
+
end
|
25
|
+
|
26
|
+
it_behaves_like :calculation do
|
27
|
+
let(:array) { [0, 0, 1] }
|
28
|
+
let(:result) { 0.3333 }
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context '#geometric_mean' do
|
33
|
+
shared_examples_for :calculation do
|
34
|
+
let(:delta) { 0.001 }
|
35
|
+
subject { described_class.new(array) }
|
36
|
+
specify { expect(subject.geometric_mean).to be_within(delta).of(result) }
|
37
|
+
end
|
38
|
+
|
39
|
+
it do
|
40
|
+
subject = described_class.new([])
|
41
|
+
expect(subject.geometric_mean).to be_nil
|
42
|
+
end
|
43
|
+
|
44
|
+
it_behaves_like :calculation do
|
45
|
+
let(:array) { [0] }
|
46
|
+
let(:result) { 0 }
|
47
|
+
end
|
48
|
+
|
49
|
+
it_behaves_like :calculation do
|
50
|
+
let(:array) { [1] }
|
51
|
+
let(:result) { 1 }
|
52
|
+
end
|
53
|
+
|
54
|
+
it_behaves_like :calculation do
|
55
|
+
let(:array) { [2, 8] }
|
56
|
+
let(:result) { 4 }
|
57
|
+
end
|
58
|
+
|
59
|
+
it_behaves_like :calculation do
|
60
|
+
let(:array) { [4, 1, 0.03125].shuffle }
|
61
|
+
let(:result) { 0.5 }
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context '#harmonic_mean' do
|
66
|
+
shared_examples_for :calculation do
|
67
|
+
let(:delta) { 0.001 }
|
68
|
+
subject { described_class.new(array) }
|
69
|
+
it { expect(subject.harmonic_mean).to be_within(delta).of(result) }
|
70
|
+
end
|
71
|
+
|
72
|
+
it do
|
73
|
+
subject = described_class.new([])
|
74
|
+
expect(subject.harmonic_mean).to be_nil
|
75
|
+
end
|
76
|
+
|
77
|
+
it do
|
78
|
+
subject = described_class.new([0])
|
79
|
+
expect(subject.harmonic_mean).to be_nil
|
80
|
+
end
|
81
|
+
|
82
|
+
it_behaves_like :calculation do
|
83
|
+
let(:array) { [1] }
|
84
|
+
let(:result) { 1 }
|
85
|
+
end
|
86
|
+
|
87
|
+
it_behaves_like :calculation do
|
88
|
+
let(:array) { [2, 2] }
|
89
|
+
let(:result) { 2 }
|
90
|
+
end
|
91
|
+
|
92
|
+
it_behaves_like :calculation do
|
93
|
+
let(:array) { [1, 2, 4].shuffle }
|
94
|
+
let(:result) { 12.0 / 7.0 }
|
95
|
+
end
|
96
|
+
|
97
|
+
it_behaves_like :calculation do
|
98
|
+
let(:array) { [2, 2, 5, 7].shuffle }
|
99
|
+
let(:result) { 140.0 / 47.0 }
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
context '#quadratic_mean' do
|
104
|
+
shared_examples_for :calculation do
|
105
|
+
let(:delta) { 0.001 }
|
106
|
+
subject { described_class.new(array) }
|
107
|
+
it { expect(subject.quadratic_mean).to be_within(delta).of(result) }
|
108
|
+
end
|
109
|
+
|
110
|
+
it do
|
111
|
+
subject = described_class.new([])
|
112
|
+
expect(subject.quadratic_mean).to be_nil
|
113
|
+
end
|
114
|
+
|
115
|
+
it_behaves_like :calculation do
|
116
|
+
let(:array) { [0] }
|
117
|
+
let(:result) { 0 }
|
118
|
+
end
|
119
|
+
|
120
|
+
it_behaves_like :calculation do
|
121
|
+
let(:array) { [1] }
|
122
|
+
let(:result) { 1 }
|
123
|
+
end
|
124
|
+
|
125
|
+
it_behaves_like :calculation do
|
126
|
+
let(:array) { [1, 1] }
|
127
|
+
let(:result) { 1 }
|
128
|
+
end
|
129
|
+
|
130
|
+
it_behaves_like :calculation do
|
131
|
+
let(:array) { [2, 2] }
|
132
|
+
let(:result) { 2 }
|
133
|
+
end
|
134
|
+
|
135
|
+
it_behaves_like :calculation do
|
136
|
+
let(:array) { [2, 2, 5, 7].shuffle }
|
137
|
+
let(:delta) { 0.01 }
|
138
|
+
let(:result) { 4.53 }
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
context '#power_mean' do
|
143
|
+
shared_examples_for :calculation do
|
144
|
+
let(:delta) { 0.001 }
|
145
|
+
let(:exponent) { 0 }
|
146
|
+
subject { described_class.new(array) }
|
147
|
+
it do
|
148
|
+
expect(subject.power_mean exponent).to be_within(delta).of(result)
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
it do
|
153
|
+
subject = described_class.new([])
|
154
|
+
expect(subject.power_mean 0).to be_nil
|
155
|
+
end
|
156
|
+
|
157
|
+
it_behaves_like :calculation do
|
158
|
+
let(:array) { [2, 8] }
|
159
|
+
let(:exponent) { 0 }
|
160
|
+
let(:result) { 4 }
|
161
|
+
end
|
162
|
+
|
163
|
+
it_behaves_like :calculation do
|
164
|
+
let(:array) { [2, 8] }
|
165
|
+
let(:exponent) { 1 }
|
166
|
+
let(:result) { 5 }
|
167
|
+
end
|
168
|
+
|
169
|
+
it_behaves_like :calculation do
|
170
|
+
let(:array) { [2, 8] }
|
171
|
+
let(:exponent) { 2 }
|
172
|
+
let(:delta) { 0.01 }
|
173
|
+
let(:result) { 5.83 }
|
174
|
+
end
|
175
|
+
|
176
|
+
it_behaves_like :calculation do
|
177
|
+
let(:array) { [1, 2, 3, 4, 5] }
|
178
|
+
let(:exponent) { 3 }
|
179
|
+
let(:delta) { 0.01 }
|
180
|
+
let(:result) { 3.56 }
|
181
|
+
end
|
182
|
+
|
183
|
+
it_behaves_like :calculation do
|
184
|
+
let(:array) { [2, 8] }
|
185
|
+
let(:exponent) { -Float::INFINITY }
|
186
|
+
let(:result) { 2 }
|
187
|
+
end
|
188
|
+
|
189
|
+
it_behaves_like :calculation do
|
190
|
+
let(:array) { [2, 8] }
|
191
|
+
let(:exponent) { +Float::INFINITY }
|
192
|
+
let(:result) { 8 }
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe StatisticalMethods::SummaryStatistic::Location::Median do
|
4
|
+
context '#median' do
|
5
|
+
shared_examples_for :calculation do
|
6
|
+
let(:delta) { 0.001 }
|
7
|
+
subject { described_class.new(array) }
|
8
|
+
it { expect(subject.find).to be_within(delta).of(result) }
|
9
|
+
end
|
10
|
+
|
11
|
+
it do
|
12
|
+
subject = described_class.new([])
|
13
|
+
expect(subject.find).to be_nil
|
14
|
+
end
|
15
|
+
|
16
|
+
it_behaves_like :calculation do
|
17
|
+
let(:array) { [0] }
|
18
|
+
let(:result) { 0.0 }
|
19
|
+
end
|
20
|
+
|
21
|
+
it_behaves_like :calculation do
|
22
|
+
let(:array) { [0, 1] }
|
23
|
+
let(:result) { 0.5 }
|
24
|
+
end
|
25
|
+
|
26
|
+
it_behaves_like :calculation do
|
27
|
+
let(:array) { [0, 1, 2] }
|
28
|
+
let(:result) { 1.0 }
|
29
|
+
end
|
30
|
+
|
31
|
+
it_behaves_like :calculation do
|
32
|
+
let(:array) { [1, 2, 2, 3, 4, 6, 6, 7, 7, 7].shuffle }
|
33
|
+
let(:result) { 5.0 }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe StatisticalMethods::SummaryStatistic::Location::Mode do
|
4
|
+
context '#mode' do
|
5
|
+
shared_examples_for :calculation do
|
6
|
+
subject { described_class.new(array) }
|
7
|
+
it { expect(subject.find).to eq(result) }
|
8
|
+
end
|
9
|
+
|
10
|
+
it do
|
11
|
+
subject = described_class.new([])
|
12
|
+
expect(subject.find).to be_nil
|
13
|
+
end
|
14
|
+
|
15
|
+
it_behaves_like :calculation do
|
16
|
+
let(:array) { [0] }
|
17
|
+
let(:result) { [0] }
|
18
|
+
end
|
19
|
+
|
20
|
+
it_behaves_like :calculation do
|
21
|
+
let(:array) { [0, 0, 1] }
|
22
|
+
let(:result) { [0] }
|
23
|
+
end
|
24
|
+
|
25
|
+
it_behaves_like :calculation do
|
26
|
+
let(:array) { [0, 1, 1] }
|
27
|
+
let(:result) { [1] }
|
28
|
+
end
|
29
|
+
|
30
|
+
it_behaves_like :calculation do
|
31
|
+
let(:array) { [0, 1, 2, 1, 0] }
|
32
|
+
let(:result) { [0, 1] }
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
File without changes
|
File without changes
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'statistical_methods/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'statistical_methods'
|
8
|
+
spec.version = StatisticalMethods::VERSION
|
9
|
+
spec.authors = ['Aleksander Malaszkiewicz']
|
10
|
+
spec.email = ['info@fractalsoft.org']
|
11
|
+
spec.summary = %q{Statistical methods}
|
12
|
+
spec.homepage = ''
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = %w[lib]
|
19
|
+
|
20
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
21
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: statistical_methods
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Aleksander Malaszkiewicz
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-06-30 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.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- info@fractalsoft.org
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- ".rspec"
|
50
|
+
- ".rubocop.yml"
|
51
|
+
- ".ruby-gemset"
|
52
|
+
- ".ruby-version"
|
53
|
+
- ".travis.yml"
|
54
|
+
- Gemfile
|
55
|
+
- Guardfile
|
56
|
+
- LICENSE.txt
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- lib/statistical_methods.rb
|
60
|
+
- lib/statistical_methods/computer.rb
|
61
|
+
- lib/statistical_methods/core.rb
|
62
|
+
- lib/statistical_methods/core/array.rb
|
63
|
+
- lib/statistical_methods/leader.rb
|
64
|
+
- lib/statistical_methods/summary_statistic.rb
|
65
|
+
- lib/statistical_methods/summary_statistic/dependence.rb
|
66
|
+
- lib/statistical_methods/summary_statistic/dependence/.keep
|
67
|
+
- lib/statistical_methods/summary_statistic/location.rb
|
68
|
+
- lib/statistical_methods/summary_statistic/location/.keep
|
69
|
+
- lib/statistical_methods/summary_statistic/location/average.rb
|
70
|
+
- lib/statistical_methods/summary_statistic/location/median.rb
|
71
|
+
- lib/statistical_methods/summary_statistic/location/mode.rb
|
72
|
+
- lib/statistical_methods/summary_statistic/shape.rb
|
73
|
+
- lib/statistical_methods/summary_statistic/shape/.keep
|
74
|
+
- lib/statistical_methods/summary_statistic/spread.rb
|
75
|
+
- lib/statistical_methods/summary_statistic/spread/.keep
|
76
|
+
- lib/statistical_methods/summary_statistic/spread/statistical_dispersion.rb
|
77
|
+
- lib/statistical_methods/version.rb
|
78
|
+
- spec/core/array_spec.rb
|
79
|
+
- spec/spec_helper.rb
|
80
|
+
- spec/statistical_methods/computer_spec.rb
|
81
|
+
- spec/statistical_methods/leader_spec.rb
|
82
|
+
- spec/statistical_methods/summary_statistic/dependence/.keep
|
83
|
+
- spec/statistical_methods/summary_statistic/location/.keep
|
84
|
+
- spec/statistical_methods/summary_statistic/location/average_spec.rb
|
85
|
+
- spec/statistical_methods/summary_statistic/location/median_spec.rb
|
86
|
+
- spec/statistical_methods/summary_statistic/location/mode_spec.rb
|
87
|
+
- spec/statistical_methods/summary_statistic/shape/.keep
|
88
|
+
- spec/statistical_methods/summary_statistic/spread/.keep
|
89
|
+
- statistical_methods.gemspec
|
90
|
+
homepage: ''
|
91
|
+
licenses:
|
92
|
+
- MIT
|
93
|
+
metadata: {}
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 2.4.7
|
111
|
+
signing_key:
|
112
|
+
specification_version: 4
|
113
|
+
summary: Statistical methods
|
114
|
+
test_files:
|
115
|
+
- spec/core/array_spec.rb
|
116
|
+
- spec/spec_helper.rb
|
117
|
+
- spec/statistical_methods/computer_spec.rb
|
118
|
+
- spec/statistical_methods/leader_spec.rb
|
119
|
+
- spec/statistical_methods/summary_statistic/dependence/.keep
|
120
|
+
- spec/statistical_methods/summary_statistic/location/.keep
|
121
|
+
- spec/statistical_methods/summary_statistic/location/average_spec.rb
|
122
|
+
- spec/statistical_methods/summary_statistic/location/median_spec.rb
|
123
|
+
- spec/statistical_methods/summary_statistic/location/mode_spec.rb
|
124
|
+
- spec/statistical_methods/summary_statistic/shape/.keep
|
125
|
+
- spec/statistical_methods/summary_statistic/spread/.keep
|