diff --git a/lib/descriptive_statistics/class_methods.rb b/lib/descriptive_statistics/class_methods.rb index 1988d1b..040fc1b 100644 --- a/lib/descriptive_statistics/class_methods.rb +++ b/lib/descriptive_statistics/class_methods.rb @@ -17,7 +17,7 @@ def empty_collection_default_value=(value) end define_method("#{m}_empty_collection_default_value=") do |value| default_values[m] = value - end + end end DescriptiveStatistics.instance_methods.each do |m| @@ -27,7 +27,7 @@ def empty_collection_default_value=(value) private def default_values - @default_values ||= {} + @default_values ||= {} end end diff --git a/lib/descriptive_statistics/mean.rb b/lib/descriptive_statistics/mean.rb index cda8ffa..2d0076d 100644 --- a/lib/descriptive_statistics/mean.rb +++ b/lib/descriptive_statistics/mean.rb @@ -1,8 +1,8 @@ module DescriptiveStatistics def mean(collection = self) values = Support::convert(collection) - return DescriptiveStatistics.mean_empty_collection_default_value unless values.size > 0 + return DescriptiveStatistics.mean_empty_collection_default_value if values.empty? values.sum / values.number - end + end end diff --git a/lib/descriptive_statistics/median.rb b/lib/descriptive_statistics/median.rb index 78657fe..8c9e8aa 100644 --- a/lib/descriptive_statistics/median.rb +++ b/lib/descriptive_statistics/median.rb @@ -1,8 +1,8 @@ module DescriptiveStatistics def median(collection = self) values = Support::convert(collection) - return DescriptiveStatistics.median_empty_collection_default_value unless values.size > 0 + return DescriptiveStatistics.median_empty_collection_default_value if values.empty? values.percentile(50) - end + end end diff --git a/lib/descriptive_statistics/mode.rb b/lib/descriptive_statistics/mode.rb index ef6d09d..3b4fcc2 100644 --- a/lib/descriptive_statistics/mode.rb +++ b/lib/descriptive_statistics/mode.rb @@ -1,12 +1,12 @@ module DescriptiveStatistics def mode(collection = self) values = Support::extract(collection) - return unless values.size > 0 + return if values.to_a.empty? values .group_by { |e| e } .values .max_by(&:size) .first - end + end end diff --git a/lib/descriptive_statistics/percentile.rb b/lib/descriptive_statistics/percentile.rb index 15d6926..776d590 100644 --- a/lib/descriptive_statistics/percentile.rb +++ b/lib/descriptive_statistics/percentile.rb @@ -1,17 +1,15 @@ module DescriptiveStatistics def percentile(p, collection = self) values = Support::convert(collection) - return DescriptiveStatistics.percentile_empty_collection_default_value unless values.size > 0 - return values.first unless values.size > 1 - - sorted = values.sort - return sorted[-1] if p == 100 - rank = p / 100.0 * (values.number - 1) - lrank = rank.floor - d = rank - lrank - lower = sorted[lrank] - upper = sorted[lrank+1] - lower + (upper - lower) * d + if values.empty?; DescriptiveStatistics.percentile_empty_collection_default_value + elsif values.size == 1; values.first + else + values.sort! + return values.last if p == 100 + rank = p / 100.0 * (values.size - 1) + lower, upper = values[rank.floor,2] + lower + (upper - lower) * (rank - rank.floor) + end end end diff --git a/lib/descriptive_statistics/percentile_rank.rb b/lib/descriptive_statistics/percentile_rank.rb index 674ea6f..6901777 100644 --- a/lib/descriptive_statistics/percentile_rank.rb +++ b/lib/descriptive_statistics/percentile_rank.rb @@ -2,8 +2,8 @@ module DescriptiveStatistics # percent of cases that are at or below a score def percentile_rank(p, collection = self) values = Support::convert(collection) - return DescriptiveStatistics.percentile_rank_empty_collection_default_value unless values.size > 0 + return DescriptiveStatistics.percentile_rank_empty_collection_default_value if values.empty? - return (((values.sort.rindex{ |x| x <= p } || -1.0) + 1.0)) / values.number * 100.0 + return (((values.sort.rindex { |x| x <= p } || -1.0) + 1.0)) / values.number * 100.0 end end diff --git a/lib/descriptive_statistics/range.rb b/lib/descriptive_statistics/range.rb index 6f2c2ce..8189f2e 100644 --- a/lib/descriptive_statistics/range.rb +++ b/lib/descriptive_statistics/range.rb @@ -1,8 +1,8 @@ module DescriptiveStatistics def range(collection = self) values = Support::convert(collection) - return DescriptiveStatistics.range_empty_collection_default_value unless values.size > 0 + return DescriptiveStatistics.range_empty_collection_default_value if values.empty? values.max - values.min - end + end end diff --git a/lib/descriptive_statistics/standard_deviation.rb b/lib/descriptive_statistics/standard_deviation.rb index f82e856..4e71654 100644 --- a/lib/descriptive_statistics/standard_deviation.rb +++ b/lib/descriptive_statistics/standard_deviation.rb @@ -1,7 +1,7 @@ module DescriptiveStatistics def standard_deviation(collection = self) values = Support::convert(collection) - return DescriptiveStatistics.standard_deviation_empty_collection_default_value unless values.size > 0 + return DescriptiveStatistics.standard_deviation_empty_collection_default_value if values.empty? Math.sqrt(values.variance) end diff --git a/lib/descriptive_statistics/sum.rb b/lib/descriptive_statistics/sum.rb index ff44be4..db068fb 100644 --- a/lib/descriptive_statistics/sum.rb +++ b/lib/descriptive_statistics/sum.rb @@ -1,8 +1,8 @@ module DescriptiveStatistics def sum(collection = self) values = Support::convert(collection) - return DescriptiveStatistics.sum_empty_collection_default_value unless values.size > 0 + return DescriptiveStatistics.sum_empty_collection_default_value if values.empty? - return values.inject(:+) - end + return values.reduce(:+) + end end diff --git a/lib/descriptive_statistics/support/convert.rb b/lib/descriptive_statistics/support/convert.rb index 1b8da12..ee0486e 100644 --- a/lib/descriptive_statistics/support/convert.rb +++ b/lib/descriptive_statistics/support/convert.rb @@ -15,7 +15,7 @@ def self.extract(from_enumerable) private def self.extend(enumerable) - enumerable.extend(DescriptiveStatistics) + enumerable.extend(DescriptiveStatistics) end def self.to_float(enumerable) diff --git a/lib/descriptive_statistics/variance.rb b/lib/descriptive_statistics/variance.rb index 257f2cd..ad3f7ab 100644 --- a/lib/descriptive_statistics/variance.rb +++ b/lib/descriptive_statistics/variance.rb @@ -1,9 +1,9 @@ module DescriptiveStatistics def variance(collection = self) values = Support::convert(collection) - return DescriptiveStatistics.variance_empty_collection_default_value unless values.size > 0 + return DescriptiveStatistics.variance_empty_collection_default_value if values.empty? mean = values.mean - values.map{ |sample| (mean - sample) ** 2 }.inject(:+) / values.number + values.map { |sample| (mean - sample) ** 2 }.reduce(:+) / values.number end end diff --git a/spec/enumerable_spec.rb b/spec/enumerable_spec.rb index 48ffebf..e1285a5 100644 --- a/spec/enumerable_spec.rb +++ b/spec/enumerable_spec.rb @@ -3,7 +3,7 @@ describe "DescriptiveStatistics" do require 'descriptive_statistics' - class Foo + class Foo include Enumerable attr_accessor :bar, :baz, :bat diff --git a/test/test.rb b/test/test.rb index d49617e..9cbcce1 100644 --- a/test/test.rb +++ b/test/test.rb @@ -3,7 +3,7 @@ require './lib/descriptive_statistics' class TestData < MiniTest::Unit::TestCase - + def setup @data = [] CSV.foreach("test/testdata.csv") do |row| @@ -14,31 +14,31 @@ def setup def test_sum @data.each do |test_case| assert_equal test_case[0,10].sum.round(6), test_case[10].round(6) - end + end end def test_mean @data.each do |test_case| assert_equal test_case[0,10].mean.round(6), test_case[11].round(6) - end + end end - + def test_median @data.each do |test_case| assert_equal test_case[0,10].median.round(6), test_case[12].round(6) - end + end end def test_variance @data.each do |test_case| assert_equal test_case[0,10].variance.round(6), test_case[13].round(6) - end + end end - + def test_standard_deviation @data.each do |test_case| assert_equal test_case[0,10].standard_deviation.round(6), test_case[14].round(6) - end + end end def test_percentile @@ -54,13 +54,13 @@ def test_percentile assert_equal test_case[0,10].percentile(80).round(6), test_case[23].round(6) assert_equal test_case[0,10].percentile(90).round(6), test_case[24].round(6) assert_equal test_case[0,10].percentile(100).round(6), test_case[25].round(6) - end + end end def test_percentile_rank @data.each do |test_case| assert_equal test_case[0,10].percentile_rank(50).round(6), test_case[26].round(6) - end + end end def test_percentile_rank_for_smallest_number @@ -70,14 +70,14 @@ def test_percentile_rank_for_smallest_number def test_mode assert_equal [1,3,6,9,4,5,2,3,4,1,6,7,8,3,2,3,5,7,8,5,6,5,6,5,4,5,5,5].mode, 5 - end + end def test_range assert_equal [17, 5, 3, 23, 33, 30, 45, 37].range, 42 assert_equal [].range, nil assert_equal [1, 1.0].range, 0 end - + def test_descriptive_statistics @data.each do |test_case| stat = test_case[0, 10].descriptive_statistics @@ -95,7 +95,7 @@ def test_descriptive_statistics assert_equal test_case[0, 10].percentile(75), stat[:q3] end end - + def test_median_is_percentile50 @data.each do |test_case| assert_equal test_case[0, 10].median, test_case[0, 10].percentile(50)