descriptive_statistics 1.1.5 → 2.5.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,15 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 36155709ef3b8ff6a6a38dd0335e3d0038fce127
4
- data.tar.gz: 7aa6db66838780f71554af0917525ad2afb7d3a6
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ OWY4YTM5Y2MxOTc3Mzc5MWYzNzk1NjRmZjVlYzljYjY1MDQwYWIwMg==
5
+ data.tar.gz: !binary |-
6
+ ZTA4MTc2MmY4NjMzM2EzOTJmYTkyYTJjODk0NGE5MjM5NjU0OWMwNA==
5
7
  SHA512:
6
- metadata.gz: 4cf3f0c2f98e7d526303c77e1a3242d7180461cbbbfeb4a868840c0acca50b875d14ce346a2f79aeb3f7aad950f7b78dd76053b715d067e02066042af5c933d0
7
- data.tar.gz: 3571f09ba9a4d1b3b4c9ca60ee2c37e81225eaa1eb00e46f8d67d50d5cdc155f1b3bf771c507e84bb68679971ae1d4fc916a6dfb5152d286fa59dfcd24f1434d
8
+ metadata.gz: !binary |-
9
+ ODE0YmJlMWIwZTRkN2FhODU2MGU4MzZmMTJlMTBlNWJkNjE3MmIzNDlhZDIx
10
+ NDc2ZTRmMzNiYzU1OGEyNWFjZmE2MmJkNjNhMjUxYzdkOGRkNTc0YmQxZjky
11
+ OTE0ZTc1OGQwOTlmYWFlNGNmZmJhMmNmMGIxY2YxZTBlYTg4MTA=
12
+ data.tar.gz: !binary |-
13
+ NDNjZTg1OTQ4ZjJkMWQxNWU2ZGUwOThmZjM0MDdiMGI2MmM1NTg0OWZjMjNj
14
+ ZGVkNTBmYWIwN2M2NDZmYTI5MGNhYjBiODIwMzQ2ZGY2MTczZjE3MDQxNjQ3
15
+ YjY2ODIwNjUwMmRhZGUwMDZlZDA1YjVkYWViNjMxMWVmNDgyZGU=
@@ -0,0 +1,36 @@
1
+ module DescriptiveStatistics
2
+
3
+ class << self
4
+
5
+ def empty_collection_default_value
6
+ @empty_collection_default_value
7
+ end
8
+
9
+ def empty_collection_default_value=(value)
10
+ @empty_collection_default_value = value
11
+ DescriptiveStatistics.instance_methods.each { |m| default_values[m] = value }
12
+ end
13
+
14
+ DescriptiveStatistics.instance_methods.each do |m|
15
+ define_method("#{m}_empty_collection_default_value") do
16
+ default_values[m]
17
+ end
18
+ define_method("#{m}_empty_collection_default_value=") do |value|
19
+ default_values[m] = value
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def default_values
26
+ @default_values ||= {}
27
+ end
28
+
29
+ end
30
+
31
+ DescriptiveStatistics.instance_methods.each do |method|
32
+ module_function method
33
+ public method
34
+ end
35
+
36
+ end
@@ -1,17 +1,17 @@
1
1
  module DescriptiveStatistics
2
- def descriptive_statistics
3
- return { :number => self.size.to_f,
4
- :sum => self.sum,
5
- :variance => self.variance,
6
- :standard_deviation => self.standard_deviation,
7
- :min => self.min,
8
- :max => self.max,
9
- :mean => self.mean,
10
- :mode => self.mode,
11
- :median => self.median,
12
- :range => self.range,
13
- :q1 => self.percentile(25),
14
- :q2 => self.percentile(50),
15
- :q3 => self.percentile(75) }
2
+ def descriptive_statistics(&block)
3
+ return { :number => self.number(&block),
4
+ :sum => self.sum(&block),
5
+ :variance => self.variance(&block),
6
+ :standard_deviation => self.standard_deviation(&block),
7
+ :min => self.min(&block),
8
+ :max => self.max(&block),
9
+ :mean => self.mean(&block),
10
+ :mode => self.mode(&block),
11
+ :median => self.median(&block),
12
+ :range => self.range(&block),
13
+ :q1 => self.percentile(25, &block),
14
+ :q2 => self.percentile(50, &block),
15
+ :q3 => self.percentile(75, &block) }
16
16
  end
