descriptive_statistics 1.1.5 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 36155709ef3b8ff6a6a38dd0335e3d0038fce127
4
- data.tar.gz: 7aa6db66838780f71554af0917525ad2afb7d3a6
3
+ metadata.gz: 3692ef50fac5b204943e2f89a12954beaf2b8f5e
4
+ data.tar.gz: 3ebba1de3d2119bc7bd4576e6b31ab12d5e21ce5
5
5
  SHA512:
6
- metadata.gz: 4cf3f0c2f98e7d526303c77e1a3242d7180461cbbbfeb4a868840c0acca50b875d14ce346a2f79aeb3f7aad950f7b78dd76053b715d067e02066042af5c933d0
7
- data.tar.gz: 3571f09ba9a4d1b3b4c9ca60ee2c37e81225eaa1eb00e46f8d67d50d5cdc155f1b3bf771c507e84bb68679971ae1d4fc916a6dfb5152d286fa59dfcd24f1434d
6
+ metadata.gz: 2dc2caafb81f25a3e4432c2b0ef4c8f373416ed945e2faa153e743bc062210787249770182ec58795571f5935caab4c509a07d2ea435adfa6f61988fb59d5f5f
7
+ data.tar.gz: 11adc15ecd3ed6220a317d838e88d2b42d271fc8b13153e7de549a38286212ee26262e332fcdfe5884a4e96864bf1556a85bec8f743bac403e950929004f1e08
@@ -1,6 +1,6 @@
1
1
  module DescriptiveStatistics
2
2
  def descriptive_statistics
3
- return { :number => self.size.to_f,
3
+ return { :number => self.number,
4
4
  :sum => self.sum,
5
5
  :variance => self.variance,
6
6
  :standard_deviation => self.standard_deviation,
@@ -1,5 +1,8 @@
1
1
  module DescriptiveStatistics
2
2
  def mean
3
- self.sum / self.number
3
+ values = Support::convert(self)
4
+ return unless values.size > 0
5
+
6
+ values.sum / values.number
4
7
  end
5
8
  end
@@ -1,5 +1,8 @@
1
1
  module DescriptiveStatistics
2
2
  def median
3
- self.percentile(50)
3
+ values = Support::convert(self)
4
+ return unless values.size > 0
5
+
6
+ values.percentile(50)
4
7
  end
5
8
  end
@@ -1,5 +1,12 @@
1
1
  module DescriptiveStatistics
2
2
  def mode
3
- self.group_by { |e| e }.values.max_by(&:size).first
3
+ values = Support::extract(self)
4
+ return unless values.size > 0
5
+
6
+ values
7
+ .group_by { |e| e }
8
+ .values
9
+ .max_by(&:size)
10
+ .first
4
11
  end
5
12
  end
@@ -1,5 +1,7 @@
1
1
  module DescriptiveStatistics
2
2
  def number
3
- self.size.to_f
3
+ values = Support::extract(self)
4
+
5
+ values.size.to_f
4
6
  end
5
7
  end
@@ -1,12 +1,15 @@
1
1
  module DescriptiveStatistics
2
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)
3
+ values = Support::convert(self)
4
+ return unless values.size > 0
5
+
6
+ sorted = values.sort
7
+ return sorted[-1] if p == 100
8
+ rank = p / 100.0 * (values.number - 1)
6
9
  lrank = rank.floor
7
10
  d = rank - lrank
8
- lower = sorted[lrank].to_f
9
- upper = sorted[lrank+1].to_f
11
+ lower = sorted[lrank]
12
+ upper = sorted[lrank+1]
10
13
  lower + (upper - lower) * d
11
14
  end
12
15
  end
@@ -1,6 +1,9 @@
1
1
  module DescriptiveStatistics
2
2
  # percent of cases that are at or below a score
3
3
  def percentile_rank(p)
4
- return (((sort.rindex{ |x| x <= p} || -1.0) + 1.0)) / number * 100.0
4
+ values = Support::convert(self)
5
+ return unless values.size > 0
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
2
  def range
3
- return 0 unless self.number > 0
4
- self.max - self.min
3
+ values = Support::convert(self)
4
+ return unless values.size > 0
5
+
6
+ values.max - values.min
5
7
  end
6
8
  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'
@@ -1,5 +1,8 @@
1
1
  module DescriptiveStatistics
2
2
  def standard_deviation
3
- Math.sqrt(self.variance)
3
+ values = Support::convert(self)
4
+ return unless values.size > 0
5
+
6
+ Math.sqrt(values.variance)
4
7
  end
5
8
  end
@@ -1,5 +1,8 @@
1
1
  module DescriptiveStatistics
2
2
  def sum
3
- return self.inject(:+)
3
+ values = Support::convert(self)
4
+ return unless values.size > 0
5
+
6
+ return values.inject(:+)
4
7
  end
5
8
  end
@@ -0,0 +1,38 @@
1
+ require 'set'
2
+
3
+ module DescriptiveStatistics
4
+
5
+ module Support
6
+
7
+ def self.convert(from_enumerable)
8
+ extend to_float to_array from_enumerable
9
+ end
10
+
11
+ def self.extract(from_enumerable)
12
+ extend to_array from_enumerable
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_array(enumerable)
26
+ case enumerable
27
+ when Hash
28
+ enumerable.values.each
29
+ when Set
30
+ enumerable.to_a.each
31
+ else
32
+ enumerable.each
33
+ end
34
+ end
35
+
36
+ end
37
+
38
+ end
@@ -1,6 +1,9 @@
1
1
  module DescriptiveStatistics
2
2
  def variance
3
- mean = self.mean
4
- self.map{ |sample| (mean - sample) ** 2 }.sum / self.number
3
+ values = Support::convert(self)
4
+ return unless values.size > 0
5
+
6
+ mean = values.mean
7
+ values.map{ |sample| (mean - sample) ** 2 }.inject(:+) / 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.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Derrick Parkhurst
@@ -12,9 +12,10 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2014-05-29 00:00:00.000000000 Z
15
+ date: 2014-08-30 00:00:00.000000000 Z
16
16
  dependencies: []
17
- description: Adds descriptive statistics methods to Enumerable for use on collections
17
+ description: Adds descriptive statistics methods to Enumerable module for use on collections
18
+ or Numeric data
18
19
  email: derrick.parkhurst@gmail.com
19
20
  executables: []
20
21
  extensions: []
@@ -33,6 +34,7 @@ files:
33
34
  - lib/descriptive_statistics/safe.rb
34
35
  - lib/descriptive_statistics/standard_deviation.rb
35
36
  - lib/descriptive_statistics/sum.rb
37
+ - lib/descriptive_statistics/support/convert.rb
36
38
  - lib/descriptive_statistics/variance.rb
37
39
  homepage: https://github.com/thirtysixthspan/descriptive_statistics
38
40
  licenses: