descriptive_statistics 2.3.0 → 2.4.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: e1879dad6f99acc6168dc63b2574ca72b3225d05
4
- data.tar.gz: 1cc44d9e5259ba71a343556886a0ebc814800475
3
+ metadata.gz: e28f48a7fc39460fc375049b65f79956f52cec0b
4
+ data.tar.gz: 71570533e928cc63aaab9afbf7b16d02930b13d0
5
5
  SHA512:
6
- metadata.gz: af8be7209ff639f578b7ddd01eebcd4149dfe75d94607d99964aafa353823a141737c34852213e46fba62877e36ab22905f768d7b3b2b9d27214c6724e252a58
7
- data.tar.gz: 5180ca2d716200159d3843faf882a12ef167020d65dcca31efdf010368318c323538f564ea81d6337e13bcb19069a529c250ec8e637443ed3c0fa8b8d5679b0e
6
+ metadata.gz: 4c95bbdcfc03c5a90a70a0b165a38cc12f42601c6a4b4202ade3adf5793e87359a7f1acc975c7c89d4bbec9076b860cff29fa1f7d10eac2ca930c23eb4895205
7
+ data.tar.gz: 0c4f66b5cb53468b89f0d6d88c7214ce4f295adf0f4dd3d9cc96f0ccc583df684a1f1b84b476992c3dd6a88bcf0b4ff580f738bbbdd286ff9bc1e68bf5c81bce
@@ -17,19 +17,20 @@ module DescriptiveStatistics
17
17
  end
18
18
  define_method("#{m}_empty_collection_default_value=") do |value|
19
19
  default_values[m] = value
20
- end
21
- end
22
-
23
- DescriptiveStatistics.instance_methods.each do |m|
24
- define_method(m, DescriptiveStatistics.instance_method(m))
20
+ end
25
21
  end
26
22
 
27
23
  private
28
24
 
29
25
  def default_values
30
- @default_values ||= {}
26
+ @default_values ||= {}
31
27
  end
32
28
 
33
29
  end
34
30
 
31
+ DescriptiveStatistics.instance_methods.each do |method|
32
+ module_function method
33
+ public method
34
+ end
35
+
35
36
  end
@@ -1,17 +1,17 @@
1
1
  module DescriptiveStatistics
2
- def descriptive_statistics
3
- return { :number => self.number,
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,8 +1,8 @@
1
1
  module DescriptiveStatistics
2
- def mean(collection = self)
3
- values = Support::convert(collection)
4
- return DescriptiveStatistics.mean_empty_collection_default_value unless values.size > 0
2
+ def mean(collection = self, &block)
3
+ values = Support::convert(collection, &block)
4
+ return DescriptiveStatistics.mean_empty_collection_default_value if values.empty?
5
5
 
6
6
  values.sum / values.number
7
- end
7
+ end
8
8
  end
@@ -1,8 +1,8 @@
1
1
  module DescriptiveStatistics
2
- def median(collection = self)
3
- values = Support::convert(collection)
4
- return DescriptiveStatistics.median_empty_collection_default_value unless values.size > 0
2
+ def median(collection = self, &block)
3
+ values = Support::convert(collection, &block)
4
+ return DescriptiveStatistics.median_empty_collection_default_value if values.empty?
5
5
 
6
6
  values.percentile(50)
7
- end
7
+ end
8
8
  end
@@ -1,12 +1,12 @@
1
1
  module DescriptiveStatistics
2
- def mode(collection = self)
3
- values = Support::extract(collection)
4
- return unless values.size > 0
2
+ def mode(collection = self, &block)
3
+ values = Support::extract(collection, &block)
4
+ return if values.to_a.empty?
5
5
 
6
6
  values
7
7
  .group_by { |e| e }
8
8
  .values
9
9
  .max_by(&:size)
10
10
  .first
11
- end
11
+ end
12
12
  end
@@ -1,7 +1,7 @@
1
1
  module DescriptiveStatistics
2
- def number(collection = self)
3
- values = Support::extract(collection)
2
+ def number(collection = self, &block)
3
+ values = Support::extract(collection, &block)
4
4
 
5
- values.size.to_f
5
+ values.to_a.size.to_f
6
6
  end
7
7
  end
@@ -1,17 +1,14 @@
1
1
  module DescriptiveStatistics
2
- def percentile(p, collection = self)
3
- values = Support::convert(collection)
4
- return DescriptiveStatistics.percentile_empty_collection_default_value unless values.size > 0
2
+ def percentile(p, collection = self, &block)
3
+ values = Support::convert(collection, &block)
5
4
 
6
- return values.first unless values.size > 1
5
+ return DescriptiveStatistics.percentile_empty_collection_default_value if values.empty?
6
+ return values.first if values.size == 1
7
7
 
8
- sorted = values.sort
9
- return sorted[-1] if p == 100
10
- rank = p / 100.0 * (values.number - 1)
11
- lrank = rank.floor
12
- d = rank - lrank
13
- lower = sorted[lrank]
14
- upper = sorted[lrank+1]
15
- lower + (upper - lower) * d
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)
16
13
  end
17
14
  end
@@ -1,9 +1,9 @@
1
1
  module DescriptiveStatistics
2
2
  # percent of cases that are at or below a score
3
- def percentile_rank(p, collection = self)
4
- values = Support::convert(collection)
5
- return DescriptiveStatistics.percentile_rank_empty_collection_default_value unless values.size > 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
6
 
7
- return (((values.sort.rindex{ |x| x <= p } || -1.0) + 1.0)) / values.number * 100.0
7
+ return (((values.sort.rindex { |x| x <= p } || -1.0) + 1.0)) / values.number * 100.0
8
8
  end
9
9
  end
@@ -1,8 +1,8 @@
1
1
  module DescriptiveStatistics
2
- def range(collection = self)
3
- values = Support::convert(collection)
4
- return DescriptiveStatistics.range_empty_collection_default_value unless values.size > 0
2
+ def range(collection = self, &block)
3
+ values = Support::convert(collection, &block)
4
+ return DescriptiveStatistics.range_empty_collection_default_value if values.empty?
5
5
 
6
6
  values.max - values.min
7
- end
7
+ end
8
8
  end
@@ -1,7 +1,7 @@
1
1
  module DescriptiveStatistics
2
- def standard_deviation(collection = self)
3
- values = Support::convert(collection)
4
- return DescriptiveStatistics.standard_deviation_empty_collection_default_value unless values.size > 0
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
5
 
6
6
  Math.sqrt(values.variance)
7
7
  end
@@ -1,8 +1,8 @@
1
1
  module DescriptiveStatistics
2
- def sum(collection = self)
3
- values = Support::convert(collection)
4
- return DescriptiveStatistics.sum_empty_collection_default_value unless values.size > 0
2
+ def sum(collection = self, &block)
3
+ values = Support::convert(collection, &block)
4
+ return DescriptiveStatistics.sum_empty_collection_default_value if values.empty?
5
5
 
6
- return values.inject(:+)
7
- end
6
+ return values.reduce(:+)
7
+ end
8
8
  end
@@ -4,24 +4,29 @@ module DescriptiveStatistics
4
4
 
5
5
  module Support
6
6
 
7
- def self.convert(from_enumerable)
8
- extend to_float to_array from_enumerable
7
+ def self.convert(from_enumerable, &block)
8
+ extend to_float to_value(to_array(from_enumerable), &block)
9
9
  end
10
10
 
11
- def self.extract(from_enumerable)
12
- extend to_array from_enumerable
11
+ def self.extract(from_enumerable, &block)
12
+ extend to_value(to_array(from_enumerable), &block)
13
13
  end
14
14
 
15
15
  private
16
16
 
17
17
  def self.extend(enumerable)
18
- enumerable.extend(DescriptiveStatistics)
18
+ enumerable.extend(DescriptiveStatistics)
19
19
  end
20
20
 
21
21
  def self.to_float(enumerable)
22
22
  enumerable.map(&:to_f)
23
23
  end
24
24
 
25
+ def self.to_value(enumerable, &block)
26
+ return enumerable unless block_given?
27
+ enumerable.map { |object| yield object }
28
+ end
29
+
25
30
  def self.to_array(enumerable)
26
31
  case enumerable
27
32
  when Hash
@@ -1,9 +1,9 @@
1
1
  module DescriptiveStatistics
2
- def variance(collection = self)
3
- values = Support::convert(collection)
4
- return DescriptiveStatistics.variance_empty_collection_default_value unless values.size > 0
2
+ def variance(collection = self, &block)
3
+ values = Support::convert(collection, &block)
4
+ return DescriptiveStatistics.variance_empty_collection_default_value if values.empty?
5
5
 
6
6
  mean = values.mean
7
- values.map{ |sample| (mean - sample) ** 2 }.inject(:+) / values.number
7
+ values.map { |sample| (mean - sample) ** 2 }.reduce(:+) / values.number
8
8
  end
9
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: 2.3.0
4
+ version: 2.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Derrick Parkhurst
@@ -9,10 +9,11 @@ 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-09-08 00:00:00.000000000 Z
16
+ date: 2014-11-15 00:00:00.000000000 Z
16
17
  dependencies: []
17
18
  description: Adds descriptive statistics methods to Enumerable module for use on collections
18
19
  or Numeric data
@@ -58,7 +59,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
58
59
  version: '0'
59
60
  requirements: []
60
61
  rubyforge_project:
61
- rubygems_version: 2.2.2
62
+ rubygems_version: 2.4.3
62
63
  signing_key:
63
64
  specification_version: 4
64
65
  summary: Descriptive Statistics