17
17
  end
@@ -1,11 +1,9 @@
1
1
  module Enumerable
2
2
  include DescriptiveStatistics
3
3
 
4
- # This is necessary because otherwise objects which
5
- # have already included Enumerable (such as Array) won't
6
- # be able to access DescriptiveStatistics's methods.
7
- # It is an evil hack though :-/
8
- DescriptiveStatistics.instance_methods.each do |m|
9
- define_method(m, DescriptiveStatistics.instance_method(m))
4
+ DescriptiveStatistics.instance_methods.each do |name|
5
+ method = DescriptiveStatistics.instance_method(name)
6
+ define_method(name, method)
10
7
  end
8
+
11
9
  end
@@ -1,5 +1,8 @@
1
1
  module DescriptiveStatistics
2
- def mean
3
- self.sum / self.number
4
- end
2
+ def mean(collection = self, &block)
3
+ values = Support::convert(collection, &block)
4
+ return DescriptiveStatistics.mean_empty_collection_default_value if values.empty?
5
+
6
+ values.sum / values.number
7
+ end
5
8
  end
@@ -1,5 +1,8 @@
1
1
  module DescriptiveStatistics
2
- def median
3
- self.percentile(50)
4
- end
2
+ def median(collection = self, &block)
3
+ values = Support::convert(collection, &block)
4
+ return DescriptiveStatistics.median_empty_collection_default_value if values.empty?
5
+
6
+ values.percentile(50)
7
+ end
5
8
  end
@@ -1,5 +1,12 @@
1
1
  module DescriptiveStatistics
2
- def mode
3
- self.group_by { |e| e }.values.max_by(&:size).first
4
- end
2
+ def mode(collection = self, &block)
3
+ values = Support::extract(collection, &block)
4
+ return if values.to_a.empty?
5
+
6
+ values
7
+ .group_by { |e| e }
8
+ .values
9
+ .max_by(&:size)
10
+ .first
11
+ end
5
12
  end
@@ -1,5 +1,7 @@
1
1
  module DescriptiveStatistics
2
- def number
3
- self.size.to_f
2
+ def number(collection = self, &block)
3
+ values = Support::extract(collection, &block)
4
+
5
+ values.to_a.size.to_f
4
6
  end
5
7
  end
@@ -1,12 +1,14 @@
1
1
  module DescriptiveStatistics
2
- def percentile(p)
3
- sorted = self.sort
4
- return sorted[-1].to_f if p == 100
5
- rank = p.to_f / 100.to_f * (self.number - 1)
6
- lrank = rank.floor
7
- d = rank - lrank
8
- lower = sorted[lrank].to_f
9
- upper = sorted[lrank+1].to_f
10
- lower + (upper - lower) * d
2
+ def percentile(p, collection = self, &block)
3
+ values = Support::convert(collection, &block)
4
+
5
+ return DescriptiveStatistics.percentile_empty_collection_default_value if values.empty?
6
+ return values.first if values.size == 1
7
+
8
+ values.sort!
9
+ return values.last if p == 100
10
+ rank = p / 100.0 * (values.size - 1)
11
+ lower, upper = values[rank.floor,2]
12
+ lower + (upper - lower) * (rank - rank.floor)
11
13
  end
12
14
  end
@@ -1,6 +1,9 @@
1
1
  module DescriptiveStatistics
2
2
  # percent of cases that are at or below a score
3
- def percentile_rank(p)
4
- return (((sort.rindex{ |x| x <= p} || -1.0) + 1.0)) / number * 100.0
3
+ def percentile_rank(p, collection = self, &block)
4
+ values = Support::convert(collection, &block)
5
+ return DescriptiveStatistics.percentile_rank_empty_collection_default_value if values.empty?
6
+
7
+ return (((values.sort.rindex { |x| x <= p } || -1.0) + 1.0)) / values.number * 100.0
5
8
  end
6
9
  end
@@ -1,6 +1,8 @@
1
1
  module DescriptiveStatistics
2
- def range
3
- return 0 unless self.number > 0
4
- self.max - self.min
5
- end
2
+ def range(collection = self, &block)
3
+ values = Support::convert(collection, &block)
4
+ return DescriptiveStatistics.range_empty_collection_default_value if values.empty?
5
+
6
+ values.max - values.min
7
+ end
6
8
  end
@@ -0,0 +1,33 @@
1
+ require "descriptive_statistics/safe"
2
+
3
+ module DescriptiveStatistics
4
+
5
+ module Refinement
6
+
7
+ def self.new(*klasses)
8
+ refinement_module = Module.new
9
+
10
+ klasses.each do |klass|
11
+
12
+ refinement_module.instance_eval do
13
+
14
+ refine klass do
15
+
16
+ DescriptiveStatistics.instance_methods.each do |name|
17
+ method = DescriptiveStatistics.instance_method(name)
18
+ define_method(name, method)
19
+ end
20
+
21
+ end
22
+
23
+ end
24
+
25
+ end
26
+
27
+ return refinement_module
28
+
29
+ end
30
+
31
+ end
32
+
33
+ end
@@ -1,3 +1,4 @@
1
+ require "descriptive_statistics/support/convert"
1
2
  require 'descriptive_statistics/number.rb'
2
3
  require 'descriptive_statistics/sum.rb'
3
4
  require 'descriptive_statistics/mean.rb'
@@ -9,4 +10,6 @@ require 'descriptive_statistics/percentile.rb'
9
10
  require 'descriptive_statistics/percentile_rank.rb'
10
11
  require 'descriptive_statistics/range.rb'
11
12
  require 'descriptive_statistics/descriptive_statistics.rb'
13
+ require 'descriptive_statistics/stats.rb'
14
+ require 'descriptive_statistics/class_methods.rb'
12
15
 
@@ -1,5 +1,8 @@
1
1
  module DescriptiveStatistics
2
- def standard_deviation
3
- Math.sqrt(self.variance)
2
+ def standard_deviation(collection = self, &block)
3
+ values = Support::convert(collection, &block)
4
+ return DescriptiveStatistics.standard_deviation_empty_collection_default_value if values.empty?
5
+
6
+ Math.sqrt(values.variance)
4
7
  end
5
8
  end
@@ -0,0 +1,9 @@
1
+ require 'delegate'
2
+
3
+ module DescriptiveStatistics
4
+
5
+ class Stats < SimpleDelegator
6
+ include DescriptiveStatistics
7
+ end
8
+
9
+ end
@@ -1,5 +1,8 @@
1
1
  module DescriptiveStatistics
2
- def sum
3
- return self.inject(:+)
4
- end
2
+ def sum(collection = self, &block)
3
+ values = Support::convert(collection, &block)
4
+ return DescriptiveStatistics.sum_empty_collection_default_value if values.empty?
5
+
6
+ return values.reduce(:+)
7
+ end
5
8
  end
