diff --git a/lib/descriptive_statistics/coefficient_of_variation.rb b/lib/descriptive_statistics/coefficient_of_variation.rb new file mode 100644 index 0000000..4a87177 --- /dev/null +++ b/lib/descriptive_statistics/coefficient_of_variation.rb @@ -0,0 +1,11 @@ +module DescriptiveStatistics + def coefficient_of_variation(collection = self, &block) + values = Support::convert(collection, &block) + + if values.empty? + return DescriptiveStatistics.coefficient_of_variation_empty_collection_default_value + end + + 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..b88e551 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/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 53d93a3..44c35fd 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) @@ -56,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