From 5ed1c0815233b09db7d97e1bd0735a4b6206d31f Mon Sep 17 00:00:00 2001 From: Lidiane Taquehara Date: Wed, 13 Jul 2016 11:03:32 +0000 Subject: [PATCH 1/3] Class method to calculate Coefficient of Variation --- lib/descriptive_statistics/coefficient_of_variation.rb | 8 ++++++++ lib/descriptive_statistics/descriptive_statistics.rb | 1 + lib/descriptive_statistics/safe.rb | 1 + spec/safe/class_method_spec.rb | 4 ++++ 4 files changed, 14 insertions(+) create mode 100644 lib/descriptive_statistics/coefficient_of_variation.rb diff --git a/lib/descriptive_statistics/coefficient_of_variation.rb b/lib/descriptive_statistics/coefficient_of_variation.rb new file mode 100644 index 0000000..7cf68e1 --- /dev/null +++ b/lib/descriptive_statistics/coefficient_of_variation.rb @@ -0,0 +1,8 @@ +module DescriptiveStatistics + def coefficient_of_variation(collection = self, &block) + values = Support::convert(collection, &block) + return DescriptiveStatistics.coefficient_of_variation_empty_collection_default_value if values.empty? + + values.standard_deviation / values.mean + end +end diff --git a/lib/descriptive_statistics/descriptive_statistics.rb b/lib/descriptive_statistics/descriptive_statistics.rb index 1a35fed..89e5bb1 100644 --- a/lib/descriptive_statistics/descriptive_statistics.rb +++ b/lib/descriptive_statistics/descriptive_statistics.rb @@ -4,6 +4,7 @@ def descriptive_statistics(&block) :sum => self.sum(&block), :variance => self.variance(&block), :standard_deviation => self.standard_deviation(&block), + :coefficient_of_variation => self.coefficient_of_variation(&block), :min => self.min(&block), :max => self.max(&block), :mean => self.mean(&block), diff --git a/lib/descriptive_statistics/safe.rb b/lib/descriptive_statistics/safe.rb index 9c45acd..0ce8796 100644 --- a/lib/descriptive_statistics/safe.rb +++ b/lib/descriptive_statistics/safe.rb @@ -6,6 +6,7 @@ require 'descriptive_statistics/mode.rb' require 'descriptive_statistics/variance.rb' require 'descriptive_statistics/standard_deviation.rb' +require 'descriptive_statistics/coefficient_of_variation.rb' require 'descriptive_statistics/percentile.rb' require 'descriptive_statistics/percentile_rank.rb' require 'descriptive_statistics/range.rb' diff --git a/spec/safe/class_method_spec.rb b/spec/safe/class_method_spec.rb index 53d93a3..41be8ce 100644 --- a/spec/safe/class_method_spec.rb +++ b/spec/safe/class_method_spec.rb @@ -32,6 +32,10 @@ expect(subject.standard_deviation(data)).to eql(2.778310325442932) end + it "calculates the coefficient of variation" do + expect(subject.coefficient_of_variation(data)).to eql (0.5659521033309676) + end + it "calculates the percentile" do expect(subject.percentile(30, data)).to eql(3.0) expect(subject.percentile(50, data)).to eql(5.0) From 097fd8e021c442335a8ef117b378f7b18ade4357 Mon Sep 17 00:00:00 2001 From: Lidiane Taquehara Date: Thu, 14 Jul 2016 05:22:22 -0300 Subject: [PATCH 2/3] =?UTF-8?q?Implementa=C3=A7=C3=A3o=20de=20specs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../descriptive_statistics.rb | 2 +- spec/monkeypatch/array_spec.rb | 6 +++++- spec/monkeypatch/empty_collection_spec.rb | 18 +++++++++++++++++- spec/monkeypatch/hash_spec.rb | 6 +++++- spec/monkeypatch/object_spec.rb | 6 +++++- spec/monkeypatch/set_spec.rb | 6 +++++- spec/monkeypatch/single_value_spec.rb | 6 +++++- spec/safe/class_method_spec.rb | 4 ++-- spec/safe/extend_spec.rb | 6 +++++- spec/safe/stats_spec.rb | 6 +++++- 10 files changed, 55 insertions(+), 11 deletions(-) diff --git a/lib/descriptive_statistics/descriptive_statistics.rb b/lib/descriptive_statistics/descriptive_statistics.rb index 89e5bb1..b88e551 100644 --- a/lib/descriptive_statistics/descriptive_statistics.rb +++ b/lib/descriptive_statistics/descriptive_statistics.rb @@ -4,7 +4,7 @@ def descriptive_statistics(&block) :sum => self.sum(&block), :variance => self.variance(&block), :standard_deviation => self.standard_deviation(&block), - :coefficient_of_variation => self.coefficient_of_variation(&block), + :coefficient_of_variation => self.coefficient_of_variation(&block), :min => self.min(&block), :max => self.max(&block), :mean => self.mean(&block), diff --git a/spec/monkeypatch/array_spec.rb b/spec/monkeypatch/array_spec.rb index 6f65437..a7130d5 100644 --- a/spec/monkeypatch/array_spec.rb +++ b/spec/monkeypatch/array_spec.rb @@ -37,6 +37,10 @@ expect(subject.standard_deviation).to eql(2.778310325442932) end + it "calculates the coefficient of variation" do + expect(subject.coefficient_of_variation).to eql(0.5659521033309676) + end + it "calculates the percentile" do expect(subject.percentile(30)).to eql(3.0) expect(subject.percentile(50)).to eql(5.0) @@ -61,4 +65,4 @@ end -end \ No newline at end of file +end diff --git a/spec/monkeypatch/empty_collection_spec.rb b/spec/monkeypatch/empty_collection_spec.rb index 4a74b73..78dcf9d 100644 --- a/spec/monkeypatch/empty_collection_spec.rb +++ b/spec/monkeypatch/empty_collection_spec.rb @@ -35,6 +35,10 @@ expect(subject.standard_deviation).to eql(nil) end + it "calculates the coefficient of variation" do + expect(subject.coefficient_of_variation).to eql(nil) + end + it "calculates the percentile" do expect(subject.percentile(30)).to eql(nil) expect(subject.percentile(50)).to eql(nil) @@ -93,6 +97,10 @@ expect(subject.standard_deviation).to eql(0.0) end + it "calculates the coefficient of variation" do + expect(subject.coefficient_of_variation).to eql(0.0) + end + it "calculates the percentile" do expect(subject.percentile(30)).to eql(0.0) expect(subject.percentile(50)).to eql(0.0) @@ -148,6 +156,10 @@ expect(subject.standard_deviation).to eql(nil) end + it "calculates the coefficient_of_variation" do + expect(subject.coefficient_of_variation).to eql(nil) + end + it "calculates the percentile" do expect(subject.percentile(30)).to eql(nil) expect(subject.percentile(50)).to eql(nil) @@ -203,6 +215,10 @@ expect(subject.standard_deviation).to eql(nil) end + it "calculates the coefficient_of_variation" do + expect(subject.coefficient_of_variation).to eql(nil) + end + it "calculates the percentile" do expect(subject.percentile(30)).to eql(nil) expect(subject.percentile(50)).to eql(nil) @@ -227,4 +243,4 @@ end -end \ No newline at end of file +end diff --git a/spec/monkeypatch/hash_spec.rb b/spec/monkeypatch/hash_spec.rb index 0422d5f..f475716 100644 --- a/spec/monkeypatch/hash_spec.rb +++ b/spec/monkeypatch/hash_spec.rb @@ -37,6 +37,10 @@ expect(subject.standard_deviation).to eql(2.778310325442932) end + it "calculates the coefficient_of_variation" do + expect(subject.coefficient_of_variation).to eql(0.5659521033309676) + end + it "calculates the percentile" do expect(subject.percentile(30)).to eql(3.0) expect(subject.percentile(50)).to eql(5.0) @@ -61,4 +65,4 @@ end -end \ No newline at end of file +end diff --git a/spec/monkeypatch/object_spec.rb b/spec/monkeypatch/object_spec.rb index 574a1ef..b4a83d0 100644 --- a/spec/monkeypatch/object_spec.rb +++ b/spec/monkeypatch/object_spec.rb @@ -41,6 +41,10 @@ expect(subject.standard_deviation(&:price)).to eql(2.0850659461993044) end + it "calculates the coefficient_of_variation" do + expect(subject.coefficient_of_variation(&:price)).to eql(0.6043669409273346) + end + it "calculates the percentile" do expect(subject.percentile(30, &:price)).to eql(2.3) expect(subject.percentile(50, &:price)).to eql(3.8) @@ -116,4 +120,4 @@ end end -end \ No newline at end of file +end diff --git a/spec/monkeypatch/set_spec.rb b/spec/monkeypatch/set_spec.rb index 77e5951..a77781c 100644 --- a/spec/monkeypatch/set_spec.rb +++ b/spec/monkeypatch/set_spec.rb @@ -37,6 +37,10 @@ expect(subject.standard_deviation).to eql(2.799416848895061) end + it "calculates the coefficient_of_variation" do + expect(subject.coefficient_of_variation).to eql(0.576350527713689) + end + it "calculates the percentile" do expect(subject.percentile(30)).to eql(2.8) expect(subject.percentile(50)).to eql(5.0) @@ -61,4 +65,4 @@ end -end \ No newline at end of file +end diff --git a/spec/monkeypatch/single_value_spec.rb b/spec/monkeypatch/single_value_spec.rb index 1c09711..56b8f4a 100644 --- a/spec/monkeypatch/single_value_spec.rb +++ b/spec/monkeypatch/single_value_spec.rb @@ -31,6 +31,10 @@ expect(subject.standard_deviation).to eql(0.0) end + it "calculates the coefficient_of_variation" do + expect(subject.coefficient_of_variation).to eql(0.0) + end + it "calculates the percentile" do expect(subject.percentile(30)).to eql(2.0) expect(subject.percentile(50)).to eql(2.0) @@ -55,4 +59,4 @@ end -end \ No newline at end of file +end diff --git a/spec/safe/class_method_spec.rb b/spec/safe/class_method_spec.rb index 41be8ce..44c35fd 100644 --- a/spec/safe/class_method_spec.rb +++ b/spec/safe/class_method_spec.rb @@ -33,7 +33,7 @@ end it "calculates the coefficient of variation" do - expect(subject.coefficient_of_variation(data)).to eql (0.5659521033309676) + expect(subject.coefficient_of_variation(data)).to eql(0.5659521033309676) end it "calculates the percentile" do @@ -60,4 +60,4 @@ end -end \ No newline at end of file +end diff --git a/spec/safe/extend_spec.rb b/spec/safe/extend_spec.rb index a34356f..324f8be 100644 --- a/spec/safe/extend_spec.rb +++ b/spec/safe/extend_spec.rb @@ -42,6 +42,10 @@ expect(subject.standard_deviation).to eql(2.778310325442932) end + it "calculates the coefficient of variation" do + expect(subject.coefficient_of_variation).to eql (0.5659521033309676) + end + it "calculates the percentile" do expect(subject.percentile(30)).to eql(3.0) expect(subject.percentile(50)).to eql(5.0) @@ -66,4 +70,4 @@ end -end \ No newline at end of file +end diff --git a/spec/safe/stats_spec.rb b/spec/safe/stats_spec.rb index 1cd3d57..b6202a1 100644 --- a/spec/safe/stats_spec.rb +++ b/spec/safe/stats_spec.rb @@ -31,6 +31,10 @@ expect(subject.standard_deviation).to eql(2.778310325442932) end + it "calculates the coefficient of variation" do + expect(subject.coefficient_of_variation).to eql (0.5659521033309676) + end + it "calculates the percentile" do expect(subject.percentile(30)).to eql(3.0) expect(subject.percentile(50)).to eql(5.0) @@ -55,4 +59,4 @@ end -end \ No newline at end of file +end From 72d851090ddba7bca0a3e5a1376c689f7c0ce6a3 Mon Sep 17 00:00:00 2001 From: Lidiane Taquehara Date: Thu, 14 Jul 2016 07:04:33 -0300 Subject: [PATCH 3/3] Broke conditional in lines (too long) --- lib/descriptive_statistics/coefficient_of_variation.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/descriptive_statistics/coefficient_of_variation.rb b/lib/descriptive_statistics/coefficient_of_variation.rb index 7cf68e1..4a87177 100644 --- a/lib/descriptive_statistics/coefficient_of_variation.rb +++ b/lib/descriptive_statistics/coefficient_of_variation.rb @@ -1,7 +1,10 @@ module DescriptiveStatistics def coefficient_of_variation(collection = self, &block) values = Support::convert(collection, &block) - return DescriptiveStatistics.coefficient_of_variation_empty_collection_default_value if values.empty? + + if values.empty? + return DescriptiveStatistics.coefficient_of_variation_empty_collection_default_value + end values.standard_deviation / values.mean end