@@ -0,0 +1,43 @@
1
+ require 'set'
2
+
3
+ module DescriptiveStatistics
4
+
5
+ module Support
6
+
7
+ def self.convert(from_enumerable, &block)
8
+ extend to_float to_value(to_array(from_enumerable), &block)
9
+ end
10
+
11
+ def self.extract(from_enumerable, &block)
12
+ extend to_value(to_array(from_enumerable), &block)
13
+ end
14
+
15
+ private
16
+
17
+ def self.extend(enumerable)
18
+ enumerable.extend(DescriptiveStatistics)
19
+ end
20
+
21
+ def self.to_float(enumerable)
22
+ enumerable.map(&:to_f)
23
+ end
24
+
25
+ def self.to_value(enumerable, &block)
26
+ return enumerable unless block_given?
27
+ enumerable.map { |object| yield object }
28
+ end
29
+
30
+ def self.to_array(enumerable)
31
+ case enumerable
32
+ when Hash
33
+ enumerable.values.each
34
+ when Set
35
+ enumerable.to_a.each
36
+ else
37
+ enumerable.each
38
+ end
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -1,6 +1,9 @@
1
1
  module DescriptiveStatistics
2
- def variance
3
- mean = self.mean
4
- self.map{ |sample| (mean - sample) ** 2 }.sum / self.number
2
+ def variance(collection = self, &block)
3
+ values = Support::convert(collection, &block)
4
+ return DescriptiveStatistics.variance_empty_collection_default_value if values.empty?
5
+
6
+ mean = values.mean
7
+ values.map { |sample| (mean - sample) ** 2 }.reduce(:+) / values.number
5
8
  end
6
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: descriptive_statistics
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.5
4
+ version: 2.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Derrick Parkhurst
@@ -9,18 +9,21 @@ authors:
9
9
  - Daniel Farrell
10
10
  - Graham Malmgren
11
11
  - Guy Shechter
12
+ - Charlie Egan
12
13
  autorequire:
13
14
  bindir: bin
14
15
  cert_chain: []
15
- date: 2014-05-29 00:00:00.000000000 Z
16
+ date: 2014-12-19 00:00:00.000000000 Z
16
17
  dependencies: []
17
- description: Adds descriptive statistics methods to Enumerable for use on collections
18
+ description: Adds descriptive statistics methods to Enumerable module for use on collections
19
+ or Numeric data
18
20
  email: derrick.parkhurst@gmail.com
19
21
  executables: []
20
22
  extensions: []
21
23
  extra_rdoc_files: []
22
24
  files:
23
25
  - lib/descriptive_statistics.rb
26
+ - lib/descriptive_statistics/class_methods.rb
24
27
  - lib/descriptive_statistics/descriptive_statistics.rb
25
28
  - lib/descriptive_statistics/enumerable_extension.rb
26
29
  - lib/descriptive_statistics/mean.rb
@@ -30,9 +33,12 @@ files:
30
33
  - lib/descriptive_statistics/percentile.rb
31
34
  - lib/descriptive_statistics/percentile_rank.rb
32
35
  - lib/descriptive_statistics/range.rb
36
+ - lib/descriptive_statistics/refinement.rb
33
37
  - lib/descriptive_statistics/safe.rb
34
38
  - lib/descriptive_statistics/standard_deviation.rb
39
+ - lib/descriptive_statistics/stats.rb
35
40
  - lib/descriptive_statistics/sum.rb
41
+ - lib/descriptive_statistics/support/convert.rb
36
42
  - lib/descriptive_statistics/variance.rb
37
43
  homepage: https://github.com/thirtysixthspan/descriptive_statistics
38
44
  licenses:
@@ -44,17 +50,17 @@ require_paths:
44
50
  - lib
45
51
  required_ruby_version: !ruby/object:Gem::Requirement
46
52
  requirements:
47
- - - ">="
53
+ - - ! '>='
48
54
  - !ruby/object:Gem::Version
49
55
  version: '0'
50
56
  required_rubygems_version: !ruby/object:Gem::Requirement
51
57
  requirements:
52
- - - ">="
58
+ - - ! '>='
53
59
  - !ruby/object:Gem::Version
54
60
  version: '0'
55
61
  requirements: []
56
62
  rubyforge_project:
57
- rubygems_version: 2.2.2
63
+ rubygems_version: 2.4.3
58
64
  signing_key:
59
65
  specification_version: 4
60
66
  summary: Descriptive Statistics