Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/descriptive_statistics/coefficient_of_variation.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions lib/descriptive_statistics/descriptive_statistics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions lib/descriptive_statistics/safe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
6 changes: 5 additions & 1 deletion spec/monkeypatch/array_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -61,4 +65,4 @@

end

end
end
18 changes: 17 additions & 1 deletion spec/monkeypatch/empty_collection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -227,4 +243,4 @@

end

end
end
6 changes: 5 additions & 1 deletion spec/monkeypatch/hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -61,4 +65,4 @@

end

end
end
6 changes: 5 additions & 1 deletion spec/monkeypatch/object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -116,4 +120,4 @@
end
end

end
end
6 changes: 5 additions & 1 deletion spec/monkeypatch/set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -61,4 +65,4 @@

end

end
end
6 changes: 5 additions & 1 deletion spec/monkeypatch/single_value_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -55,4 +59,4 @@

end

end
end
6 changes: 5 additions & 1 deletion spec/safe/class_method_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -56,4 +60,4 @@

end

end
end
6 changes: 5 additions & 1 deletion spec/safe/extend_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -66,4 +70,4 @@

end

end
end
6 changes: 5 additions & 1 deletion spec/safe/stats_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -55,4 +59,4 @@

end

end
end